The CoW Protocol SDK provides seamless cross-chain token transfers through integration with multiple bridge providers. This enables you to swap tokens across different blockchain networks in a single transaction.
const parameters: QuoteBridgeRequest = { // Cross-chain orders are always SELL orders (BUY not supported yet) kind: OrderKind.SELL, // Sell token (and source chain) sellTokenChainId: SupportedChainId.ARBITRUM_ONE, sellTokenAddress: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14', sellTokenDecimals: 18, // Buy token (and target chain) buyTokenChainId: SupportedChainId.BASE, buyTokenAddress: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59', buyTokenDecimals: 18, // Amount to sell amount: '120000000000000000', signer: adapter.signer, // Optional parameters appCode: 'YOUR_APP_CODE',}// Get a quote (and the post callback) for a cross-chain swapconst quoteResult = await sdk.getQuote(parameters)// Assert that the quote result is of type BridgeQuoteAndPost// (type for cross-chain quotes, as opposed to QuoteAndPost for single-chain quotes).assertIsBridgeQuoteAndPost(quoteResult)const { swap, bridge, postSwapOrderFromQuote } = quoteResult// Display all data related to the swap (costs, amounts, appData including the bridging hook, etc.)console.log('Swap info', swap)// Display all data related to the bridge (costs, amounts, provider info, hook, and the bridging quote)console.log('Bridge info', bridge)// Get the buy amount after slippage in the target chainconst { buyAmount } = bridge.amountsAndCosts.afterSlippageif (confirm(`You will get at least: ${buyAmount}, ok?`)) { const orderId = await postSwapOrderFromQuote() console.log('Order created, id: ', orderId)}
Cross-chain orders are always SELL orders. BUY orders are not yet supported for cross-chain swaps.
let currentBest: MultiQuoteResult | null = nullconst bestQuote = await bridgingSdk.getBestQuote({ quoteBridgeRequest: parameters, options: { // Called whenever a better quote is found onQuoteResult: (result) => { currentBest = result console.log(`🚀 New best quote from ${result.providerDappId}!`) if (result.quote) { const buyAmount = result.quote.bridge.amountsAndCosts.afterSlippage.buyAmount console.log(`Better quote found: ${buyAmount} tokens`) // Update UI immediately with the new best quote updateBestQuoteInUI(result) } }, totalTimeout: 20000, // 20 seconds total timeout providerTimeout: 5000 // 5 seconds per provider timeout }})console.log('Final best quote:', bestQuote)
Use getBestQuote() when you only need the single best quote. Use getMultiQuotes() when you need to display all available options to users.
// Initially, all configured providers are availableconst allProviders = bridgingSdk.getAvailableProviders()console.log(`Total providers: ${allProviders.length}`)// Filter to use only specific providersbridgingSdk.setAvailableProviders(['across', 'hop-protocol'])// Now only the specified providers will be usedconst filteredProviders = bridgingSdk.getAvailableProviders()console.log(`Active providers: ${filteredProviders.length}`)// Reset to use all providers againbridgingSdk.setAvailableProviders([])const resetProviders = bridgingSdk.getAvailableProviders()console.log(`Reset to all providers: ${resetProviders.length}`)
The multi-quote methods support two types of timeouts:
Copy
Ask AI
const results = await bridgingSdk.getMultiQuotes({ quoteBridgeRequest: parameters, options: { // Global timeout: Maximum time to wait for all providers to complete totalTimeout: 30000, // 30 seconds (default) // Individual provider timeout: Maximum time each provider has to respond providerTimeout: 15000, // 15 seconds (default) onQuoteResult: (result) => { // Handle progressive results console.log(`Received result from ${result.providerDappId}`); } }});
How timeouts work:
providerTimeout: Each provider has this amount of time to complete their quote request. If exceeded, that provider returns a timeout error.
totalTimeout: The total time to wait for all providers. After this time, any remaining providers are marked as timed out.
Providers that complete within their individual timeout but after the global timeout will still be included in the final results.