UNPKG

@getclave/lifi-sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

78 lines (77 loc) 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTokenBalancesByChain = exports.getTokenBalances = exports.getTokenBalance = void 0; const config_1 = require("../config"); const errors_1 = require("../errors/errors"); const typeguards_1 = require("../typeguards"); /** * Returns the balances of a specific token a wallet holds across all aggregated chains. * @param walletAddress - A wallet address. * @param token - A Token object. * @returns An object containing the token and the amounts on different chains. * @throws {BaseError} Throws a ValidationError if parameters are invalid. */ const getTokenBalance = async (walletAddress, token) => { const tokenAmounts = await (0, exports.getTokenBalances)(walletAddress, [token]); return tokenAmounts.length ? tokenAmounts[0] : null; }; exports.getTokenBalance = getTokenBalance; /** * Returns the balances for a list tokens a wallet holds across all aggregated chains. * @param walletAddress - A wallet address. * @param tokens - A list of Token objects. * @returns A list of objects containing the tokens and the amounts on different chains. * @throws {BaseError} Throws a ValidationError if parameters are invalid. */ const getTokenBalances = async (walletAddress, tokens) => { // split by chain const tokensByChain = tokens.reduce((tokens, token) => { if (!tokens[token.chainId]) { tokens[token.chainId] = []; } tokens[token.chainId].push(token); return tokens; }, {}); const tokenAmountsByChain = await (0, exports.getTokenBalancesByChain)(walletAddress, tokensByChain); return Object.values(tokenAmountsByChain).flat(); }; exports.getTokenBalances = getTokenBalances; /** * This method queries the balances of tokens for a specific list of chains for a given wallet. * @param walletAddress - A wallet address. * @param tokensByChain - A list of token objects organized by chain ids. * @returns A list of objects containing the tokens and the amounts on different chains organized by the chosen chains. * @throws {BaseError} Throws a ValidationError if parameters are invalid. */ const getTokenBalancesByChain = async (walletAddress, tokensByChain) => { if (!walletAddress) { throw new errors_1.ValidationError('Missing walletAddress.'); } const tokenList = Object.values(tokensByChain).flat(); const invalidTokens = tokenList.filter((token) => !(0, typeguards_1.isToken)(token)); if (invalidTokens.length) { throw new errors_1.ValidationError('Invalid tokens passed.'); } const tokenAmountsByChain = {}; const tokenAmountsSettled = await Promise.allSettled(Object.keys(tokensByChain).map(async (chainIdStr) => { const chainId = Number.parseInt(chainIdStr); const chain = await config_1.config.getChainById(chainId); const provider = config_1.config .get() .providers.find((provider) => provider.isAddress(walletAddress)); if (!provider) { throw new Error(`SDK Token Provider for ${chain.chainType} is not found.`); } const tokenAmounts = await provider.getBalance(walletAddress, tokensByChain[chainId]); tokenAmountsByChain[chainId] = tokenAmounts; })); if (config_1.config.get().debug) { for (const result of tokenAmountsSettled) { if (result.status === 'rejected') { console.warn("Couldn't fetch token balance.", result.reason); } } } return tokenAmountsByChain; }; exports.getTokenBalancesByChain = getTokenBalancesByChain;