UNPKG

@funded-labs/plug-controller

Version:

Internet Computer Plug wallet's controller

44 lines (43 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatTransactions = exports.formatTransaction = void 0; const constants_1 = require("./constants"); const amountParser_1 = require("./helpers/amountParser"); const formatTransaction = (transaction, principalId, accountId, network, icpPrice) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const { details, hash, canisterInfo, caller, timestamp, } = transaction || {}; const { sonicData, fee } = details || {}; const amount = (0, amountParser_1.parseAmount)(details); const getSymbol = () => { var _a, _b, _c, _d, _e; if ('tokenRegistryInfo' in ((details === null || details === void 0 ? void 0 : details.canisterInfo) || [])) return details === null || details === void 0 ? void 0 : details.canisterInfo.tokenRegistryInfo.symbol; if ('nftRegistryInfo' in ((details === null || details === void 0 ? void 0 : details.canisterInfo) || [])) return 'NFT'; return (_e = (_b = (_a = details === null || details === void 0 ? void 0 : details.currency) === null || _a === void 0 ? void 0 : _a.symbol) !== null && _b !== void 0 ? _b : (_d = (_c = sonicData === null || sonicData === void 0 ? void 0 : sonicData.token) === null || _c === void 0 ? void 0 : _c.details) === null || _d === void 0 ? void 0 : _d.symbol) !== null && _e !== void 0 ? _e : ''; }; const isOwnTx = [principalId, accountId].includes(transaction === null || transaction === void 0 ? void 0 : transaction.caller); const getType = () => { const { type } = transaction; if (type.toUpperCase() === 'TRANSFER') { return isOwnTx ? 'SEND' : 'RECEIVE'; } if (type.toUpperCase() === 'LIQUIDITY') { return `${type.includes('removeLiquidity') ? 'Remove' : 'Add'} Liquidity`; } return type.toUpperCase(); }; const asset = (0, amountParser_1.formatAssetBySymbol)(amount, getSymbol(), icpPrice); const trx = Object.assign(Object.assign({}, asset), { type: getType(), hash, to: (_b = (_a = details === null || details === void 0 ? void 0 : details.to) === null || _a === void 0 ? void 0 : _a.icns) !== null && _b !== void 0 ? _b : (_c = details === null || details === void 0 ? void 0 : details.to) === null || _c === void 0 ? void 0 : _c.principal, from: ((_e = (_d = details === null || details === void 0 ? void 0 : details.from) === null || _d === void 0 ? void 0 : _d.icns) !== null && _e !== void 0 ? _e : (_f = details === null || details === void 0 ? void 0 : details.from) === null || _f === void 0 ? void 0 : _f.principal) || caller, date: timestamp, status: constants_1.ACTIVITY_STATUS[details === null || details === void 0 ? void 0 : details.status], logo: ((_h = (_g = details === null || details === void 0 ? void 0 : details.sonicData) === null || _g === void 0 ? void 0 : _g.token) === null || _h === void 0 ? void 0 : _h.logo) || ((_j = details === null || details === void 0 ? void 0 : details.canisterInfo) === null || _j === void 0 ? void 0 : _j.icon) || '', symbol: getSymbol(), canisterId: (details === null || details === void 0 ? void 0 : details.canisterId) || ((_k = details === null || details === void 0 ? void 0 : details.canisterInfo) === null || _k === void 0 ? void 0 : _k.canisterId), canisterInfo: canisterInfo || (details === null || details === void 0 ? void 0 : details.canisterInfo), details: Object.assign(Object.assign({}, details), { amount, fee: (0, amountParser_1.parseFee)(details.fee), caller, token: (details === null || details === void 0 ? void 0 : details.canisterId) && network.tokens.find((token) => token.canisterId === details.canisterId) }) }); return trx; }; exports.formatTransaction = formatTransaction; const formatTransactions = (transactions, principalId, accountId, network, icpPrice) => { const parsedTrx = (transactions === null || transactions === void 0 ? void 0 : transactions.map((trx) => (0, exports.formatTransaction)(trx, principalId, accountId, network, icpPrice))) || []; const sortedTransactions = { total: parsedTrx.length, transactions: parsedTrx.sort((a, b) => b.date - a.date < 0 ? -1 : 1), }; return sortedTransactions; }; exports.formatTransactions = formatTransactions;