@shogun-sdk/money-legos
Version:
Shogun Money Legos: clients and types for quotes, memes, prices, balances, fees, validations, etc.
51 lines • 1.79 kB
JavaScript
import { formatUnits } from 'viem';
import { compareAddresses } from './address.js';
import { NATIVE_TOKEN, SOL_NATIVE } from '../config/addresses.js';
import { MIN_NATIVE_TOKEN_BALANCE } from './rpc.js';
export const getMaxAmount = ({ balance, tokenAddress, chainId, decimals, fees, quote, }) => {
if (!balance || !decimals || Number.isNaN(Number(balance))) {
return {
maxAmount: null,
needRecalculateMaxValue: false,
};
}
const isNative = compareAddresses(tokenAddress, NATIVE_TOKEN.ETH) || compareAddresses(tokenAddress, SOL_NATIVE);
if (isNative) {
let txCost = BigInt(0);
if (quote) {
txCost = fees?.estimatedFees || BigInt(0);
if (txCost > balance) {
return {
maxAmount: formatUnits(balance, decimals),
needRecalculateMaxValue: true,
};
}
else {
return {
maxAmount: formatUnits(balance - txCost, decimals),
needRecalculateMaxValue: false,
};
}
}
else {
txCost = (isNative ? MIN_NATIVE_TOKEN_BALANCE[chainId] : BigInt(0)) ?? BigInt(0);
if (balance < (txCost ?? 0)) {
return {
maxAmount: formatUnits(balance, decimals),
needRecalculateMaxValue: true,
};
}
}
return {
maxAmount: formatUnits(balance - txCost, decimals),
needRecalculateMaxValue: true,
};
}
else {
return {
maxAmount: formatUnits(balance, decimals),
needRecalculateMaxValue: false,
};
}
};
//# sourceMappingURL=token.js.map