typescript-sdk
typescript-sdk copied to clipboard
Swap through multiple pools in one transaction
I can't find a way how to swap through multiple pools in one transaction. For example, I want to swap from GST to USDC, then USDC to USDT and do it in 1 transaction
Swapping through multiple pools in one transaction is possible in Orca. To do this, you can use the multi-hop feature.
Here's an example of how you can swap from GST to USDC, then USDC to USDT in one transaction:
-
First, you need to get the
routefor the trade. You can get the route by calling thegetRoutefunction on theTokenSwapprogram. ThegetRoutefunction takes two arguments: thefromtoken address and thetotoken address. You can get the token addresses from the Orca website or from the Solana Explorer. -
Once you have the route, you can call the
swapfunction on theTokenSwapprogram. Theswapfunction takes several arguments, including thefromtoken address, thetotoken address, theamountIn(the amount of thefromtoken you want to swap), theminimumAmountOut(the minimum amount of thetotoken you want to receive), and thedeadline(the deadline for the trade). -
To swap through multiple pools, you can call the
swapfunction multiple times, passing the output token from the previous swap as the input token for the next swap.
Here's an example of how you can swap from GST to USDC, then USDC to USDT in one transaction using the Solana CLI:
# Get the route for the first trade (GST to USDC)
solana program call --address <TOKEN_SWAP_ADDRESS> --data "<GET_ROUTE_DATA>" --no-verify
# Swap GST to USDC
solana program call --address <TOKEN_SWAP_ADDRESS> --data "<SWAP_DATA>" --no-verify
# Get the route for the second trade (USDC to USDT)
solana program call --address <TOKEN_SWAP_ADDRESS> --data "<GET_ROUTE_DATA>" --no-verify
# Swap USDC to USDT
solana program call --address <TOKEN_SWAP_ADDRESS> --data "<SWAP_DATA>" --no-verify
You will need to replace <TOKEN_SWAP_ADDRESS>, <GET_ROUTE_DATA>, and <SWAP_DATA> with the appropriate values for your trade.
To swap through multiple pools in one transaction, you can use the multihop feature of Uniswap V3. This feature allows you to swap between two tokens through multiple pools in a single transaction.
Here's an example of how you can use the multihop feature to swap from GST to USDC, then USDC to USDT:
-
First, you need to identify the pools that you want to use for the swap. In this case, you need to find a pool that has GST and USDC, and another pool that has USDC and USDT.
-
Once you have identified the pools, you need to get the current prices of the tokens in each pool. You can use the
getTick,getSqrtRatioAtTick, andtickSpacingfunctions to get the current prices of the tokens. -
Next, you need to calculate the optimal route for the swap. You can use the
Routestruct to define the route for the swap. In this case, the route would beGST -> USDC -> USDT. -
After defining the route, you can use the
exactInputfunction to execute the swap. This function takes the following parameters:
path: The route for the swap.amountIn: The amount of the input token to swap.amountOutMinimum: The minimum amount of the output token to receive.recipient: The address that will receive the output tokens.deadline: The deadline for the swap.
- Finally, you can call the
exactInputfunction with the appropriate parameters to execute the swap.
Here's some sample code that demonstrates how to use the multihop feature to swap from GST to USDC, then USDC to USDT:
// Define the route for the swap
let route = Route::new(
vec![
PoolAddress::new("0x...")?, // Pool with GST and USDC
PoolAddress::new("0x...")?, // Pool with USDC and USDT
],
Token::new("0x...", 18), // GST
Token::new("0x...", 6), // USDT
)?;
// Get the current prices of the tokens in each pool
let tick0 = pool0.getTick(tickSpacing)?;
let tick1 = pool1.getTick(tickSpacing)?;
let sqrtRatioX96_0 = getSqrtRatioAtTick(tick0)?;
let sqrtRatioX96_1 = getSqrtRatioAtTick(tick1)?;
// Calculate the optimal input amount for the swap
let amountIn = calculateInputAmount(
TokenAmount::new(U256::from(1000), 18), // 1000 GST
sqrtRatioX96_0,
sqrtRatioX96_1,
route.fee,
)?;
// Execute the swap
exactInput(
&route,
TokenAmount::new(amountIn, 18), // Input amount in GST
TokenAmount::new(U256::from(0), 6), // Minimum output amount in USDT
recipient,
U256::from(1234567890), // Deadline
)?;
Note that you will need to replace the pool and token addresses with the actual addresses of the pools and tokens that you want to use for the swap.