solveOptimalUsdcAmount
The solveOptimalUsdcAmount function finds the optimal USDC amount to bridge by comparing multiple bridge providers. This is particularly useful when you need to bridge tokens to USDC on Avalanche with additional deposit calldata.
Import
import { solveOptimalUsdcAmount } from '@silentswap/sdk';Basic Usage
import { solveOptimalUsdcAmount } from '@silentswap/sdk';
const result = await solveOptimalUsdcAmount(
1, // Source chain ID (Ethereum)
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // Source token (USDC)
'1000000', // Source amount in token units (1 USDC = 1e6)
'0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb', // User address
'0x...' // Optional deposit calldata
);
console.log('USDC Amount Out:', result.usdcAmountOut);
console.log('Actual Amount In:', result.actualAmountIn);
console.log('Provider:', result.provider);
console.log('Allowance Target:', result.allowanceTarget);API Reference
Function Signature
function solveOptimalUsdcAmount(
srcChainId: number,
srcToken: string,
srcAmount: string,
userAddress: string,
depositCalldata?: string,
maxImpactPercent?: number
): Promise<SolveUsdcResult>Parameters
srcChainId(number): Source chain IDsrcToken(string): Source token address (use'0x0'for native tokens)srcAmount(string): Source amount in token units (as string)userAddress(string): User's EVM addressdepositCalldata(string, optional): Deposit calldata for post-bridge execution. If not provided, a phony deposit calldata will be created automatically.maxImpactPercent(number, optional): Maximum price impact percentage allowed (default: from SDK constants)
Return Value
interface SolveUsdcResult {
/** Optimal USDC amount out (in microUSDC) */
usdcAmountOut: bigint;
/** Actual input amount required */
actualAmountIn: bigint;
/** Bridge provider used */
provider: BridgeProvider;
/** Allowance target address (if required by provider) */
allowanceTarget: string;
}Examples
With Deposit Calldata
import { solveOptimalUsdcAmount } from '@silentswap/sdk';
import { encodeFunctionData } from 'viem';
// Create deposit calldata
const depositCalldata = encodeFunctionData({
abi: depositorAbi,
functionName: 'deposit',
args: [/* ... */],
});
const result = await solveOptimalUsdcAmount(
1, // Ethereum
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
'1000000', // 1 USDC
'0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
depositCalldata
);Without Deposit Calldata (Auto Phony)
import { solveOptimalUsdcAmount } from '@silentswap/sdk';
// Phony deposit calldata will be created automatically
const result = await solveOptimalUsdcAmount(
1,
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
'1000000',
'0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
// depositCalldata omitted - will use phony
);With Custom Max Impact
import { solveOptimalUsdcAmount } from '@silentswap/sdk';
const result = await solveOptimalUsdcAmount(
1,
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
'1000000',
'0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
undefined, // No deposit calldata
3.0 // Max 3% price impact
);Error Handling
import { solveOptimalUsdcAmount } from '@silentswap/sdk';
try {
const result = await solveOptimalUsdcAmount(/* ... */);
// Use result
} catch (err) {
if (err instanceof AggregateError) {
// Both providers failed
console.error('All providers failed:', err.errors);
} else if (err.message.includes('Price impact too high')) {
// Price impact exceeded maxImpactPercent
console.error('Price impact too high, try a smaller amount');
} else {
console.error('Solve error:', err);
}
}How It Works
- Initial Quote: Gets an initial quote to estimate expected USDC output
- Overshoot Calculation: Calculates a target USDC amount (base + 1-5% or max +100 USDC)
- Iterative Solving: Tries up to 4 times to find the optimal amount that fits within the source amount budget
- Provider Comparison: Compares multiple providers, selecting the best rate
- Price Impact Check: Ensures the price impact doesn't exceed
maxImpactPercent
Use Cases
Silent Swap Integration
This function is commonly used in Silent Swap operations where you need to:
- Bridge tokens to USDC on Avalanche
- Execute a deposit transaction after bridging
- Find the optimal USDC amount that fits within your budget
Backend Processing
import { solveOptimalUsdcAmount } from '@silentswap/sdk';
async function processBridge(userAddress: string, amount: string) {
const result = await solveOptimalUsdcAmount(
1,
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
amount,
userAddress
);
// Store result for transaction execution
return {
usdcOut: result.usdcAmountOut.toString(),
amountIn: result.actualAmountIn.toString(),
provider: result.provider,
};
}Best Practices
- Handle Price Impact: Always check if the result fits your price impact requirements
- Use Allowance Target: If provided, use the
allowanceTargetfor token approvals - Cache Results: Store results to avoid recalculating for the same parameters
- Monitor Provider Selection: Log which provider was selected for analytics
Related Functions
getBridgeQuote- Get quotes for cross-chain swapsexecuteBridgeTransaction- Execute bridge transactions