Calculate Max for Bridge
Compute the maximum amount that can be bridged to a destination token across all same-currency holdings on other chains.
Note
SET UP THE SDK BEFORE YOU START:Install and initialize the client first — see Installation and SDK Setup.
calculateMaxForBridge() function to compute the maximum amount that can be bridged to a destination token across all same-currency holdings on other chains. This is useful for populating a "Max" button before calling bridge().
The max is sized against the provider the bridge will actually use: the summed bridge amount is checked against the Mayan threshold (the same decision the real bridge makes), and the receivable max is computed for that provider. A small safety haircut is applied so the suggested amount survives fee drift before execution. The returned provider tells you which path was used.
Method signature
calculateMaxForBridge(
input: BridgeMaxParams,
): Promise<BridgeMaxResult>Parameters
export type BridgeMaxParams = {
toChainId: number;
toTokenSymbol: string;
/** Restrict which source chains are considered (chain IDs). Empty/omitted = all. */
sources?: number[];
};BridgeMaxParams: Parameters for computing the max bridgeable amount.toChainId(number, required): The chain ID of the destination chain.toTokenSymbol(string, required): The symbol of the destination token (e.g.'USDC').sources(number[], optional): Restrict which source chain IDs are considered. When omitted, all same-currency holdings are used.
Example
import type { BridgeMaxParams, BridgeMaxResult } from '@avail-project/nexus-core';
const max: BridgeMaxResult = await client.calculateMaxForBridge({
toChainId: 8453,
toTokenSymbol: 'USDC',
} satisfies BridgeMaxParams);
console.log(`Max bridgeable: ${max.maxAmount} ${max.symbol} via ${max.provider}`);
console.log('Sources used:', max.sources);
// Feed the raw amount straight into a bridge:
await client.bridge({
toChainId: 8453,
toTokenSymbol: 'USDC',
toAmountRaw: max.maxAmountRaw,
});const max = await client.calculateMaxForBridge({
toChainId: 8453,
toTokenSymbol: 'USDC',
sources: [10, 42161], // only Optimism + Arbitrum balances
});Warning
calculateMaxForBridge() throws typed NexusError subclasses on failure. Branch on error.category / error.code.Return Value
BridgeMaxResult object.
export type BridgeMaxResult = {
toChainId: number;
toTokenSymbol: string;
/** Provider the max was sized against ('mayan' once the amount clears the threshold). */
provider: 'nexus' | 'mayan';
maxAmount: string; // Human-readable decimal string
maxAmountRaw: bigint; // Raw integer units — suitable for toAmountRaw in bridge()
symbol: string;
decimals: number;
sources: {
chainId: number;
tokenAddress: Hex;
symbol: string;
decimals: number;
amount: string; // Human-readable portion drawn from this source
}[];
};How is this guide?