typescript-sdk
typescript-sdk copied to clipboard
USDT => USDC => AVAX, how can I do this swap by this SDK
I know how to swap from A to B directly if the pool(A & B) exist. but the pool not exist, but can use a best route, how can I do it. For example USDT => USDC => AVAX
thanks.
uint c = a + b; assert(c>=a & c>=b)
To perform a swap from USDT to USDC to AVAX using the Orca SDK, you can follow these steps:
-
First, you need to get the pool addresses for the USDT/USDC and USDC/AVAX pairs. You can do this by calling the
getPoolAddress()function of theOrcaUtlisclass, passing in the token addresses for USDT, USDC, and AVAX. -
Once you have the pool addresses, you can check if these pools exist by calling the
poolExists()function of theOrcaPoolCacheclass, passing in the pool addresses. -
If the pools exist, you can perform the swap directly by calling the
swap()function of theOrcaPoolclass for each pool, passing in the input token, output token, and the amount to swap. -
If one or both of the pools do not exist, you can use the
getBestRoute()function of theOrcaSwapRouterclass to get the best route for the swap. This function takes in the input token, output token, and the amount to swap, and returns the best route as an array of pool addresses. -
Once you have the best route, you can perform the swap by calling the
swapExactInput()orswapExactOutput()function of theOrcaSwapRouterclass, passing in the input token, output token, the amount to swap, the minimum amount to receive (forswapExactInput()), or the maximum amount to pay (forswapExactOutput()), and the best route array.
Here's some sample code to perform the swap:
const { OrcaUtils, OrcaPoolCache, OrcaSwapRouter } = require('@orca-so/sdk');
// Token addresses
const USDT_ADDRESS = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const USDC_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
const AVAX_ADDRESS = '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7';
// Amount to swap
const AMOUNT = '1000000000000000000'; // 1 USDT
async function swap() {
const pool1Address = await OrcaUtils.getPoolAddress(USDT_ADDRESS, USDC_ADDRESS);
const pool2Address = await OrcaUtils.getPoolAddress(USDC_ADDRESS, AVAX_ADDRESS);
const pool1Exists = await OrcaPoolCache.poolExists(pool1Address);
const pool2Exists = await OrcaPoolCache.poolExists(pool2Address);
if (pool1Exists && pool2Exists) {
// Direct swap
const pool1 = new OrcaPool(pool1Address);
const pool2 = new OrcaPool(pool2Address);
const usdtToUsdc = await pool1.swap(USDT_ADDRESS, USDC_ADDRESS, AMOUNT);
const usdcToAvax = await pool2.swap(USDC_ADDRESS, AVAX_ADDRESS, usdtToUsdc.outputAmount);
} else {
// Best route swap
const router = new OrcaSwapRouter();
const bestRoute = await router.getBestRoute(USDT_ADDRESS, AVAX_ADDRESS, AMOUNT);
const result = await router.swapExactInput(
USDT_ADDRESS,
AVAX_ADDRESS,
AMOUNT,
'0',
bestRoute
);
}
}
swap();
Note that this is just an example and you may need to modify the code to fit your specific use case. Also, make sure to handle errors and check the return values of each function to ensure the swap was successful.
To swap from USDT to AVAX via USDC using the Orca SDK, you can follow these steps:
-
Check if there is a direct pool for USDT/AVAX. If there is, you can use the
tradeExactInputortradeExactOutputfunction to swap directly from USDT to AVAX. -
If there is no direct pool for USDT/AVAX, you can check if there is a pool for USDT/USDC and USDC/AVAX. If both pools exist, you can use the
tradeExactInputortradeExactOutputfunction twice to swap from USDT to USDC and then from USDC to AVAX. -
If there is no direct pool for USDT/AVAX and no pools for USDT/USDC and USDC/AVAX, you can use the
getBestPathfunction to find the best route for the swap. ThegetBestPathfunction returns an array of token addresses representing the path to take for the swap. You can then use thetradeExactInputortradeExactOutputfunction with the path array to execute the swap.
Here is an example code snippet for swapping from USDT to AVAX via USDC:
const orca = require('@orca-so/sdk');
const usdtAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const usdcAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
const avaxAddress = '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7';
const usdtAmount = 100;
const slippageTolerance = 0.5;
const usdtUsdcPool = orca.getPool(usdtAddress, usdcAddress);
const usdcAvaxPool = orca.getPool(usdcAddress, avaxAddress);
if (usdtUsdcPool && usdcAvaxPool) {
// Direct pool exists, swap from USDT to AVAX
const path = [usdtAddress, usdcAddress, avaxAddress];
const amountOutMin = orca.getAmountOutMin(path, usdtAmount, slippageTolerance);
const tx = orca.tradeExactInput(path, usdtAmount, amountOutMin);
console.log(tx);
} else if (usdtUsdcPool && !usdcAvaxPool) {
// Swap from USDT to USDC, then from USDC to AVAX
const usdcAmount = orca.getAmountOut(usdtAddress, usdcAddress, usdtAmount);
const path1 = [usdtAddress, usdcAddress];
const path2 = [usdcAddress, avaxAddress];
const amountOutMin1 = orca.getAmountOutMin(path1, usdtAmount, slippageTolerance);
const amountOutMin2 = orca.getAmountOutMin(path2, usdcAmount, slippageTolerance);
const tx1 = orca.tradeExactInput(path1, usdtAmount, amountOutMin1);
const tx2 = orca.tradeExactInput(path2, usdcAmount, amountOutMin2);
console.log(tx1, tx2);
} else {
// Find the best path and swap
const path = orca.getBestPath(usdtAddress, avaxAddress);
const amountOutMin = orca.getAmountOutMin(path, usdtAmount, slippageTolerance);
const tx = orca.tradeExactInput(path, usdtAmount, amountOutMin);
console.log(tx);
}