@ledgerhq/coin-algorand
Version:
Ledger Algorand Coin integration
74 lines • 3.13 kB
JavaScript
import network from "@ledgerhq/live-network";
import { getEnv } from "@ledgerhq/live-env";
import { BigNumber } from "bignumber.js";
const LIMIT = 100; // Max nb of transactions per request
const BASE_URL = getEnv("API_ALGORAND_BLOCKCHAIN_EXPLORER_API_ENDPOINT");
const INDEXER_URL = `${BASE_URL}/idx2/v2`;
const fullUrl = (route) => `${INDEXER_URL}${route}?limit=${LIMIT}`;
export 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 network({
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;
};
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(info.amount),
recipientAddress: info.receiver,
closeAmount: info["close-amount"] === undefined ? undefined : new 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(info.amount),
assetId: info["asset-id"].toString(),
assetRecipientAddress: info.receiver,
assetSenderAddress: tx.sender,
assetCloseAmount: info["close-amount"] === undefined ? undefined : new 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(tx["sender-rewards"]),
recipientRewards: new BigNumber(tx["receiver-rewards"]),
closeRewards: tx["close-rewards"] === undefined ? undefined : new BigNumber(tx["close-rewards"]),
closeAmount: tx["closing-amount"] === undefined ? undefined : new BigNumber(tx["closing-amount"]),
fee: new BigNumber(tx.fee),
note: tx.note,
type: tx["tx-type"],
details,
};
};
//# sourceMappingURL=indexer.js.map