UNPKG

@ledgerhq/coin-algorand

Version:
81 lines 3.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountTransactions = void 0; const live_network_1 = __importDefault(require("@ledgerhq/live-network")); const live_env_1 = require("@ledgerhq/live-env"); const bignumber_js_1 = require("bignumber.js"); const LIMIT = 100; // Max nb of transactions per request const BASE_URL = (0, live_env_1.getEnv)("API_ALGORAND_BLOCKCHAIN_EXPLORER_API_ENDPOINT"); const INDEXER_URL = `${BASE_URL}/idx2/v2`; const fullUrl = (route) => `${INDEXER_URL}${route}?limit=${LIMIT}`; const getAccountTransactions = async (address, startAt) => { const url = fullUrl(`/accounts/${address}/transactions`); let nextToken; let newRawTxs = []; const mergedTxs = []; do { let nextUrl = url; if (startAt) { nextUrl = nextUrl.concat(`&min-round=${startAt}`); } if (nextToken) { nextUrl = nextUrl.concat(`&next=${nextToken}`); } const { data } = await (0, live_network_1.default)({ url: nextUrl, }); // FIXME: what is the correct type? Properly type response from api above (data) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore nextToken = data["next-token"]; newRawTxs = data.transactions; newRawTxs.map(parseRawTransaction).forEach(tx => mergedTxs.push(tx)); } while (newRawTxs.length >= LIMIT); return mergedTxs; }; exports.getAccountTransactions = getAccountTransactions; const parseRawTransaction = (tx) => { let details = undefined; if (tx["tx-type"] === "pay") { // If "tx-type" is "pay", we know we received a "payment-transaction" 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") { // If "tx-type" is "axfer", we know we received a "asset-transfer-transaction" 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