raydium-io / raydium-sdk-V2-demo

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

swap clmm error #23

Closed BobosanCrypto closed 4 months ago

BobosanCrypto commented 4 months ago

logs: [ 'Program CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK invoke [1]', 'Program log: Instruction: SwapV2', 'Program log: AnchorError caused by account: observation_state. Error Code: AccountOwnedByWrongProgram. Error Number: 3007. Error Message: The given account is owned by a different program than expected.', 'Program log: Left:', 'Program log: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', 'Program log: Right:', 'Program log: CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK', 'Program CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK consumed 12976 of 200000 compute units', 'Program CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK failed: custom program error: 0xbbf' ]

cruzshia commented 4 months ago

please provide your code, error logs don't have enough information.

BobosanCrypto commented 4 months ago

on create: https://solscan.io/tx/5zJUedcdQibeTDuDzsTr97TFjPydk2gUwC6Q5834WUyKDAKFN9iUobzJFKQFeUWDPTxYAWXwTBikF12wyiZFzLUw on swap v2 : https://solscan.io/tx/f2ozxCbbc8HTcD3WUmwkw8gvFfr8Kn99sJUgokZqgseDDSefsXEHrHrvXkB2kRdX5kqBaTsf3ADXWr1bRCVgZpc

use

BobosanCrypto commented 4 months ago

export const swap = async () => { const raydium = await initSdk() const poolId = '2QdhepnKRTLjjSqPL1PtKNwqrUkoLee5Gqs8bvZhRdMv' const inputAmount = new BN(100) // RAY-USDC pool // note: api doesn't support get devnet pool info const data = await raydium.api.fetchPoolById({ ids: poolId }) const poolInfo = data[0] as ApiV3PoolInfoConcentratedItem

const clmmPoolInfo = await PoolUtils.fetchComputeClmmInfo({ connection: raydium.connection, poolInfo, })

const tickCache = await PoolUtils.fetchMultiplePoolTickArrays({ connection: raydium.connection, poolKeys: [clmmPoolInfo], })

const { minAmountOut, remainingAccounts } = await PoolUtils.computeAmountOutFormat({ poolInfo: clmmPoolInfo, tickArrayCache: tickCache[poolId], amountIn: inputAmount, tokenOut: poolInfo.mintB, slippage: 0.1, epochInfo: await raydium.fetchEpochInfo(), })

const { execute } = await raydium.clmm.swap({ poolInfo, inputMint: poolInfo.mintA.address, amountIn: inputAmount, amountOutMin: minAmountOut.amount.raw, ownerInfo: {}, remainingAccounts, txVersion, })

const { txId } = await execute() console.log('swapped in clmm pool:', { txId })

}

swap()

cruzshia commented 4 months ago

pull latest code and run yarn install update sdk.

BobosanCrypto commented 4 months ago

OK, I'll try, thanks

BobosanCrypto commented 4 months ago

clmm is ok, how to combine clmm and cpmm transactions, Transaction too large?

cruzshia commented 4 months ago

I don't recommend combine them in to 1 tx cuz there might have unexpected issues(e.g. tx too large, duplicate create token acc..etc), if you still want to do that, sdk tx related functions return transaction and builder which contains all tx data, you can combine/reorganize them by yourself.

const { execute, transaction, builder } = await raydium.clmm.swap({
    poolInfo,
    inputMint: poolInfo.mintA.address,
    amountIn: inputAmount,
    amountOutMin: minAmountOut.amount.raw,
    observationId: clmmPoolInfo.observationId,
    ownerInfo: {},
    remainingAccounts,
    txVersion,
  })
  builder.allInstructions
  builder.AllTxData
BobosanCrypto commented 4 months ago

https://solscan.io/tx/2guKCu3NXBghEfyPHy67CLwp1obZfYoMkuBLUb9PsWxjYVYPCV5ae4PitJzc4zi3a9KT5tfrKy2HP9vQHud12r7Z amm and clmm can be combined, if cpmm data is too large. I try to deploy it but it always fails, why? I want to make arbitration

cruzshia commented 4 months ago

different type of pool definitely has different contract and instructions, and tx has size limits, so you can't just combine them, you should check is over limit or not. reference: https://solana.com/docs/core/transactions#transaction-size.