typescript-sdk icon indicating copy to clipboard operation
typescript-sdk copied to clipboard

USDT => USDC => AVAX, how can I do this swap by this SDK

Open eastonqiu opened this issue 3 years ago • 3 comments

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.

eastonqiu avatar May 26 '22 14:05 eastonqiu

uint c = a + b; assert(c>=a & c>=b)

aUsABuisnessman avatar May 27 '22 05:05 aUsABuisnessman

To perform a swap from USDT to USDC to AVAX using the Orca SDK, you can follow these steps:

  1. 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 the OrcaUtlis class, passing in the token addresses for USDT, USDC, and AVAX.

  2. Once you have the pool addresses, you can check if these pools exist by calling the poolExists() function of the OrcaPoolCache class, passing in the pool addresses.

  3. If the pools exist, you can perform the swap directly by calling the swap() function of the OrcaPool class for each pool, passing in the input token, output token, and the amount to swap.

  4. If one or both of the pools do not exist, you can use the getBestRoute() function of the OrcaSwapRouter class 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.

  5. Once you have the best route, you can perform the swap by calling the swapExactInput() or swapExactOutput() function of the OrcaSwapRouter class, passing in the input token, output token, the amount to swap, the minimum amount to receive (for swapExactInput()), or the maximum amount to pay (for swapExactOutput()), 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.

staccDOTsol avatar Mar 23 '23 04:03 staccDOTsol

To swap from USDT to AVAX via USDC using the Orca SDK, you can follow these steps:

  1. Check if there is a direct pool for USDT/AVAX. If there is, you can use the tradeExactInput or tradeExactOutput function to swap directly from USDT to AVAX.

  2. 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 tradeExactInput or tradeExactOutput function twice to swap from USDT to USDC and then from USDC to AVAX.

  3. If there is no direct pool for USDT/AVAX and no pools for USDT/USDC and USDC/AVAX, you can use the getBestPath function to find the best route for the swap. The getBestPath function returns an array of token addresses representing the path to take for the swap. You can then use the tradeExactInput or tradeExactOutput function 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);
}

staccDOTsol avatar Mar 24 '23 03:03 staccDOTsol