@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
51 lines • 2.12 kB
JavaScript
import { withDedupe } from '../../utils/withDedupe.js';
import { callSuiWithRetry } from './suiClient.js';
import { SuiTokenLongAddress, SuiTokenShortAddress } from './types.js';
export 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([
withDedupe(() => callSuiWithRetry((client) => client.getAllBalances({
owner: walletAddress,
})), { id: `${getSuiBalanceDefault.name}.getAllBalances` }),
withDedupe(() => 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 === SuiTokenShortAddress);
if (suiTokenBalance?.totalBalance) {
walletTokenAmounts[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