@saros-finance/sdk Examples

Learn how to work with the Saros SDK with these complete examples.

Swap
Swap tokens using the Saros SDK.
import sarosSdk from '@saros-finance/sdk';
import { PublicKey } from '@solana/web3.js';

const { getSwapAmountSaros, swapSaros, genConnectionSolana } = sarosSdk;

const connection = genConnectionSolana();
const accountSol = 'YOUR_WALLET_PUBLIC_KEY';

const USDC_TOKEN = {
  mintAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
  addressSPL: 'FXRiEosEvHnpc3XZY1NS7an2PB1SunnYW1f5zppYhXb3',
  decimals: '6'
};

const C98_TOKEN = {
  mintAddress: 'C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9',
  addressSPL: 'EKCdCBjfQ6t5FBfDC2zvmr27PgfVVZU37C8LUE4UenKb',
  decimals: '6'
};

const poolParams = {
  address: '2wUvdZA8ZsY714Y5wUL9fkFmupJGGwzui2N74zqJWgty',
  tokens: {
    [C98_TOKEN.mintAddress]: C98_TOKEN,
    [USDC_TOKEN.mintAddress]: USDC_TOKEN,
  },
  tokenIds: [
    'C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9',
    'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
  ],
};

async function performSwap() {
  const fromTokenAccount = C98_TOKEN.addressSPL;
  const toTokenAccount = USDC_TOKEN.addressSPL;
  const fromMint = C98_TOKEN.mintAddress;
  const toMint = USDC_TOKEN.mintAddress;
  const fromAmount = 1;
  const SLIPPAGE = 0.5;

  const estSwap = await getSwapAmountSaros(
    connection,
    fromMint,
    toMint,
    fromAmount,
    SLIPPAGE,
    poolParams
  );

  const { amountOutWithSlippage } = estSwap;

  const result = await swapSaros(
    connection,
    fromTokenAccount.toString(),
    toTokenAccount.toString(),
    fromAmount,
    amountOutWithSlippage,
    null,
    new PublicKey(poolParams.address),
    new PublicKey('SSwapUtytfBdBn1b9NUGG6foMVPtcWgpRU32HToDUZr'),
    accountSol,
    fromMint,
    toMint
  );

  console.log(result);
}

performSwap();