1. Overview of Executing a Token Swap
The SDK allows you to swap tokens between different pools on Orca. It handles the calculation of token amounts, manages slippage, and assembles the necessary instructions for executing the swap. With this function, you can:- Swap an exact amount of input tokens for the maximum possible output.
- Specify the desired amount of output tokens and determine the necessary input.
- Control slippage to manage your risk during volatile market conditions.
2. Getting Started Guide
Before we begin, you should have an understanding of what ticks are and how they are stored. If not, you can reference Price & Ticks.Generating a swap quote by input or output token
Generate quote with one of the quote functions:swapQuoteByInputTokenif you want an estimate on the amount of outputToken received on an amount of inputToken.swapQuoteByOutputTokenif you want an estimate on the amount of inputToken needed to receive a set amount of outputToken.
SwapQuote object contains the estimations on the expected amount of tokenIn, tokenOut, fees, projected ending sqrtPrice. When you are ready, plug the quote object directly into the swapIx to perform the trade.
TypeScript
3. Example Usage
Suppose you are developing an arbitrage bot that looks for price discrepancies between different liquidity pools on Orca. By using the SDK, the bot can retrieve the quote object for a potential swap, which includes details about the token amounts and expected output. The bot can quickly compare quotes from multiple pools to identify arbitrage opportunities and execute profitable swaps. You can also use the swap function as part of an LP rebalancing agent: an autonomous agent can periodically check the current pool price, compare it to an open position’s tick range, and trigger a swap to rebalance its token holdings before re-opening a position at the optimal range. Combine this with Monitor Positions and Open Position to build a complete position management loop.4. Next Steps
After successfully executing a token swap, you might want to:- Open a Position: Provide liquidity to the pool and earn fees.
- Monitor Positions: Track your position’s performance over time.
- Build more complex trading strategies by combining multiple swaps.
