The Nexus SDK includes intelligent optimization algorithms that automatically choose the most efficient execution path for your operations. These optimizations reduce costs, improve speed, and enhance user experience without requiring any additional code.
All optimizations are automatic and transparent. The SDK analyzes available balances and routes to determine the optimal execution strategy.
During bridge-and-execute operations, the SDK automatically detects when you already have sufficient funds on the destination chain and skips the bridge entirely.
If you have some balance but not enough, the SDK bridges only the shortfall:
// You need 100 USDC on Ethereum// You have 40 USDC already on Ethereum// SDK automatically bridges only 60 USDCconst result = await sdk.bridgeAndExecute({ token: 'USDC', amount: 100_000_000n, // 100 USDC needed toChainId: 1, execute: { /* ... */ },});// Result includes intent showing only 60 USDC was bridgedconsole.log('Bridged amount:', result.intent?.destination.amount); // "60"
The SDK accounts for gas requirements and token approvals when calculating the optimal bridge amount.
For native tokens (ETH, MATIC, etc.), the optimization considers both:
Value requirement — ETH needed for the operation
Gas requirement — ETH needed to pay for gas
// Execute operation with native ETHconst result = await sdk.bridgeAndExecute({ token: 'ETH', amount: 1_000_000_000_000_000_000n, // 1 ETH toChainId: 1, execute: { to: '0xContract', value: 1_000_000_000_000_000_000n, // Send 1 ETH to contract data: '0x...', // Plus execute some function },});// SDK ensures you have:// 1. Enough ETH for the value transfer (1 ETH)// 2. Enough ETH for gas// Bridges the total deficit if needed
// Transfer USDC to a recipient on Arbitrumconst result = await sdk.bridgeAndTransfer({ token: 'USDC', amount: 50_000_000n, // 50 USDC toChainId: 42161, // Arbitrum recipient: '0x742d35Cc6634C0532925a3b8D4C9db96c4b4Db45',});console.log('Transfer complete:', result.explorerUrl);// If you had 50+ USDC on Arbitrum already:// - SDK uses direct transfer (single transaction)// - Explorer URL is the direct transfer on Arbitrum// If you didn't have enough:// - SDK bridges from other chains// - Explorer URL is the destination chain transaction
// Conditions for direct transfer:// 1. Sufficient token balance on destination chain// 2. Sufficient gas balance for transaction// 3. Same token (no swap needed)// ✅ Direct transfer will be usedawait sdk.bridgeAndTransfer({ token: 'USDC', amount: 10_000_000n, // You have 10+ USDC on destination toChainId: 137, // Polygon recipient: '0x...', // And you have MATIC for gas});// ❌ Bridge will be used (insufficient balance)await sdk.bridgeAndTransfer({ token: 'USDC', amount: 100_000_000n, // You only have 10 USDC on destination toChainId: 137, recipient: '0x...',});
// Specify gas to supply on destinationconst result = await sdk.bridge({ token: 'USDC', amount: 100_000_000n, toChainId: 137, // Polygon gas: 1_000_000_000_000_000_000n, // Supply 1 MATIC for gas});// SDK automatically:// 1. Bridges 100 USDC// 2. Also bridges 1 MATIC for gas// 3. Delivers both to destination in single operation
For execute operations, gas requirements are automatically calculated:
const result = await sdk.bridgeAndExecute({ token: 'USDC', amount: 100_000_000n, toChainId: 1, execute: { to: '0xContract', data: '0x...', gas: 500000n, // Specify gas limit },});// SDK automatically:// 1. Estimates gas price on Ethereum// 2. Calculates ETH needed for gas// 3. Bridges additional ETH if you don't have enough// 4. Executes the contract call
The SDK caches allowance checks to reduce RPC calls:
// First operation - checks allowanceawait sdk.bridge({ /* ... */ });// Second operation within same session// - Uses cached allowance if no new approval neededawait sdk.bridge({ /* ... */ });