Swapping
Integrations
Javascript SDK
Swap Assets
Overview

Swap Assets

There are two ways to initiate a swap on Chainflip. Both options are fire-and-forget - once the assets are sent to a deposit channel or the vault swap transaction is submitted, the user doesn't need to do anything for the swap to take place.

  1. Request a deposit address: The requestDepositAddress method generates a deposit address on the source chain. The user sends funds to this address to initiate a swap.

  2. Trigger a vault swap: The encodeVaultSwapData method returns an unsigned transaction that encodes the details of the swap. The user signs and submits this transaction to initiate the swap.

Check How Swapping Works and Swapping Basics for more information about how Chainflip swaps work.

Swap Features

The following features are supported by both, deposit channels and vault swaps:

Slippage Protection: fillOrKillParams

The fillOrKillParams object sets a minimum accepted price when initiating a swap. This protects the swapper against price changes between a quote and the execution of a swap. If the minimum price cannot be met with the available liquidity during the specified number of blocks, deposited assets will be refunded to the specified refund address.

ParamDescriptionData type
slippageTolerancePercent(required)The percent of slippage that is acceptable for the swap. The estimatedPricefield from the quote is used to calculate the minimum price for the channel.This field must be left out when specifying a minPrice.string
minPrice(required)Minimum accepted price for swaps triggered through the deposit channel.This field must be left out when specifying a slippageTolerancePercent.string
refundAddress(required)Address on the source chain to which the refund will be sent, if the minimum price cannot be met.string
retryDurationBlocks(required)Number of State Chain blocks after which a deposit is refunded, if the minimum price cannot be met. One State Chain block corresponds to 6 seconds.number

A slippage tolerance can be specified by using the slippageTolerancePercent property. slippageTolerancePercent must be a number between 0 and 100. This percent is used together with the estimatedPrice of the quote to calculate a minimum accepted swap price:

const minimumPrice = quote.estimatedPrice * (1 - Number(slippageTolerancePercent) / 100);

The slippage tolerance is applied relative to the QUOTED price and not the market or index rate. Prices are checked on the AMM level and do not include deposit or broadcast fees.

Alternatively, you can manually provide a minPrice value. The minimum price is the ratio between the human readable destination amount and the human readable source amount, e.g. 3500 for a swap from ETH to USDC if you want to receive after least 3500 USDC per ETH.

Refunds are subject to a broadcast fee on the source chain to pay for the transactions sent by the Chainflip protocol. The boost or deposit fee paid will not be refunded.

Example

const fillOrKillParams = {
  slippageTolerancePercent: quote.recommendedSlippageTolerancePercent, // use recommended slippage tolerance from quote
  refundAddress: '0xa56A6be23b6Cf39D9448FF6e897C29c41c8fbDFF', // address to which assets are refunded
  retryDurationBlocks: 100, // 100 blocks * 6 seconds = 10 minutes before deposits are refunded
}

Cross-Chain Messaging: ccmParams

The optional ccmParams object enables to pass a Cross-Chain Message to a receiver contract/program on the destination chain.

ParamDescriptionData type
message(required)Message (bytes) that is passed to the destination address on the destination chain. The message must be shorter than 15k bytes.string
gasBudget(required)Gas budget for the call on the destination chain. This amount is the amount of gas or compute units required by the user logic on the destination chain.number
ccmAdditionalData(optional)Additional bytes required for the CCM call on the destination chain. This is required for Solana to pass the accounts that are required for the call.string

Example

const ccmParams = {
  message: "0xdeadc0de",
  gasBudget: 250_000,
}