@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
38 lines (37 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkBalance = void 0;
const viem_1 = require("viem");
const errors_1 = require("../errors/errors");
const balance_1 = require("../services/balance");
const sleep_1 = require("../utils/sleep");
const checkBalance = async (walletAddress, step, depth = 0) => {
const token = await (0, balance_1.getTokenBalance)(walletAddress, step.action.fromToken);
if (token) {
const currentBalance = token.amount ?? 0n;
const neededBalance = BigInt(step.action.fromAmount);
if (currentBalance < neededBalance) {
if (depth <= 3) {
await (0, sleep_1.sleep)(200);
await (0, exports.checkBalance)(walletAddress, step, depth + 1);
}
else if ((neededBalance *
BigInt((1 - (step.action.slippage ?? 0)) * 1_000_000_000)) /
1000000000n <=
currentBalance) {
// adjust amount in slippage limits
step.action.fromAmount = currentBalance.toString();
}
else {
const needed = (0, viem_1.formatUnits)(neededBalance, token.decimals);
const current = (0, viem_1.formatUnits)(currentBalance, token.decimals);
let errorMessage = `Your ${token.symbol} balance is too low, you try to transfer ${needed} ${token.symbol}, but your wallet only holds ${current} ${token.symbol}. No funds have been sent.`;
if (currentBalance !== 0n) {
errorMessage += `If the problem consists, please delete this transfer and start a new one with a maximum of ${current} ${token.symbol}.`;
}
throw new errors_1.BalanceError('The balance is too low.', new Error(errorMessage));
}
}
}
};
exports.checkBalance = checkBalance;