UNPKG

@ledgerhq/coin-algorand

Version:
83 lines 3.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAllAccountTransactions = exports.getAccountTransactions = void 0; const live_network_1 = __importDefault(require("@ledgerhq/live-network")); const bignumber_js_1 = require("bignumber.js"); const config_1 = __importDefault(require("../config")); const LIMIT = 100; const getIndexerUrl = () => config_1.default.getCoinConfig().indexer; const getAccountTransactions = async (address, options) => { const limit = options?.limit ?? LIMIT; const url = `${getIndexerUrl()}/accounts/${address}/transactions?limit=${limit}`; let nextUrl = url; if (options?.minRound) { nextUrl = nextUrl.concat(`&min-round=${options.minRound}`); } if (options?.nextToken) { nextUrl = nextUrl.concat(`&next=${options.nextToken}`); } const { data } = await (0, live_network_1.default)({ url: nextUrl, }); const transactions = data.transactions.map(parseRawTransaction); const nextToken = data["next-token"]; return { transactions, nextToken }; }; exports.getAccountTransactions = getAccountTransactions; const getAllAccountTransactions = async (address, startAt) => { let nextToken; const mergedTxs = []; do { const result = await (0, exports.getAccountTransactions)(address, { minRound: startAt, nextToken, }); nextToken = result.nextToken; mergedTxs.push(...result.transactions); } while (nextToken); return mergedTxs; }; exports.getAllAccountTransactions = getAllAccountTransactions; const parseRawTransaction = (tx) => { let details = undefined; if (tx["tx-type"] === "pay") { const info = tx["payment-transaction"]; const paymentInfo = { amount: new bignumber_js_1.BigNumber(info.amount), recipientAddress: info.receiver, closeAmount: info["close-amount"] === undefined ? undefined : new bignumber_js_1.BigNumber(info["close-amount"]), closeToAddress: info["close-remainder-to"], }; details = paymentInfo; } else if (tx["tx-type"] === "axfer") { const info = tx["asset-transfer-transaction"]; const assetTransferInfo = { assetAmount: new bignumber_js_1.BigNumber(info.amount), assetId: info["asset-id"].toString(), assetRecipientAddress: info.receiver, assetSenderAddress: tx.sender, assetCloseAmount: info["close-amount"] === undefined ? undefined : new bignumber_js_1.BigNumber(info["close-amount"]), assetCloseToAddress: info["close-to"], }; details = assetTransferInfo; } return { id: tx.id, timestamp: tx["round-time"].toString(), round: tx["confirmed-round"], senderAddress: tx.sender, senderRewards: new bignumber_js_1.BigNumber(tx["sender-rewards"]), recipientRewards: new bignumber_js_1.BigNumber(tx["receiver-rewards"]), closeRewards: tx["close-rewards"] === undefined ? undefined : new bignumber_js_1.BigNumber(tx["close-rewards"]), closeAmount: tx["closing-amount"] === undefined ? undefined : new bignumber_js_1.BigNumber(tx["closing-amount"]), fee: new bignumber_js_1.BigNumber(tx.fee), note: tx.note, type: tx["tx-type"], details, }; }; //# sourceMappingURL=indexer.js.map