UNPKG

@ledgerhq/live-common

Version:
135 lines 5.45 kB
import { BigNumber } from "bignumber.js"; import { formatCurrencyUnit } from "./currencies"; import { getAccountCurrency, getMainAccount, flattenAccounts } from "./account"; import { flattenOperationWithInternalsAndNfts } from "./operation"; import { calculate } from "@ledgerhq/live-countervalues/logic"; import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName"; import { accountNameWithDefaultSelector } from "@ledgerhq/live-wallet/store"; const newLine = "\r\n"; const fields = [ { title: "Operation Date", cell: (_account, _parentAccount, op) => op.date.toISOString(), }, { title: "Status", cell: (_account, _parentAccount, op) => { return op.hasFailed ? "Failed" : "Confirmed"; }, }, { title: "Currency Ticker", cell: account => getAccountCurrency(account).ticker, }, { title: "Operation Type", cell: (_account, _parentAccount, op) => op.type, }, { title: "Operation Amount", cell: (account, parentAccount, op) => formatCurrencyUnit(getAccountCurrency(account).units[0], op.value, { disableRounding: true, useGrouping: false, }), }, { title: "Operation Fees", cell: (account, parentAccount, op) => "TokenAccount" === account.type ? "" : formatCurrencyUnit(getAccountCurrency(account).units[0], op.fee, { disableRounding: true, useGrouping: false, }), }, { title: "Operation Hash", cell: (_account, _parentAccount, op) => op.hash, }, { title: "Account Name", cell: (account, parentAccount, _op, _counterValueCurrency, _countervalueState, walletState) => { const main = getMainAccount(account, parentAccount); return walletState ? accountNameWithDefaultSelector(walletState, main) : getDefaultAccountName(main); }, }, { title: "Account xpub", cell: (account, parentAccount) => { const main = getMainAccount(account, parentAccount); return main.xpub || main.freshAddress; }, }, { title: "Countervalue Ticker", cell: (account, parentAccount, op, countervalueCurrency) => { return countervalueCurrency?.ticker ?? ""; }, }, { title: "Countervalue at Operation Date", cell: (account, parentAccount, op, counterValueCurrency, countervalueState) => { const value = counterValueCurrency && countervalueState ? calculate(countervalueState, { from: getAccountCurrency(account), to: counterValueCurrency, value: op.value.toNumber(), disableRounding: true, date: op.date, }) : null; return value && counterValueCurrency ? formatCurrencyUnit(counterValueCurrency.units[0], new BigNumber(value), { disableRounding: true, useGrouping: false, }) : ""; }, }, { title: "Countervalue at CSV Export", cell: (account, parentAccount, op, counterValueCurrency, countervalueState) => { const value = counterValueCurrency && countervalueState ? calculate(countervalueState, { from: getAccountCurrency(account), to: counterValueCurrency, value: op.value.toNumber(), disableRounding: true, }) : null; return value && counterValueCurrency ? formatCurrencyUnit(counterValueCurrency.units[0], new BigNumber(value), { disableRounding: true, useGrouping: false, }) : ""; }, }, ]; const accountRows = (account, parentAccount, counterValueCurrency, countervalueState, walletState) => account.operations .reduce((ops, op) => ops.concat(flattenOperationWithInternalsAndNfts(op)), []) .map(operation => fields.map(field => field.cell(account, parentAccount, operation, counterValueCurrency, countervalueState, walletState))); const accountsRows = (accounts, counterValueCurrency, countervalueState, walletState) => { return flattenAccounts(accounts).reduce((all, account) => { const parentAccount = account.type !== "Account" ? accounts.find(a => a.id === account.parentId) : null; return all.concat(accountRows(account, parentAccount, counterValueCurrency, countervalueState, walletState)); }, []); }; const mapRowValue = (row) => { const rowWithoutNewlines = row.map(value => { if (value) { return value.replace(/[,\n\r]/g, ""); } return value; }); return rowWithoutNewlines.join(","); }; export const accountsOpToCSV = (accounts, counterValueCurrency, countervalueState, // cvs state required for countervalues export walletState) => { const header = fields.map(field => field.title).join(",") + newLine; const accountsRowsRes = accountsRows(accounts, counterValueCurrency, countervalueState, walletState); const rows = accountsRowsRes.map(mapRowValue).join(newLine); return header + rows; }; //# sourceMappingURL=csvExport.js.map