Skip to content

getBridgeQuote

The getBridgeQuote function fetches quotes from multiple bridge providers and automatically selects the best option based on retention rate.

Import

import { getBridgeQuote } from '@silentswap/sdk';

Basic Usage

import { getBridgeQuote } from '@silentswap/sdk';
 
const quote = await getBridgeQuote(
  1, // Source chain ID (Ethereum)
  '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // Source token (USDC)
  '1000000', // Amount in token units (1 USDC = 1e6)
  43114, // Destination chain ID (Avalanche)
  '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E', // Destination token (USDC.e)
  '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb' // User address
);
 
console.log('Provider:', quote.provider);
console.log('Output Amount:', quote.outputAmount);
console.log('Fee (USD):', quote.feeUsd);
console.log('Slippage:', quote.slippage);
console.log('Retention Rate:', quote.retentionRate);

API Reference

Function Signature

function getBridgeQuote(
  srcChainId: number,
  srcToken: string,
  srcAmount: string,
  dstChainId: number,
  dstToken: string,
  userAddress: `0x${string}`,
  signal?: AbortSignal
): Promise<BridgeQuoteResult>

Parameters

  • srcChainId (number): Source chain ID (e.g., 1 for Ethereum, 43114 for Avalanche)
  • srcToken (string): Source token address (use '0x0' or '0x0000000000000000000000000000000000000000' for native tokens)
  • srcAmount (string): Amount to bridge in source token units (as string to avoid precision loss)
  • dstChainId (number): Destination chain ID
  • dstToken (string): Destination token address
  • userAddress (0x${string}): User's EVM address
  • signal (AbortSignal, optional): Abort signal for cancelling the request

Return Value

interface BridgeQuoteResult {
  /** Selected bridge provider */
  provider: BridgeProvider;
  /** Output amount in destination token units (as string) */
  outputAmount: string;
  /** Input amount required in source token units (as string) */
  inputAmount: string;
  /** Total fee in USD */
  feeUsd: number;
  /** Price slippage/impact percentage */
  slippage: number;
  /** Estimated time in seconds */
  estimatedTime: number;
  /** Retention rate (how much value is retained after fees, 0-1) */
  retentionRate: number;
  /** Number of transactions required */
  txCount: number;
  /** Raw response from the provider */
  rawResponse: any; // Raw response from the selected provider
}

Examples

Bridge Native Token

import { getBridgeQuote } from '@silentswap/sdk';
 
// Bridge ETH from Ethereum to Avalanche
const quote = await getBridgeQuote(
  1, // Ethereum
  '0x0', // Native ETH
  '1000000000000000000', // 1 ETH (in wei)
  43114, // Avalanche
  '0x0', // Native AVAX
  '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
);

With Abort Signal

import { getBridgeQuote } from '@silentswap/sdk';
 
const controller = new AbortController();
 
// Set timeout
setTimeout(() => controller.abort(), 10000); // 10 second timeout
 
try {
  const quote = await getBridgeQuote(
    1,
    '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
    '1000000',
    43114,
    '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E',
    '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
    controller.signal
  );
} catch (err) {
  if (err.name === 'AbortError') {
    console.log('Request cancelled');
  }
}

Converting for Execution

To execute a bridge transaction, convert the quote result:

import { getBridgeQuote, convertQuoteResultToQuote, executeBridgeTransaction } from '@silentswap/sdk';
 
// Get quote result
const quoteResult = await getBridgeQuote(
  1,
  '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
  '1000000',
  43114,
  '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E',
  '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
);
 
// Convert to executable quote
const quote = convertQuoteResultToQuote(quoteResult, 1);
 
// Execute
await executeBridgeTransaction(quote, walletClient, connector, console.log);

Error Handling

import { getBridgeQuote } from '@silentswap/sdk';
 
try {
  const quote = await getBridgeQuote(/* ... */);
  // Use quote
} catch (err) {
  if (err instanceof AggregateError) {
    // Both providers failed
    console.error('All providers failed:', err.errors);
  } else {
    // Other error
    console.error('Quote error:', err);
  }
}

How It Works

  1. Parallel Fetching: Fetches quotes from multiple providers simultaneously
  2. Provider Comparison: Calculates retention rate for each provider
  3. Best Selection: Automatically selects the provider with the highest retention rate
  4. Graceful Fallback: If one provider fails, uses another available provider

Converting to Executable Quote

getBridgeQuote returns a BridgeQuoteResult which contains quote information. To execute a bridge transaction, you need to convert it to a BridgeQuote with transaction data:

import { getBridgeQuote, convertQuoteResultToQuote } from '@silentswap/sdk';
 
// Get quote result
const quoteResult = await getBridgeQuote(/* ... */);
 
// Convert to executable quote with transactions
const quote = convertQuoteResultToQuote(quoteResult, srcChainId);
 
// Now you can execute
await executeBridgeTransaction(quote, walletClient, connector, console.log);

Related Functions