Make sure you have your wallet set up. For this tutorial, I’m using a wallet
loaded from the command line.
Swap Types and Directions
Before we dive into the code, it’s important to understand the different ways you can swap tokens with Saros DLMM.Swap Types
There are two types of swaps available:- Exact Input: You specify the exact amount of tokens you want to swap, and the pool will give you the corresponding amount of the other token in return.
- Exact Output: You specify the exact amount of tokens you want to receive, and the pool will calculate how much of the other token you need to provide.
Swap Direction
Swaps can be performed in two directions:- Swap for Y: Swap token X (base token) for token Y (quote token).
- Swap for X: Swap token Y (quote token) for token X (base token).
1. Getting a Quote
Before performing a swap, it’s a good practice to get a quote to see how many tokens you will receive. This helps you to check the price and potential slippage.getQuote:
amount: The amount of tokens you want to swap. In this case,1e4lamports of the input token.isExactInput: We set this totruebecause we are using the “Exact Input” swap type. We are specifying the exact amount of tokens we want to send.swapForY: We set this totrueto swap token X for token Y (PYUSD for WSOL in our case).pair,tokenBase,tokenQuote: These are the details of the pool and tokens we are interacting with.slippage: The percentage of price change you are willing to tolerate. Here it’s0.5%.
getQuote function returns the amount to be swapped and otherAmountOffset, which is the minimum amount of the other token you will receive after accounting for slippage.
2. Executing the Swap
Once we have the quote, we can use it to execute the swap. Theswap function will create the transaction for us.
getQuote. The amount and otherAmountOffset are taken directly from the response of the getQuote call.
