Performing a swap

Javascript example using viem

Quote Swap

You may first want to get a quote before executing a transaction to see the estimated amount you will receive.

async function getSwapQuote(
  payTokenAddress: string,
  receiveTokenAddress: string,
  payAmount: string
) {
  try {
    const data = await client.readContract({
      address: contractAddress,
      abi: poolABI,
      functionName: 'quoteSwap',
      args: [payTokenAddress, receiveTokenAddress, payAmount],
    });
    console.log('data', data);
  } catch (error) {
    console.error('Error in quoteSwap:', error);
  }
}

The first item of the array response will be the expected amount of the receiveToken (dont forget to format in decimals), the second item is the fee amount in LP tokens.

Actual Swap

To Perform the actual swap, will require a 4th parameter, minReceiveAmount. This is basically how you set slippage. set to 0 means no slippage.

Sample response.

and the types

Last updated