@raydium-io/raydium-sdk-v2
Version:
An SDK for building applications on top of Raydium.
1 lines • 888 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/raydium/tradeV2/instrument.ts","../../../src/common/accountInfo.ts","../../../src/common/logger.ts","../../../src/common/bignumber.ts","../../../node_modules/decimal.js/decimal.mjs","../../../src/module/amount.ts","../../../src/module/formatter.ts","../../../src/module/fraction.ts","../../../src/common/constant.ts","../../../src/raydium/token/constant.ts","../../../src/module/token.ts","../../../src/common/pubKey.ts","../../../src/module/currency.ts","../../../src/module/percent.ts","../../../src/module/price.ts","../../../src/common/lodash.ts","../../../src/common/pda.ts","../../../src/common/txTool/txUtils.ts","../../../src/common/txTool/txType.ts","../../../src/common/programId.ts","../../../src/common/transfer.ts","../../../src/common/txTool/lookupTable.ts","../../../src/common/txTool/txTool.ts","../../../src/common/utility.ts","../../../src/common/fee.ts","../../../src/marshmallow/index.ts","../../../src/marshmallow/buffer-layout.ts","../../../src/raydium/clmm/clmm.ts","../../../src/raydium/token/utils.ts","../../../src/raydium/clmm/instrument.ts","../../../src/raydium/clmm/utils/tick.ts","../../../src/raydium/clmm/utils/constants.ts","../../../src/raydium/clmm/utils/math.ts","../../../src/raydium/clmm/utils/util.ts","../../../src/raydium/clmm/utils/pda.ts","../../../src/raydium/clmm/utils/pool.ts","../../../src/raydium/clmm/utils/position.ts","../../../src/raydium/clmm/utils/tickQuery.ts","../../../src/raydium/clmm/utils/tickarrayBitmap.ts","../../../src/raydium/clmm/layout.ts","../../../src/raydium/liquidity/instruction.ts","../../../src/raydium/liquidity/layout.ts","../../../src/raydium/cpmm/layout.ts","../../../src/raydium/cpmm/instruction.ts","../../../src/raydium/cpmm/pda.ts","../../../src/raydium/cpmm/curve/calculator.ts","../../../src/raydium/cpmm/curve/constantProduct.ts","../../../src/raydium/cpmm/curve/fee.ts"],"sourcesContent":["import { PublicKey, SystemProgram, TransactionInstruction } from \"@solana/web3.js\";\nimport BN from \"bn.js\";\nimport { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from \"@solana/spl-token\";\n\nimport {\n InstructionType,\n LIQUIDITY_POOL_PROGRAM_ID_V5_MODEL,\n MEMO_PROGRAM_ID2,\n accountMeta,\n jsonInfo2PoolKeys,\n getATAAddress,\n ALL_PROGRAM_ID,\n} from \"@/common\";\nimport { seq, struct, u128, u64, u8 } from \"../../marshmallow\";\nimport {\n ClmmInstrument,\n MAX_SQRT_PRICE_X64,\n MAX_SQRT_PRICE_X64_SUB_ONE,\n MIN_SQRT_PRICE_X64,\n MIN_SQRT_PRICE_X64_ADD_ONE,\n ONE,\n getPdaExBitmapAccount,\n} from \"../clmm\";\nimport { makeAMMSwapInstruction, makeAMMSwapV2Instruction } from \"../liquidity/instruction\";\n\nimport { AmmV4Keys, AmmV5Keys, ApiV3PoolInfoItem, ClmmKeys, CpmmKeys, PoolKeys } from \"../../api/type\";\nimport { makeSwapCpmmBaseInInstruction } from \"../../raydium/cpmm\";\nimport { ComputePoolType, MakeSwapInstructionParam, ReturnTypeMakeSwapInstruction } from \"./type\";\nexport function route1Instruction(\n programId: PublicKey,\n poolInfoA: ApiV3PoolInfoItem,\n poolKeyA: PoolKeys,\n poolKeyB: PoolKeys,\n\n userSourceToken: PublicKey,\n userRouteToken: PublicKey,\n // userDestinationToken: PublicKey,\n userPdaAccount: PublicKey,\n ownerWallet: PublicKey,\n\n inputMint: PublicKey,\n\n amountIn: BN,\n amountOut: BN,\n\n tickArrayA?: PublicKey[],\n // tickArrayB?: PublicKey[],\n): TransactionInstruction {\n const dataLayout = struct([u8(\"instruction\"), u64(\"amountIn\"), u64(\"amountOut\")]);\n\n const keys: { pubkey: PublicKey; isSigner: boolean; isWritable: boolean }[] = [\n { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },\n { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },\n { pubkey: new PublicKey(poolKeyA.programId), isSigner: false, isWritable: false },\n { pubkey: new PublicKey(poolKeyA.id), isSigner: false, isWritable: true },\n { pubkey: new PublicKey(poolKeyB.id), isSigner: false, isWritable: true },\n\n { pubkey: userSourceToken, isSigner: false, isWritable: true },\n { pubkey: userRouteToken, isSigner: false, isWritable: true },\n { pubkey: userPdaAccount, isSigner: false, isWritable: true },\n { pubkey: ownerWallet, isSigner: true, isWritable: false },\n ];\n\n if (poolInfoA.type === \"Concentrated\") {\n const poolKey = jsonInfo2PoolKeys(poolKeyA as ClmmKeys);\n keys.push(\n ...[\n { pubkey: poolKey.config.id, isSigner: false, isWritable: false },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n {\n pubkey: poolKey.mintA.address.equals(inputMint) ? poolKey.vault.A : poolKey.vault.B,\n isSigner: false,\n isWritable: true,\n },\n {\n pubkey: poolKey.mintA.address.equals(inputMint) ? poolKey.vault.B : poolKey.vault.A,\n isSigner: false,\n isWritable: true,\n },\n // { pubkey: poolKey.observationId, isSigner: false, isWritable: true }, // to do\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ...tickArrayA!.map((i) => ({ pubkey: i, isSigner: false, isWritable: true })),\n ],\n );\n } else if (poolInfoA.pooltype.includes(\"StablePool\")) {\n const poolKey = jsonInfo2PoolKeys(poolKeyA as AmmV5Keys);\n keys.push(\n ...[\n { pubkey: poolKey.authority, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketProgramId, isSigner: false, isWritable: false },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: new PublicKey(\"CDSr3ssLcRB6XYPJwAfFt18MZvEZp4LjHcvzBVZ45duo\"), isSigner: false, isWritable: false },\n { pubkey: poolKey.openOrders, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketId, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketBids, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketAsks, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketEventQueue, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ],\n );\n } else {\n const poolKey = jsonInfo2PoolKeys(poolKeyA as AmmV4Keys);\n keys.push(\n ...[\n { pubkey: poolKey.authority, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketProgramId, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketAuthority, isSigner: false, isWritable: false },\n { pubkey: poolKey.openOrders, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketId, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketBids, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketAsks, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketEventQueue, isSigner: false, isWritable: true },\n ...(poolKey.marketProgramId.toString() === \"srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX\"\n ? [\n { pubkey: poolKey.marketBaseVault, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketQuoteVault, isSigner: false, isWritable: true },\n ]\n : [\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ]),\n ],\n );\n }\n\n const data = Buffer.alloc(dataLayout.span);\n dataLayout.encode(\n {\n instruction: 4,\n amountIn,\n amountOut,\n },\n data,\n );\n\n return new TransactionInstruction({\n keys,\n programId,\n data,\n });\n}\n\nexport function route2Instruction(\n programId: PublicKey,\n poolInfoB: ApiV3PoolInfoItem,\n poolKeyA: PoolKeys,\n poolKeyB: PoolKeys,\n\n // userSourceToken: PublicKey,\n userRouteToken: PublicKey,\n userDestinationToken: PublicKey,\n userPdaAccount: PublicKey,\n ownerWallet: PublicKey,\n\n routeMint: PublicKey,\n\n // tickArrayA?: PublicKey[],\n tickArrayB?: PublicKey[],\n): TransactionInstruction {\n const dataLayout = struct([u8(\"instruction\")]);\n\n const keys: { pubkey: PublicKey; isSigner: boolean; isWritable: boolean }[] = [\n { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },\n { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },\n { pubkey: new PublicKey(String(poolKeyB.programId)), isSigner: false, isWritable: false },\n { pubkey: new PublicKey(String(poolKeyB.id)), isSigner: false, isWritable: true },\n { pubkey: new PublicKey(String(poolKeyA.id)), isSigner: false, isWritable: true },\n\n { pubkey: userRouteToken, isSigner: false, isWritable: true },\n { pubkey: userDestinationToken, isSigner: false, isWritable: true },\n { pubkey: userPdaAccount, isSigner: false, isWritable: true },\n { pubkey: ownerWallet, isSigner: true, isWritable: false },\n ];\n\n if (poolInfoB.type === \"Concentrated\") {\n const poolKey = jsonInfo2PoolKeys(poolKeyB as ClmmKeys);\n keys.push(\n ...[\n { pubkey: poolKey.config.id, isSigner: false, isWritable: false },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n {\n pubkey: poolKey.mintA.address.equals(routeMint) ? poolKey.vault.A : poolKey.vault.B,\n isSigner: false,\n isWritable: true,\n },\n {\n pubkey: poolKey.mintA.address.equals(routeMint) ? poolKey.vault.B : poolKey.vault.A,\n isSigner: false,\n isWritable: true,\n },\n // { pubkey: poolKey.observationId, isSigner: false, isWritable: true }, // to do\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ...tickArrayB!.map((i) => ({ pubkey: i, isSigner: false, isWritable: true })),\n ],\n );\n } else if (poolInfoB.pooltype.includes(\"StablePool\")) {\n const poolKey = jsonInfo2PoolKeys(poolKeyB as AmmV5Keys);\n keys.push(\n ...[\n { pubkey: poolKey.authority, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketProgramId, isSigner: false, isWritable: false },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: new PublicKey(\"CDSr3ssLcRB6XYPJwAfFt18MZvEZp4LjHcvzBVZ45duo\"), isSigner: false, isWritable: false },\n { pubkey: poolKey.openOrders, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketId, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketBids, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketAsks, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketEventQueue, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ],\n );\n } else {\n const poolKey = jsonInfo2PoolKeys(poolKeyB as AmmV4Keys);\n keys.push(\n ...[\n { pubkey: poolKey.authority, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketProgramId, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketAuthority, isSigner: false, isWritable: false },\n { pubkey: poolKey.openOrders, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketId, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketBids, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketAsks, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketEventQueue, isSigner: false, isWritable: true },\n ...(poolKey.marketProgramId.toString() === \"srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX\"\n ? [\n { pubkey: poolKey.marketBaseVault, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketQuoteVault, isSigner: false, isWritable: true },\n ]\n : [\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ]),\n ],\n );\n }\n\n const data = Buffer.alloc(dataLayout.span);\n dataLayout.encode(\n {\n instruction: 5,\n },\n data,\n );\n\n return new TransactionInstruction({\n keys,\n programId,\n data,\n });\n}\n\n/*\nfunction makeInnerInsKey(\n itemPool: ComputePoolType,\n itemPoolKey: PoolKeys,\n inMint: string,\n userInAccount: PublicKey,\n userOutAccount: PublicKey,\n remainingAccount: PublicKey[] | undefined,\n): accountMeta[] {\n if (itemPool.version === 4) {\n const poolKey = jsonInfo2PoolKeys(itemPoolKey as AmmV4Keys);\n\n return [\n { pubkey: poolKey.programId, isSigner: false, isWritable: false },\n { pubkey: userInAccount, isSigner: false, isWritable: true },\n { pubkey: userOutAccount, isSigner: false, isWritable: true },\n\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.authority, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketProgramId, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketAuthority, isSigner: false, isWritable: true },\n\n { pubkey: poolKey.openOrders, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketId, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketBids, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketAsks, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketEventQueue, isSigner: false, isWritable: true },\n ...(poolKey.marketProgramId.toString() === \"srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX\"\n ? [\n { pubkey: poolKey.marketBaseVault, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketQuoteVault, isSigner: false, isWritable: true },\n ]\n : [\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ]),\n ];\n } else if (itemPool.version === 5) {\n const poolKey = jsonInfo2PoolKeys(itemPoolKey as AmmV4Keys);\n\n return [\n { pubkey: poolKey.programId, isSigner: false, isWritable: false },\n { pubkey: userInAccount, isSigner: false, isWritable: true },\n { pubkey: userOutAccount, isSigner: false, isWritable: true },\n\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.authority, isSigner: false, isWritable: false },\n { pubkey: poolKey.marketProgramId, isSigner: false, isWritable: false },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: new PublicKey(\"CDSr3ssLcRB6XYPJwAfFt18MZvEZp4LjHcvzBVZ45duo\"), isSigner: false, isWritable: false },\n { pubkey: poolKey.openOrders, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketId, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketBids, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketAsks, isSigner: false, isWritable: true },\n { pubkey: poolKey.marketEventQueue, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n ];\n } else if (itemPool.version === 6) {\n const pool = itemPool;\n const poolKey = jsonInfo2PoolKeys(itemPoolKey as ClmmKeys);\n const baseIn = pool.mintA.address === inMint;\n return [\n { pubkey: new PublicKey(String(itemPool.programId)), isSigner: false, isWritable: false },\n { pubkey: userInAccount, isSigner: false, isWritable: true },\n { pubkey: userOutAccount, isSigner: false, isWritable: true },\n { pubkey: poolKey.config.id, isSigner: false, isWritable: false },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: baseIn ? poolKey.vault.A : poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: baseIn ? poolKey.vault.B : poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: itemPool.observationId, isSigner: false, isWritable: true },\n ...(poolKey.mintA.programId.equals(TOKEN_2022_PROGRAM_ID) || poolKey.mintB.programId.equals(TOKEN_2022_PROGRAM_ID)\n ? [\n { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false },\n { pubkey: MEMO_PROGRAM_ID, isSigner: false, isWritable: false },\n { pubkey: baseIn ? poolKey.mintA.address : poolKey.mintB.address, isSigner: false, isWritable: false },\n { pubkey: baseIn ? poolKey.mintB.address : poolKey.mintA.address, isSigner: false, isWritable: false },\n ]\n : []),\n ...(remainingAccount ?? []).map((i) => ({ pubkey: i, isSigner: false, isWritable: true })),\n {\n pubkey: getPdaExBitmapAccount(new PublicKey(String(itemPool.programId)), new PublicKey(itemPool.id)).publicKey,\n isSigner: false,\n isWritable: true,\n },\n ];\n } else if (itemPool.version === 7) {\n const pool = itemPool;\n const poolKey = jsonInfo2PoolKeys(itemPoolKey as CpmmKeys);\n const baseIn = pool.mintA.address === inMint;\n return [\n { pubkey: new PublicKey(String(itemPool.programId)), isSigner: false, isWritable: false },\n { pubkey: userInAccount, isSigner: false, isWritable: true },\n { pubkey: userOutAccount, isSigner: false, isWritable: true },\n { pubkey: poolKey.config.id, isSigner: false, isWritable: false },\n { pubkey: poolKey.id, isSigner: false, isWritable: true },\n { pubkey: baseIn ? poolKey.vault.A : poolKey.vault.B, isSigner: false, isWritable: true },\n { pubkey: baseIn ? poolKey.vault.B : poolKey.vault.A, isSigner: false, isWritable: true },\n { pubkey: itemPool.observationId, isSigner: false, isWritable: true },\n ...(poolKey.mintA.programId.equals(TOKEN_2022_PROGRAM_ID) || poolKey.mintB.programId.equals(TOKEN_2022_PROGRAM_ID)\n ? [\n { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false },\n { pubkey: MEMO_PROGRAM_ID, isSigner: false, isWritable: false },\n { pubkey: baseIn ? poolKey.mintA.address : poolKey.mintB.address, isSigner: false, isWritable: false },\n { pubkey: baseIn ? poolKey.mintB.address : poolKey.mintA.address, isSigner: false, isWritable: false },\n ]\n : []),\n ...(remainingAccount ?? []).map((i) => ({ pubkey: i, isSigner: false, isWritable: true })),\n {\n pubkey: getPdaExBitmapAccount(new PublicKey(String(itemPool.programId)), new PublicKey(itemPool.id)).publicKey,\n isSigner: false,\n isWritable: true,\n },\n ];\n } else {\n throw Error(\"make swap ins error\");\n }\n}\n*/\n\nexport function routeInstruction(\n programId: PublicKey,\n wallet: PublicKey,\n\n userSourceToken: PublicKey,\n userRouteToken: PublicKey,\n userDestinationToken: PublicKey,\n\n inputMint: string,\n routeMint: string,\n outputMint: string,\n\n poolInfoA: ComputePoolType,\n poolInfoB: ComputePoolType,\n\n poolKeyA: PoolKeys,\n poolKeyB: PoolKeys,\n\n amountIn: BN,\n amountOut: BN,\n\n remainingAccounts: (PublicKey[] | undefined)[],\n): TransactionInstruction {\n const clmmPriceLimit: BN[] = [];\n const keys = [\n accountMeta({ pubkey: TOKEN_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: TOKEN_2022_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: SystemProgram.programId, isWritable: false }),\n accountMeta({ pubkey: wallet, isSigner: true }),\n ];\n\n keys.push(accountMeta({ pubkey: userSourceToken }));\n keys.push(accountMeta({ pubkey: userDestinationToken }));\n\n const poolInfos = [poolInfoA, poolInfoB];\n const poolKeys = [poolKeyA, poolKeyB];\n const routeMints = [inputMint, routeMint, outputMint];\n\n for (let index = 0; index < poolInfos.length; index++) {\n const _poolInfo = poolInfos[index];\n const inputIsA = routeMints[index] === _poolInfo.mintA.address;\n keys.push(accountMeta({ pubkey: new PublicKey(_poolInfo.programId), isWritable: false }));\n if (index === poolInfos.length - 1) {\n keys.push(accountMeta({ pubkey: userDestinationToken }));\n } else {\n keys.push(accountMeta({ pubkey: userRouteToken }));\n }\n keys.push(accountMeta({ pubkey: new PublicKey(routeMints[index]) }));\n keys.push(accountMeta({ pubkey: new PublicKey(routeMints[index + 1]) }));\n if (_poolInfo.version === 6) {\n const _poolKey = poolKeys[index] as ClmmKeys;\n\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.config.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? _poolKey.vault.A : _poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? _poolKey.vault.B : _poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolInfo.observationId) })); //todo\n keys.push(accountMeta({ pubkey: MEMO_PROGRAM_ID2 }));\n keys.push(\n accountMeta({\n pubkey: getPdaExBitmapAccount(new PublicKey(_poolInfo.programId), new PublicKey(_poolInfo.id)).publicKey,\n }),\n );\n clmmPriceLimit.push(clmmPriceLimitX64InsData(_poolInfo.sqrtPriceX64.toString(), inputIsA));\n for (const item of remainingAccounts[index] ?? []) {\n keys.push(accountMeta({ pubkey: new PublicKey(item) }));\n }\n } else if (_poolInfo.version === 5) {\n const _poolKey = poolKeys[index] as AmmV5Keys;\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.authority), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketProgramId) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketAuthority) }));\n keys.push(accountMeta({ pubkey: LIQUIDITY_POOL_PROGRAM_ID_V5_MODEL, isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.openOrders) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketId) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketBids) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketAsks) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketEventQueue) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketBaseVault) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketQuoteVault) }));\n } else if (_poolInfo.version === 4) {\n const _poolKey = poolKeys[index] as AmmV4Keys;\n const isSupportIdOnly = _poolInfo.status !== 1;\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.authority), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketProgramId) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketAuthority) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.openOrders) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketId) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketBids) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketAsks) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketEventQueue) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketBaseVault) }));\n keys.push(accountMeta({ pubkey: new PublicKey(isSupportIdOnly ? _poolKey.id : _poolKey.marketQuoteVault) }));\n } else if (_poolInfo.version === 7) {\n const _poolKey = poolKeys[index] as CpmmKeys;\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.authority) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.config.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? _poolKey.vault.A : _poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? _poolKey.vault.B : _poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_poolInfo.observationId) }));\n } else throw Error(\"pool type error\");\n }\n\n const dataLayout = struct([\n u8(\"insId\"),\n u64(\"amountIn\"),\n u64(\"amountOut\"),\n seq(u128(), clmmPriceLimit.length, \"clmmPriceLimit\"),\n ]);\n const data = Buffer.alloc(dataLayout.span);\n dataLayout.encode(\n {\n insId: 0,\n amountIn,\n amountOut,\n clmmPriceLimit,\n },\n data,\n );\n return new TransactionInstruction({\n keys,\n programId,\n data,\n });\n}\n\nfunction clmmPriceLimitX64InsData(x64Price: string | undefined, inputIsA: boolean): BN {\n if (x64Price) {\n if (inputIsA) {\n const _m = new BN(x64Price).div(new BN(25));\n return _m.gt(MIN_SQRT_PRICE_X64_ADD_ONE) ? _m : MIN_SQRT_PRICE_X64_ADD_ONE;\n } else {\n const _m = new BN(x64Price).mul(new BN(25));\n return _m.lt(MAX_SQRT_PRICE_X64_SUB_ONE) ? _m : MAX_SQRT_PRICE_X64_SUB_ONE;\n }\n } else {\n return inputIsA ? MIN_SQRT_PRICE_X64_ADD_ONE : MAX_SQRT_PRICE_X64_SUB_ONE;\n }\n}\n\nexport function makeSwapInstruction({\n routeProgram,\n ownerInfo,\n inputMint,\n swapInfo,\n}: MakeSwapInstructionParam): ReturnTypeMakeSwapInstruction {\n if (swapInfo.routeType === \"amm\") {\n if (swapInfo.poolInfo[0].version === 6) {\n const poolKeys = swapInfo.poolKey[0] as ClmmKeys;\n const _poolKey = jsonInfo2PoolKeys(poolKeys);\n const sqrtPriceLimitX64 = inputMint.equals(_poolKey.mintA.address)\n ? MIN_SQRT_PRICE_X64.add(ONE)\n : MAX_SQRT_PRICE_X64.sub(ONE);\n\n return ClmmInstrument.makeSwapBaseInInstructions({\n poolInfo: poolKeys,\n poolKeys,\n observationId: swapInfo.poolInfo[0].observationId,\n ownerInfo: {\n wallet: ownerInfo.wallet,\n tokenAccountA: _poolKey.mintA.address.equals(inputMint) ? ownerInfo.sourceToken : ownerInfo.destinationToken,\n tokenAccountB: _poolKey.mintA.address.equals(inputMint) ? ownerInfo.destinationToken : ownerInfo.sourceToken,\n },\n inputMint,\n amountIn: swapInfo.amountIn.amount.raw,\n amountOutMin: swapInfo.minAmountOut.amount.raw.sub(swapInfo.minAmountOut.fee?.raw ?? new BN(0)),\n sqrtPriceLimitX64,\n remainingAccounts: swapInfo.remainingAccounts[0] ?? [],\n });\n } else if (swapInfo.poolInfo[0].version === 7) {\n const poolInfo = swapInfo.poolInfo[0];\n const baseIn = inputMint.toString() === swapInfo.poolInfo[0].mintA.address;\n\n return {\n signers: [],\n instructions: [\n makeSwapCpmmBaseInInstruction(\n poolInfo.programId,\n ownerInfo.wallet,\n poolInfo.authority,\n poolInfo.configId,\n poolInfo.id,\n ownerInfo.sourceToken!,\n ownerInfo.destinationToken!,\n baseIn ? poolInfo.vaultA : poolInfo.vaultB,\n baseIn ? poolInfo.vaultB : poolInfo.vaultA,\n baseIn ? poolInfo.mintProgramA : poolInfo.mintProgramB,\n baseIn ? poolInfo.mintProgramB : poolInfo.mintProgramA,\n new PublicKey(poolInfo[baseIn ? \"mintA\" : \"mintB\"].address),\n new PublicKey(poolInfo[baseIn ? \"mintB\" : \"mintA\"].address),\n poolInfo.observationId,\n\n swapInfo.amountIn.amount.raw,\n swapInfo.minAmountOut.amount.raw,\n ),\n ],\n lookupTableAddress: [],\n instructionTypes: [baseIn ? InstructionType.CpmmSwapBaseIn : InstructionType.CpmmSwapBaseOut],\n address: {},\n };\n } else {\n const _poolKey = swapInfo.poolKey[0] as AmmV4Keys | AmmV5Keys;\n\n return {\n signers: [],\n instructions: [\n swapInfo.poolInfo[0].pooltype.includes(\"StablePool\")\n ? makeAMMSwapInstruction({\n poolKeys: _poolKey,\n version: swapInfo.poolInfo[0].pooltype.includes(\"StablePool\") ? 5 : 4,\n userKeys: {\n tokenAccountIn: ownerInfo.sourceToken,\n tokenAccountOut: ownerInfo.destinationToken,\n owner: ownerInfo.wallet,\n },\n amountIn: swapInfo.amountIn.amount.raw,\n amountOut: swapInfo.minAmountOut.amount.raw.sub(swapInfo.minAmountOut.fee?.raw ?? new BN(0)),\n fixedSide: \"in\",\n })\n : makeAMMSwapV2Instruction({\n poolKeys: _poolKey,\n version: swapInfo.poolInfo[0].pooltype.includes(\"StablePool\") ? 5 : 4,\n userKeys: {\n tokenAccountIn: ownerInfo.sourceToken,\n tokenAccountOut: ownerInfo.destinationToken,\n owner: ownerInfo.wallet,\n },\n amountIn: swapInfo.amountIn.amount.raw,\n amountOut: swapInfo.minAmountOut.amount.raw.sub(swapInfo.minAmountOut.fee?.raw ?? new BN(0)),\n fixedSide: \"in\",\n }),\n ],\n lookupTableAddress: _poolKey.lookupTableAccount ? [_poolKey.lookupTableAccount] : [],\n instructionTypes: [\n swapInfo.poolInfo[0].pooltype.includes(\"StablePool\")\n ? InstructionType.AmmV5SwapBaseIn\n : InstructionType.AmmV4SwapBaseIn,\n ],\n address: {},\n };\n }\n } else if (swapInfo.routeType === \"route\") {\n const poolInfo1 = swapInfo.poolInfo[0];\n const poolInfo2 = swapInfo.poolInfo[1];\n const poolKey1 = swapInfo.poolKey[0];\n const poolKey2 = swapInfo.poolKey[1];\n\n if (ownerInfo.routeToken === undefined) throw Error(\"owner route token account check error\");\n\n return {\n signers: [],\n instructions: [\n routeInstruction(\n routeProgram,\n ownerInfo.wallet,\n ownerInfo.sourceToken,\n ownerInfo.routeToken,\n ownerInfo.destinationToken,\n\n inputMint.toString(),\n swapInfo.middleToken.mint.toString(),\n swapInfo.outputMint.toString(),\n\n poolInfo1,\n poolInfo2,\n poolKey1,\n poolKey2,\n\n swapInfo.amountIn.amount.raw,\n swapInfo.minAmountOut.amount.raw.sub(swapInfo.minAmountOut.fee?.raw ?? new BN(0)),\n\n swapInfo.remainingAccounts,\n ),\n ],\n instructionTypes: [InstructionType.RouteSwap],\n lookupTableAddress: [poolKey1.lookupTableAccount, poolKey2.lookupTableAccount].filter(\n (a) => a !== undefined,\n ) as string[],\n address: {},\n };\n } else {\n throw Error(\"route type error\");\n }\n}\n\nexport interface ApiSwapV1Out {\n id: string;\n success: boolean;\n version: \"V0\" | \"V1\";\n openTime?: undefined;\n msg: undefined;\n data: {\n swapType: \"BaseIn\" | \"BaseOut\";\n inputMint: string;\n inputAmount: string;\n outputMint: string;\n outputAmount: string;\n otherAmountThreshold: string;\n slippageBps: number;\n priceImpactPct: number;\n routePlan: {\n poolId: string;\n inputMint: string;\n outputMint: string;\n feeMint: string;\n feeRate: number;\n feeAmount: string;\n remainingAccounts?: string[];\n lastPoolPriceX64?: string;\n }[];\n };\n}\n\nexport function swapBaseInAutoAccount({\n programId,\n wallet,\n amount,\n inputAccount,\n outputAccount,\n routeInfo,\n poolKeys,\n}: {\n programId: PublicKey;\n wallet: PublicKey;\n amount: BN;\n inputAccount: PublicKey;\n outputAccount: PublicKey;\n routeInfo: ApiSwapV1Out;\n poolKeys: PoolKeys[];\n}): TransactionInstruction {\n if (routeInfo.success === false) throw Error(\"route info error\");\n const clmmPriceLimit: BN[] = [];\n const keys = [\n accountMeta({ pubkey: TOKEN_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: TOKEN_2022_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: SystemProgram.programId, isWritable: false }),\n accountMeta({ pubkey: wallet, isSigner: true }),\n ];\n const cacheAccount: { [mint: string]: PublicKey } = {\n [routeInfo.data.inputMint]: inputAccount,\n [routeInfo.data.outputMint]: outputAccount,\n };\n keys.push(accountMeta({ pubkey: cacheAccount[routeInfo.data.inputMint] }));\n keys.push(accountMeta({ pubkey: cacheAccount[routeInfo.data.outputMint] }));\n for (let index = 0; index < poolKeys.length; index++) {\n const _routeInfo = routeInfo.data.routePlan[index];\n const _poolKey = poolKeys[index];\n const inputIsA = _routeInfo.inputMint === _poolKey.mintA.address;\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.programId), isWritable: false }));\n if (index === poolKeys.length - 1) {\n keys.push(accountMeta({ pubkey: cacheAccount[_routeInfo.outputMint] }));\n } else {\n const mint = _routeInfo.outputMint;\n if (cacheAccount[mint] === undefined) {\n const ata = getATAAddress(\n wallet,\n new PublicKey(mint),\n _poolKey.programId === ALL_PROGRAM_ID.CLMM_PROGRAM_ID.toBase58() ||\n _poolKey.programId === ALL_PROGRAM_ID.CREATE_CPMM_POOL_PROGRAM.toBase58()\n ? new PublicKey(inputIsA ? _poolKey.mintB.programId : _poolKey.mintA.programId)\n : TOKEN_PROGRAM_ID,\n ).publicKey;\n cacheAccount[mint] = ata;\n }\n keys.push(accountMeta({ pubkey: cacheAccount[mint] }));\n }\n keys.push(accountMeta({ pubkey: new PublicKey(_routeInfo.inputMint) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_routeInfo.outputMint) }));\n if (_poolKey.programId === ALL_PROGRAM_ID.CLMM_PROGRAM_ID.toBase58()) {\n const poolKey = _poolKey as ClmmKeys;\n\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.config.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolKey.vault.A : poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolKey.vault.B : poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.observationId) }));\n keys.push(accountMeta({ pubkey: MEMO_PROGRAM_ID2, isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.exBitmapAccount) }));\n clmmPriceLimit.push(clmmPriceLimitX64InsData(_routeInfo.lastPoolPriceX64, inputIsA));\n for (const item of _routeInfo.remainingAccounts ?? []) {\n keys.push(accountMeta({ pubkey: new PublicKey(item) }));\n }\n } else if (_poolKey.programId === ALL_PROGRAM_ID.AMM_STABLE.toBase58()) {\n const poolKey = _poolKey as AmmV5Keys;\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.authority), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketProgramId), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketAuthority), isWritable: false }));\n keys.push(accountMeta({ pubkey: LIQUIDITY_POOL_PROGRAM_ID_V5_MODEL, isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.openOrders) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketId) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketBids) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketAsks) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketEventQueue) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketBaseVault) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.marketQuoteVault) }));\n } else if (_poolKey.programId === ALL_PROGRAM_ID.AMM_V4.toBase58()) {\n const poolKey = _poolKey as AmmV4Keys;\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.authority), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketProgramId), isWritable: false }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketAuthority), isWritable: false }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.openOrder) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.A) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.B) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketId) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.bids) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.asks) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.eventQueue) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketVaultA) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketVaultB) }))\n } else if (_poolKey.programId === ALL_PROGRAM_ID.CREATE_CPMM_POOL_PROGRAM.toBase58()) {\n const poolKey = _poolKey as CpmmKeys;\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.authority) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.config.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolKey.vault.A : poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolKey.vault.B : poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.observationId) }));\n } else throw Error(\"pool type error\");\n }\n const dataLayout = struct([\n u8(\"insId\"),\n u64(\"amountIn\"),\n u64(\"amountOut\"),\n seq(u128(), clmmPriceLimit.length, \"clmmPriceLimit\"),\n ]);\n const data = Buffer.alloc(dataLayout.span);\n dataLayout.encode(\n {\n insId: 0,\n amountIn: amount,\n amountOut: new BN(routeInfo.data.otherAmountThreshold),\n clmmPriceLimit,\n },\n data,\n );\n return new TransactionInstruction({\n keys,\n programId,\n data,\n });\n}\n\nexport function swapBaseOutAutoAccount({\n programId,\n wallet,\n inputAccount,\n outputAccount,\n routeInfo,\n poolKeys,\n}: {\n programId: PublicKey;\n wallet: PublicKey;\n inputAccount: PublicKey;\n outputAccount: PublicKey;\n routeInfo: ApiSwapV1Out;\n poolKeys: PoolKeys[];\n}): TransactionInstruction {\n if (routeInfo.success === false) throw Error(\"route info error\");\n const clmmPriceLimit: BN[] = [];\n const keys = [\n accountMeta({ pubkey: TOKEN_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: TOKEN_2022_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isWritable: false }),\n accountMeta({ pubkey: SystemProgram.programId, isWritable: false }),\n accountMeta({ pubkey: wallet, isSigner: true }),\n ];\n const cacheAccount: { [mint: string]: PublicKey } = {\n [routeInfo.data.inputMint]: inputAccount,\n [routeInfo.data.outputMint]: outputAccount,\n };\n for (let index = poolKeys.length - 1; index >= 0; index--) {\n const _routeInfo = routeInfo.data.routePlan[index];\n const _poolKey = poolKeys[index];\n const inputIsA = _routeInfo.inputMint === _poolKey.mintA.address;\n keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.programId) }));\n if (index === 0) {\n keys.push(accountMeta({ pubkey: cacheAccount[_routeInfo.inputMint] }));\n } else {\n const mint = _routeInfo.inputMint;\n if (cacheAccount[mint] === undefined) {\n const ata = getATAAddress(\n wallet,\n new PublicKey(mint),\n _poolKey.programId === ALL_PROGRAM_ID.CLMM_PROGRAM_ID.toBase58() ||\n _poolKey.programId === ALL_PROGRAM_ID.CREATE_CPMM_POOL_PROGRAM.toBase58()\n ? new PublicKey(inputIsA ? _poolKey.mintA.programId : _poolKey.mintB.programId)\n : TOKEN_PROGRAM_ID,\n ).publicKey;\n cacheAccount[mint] = ata;\n }\n keys.push(accountMeta({ pubkey: cacheAccount[mint] }));\n }\n if (index === poolKeys.length - 1) {\n keys.push(accountMeta({ pubkey: cacheAccount[_routeInfo.outputMint] }));\n } else {\n const mint = _routeInfo.outputMint;\n if (cacheAccount[mint] === undefined) {\n const ata = getATAAddress(\n wallet,\n new PublicKey(mint),\n _poolKey.programId === ALL_PROGRAM_ID.CLMM_PROGRAM_ID.toBase58() ||\n _poolKey.programId === ALL_PROGRAM_ID.CREATE_CPMM_POOL_PROGRAM.toBase58()\n ? new PublicKey(inputIsA ? _poolKey.mintB.programId : _poolKey.mintA.programId)\n : TOKEN_PROGRAM_ID,\n ).publicKey;\n cacheAccount[mint] = ata;\n }\n keys.push(accountMeta({ pubkey: cacheAccount[mint] }));\n }\n keys.push(accountMeta({ pubkey: new PublicKey(_routeInfo.inputMint) }));\n keys.push(accountMeta({ pubkey: new PublicKey(_routeInfo.outputMint) }));\n if (_poolKey.programId === ALL_PROGRAM_ID.CLMM_PROGRAM_ID.toBase58()) {\n const poolKey = _poolKey as ClmmKeys;\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.config.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolKey.vault.A : poolKey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolKey.vault.B : poolKey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.observationId) }));\n keys.push(accountMeta({ pubkey: MEMO_PROGRAM_ID2, isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolKey.exBitmapAccount) }));\n clmmPriceLimit.push(clmmPriceLimitX64InsData(_routeInfo.lastPoolPriceX64, inputIsA));\n for (const item of _routeInfo.remainingAccounts ?? []) {\n keys.push(accountMeta({ pubkey: new PublicKey(item) }));\n }\n } else if (_poolKey.programId === ALL_PROGRAM_ID.AMM_STABLE.toBase58()) {\n const poolkey = _poolKey as AmmV5Keys;\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.authority), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketProgramId), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketAuthority), isWritable: false }));\n keys.push(accountMeta({ pubkey: LIQUIDITY_POOL_PROGRAM_ID_V5_MODEL, isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.openOrders) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketId) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketBids) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketAsks) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketEventQueue) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketBaseVault) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.marketQuoteVault) }));\n } else if (_poolKey.programId === ALL_PROGRAM_ID.AMM_V4.toBase58()) {\n const poolkey = _poolKey as AmmV4Keys;\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.authority), isWritable: false }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketProgramId), isWritable: false }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketAuthority), isWritable: false }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.openOrder) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.A) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(poolKey.vault.B) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketId) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.bids) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.asks) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.eventQueue) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketVaultA) }))\n // keys.push(accountMeta({ pubkey: new PublicKey(_poolKey.marketVaultB) }))\n } else if (_poolKey.programId === ALL_PROGRAM_ID.CREATE_CPMM_POOL_PROGRAM.toBase58()) {\n const poolkey = _poolKey as CpmmKeys;\n\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.authority) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.config.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.id) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolkey.vault.A : poolkey.vault.B) }));\n keys.push(accountMeta({ pubkey: new PublicKey(inputIsA ? poolkey.vault.B : poolkey.vault.A) }));\n keys.push(accountMeta({ pubkey: new PublicKey(poolkey.observationId) }));\n } else throw Error(\"pool type error\");\n }\n const dataLayout = struct([\n u8(\"insId\"),\n u64(\"amountIn\"),\n u64(\"amountOut\"),\n seq(u128(), clmmPriceLimit.length, \"clmmPriceLimit\"),\n ]);\n const data = Buffer.alloc(dataLayout.span);\n dataLayout.encode(\n {\n insId: 1,\n amountIn: new BN(routeInfo.data.otherAmountThreshold),\n amountOut: new BN(routeInfo.data.outputAmount),\n clmmPriceLimit,\n },\n data,\n );\n return new TransactionInstruction({\n keys,\n programId,\n data,\n });\n}\n","import { AccountInfo, Commitment, Connection, PublicKey } from \"@solana/web3.js\";\nimport { ReturnTypeFetchMultipleMintInfos } from \"../raydium/type\";\nimport { WSOLMint, chunkArray, solToWSol } from \"./\";\nimport { createLogger } from \"./logger\";\nimport { MINT_SIZE, TOKEN_PROGRAM_ID, getTransferFeeConfig, unpackMint } from \"@solana/spl-token\";\n\ninterface MultipleAc