Swapping
Integrations
Javascript SDK
Working Examples

Working Examples

Swap ETH to BTC via deposit channel

import { SwapSDK, Chains, Assets } from '@chainflip/sdk/swap';
 
// Initialize SDK
const swapSDK = new SwapSDK({
  network: 'perseverance',
});
 
// Fetch quotes for swap
const { quotes } = await swapSDK.getQuoteV2({
  srcChain: Chains.Ethereum,
  srcAsset: Assets.ETH,
  destChain: Chains.Bitcoin,
  destAsset: Assets.BTC,
  amount: (0.1e18).toString(), // 0.1 ETH
});
 
// find regular quote
const quote = quotes.find((quote) => quote.type === 'REGULAR');
console.log('quote', quote);
 
// Request deposit address for swap
const depositAddress = await swapSDK.requestDepositAddress({
  srcChain: Chains.Ethereum,
  srcAsset: Assets.ETH,
  destChain: Chains.Bitcoin,
  destAsset: Assets.BTC,
  amount: (0.1e18).toString(), // 0.1 ETH
  destAddress: 'tb1p8p3xsgaeltylmvyrskt3mup5x7lznyrh7vu2jvvk7mn8mhm6clksl5k0sm',
});
console.log('depositAddress', depositAddress);
 
// Fetch swap status
const status = await swapSDK.getStatusV2({
  id: depositAddress.depositChannelId,
});
console.log('status', status);

Swap ETH to BTC via Vault contract

import { SwapSDK, Chains, Assets } from '@chainflip/sdk/swap';
import { getDefaultProvider, Wallet } from 'ethers';
 
// Initialize SDK
const swapSDK = new SwapSDK({
  network: 'perseverance',
  signer: new Wallet(
    process.env.PRIVATE_KEY as string,
    getDefaultProvider('sepolia'),
  ),
});
 
// Fetch quote for swap
const { quotes } = await swapSDK.getQuoteV2({
  srcChain: Chains.Ethereum,
  srcAsset: Assets.ETH,
  destChain: Chains.Bitcoin,
  destAsset: Assets.BTC,
  amount: (0.1e18).toString(), // 0.1 ETH
});
 
// find regular quote
const quote = quotes.find((quote) => quote.type === 'REGULAR');
console.log('quote', quote);
 
// Initiate swap via Vault contract
const transactionHash = await swapSDK.executeSwap({
  srcChain: Chains.Ethereum,
  srcAsset: Assets.ETH,
  destChain: Chains.Bitcoin,
  destAsset: Assets.BTC,
  amount: (0.1e18).toString(), // 0.1 ETH
  destAddress: 'tb1p8p3xsgaeltylmvyrskt3mup5x7lznyrh7vu2jvvk7mn8mhm6clksl5k0sm',
});
console.log('transaction', transactionHash);
 
// Fetch swap status
const status = await swapSDK.getStatusV2({
  id: transactionHash,
});
console.log('status', status);

Swap BTC to ETH with Cross-Chain Message

import { SwapSDK, Chains, Assets } from '@chainflip/sdk/swap';
 
// Initialize SDK
const swapSDK = new SwapSDK({
  network: 'perseverance',
});
 
// Fetch quote for swap
const { quotes } = await swapSDK.getQuoteV2({
  srcChain: Chains.Bitcoin,
  srcAsset: Assets.BTC,
  destChain: Chains.Ethereum,
  destAsset: Assets.ETH,
  amount: (0.05e8).toString(), // 0.05 BTC
});
 
// find regular quote
const quote = quotes.find((quote) => quote.type === 'REGULAR');
console.log('quote', quote);
 
// Request deposit address for swap
const depositAddress = await swapSDK.requestDepositAddress({
  srcChain: Chains.Bitcoin,
  srcAsset: Assets.BTC,
  destChain: Chains.Ethereum,
  destAsset: Assets.ETH,
  amount: (0.05e8).toString(), // 0.05 BTC
  destAddress: '0xFcd3C82b154CB4717Ac98718D0Fd13EEBA3D2754',
  ccmParams: {
    message: '0xdeadc0de',
    gasBudget: (125000).toString(),
  }
});
console.log('depositAddress', depositAddress);
 
// Fetch swap status
const status = await swapSDK.getStatusV2({
  id: depositAddress.depositChannelId,
});
console.log('status', status);

Swap ETH to BTC via deposit channel (DCA swap)

import { SwapSDK, Chains, Assets } from '@chainflip/sdk/swap';
 
// Initialize SDK
const swapSDK = new SwapSDK({
  network: 'perseverance',
});
 
// Fetch quotes for swap
const { quotes } = await swapSDK.getQuoteV2({
  srcChain: Chains.Ethereum,
  srcAsset: Assets.ETH,
  destChain: Chains.Bitcoin,
  destAsset: Assets.BTC,
  amount: (5e18).toString(), // 5 ETH
});
 
// find dca quote
const dcaQuote = quotes.find((quote) => quote.type === "DCA");
console.log('quote', dcaQuote);
 
// Request deposit address for swap
const depositAddress = await swapSDK.requestDepositAddress({
  srcChain: Chains.Ethereum,
  srcAsset: Assets.ETH,
  destChain: Chains.Bitcoin,
  destAsset: Assets.BTC,
  amount: (5e18).toString(), // 5 ETH
  destAddress: 'tb1p8p3xsgaeltylmvyrskt3mup5x7lznyrh7vu2jvvk7mn8mhm6clksl5k0sm',
  // `fillOrKillParams` must be provided when requesting deposit address for DCA swaps
  fillOrKillParams: {
    minPrice: (quote.estimatedPrice * 99 / 100).toString(), // minimum accepted price for swaps through the channel
    refundAddress: '0xa56A6be23b6Cf39D9448FF6e897C29c41c8fbDFF', // address to which assets are refunded
    retryDurationBlocks: 100, // 100 blocks * 6 seconds = 10 minutes before deposits are refunded
  },
  dcaParams: {
    chunkIntervalBlocks: dcaQuote?.dcaParams?.chunkIntervalBlocks as number,
    numberOfChunks: dcaQuote?.dcaParams?.numberOfChunks as number,
  },
});
console.log('depositAddress', depositAddress);
 
// Fetch swap status
const status = await swapSDK.getStatusV2({
  id: depositAddress.depositChannelId,
});
 
console.log('status', status);