UNPKG

@lifi/sdk

Version:

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

54 lines 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSuiBalance = getSuiBalance; const withDedupe_js_1 = require("../../utils/withDedupe.js"); const suiClient_js_1 = require("./suiClient.js"); const types_js_1 = require("./types.js"); async function getSuiBalance(walletAddress, tokens) { if (tokens.length === 0) { return []; } const { chainId } = tokens[0]; for (const token of tokens) { if (token.chainId !== chainId) { console.warn('Requested tokens have to be on the same chain.'); } } return getSuiBalanceDefault(chainId, tokens, walletAddress); } const getSuiBalanceDefault = async (_chainId, tokens, walletAddress) => { const [coins, checkpoint] = await Promise.allSettled([ (0, withDedupe_js_1.withDedupe)(() => (0, suiClient_js_1.callSuiWithRetry)((client) => client.getAllBalances({ owner: walletAddress, })), { id: `${getSuiBalanceDefault.name}.getAllBalances` }), (0, withDedupe_js_1.withDedupe)(() => (0, suiClient_js_1.callSuiWithRetry)((client) => client.getLatestCheckpointSequenceNumber()), { id: `${getSuiBalanceDefault.name}.getLatestCheckpointSequenceNumber` }), ]); const coinsResult = coins.status === 'fulfilled' ? coins.value : []; const blockNumber = checkpoint.status === 'fulfilled' ? BigInt(checkpoint.value) : 0n; const walletTokenAmounts = coinsResult.reduce((tokenAmounts, coin) => { const amount = BigInt(coin.totalBalance); if (amount > 0n) { tokenAmounts[coin.coinType] = amount; } return tokenAmounts; }, {}); const suiTokenBalance = coinsResult.find((coin) => coin.coinType === types_js_1.SuiTokenShortAddress); if (suiTokenBalance?.totalBalance) { walletTokenAmounts[types_js_1.SuiTokenLongAddress] = BigInt(suiTokenBalance.totalBalance); } const tokenAmounts = tokens.map((token) => { if (walletTokenAmounts[token.address]) { return { ...token, amount: walletTokenAmounts[token.address], blockNumber, }; } return { ...token, blockNumber, }; }); return tokenAmounts; }; //# sourceMappingURL=getSuiBalance.js.map