UNPKG

@ledgerhq/coin-filecoin

Version:
55 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBalance = getBalance; const logs_1 = require("@ledgerhq/logs"); const api_1 = require("../../network/api"); const addresses_1 = require("../../network/addresses"); // getBalance returns the native FIL balance plus any ERC-20 token balances for // contracts passed in tokenContracts. The caller (e.g., the Alpaca generic sync // layer) is responsible for supplying the tracked token contract addresses. // // Zero-balance token entries are included explicitly so callers can distinguish // "zero" from "not yet synced" (specialized task constraint). async function getBalance(address, tokenContracts = []) { const balanceResponse = await (0, api_1.fetchBalances)(address); const nativeBalance = { value: BigInt(balanceResponse.total_balance), asset: { type: "native" }, // Use ?? "0" fallback before BigInt conversion — locked_balance may be absent locked: BigInt(balanceResponse.locked_balance ?? "0"), }; const balances = [nativeBalance]; if (tokenContracts.length === 0) { return balances; } // Convert the FIL address to its Ethereum-compatible form for ERC-20 queries let ethAddr = null; try { ethAddr = (0, addresses_1.convertAddressFilToEth)(address); } catch { (0, logs_1.log)("debug", `[getBalance] address not convertible to eth address: ${address}`); // Cannot fetch ERC-20 balances without an ETH-compatible address — return native only return balances; } for (const rawContract of tokenContracts) { const contractAddr = rawContract.toLowerCase(); try { const rawBalance = await (0, api_1.fetchERC20TokenBalance)(ethAddr, contractAddr); balances.push({ value: BigInt(rawBalance), asset: { type: "erc20", assetReference: contractAddr }, }); } catch (e) { (0, logs_1.log)("error", `[getBalance] failed to fetch ERC20 balance for ${contractAddr}`, e); // Include zero entry even on error — absence would be misread as "not tracked" balances.push({ value: 0n, asset: { type: "erc20", assetReference: contractAddr }, }); } } return balances; } //# sourceMappingURL=getBalance.js.map