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 quote for swap
const quote = await swapSDK.getQuote({
srcChain: Chains.Ethereum,
srcAsset: Assets.ETH,
destChain: Chains.Bitcoin,
destAsset: Assets.BTC,
amount: (0.1e18).toString(), // 0.1 ETH
});
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.getStatus({
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('goerli'),
),
});
// Fetch quote for swap
const quote = await swapSDK.getQuote({
srcChain: Chains.Ethereum,
srcAsset: Assets.ETH,
destChain: Chains.Bitcoin,
destAsset: Assets.BTC,
amount: (0.1e18).toString(), // 0.1 ETH
});
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.getStatus({
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 quote = await swapSDK.getQuote({
srcChain: Chains.Bitcoin,
srcAsset: Assets.BTC,
destChain: Chains.Ethereum,
destAsset: Assets.ETH,
amount: (0.05e8).toString(), // 0.05 BTC
});
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',
ccmMetadata: {
message: '0xdeadc0de',
gasBudget: (125000).toString(),
}
});
console.log('depositAddress', depositAddress);
// Fetch swap status
const status = await swapSDK.getStatus({
id: depositAddress.depositChannelId,
});
console.log('status', status);