Encode CF parameters
It is recommended to encode Vault swaps using the SDK's method to encode the Vault Swap data. However, some integrators might want to manually execute the smart contract call to initiate Vault Swaps.
As documented in the
encoding-reference, a
cf_parameters
bytes variable needs to be encoded as part of the Vault Swap
initiation via a smart contract call. While that can be encoded manually, the
SDK provides a method to encode it for convenience.
encodeCfParameters
Returns the hex-encoded CF parameters. This method uses the
broker_encode_cf_parameters
RPC method of the configured
Broker API.
interface EncodeCfParametersRequest {
quote: Quote | BoostQuote;
srcAddress?: string;
destAddress: string;
fillOrKillParams: FillOrKillParamsWithMinPrice | FillOrKillParamsWithSlippage;
affiliateBrokers?: { account: `cF${string}` | `0x${string}`; commissionBps: number }[];
ccmParams?: CcmParams;
brokerAccount?: `cF${string}`;
brokerCommissionBps?: number;
}
class SwapSDK {
encodeCfParameters(cfParameters: EncodeCfParametersRequest): Promise<`0x${string}`>
}
The cfParameters
object describes the swap for which the CF parameters data is encoded.
Param | Description | Data type |
---|---|---|
quote (required) | The object returned by getQuoteV2 . This object will be used to set the input and output assets, the DCA parameters (if it is a DCA quote), and the boost parameters (if it is a boost quote). | Quote |
destAddress (required) | Address where the swapped tokens will be sent to on the destination chain. | string |
fillOrKillParams (required) | Parameters to set a minimum accepted price. This allows to protect against price changes between a quote and the execution of a swap (also known as slippage protection). | Object |
srcAddress (optional) | Address that will sign and send the vault swap transaction. This parameter is required to encode vault swaps starting from Solana. | string |
brokerAccount (optional) | Broker account that recieves the commission for the swap. | string |
brokerCommissionBps (optional) | Commission charged by the broker of the swap, in basis points. This option is only available when setting a brokerAccount or the SDK is initialized with a brokerUrl | number |
affiliateBrokers (optional) | An array of objects defining affiliate accounts that take a commission in addition to brokerCommissionBps . This option is only available when setting a brokerAccount or the SDK is initialized with a brokerUrl | Array |
ccmParams (optional) | Optional parameters for passing a message to a reciever contract/program on the destination chain. | Object |
Result type
The method returns bytes as a hex-encoded string that can be passed to the State Chain.
Example
import { Chains, Assets, SwapSDK } from '@chainflip/sdk/swap';
const swapSDK = new SwapSDK({ network: 'perseverance' });
const {
quotes: [quote],
} = await swapSDK.getQuoteV2({
srcChain: Chains.Ethereum,
srcAsset: Assets.USDC,
destChain: Chains.Bitcoin,
destAsset: Assets.BTC,
amount: (500e6).toString(), // 500 USDC
});
const vaultSwapRequest = {
quote,
destAddress: 'tb1q65700ylwuua9669umxdep76a69jy3tfh55llf5',
fillOrKillParams: {
slippageTolerancePercent: quote.recommendedSlippageTolerancePercent, // use recommended slippage tolerance from quote
refundAddress: '0x36ead71325604dc15d35fae584d7b50646d81753', // address to which assets are refunded
retryDurationBlocks: 100, // 100 blocks * 6 seconds = 10 minutes before deposits are refunded
},
};
const vaultSwapData = await swapSDK.encodeCfParameters(vaultSwapRequest);
console.log(vaultSwapData);
// console output:
'0x006400000036ead71325604dc15d35fae584d7b50646d81753b8b06ebc3b32569bff571d39d21918010000000000000000000000000000000000009e8d88ae895c9b37b2dead9757a3452f7c2299704d91ddfa444d87723f94fe0c000000'