Calculate Max for Swap
Compute the maximum amount swappable to a destination token across all available sources — ideal for a Max button.
Note
SET UP THE SDK BEFORE YOU START:Install and initialize the client first — see Installation and SDK Setup.
calculateMaxForSwap() function to compute the maximum amount that can be swapped to a destination token across all available sources. This is useful for populating a "Max" button before calling swapWithExactIn() or swapWithExactOut().
Method signature
calculateMaxForSwap(
input: SwapMaxParams,
): Promise<SwapMaxResult>Parameters
export type SwapMaxParams = {
toChainId: number;
toTokenAddress: Hex;
sources?: Source[];
};
export type Source = {
tokenAddress: Hex;
chainId: number;
};SwapMaxParams: Parameters for thecalculateMaxForSwap()function.toChainId(number, required): The chain ID of the destination chain.toTokenAddress(Hex, required): The contract address of the token to receive on the destination chain.sources(Source[], optional): Restrict which source chains and tokens are considered. If omitted, all available holdings are considered.tokenAddress(Hex): The contract address of the source token.chainId(number): The chain ID of the source chain.
Example
import type { SwapMaxParams, SwapMaxResult } from '@avail-project/nexus-core';
const max = await client.calculateMaxForSwap({
toChainId: 8453,
toTokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC on Base
});
console.log(`Max swappable: ${max.maxAmount} ${max.symbol}`);
console.log('Sources used:', max.sources);const max = await client.calculateMaxForSwap({
toChainId: 8453,
toTokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
sources: [
{ chainId: 10, tokenAddress: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85' },
],
});
// Feed the raw amount straight into an exact-out swap:
await client.swapWithExactOut({
toChainId: 8453,
toTokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
toAmountRaw: max.maxAmountRaw,
});Return Value
SwapMaxResult object.
export type SwapMaxResult = {
toChainId: number;
toTokenAddress: Hex;
maxAmount: string; // Human-readable decimal string
maxAmountRaw: bigint; // Raw amount suitable for toAmountRaw in swapWithExactOut
symbol: string;
decimals: number;
sources: {
chainId: number;
tokenAddress: Hex;
symbol: string;
decimals: number;
amount: string; // Human-readable portion sourced from this chain/token
}[];
};maxAmount(string): The maximum receivable amount as a human-readable decimal string.maxAmountRaw(bigint): The same maximum in raw integer units — suitable fortoAmountRawinswapWithExactOut().symbol/decimals: Metadata for the destination token.sources: The per-source breakdown of holdings that make up the max.
Note
On failure the method throws a typed
NexusError subclass. See the full type definitions on GitHub.How is this guide?