chainstacklabs / raydium-sdk-swap-example-typescript

An example to swap tokens on Solana using the Raydium SDK, TypeScript, and Chainstack
MIT License
99 stars 35 forks source link

SWAP OUT - Seems not working (Insufficient lamports) #1

Closed youskypif closed 5 months ago

youskypif commented 5 months ago

Hi,

I'm doing a correct SWAP IN from SOL to TokenB for an amount of 0.001. The transaction is confirmed. But just after I want to do a SWAP OUT from tokenB to SOL what are the settings I must used ?

If I let the same settings but just by changing the IN to OUT, it is not working, it is still mu SOL swapped to my tokenB :-( If I inverting TokenA and TokenB into the settings and I'm putting for the TokenAAMount the number of tokenB I have it is not working also :-(.

could you please advice me ?

Many thanks Regards Greg

soos3d commented 5 months ago

Hey @youskypif, I think a good workaround would be to leave the IN parameter and just swap the addresses from this:

  tokenAAddress: "So11111111111111111111111111111111111111112", // Token to swap for the other, SOL in this case
  tokenBAddress: "FN2dH9S96vtqTBSHa5snvcRG3BbWjwQC8P1XcgZNtWfQ", // USDC address

To this:

  tokenAAddress: "FN2dH9S96vtqTBSHa5snvcRG3BbWjwQC8P1XcgZNtWfQ", // USDC address
  tokenBAddress: "So11111111111111111111111111111111111111112", // Token to swap for the other, SOL in this case

This should allow you to swap back directly. Can you try?

youskypif commented 5 months ago

Many thanks @soos3d,

I was able to do the SWAP In without any issue SOL=>APOO Swap IN, but for the Swap Out, I just swapped the addresses and changed the tokenAAmount to the corresponding amount of APOO recevied from the previous Swap IN, but I had this error "Program log: Error: insufficient funds" Swap OUT.

I just swap 0.003 SOL for testing, I have more than 1 SOL into my wallet and never mind the token I'm trying to swap OUT I have always the same error :-(

Thanks for your help and advice. Regards

soos3d commented 5 months ago

I was just able to swap USDC back to SOL just by inverting the addresses in the config. Solscan

this is the config I used! Try to only swap the addresses and leaving in as parameter.

export const swapConfig = {
  executeSwap: true, // Send tx when true, simulate tx when false
  useVersionedTransaction: true,
  tokenAAmount: 0.1, // Swap 0.01 SOL for USDT in this example
  tokenAAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // Token to swap for the other, SOL in this case
  tokenBAddress: "So11111111111111111111111111111111111111112", // USDC address
  maxLamports: 1500000, // Micro lamports for priority fee
  direction: "in" as "in" | "out", // Swap direction: 'in' or 'out'
  liquidityFile: "https://api.raydium.io/v2/sdk/liquidity/mainnet.json",
  maxRetries: 20,
};