Merge hostedMoneroApiClient_base functionality into response_parser_util (#15)

pull/40/head
HenryNguyen5 6 years ago committed by GitHub
parent 9a0e1f66ee
commit 85213bd303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -47,7 +47,12 @@ import {
validUnspentOutput,
validTxPubKey,
} from "./utils";
import { AddressTransactions, AddressInfo, UnspentOuts } from "./types";
import {
AddressTransactions,
AddressInfo,
UnspentOuts,
NormalizedTransaction,
} from "./types";
export function parseAddressInfo(
address: string,
@ -90,9 +95,9 @@ export function parseAddressInfo(
}
return {
total_received,
locked_balance,
total_sent,
total_received: new JSBigInt(total_received),
locked_balance: new JSBigInt(locked_balance),
total_sent: new JSBigInt(total_sent),
spent_outputs,
account_scanned_tx_height,
@ -123,6 +128,8 @@ export function parseAddressTransactions(
transactions,
} = normalizeAddressTransactions(data);
const normalizedTransactions: NormalizedTransaction[] = [];
for (let i = 0; i < transactions.length; i++) {
const transaction = normalizeTransaction(transactions[i]);
@ -138,14 +145,15 @@ export function parseAddressTransactions(
);
if (!isKeyImageEqual(transaction.spent_outputs[j], keyImage)) {
transaction.total_sent = new JSBigInt(transaction.total_sent)
.subtract(transaction.spent_outputs[j].amount)
.toString();
transaction.total_sent = new JSBigInt(
transaction.total_sent,
).subtract(transaction.spent_outputs[j].amount);
transaction.spent_outputs.splice(j, 1);
j--;
}
}
if (zeroTransactionAmount(transaction)) {
transactions.splice(i, 1);
i--;
@ -159,9 +167,11 @@ export function parseAddressTransactions(
);
removeEncryptedPaymentIDs(transaction);
normalizedTransactions.push(transaction);
}
sortTransactions(transactions);
sortTransactions(normalizedTransactions);
// on the other side, we convert transactions timestamp to Date obj
@ -171,7 +181,7 @@ export function parseAddressTransactions(
account_scan_start_height,
transaction_height,
blockchain_height,
serialized_transactions: transactions,
transactions: normalizedTransactions,
};
}
@ -195,6 +205,10 @@ export function parseUnspentOuts(
) {
const { outputs, per_kb_fee } = data;
if (!per_kb_fee) {
throw Error("Unexpected / missing per_kb_fee");
}
const unspentOuts = outputs || [];
for (let i = 0; i < unspentOuts.length; i++) {
@ -253,6 +267,6 @@ export function parseUnspentOuts(
return {
unspentOuts,
unusedOuts: [...unspentOuts],
per_kb_fee,
per_kb_fee: new JSBigInt(per_kb_fee),
};
}

@ -1,3 +1,5 @@
import { JSBigInt, Omit } from "types";
export interface SpentOutput {
amount: string;
key_image: string;
@ -9,7 +11,7 @@ export interface SpentOutput {
export interface AddressTransactionsTx {
id: number;
hash?: string;
timestamp?: string;
timestamp: string;
total_received?: string;
total_sent?: string;
unlock_time?: number;
@ -31,9 +33,12 @@ export interface AddressTransactions {
transactions?: AddressTransactionsTx[];
}
export interface NormalizedTransaction extends Required<AddressTransactionsTx> {
amount: string;
export interface NormalizedTransaction
extends Required<Omit<AddressTransactionsTx, "total_sent" | "timestamp">> {
total_sent: JSBigInt;
amount: JSBigInt;
approx_float_amount: number;
timestamp: Date;
}
export interface Rates {

@ -7,7 +7,7 @@ import {
AddressInfo,
UnspentOutput,
} from "./types";
import { JSBigInt } from "types";
import { JSBigInt, Omit } from "types";
export function isKeyImageEqual({ key_image }: SpentOutput, keyImage: string) {
return key_image === keyImage;
@ -55,8 +55,8 @@ export function normalizeAddressTransactions(
export function normalizeTransaction(
tx: AddressTransactionsTx,
): NormalizedTransaction {
const defaultObj: NormalizedTransaction = {
amount: "0",
const defaultObj: Omit<NormalizedTransaction, "timestamp"> = {
amount: new JSBigInt(0),
approx_float_amount: 0,
hash: "",
height: 0,
@ -65,14 +65,20 @@ export function normalizeTransaction(
coinbase: false,
mixin: 0,
spent_outputs: [] as SpentOutput[],
timestamp: "",
total_received: "0",
total_sent: "0",
total_sent: new JSBigInt(0),
unlock_time: 0,
payment_id: "",
};
const mergedObj = { ...defaultObj, ...tx };
const mergedObj = {
...defaultObj,
...tx,
total_sent: tx.total_sent
? new JSBigInt(tx.total_sent)
: defaultObj.total_sent,
timestamp: new Date(tx.timestamp),
};
return mergedObj;
}
@ -93,7 +99,7 @@ export function calculateTransactionAmount({
total_received,
total_sent,
}: NormalizedTransaction) {
return new JSBigInt(total_received).subtract(total_sent).toString();
return new JSBigInt(total_received).subtract(total_sent);
}
export function estimateTransactionAmount({ amount }: NormalizedTransaction) {
@ -131,7 +137,7 @@ export function removeEncryptedPaymentIDs(tx: NormalizedTransaction) {
* @param {AddressTransactionsTx[]} transactions
* @returns
*/
export function sortTransactions(transactions: AddressTransactionsTx[]) {
export function sortTransactions(transactions: NormalizedTransaction[]) {
return transactions.sort((a, b) => {
if (a.mempool && !b.mempool) {
return -1; // a first

Loading…
Cancel
Save