@ledgerhq/coin-near
Version:
64 lines • 2.47 kB
JavaScript
import network from "@ledgerhq/live-network/network";
import { BigNumber } from "bignumber.js";
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
import { getCoinConfig } from "../config";
const DEFAULT_TRANSACTIONS_LIMIT = 100;
const getIndexerUrl = (route) => {
const currencyConfig = getCoinConfig();
return `${currencyConfig.infra.API_NEAR_INDEXER}${route || ""}`;
};
const fetchTransactions = async (address, limit = DEFAULT_TRANSACTIONS_LIMIT) => {
const route = `/transactions?limit=${limit}&account=${address}&date=${new Date().getTime()}`;
const { data } = await network({
method: "GET",
url: getIndexerUrl(route),
});
return data?.records || [];
};
function isSender(transaction, address) {
return transaction.sender === address;
}
function getOperationType(transaction, address) {
switch (transaction.actions[0]?.data?.method_name) {
case "deposit_and_stake":
return "STAKE";
case "unstake":
case "unstake_all":
return "UNSTAKE";
case "withdraw":
case "withdraw_all":
return "WITHDRAW_UNSTAKED";
default:
return isSender(transaction, address) ? "OUT" : "IN";
}
}
function getOperationValue(transaction, type) {
const amount = transaction.actions[0].data.deposit || 0;
if (type === "OUT") {
return new BigNumber(amount).plus(transaction.fee);
}
return new BigNumber(amount);
}
async function transactionToOperation(accountId, address, transaction) {
const type = getOperationType(transaction, address);
return {
id: encodeOperationId(accountId, transaction.hash, type),
accountId,
fee: new BigNumber(transaction.fee || 0),
value: getOperationValue(transaction, type),
type,
hash: transaction.hash,
blockHash: transaction.block_hash,
blockHeight: transaction.height,
date: new Date(transaction.time),
extra: {},
senders: transaction.sender ? [transaction.sender] : [],
recipients: transaction.receiver ? [transaction.receiver] : [],
hasFailed: !transaction.success,
};
}
export const getOperations = async (accountId, address) => {
const rawTransactions = await fetchTransactions(address);
return await Promise.all(rawTransactions.map(transaction => transactionToOperation(accountId, address, transaction)));
};
//# sourceMappingURL=indexer.js.map