raydium-io/raydium-sdk

TypeError: r.mul is not a function when using CurveCalculator.swap

akwaaad opened this issue · 1 comments

Getting error when using CurveCalculator.swap

https://github.com/raydium-io/raydium-sdk-V2-demo/blob/ca8615fce7d911eece0370d58f2d67b016482aa9/src/cpmm/swap.ts#L17

TypeError: r.mul is not a function
    at Pl (/home/user/dev/swap/node_modules/@raydium-io/raydium-sdk-v2/lib/index.js:1:330741)
    at ti.tradingFee (/home/user/dev/swap/node_modules/@raydium-io/raydium-sdk-v2/lib/index.js:1:330870)
    at va.swap (/home/user/dev/swap/node_modules/@raydium-io/raydium-sdk-v2/lib/index.js:1:332145)
    at swapCPMM (/home/user/dev/swap/api.js:591:40)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /home/user/dev/swap/api.js:558:17

rpcData looking good

{
  configId: PublicKey [PublicKey(D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2)] {
    _bn: <BN: b3213fba8bf9c87fa91e47819628c383e00bea7e98c7a03e03ba1069cfc3f6f3>
  },
  poolCreator: PublicKey [PublicKey(C7h1Y2Cb6WgEvdevwtbCou84QAWAChcYQSjaYQyFGRDz)] {
    _bn: <BN: a5274794dbcb0fd9b1d723de7066d760fee1f0db70df02402148536679ffdec9>
  },
  vaultA: PublicKey [PublicKey(DGp5ATedNw8QrPfQ7d2rQfRM5pdtXFKTkLNUqutzjDeC)] {
    _bn: <BN: b6592df274cbe3e557a3af59e0423f823cab0dc1bc1cd13d3e4f0df1dd9fe61d>
  },
  vaultB: PublicKey [PublicKey(21vjV4jnYfdXb9eoKYa5B9oPStS7BB4okzUtydRLbzq1)] {
    _bn: <BN: f187143784c19abed45d053ab56c485cdd9cb4e05036ad8d40bf0ee4c6c2a64>
  },
  mintLp: PublicKey [PublicKey(J7i1qxs4inP1F9kGarWMQ2u4LA7nrt8GWwcMv67rATqb)] {
    _bn: <BN: fe4e89b451358e33cf8ae30458e44fa5c526773408833c33b542b9f8e9af8ba2>
  },
  mintA: PublicKey [PublicKey(So11111111111111111111111111111111111111112)] {
    _bn: <BN: 69b8857feab8184fb687f634618c035dac439dc1aeb3b5598a0f00000000001>
  },
  mintB: PublicKey [PublicKey(4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R)] {
    _bn: <BN: 37998ccbf2d0458b615cbcc6b1a367c4749e9fef7306622e1b1b58910120bc9a>
  },
  mintProgramA: PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)] {
    _bn: <BN: 6ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9>
  },
  mintProgramB: PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)] {
    _bn: <BN: 6ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9>
  },
  observationId: PublicKey [PublicKey(BjTjqFpR3X2zGqVej3UGKoGKmvHyFRXFvripQ5D3N7xm)] {
    _bn: <BN: 9f757f1d76dadc6f6627e83e7a21d841bffd7a0360650a06383e294566380de2>
  },
  bump: 253,
  status: 0,
  lpDecimals: 9,
  mintDecimalA: 9,
  mintDecimalB: 6,
  lpAmount: <BN: 209838b>,
  protocolFeesMintA: <BN: da9>,
  protocolFeesMintB: <BN: 114>,
  fundFeesMintA: <BN: 488>,
  fundFeesMintB: <BN: 5a>,
  openTime: <BN: 6644b27e>,
  baseReserve: <BN: 68c41a9>,
  quoteReserve: <BN: a2518e>,
  configInfo: {
    bump: 255,
    disableCreatePool: false,
    index: 0,
    tradeFeeRate: <BN: 9c4>,
    protocolFeeRate: <BN: 1d4c0>,
    fundFeeRate: <BN: 9c40>,
    createPoolFee: <BN: 8f0d180>,
    protocolOwner: PublicKey [PublicKey(ProCXqRcXJjoUd1RNoo28bSizAA6EEqt9wURZYPDc5u)] {
      _bn: <BN: 5db153a0ff487f69cde89001fec4c4695b6d4215ea244b87bd4eb7287f857a8>
    },
    fundOwner: PublicKey [PublicKey(FUNDduJTA7XcckKHKfAoEnnhuSud2JUCUZv6opWEjrBU)] {
      _bn: <BN: d7064a38f6d17b113e5f66dcfb63b8737ffce2644abf0a00b98b34356d638583>
    }
  }
}

Issue was that I was putting amountToSwap with making a new BN instance

const swapResult = CurveCalculator.swap(
        new BN(amountToSwap),
        rpcData.baseReserve,
        rpcData.quoteReserve,
        rpcData.configInfo.tradeFeeRate
    )