raydium-io / raydium-sdk-V2-demo

Open-source Typescript SDK demos
48 stars 31 forks source link

CPMM token swap insufficient lamports issue #30

Closed tushar-sahoo closed 4 months ago

tushar-sahoo commented 4 months ago
  const inputAmount = new BN(amountToSend);

            const reserveA =
                (poolInfo.mintA.address.toString() === tokenToSend) === bIsTokenSwap
                    ? rpcData.quoteReserve
                    : rpcData.baseReserve;

            const reserveB =
                (poolInfo.mintA.address.toString() === tokenToSend) === bIsTokenSwap
                    ? rpcData.baseReserve
                    : rpcData.quoteReserve;

            const baseIn =
                (poolInfo.mintA.address.toString() === tokenToSend) === bIsTokenSwap
                    ? false
                    : true;

                    console.log(baseIn);

            const swapResult = CurveCalculator.swap(
                inputAmount,
                reserveA,
                reserveB,
                rpcData.configInfo.tradeFeeRate
            );

            const swap = await raydium.cpmm.swap({
                poolInfo,
                swapResult,
                slippage: 0.005,
                baseIn: baseIn,
                computeBudgetConfig: {
                    units: 200000,
                    microLamports: 250000,
                },
            });

This fails with insufficient lamports error if the amount of SOL to be received is less than the balance of the owner account but if I do a transaction in which I get less SOL than the balance of the owner account it succeeds.

cruzshia commented 4 months ago

please ensure you have enough sol in your wallet, this isn't a bug.

tushar-sahoo commented 4 months ago

@cruzshia What I am facing is I have enough SOL in my wallet to cover the trnx fees. I am swapping from a token to SOL. But the error I am getting is that the SOL I already have in my account should be more than the SOL I will receive after the swap.

It is maybe because it is calling swap base out instead of swap base in for token to SOL swap. I did the swap on Raydium v3 UI and they are using swap base in.

cruzshia commented 4 months ago

pull latest code, run yarn install to update sdk and try again

tushar-sahoo commented 4 months ago

Thanks @cruzshia for the update! The code works fine now 💯