UNPKG

@raydium-io/raydium-sdk-v2

Version:

An SDK for building applications on top of Raydium.

1 lines 772 kB
{"version":3,"sources":["../../../src/raydium/tradeV2/instrument.ts","../../../src/raydium/clmm/clmm.ts","../../../node_modules/decimal.js/decimal.mjs","../../../src/common/logger.ts","../../../src/common/utility.ts","../../../src/module/amount.ts","../../../src/common/bignumber.ts","../../../src/module/token.ts","../../../src/common/pubKey.ts","../../../src/raydium/token/constant.ts","../../../src/module/fraction.ts","../../../src/module/formatter.ts","../../../src/module/price.ts","../../../src/module/currency.ts","../../../src/module/percent.ts","../../../src/common/txTool/txTool.ts","../../../src/common/txTool/txType.ts","../../../src/common/txTool/txUtils.ts","../../../src/common/txTool/lookupTable.ts","../../../src/common/accountInfo.ts","../../../src/common/lodash.ts","../../../src/common/programId.ts","../../../src/common/pda.ts","../../../src/common/transfer.ts","../../../src/raydium/token/utils.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/tick.ts","../../../src/marshmallow/index.ts","../../../src/marshmallow/buffer-layout.ts","../../../src/raydium/clmm/utils/tickarrayBitmap.ts","../../../src/raydium/clmm/layout.ts","../../../src/raydium/clmm/utils/tickQuery.ts","../../../src/raydium/clmm/utils/position.ts","../../../src/raydium/clmm/instrument.ts","../../../src/raydium/liquidity/instruction.ts","../../../src/raydium/liquidity/layout.ts","../../../src/raydium/liquidity/stable.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/fee.ts","../../../src/raydium/cpmm/curve/constantProduct.ts"],"sourcesContent":["import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } from \"@solana/spl-token\";\nimport { PublicKey, TransactionInstruction, SystemProgram } from \"@solana/web3.js\";\nimport BN from \"bn.js\";\n\nimport {\n ClmmInstrument,\n ONE,\n MIN_SQRT_PRICE_X64,\n MAX_SQRT_PRICE_X64,\n MIN_SQRT_PRICE_X64_ADD_ONE,\n MAX_SQRT_PRICE_X64_SUB_ONE,\n getPdaExBitmapAccount,\n} from \"../clmm\";\nimport {\n InstructionType,\n jsonInfo2PoolKeys,\n MEMO_PROGRAM_ID,\n MEMO_PROGRAM_ID2,\n LIQUIDITY_POOL_PROGRAM_ID_V5_MODEL,\n accountMeta,\n} from \"@/common\";\nimport { struct, u64, u8, seq, u128 } from \"@/marshmallow\";\nimport { makeAMMSwapInstruction } from \"../liquidity/instruction\";\n\nimport { ApiV3PoolInfoItem, PoolKeys, ClmmKeys, AmmV4Keys, AmmV5Keys, CpmmKeys } from \"@/api/type\";\nimport { ComputePoolType, MakeSwapInstructionParam, ReturnTypeMakeSwapInstruction } from \"./type\";\nimport { makeSwapCpmmBaseInInInstruction, makeSwapCpmmBaseOutInInstruction } from \"@/raydium/cpmm\";\n\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 makeSwapCpmmBaseInInInstruction(\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 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 ],\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","import { PublicKey } from \"@solana/web3.js\";\nimport Decimal from \"decimal.js\";\nimport { InstructionType, WSOLMint, fetchMultipleMintInfos, getMultipleAccountsInfoWithCustomFlags } from \"@/common\";\nimport { ApiV3PoolInfoConcentratedItem, ClmmKeys } from \"@/api/type\";\nimport { MakeTxData, MakeMultiTxData } from \"@/common/txTool/txTool\";\nimport { TxVersion } from \"@/common/txTool/txType\";\nimport { getATAAddress } from \"@/common\";\nimport { toApiV3Token, toFeeConfig } from \"@/raydium/token/utils\";\nimport { ReturnTypeFetchMultipleMintInfos, ComputeBudgetConfig } from \"@/raydium/type\";\nimport ModuleBase, { ModuleBaseProps } from \"../moduleBase\";\nimport { mockV3CreatePoolInfo, MIN_SQRT_PRICE_X64, MAX_SQRT_PRICE_X64 } from \"./utils/constants\";\nimport { SqrtPriceMath } from \"./utils/math\";\nimport {\n CreateConcentratedPool,\n IncreasePositionFromLiquidity,\n IncreasePositionFromBase,\n DecreaseLiquidity,\n OpenPositionFromBase,\n OpenPositionFromLiquidity,\n InitRewardParams,\n InitRewardsParams,\n SetRewardParams,\n SetRewardsParams,\n CollectRewardParams,\n CollectRewardsParams,\n ManipulateLiquidityExtInfo,\n OpenPositionFromLiquidityExtInfo,\n OpenPositionFromBaseExtInfo,\n ClosePositionExtInfo,\n InitRewardExtInfo,\n HarvestAllRewardsParams,\n ComputeClmmPoolInfo,\n ReturnTypeFetchMultiplePoolTickArrays,\n ClmmRpcData,\n} from \"./type\";\nimport { ClmmInstrument } from \"./instrument\";\nimport { MakeTransaction } from \"../type\";\nimport { MathUtil } from \"./utils/math\";\nimport { PoolUtils, clmmComputeInfoToApiInfo } from \"./utils/pool\";\nimport { getPdaOperationAccount, getPdaPersonalPositionAddress } from \"./utils/pda\";\nimport { ClmmPositionLayout, OperationLayout, PositionInfoLayout, PoolInfoLayout, ClmmConfigLayout } from \"./layout\";\nimport BN from \"bn.js\";\nimport { AccountLayout, TOKEN_PROGRAM_ID } from \"@solana/spl-token\";\n\nexport class Clmm extends ModuleBase {\n constructor(params: ModuleBaseProps) {\n super(params);\n }\n\n public async getClmmPoolKeys(poolId: string): Promise<ClmmKeys> {\n return ((await this.scope.api.fetchPoolKeysById({ idList: [poolId] })) as ClmmKeys[])[0];\n }\n\n public async createPool<T extends TxVersion>(\n props: CreateConcentratedPool<T>,\n ): Promise<MakeTxData<T, { mockPoolInfo: ApiV3PoolInfoConcentratedItem; address: ClmmKeys }>> {\n const {\n programId,\n owner = this.scope.owner?.publicKey || PublicKey.default,\n mint1,\n mint2,\n ammConfig,\n initialPrice,\n startTime,\n computeBudgetConfig,\n forerunCreate,\n getObserveState,\n txVersion,\n } = props;\n const txBuilder = this.createTxBuilder();\n const [mintA, mintB, initPrice] = new BN(new PublicKey(mint1.address).toBuffer()).gt(\n new BN(new PublicKey(mint2.address).toBuffer()),\n )\n ? [mint2, mint1, new Decimal(1).div(initialPrice)]\n : [mint1, mint2, initialPrice];\n\n const initialPriceX64 = SqrtPriceMath.priceToSqrtPriceX64(initPrice, mintA.decimals, mintB.decimals);\n\n const insInfo = await ClmmInstrument.createPoolInstructions({\n connection: this.scope.connection,\n programId,\n owner,\n mintA,\n mintB,\n ammConfigId: ammConfig.id,\n initialPriceX64,\n startTime,\n forerunCreate: !getObserveState && forerunCreate,\n });\n\n txBuilder.addInstruction(insInfo);\n txBuilder.addCustomComputeBudget(computeBudgetConfig);\n\n return txBuilder.versionBuild<{\n mockPoolInfo: ApiV3PoolInfoConcentratedItem;\n address: ClmmKeys;\n forerunCreate?: boolean;\n }>({\n txVersion,\n extInfo: {\n address: {\n ...insInfo.address,\n programId: programId.toString(),\n id: insInfo.address.poolId.toString(),\n mintA,\n mintB,\n openTime: startTime.toString(),\n vault: { A: insInfo.address.mintAVault.toString(), B: insInfo.address.mintBVault.toString() },\n rewardInfos: [],\n config: {\n id: ammConfig.id.toString(),\n index: ammConfig.index,\n protocolFeeRate: ammConfig.protocolFeeRate,\n tradeFeeRate: ammConfig.tradeFeeRate,\n tickSpacing: ammConfig.tickSpacing,\n fundFeeRate: ammConfig.fundFeeRate,\n description: ammConfig.description,\n defaultRange: 0,\n defaultRangePoint: [],\n },\n },\n mockPoolInfo: {\n type: \"Concentrated\",\n rewardDefaultPoolInfos: \"Clmm\",\n id: insInfo.address.poolId.toString(),\n mintA,\n mintB,\n feeRate: ammConfig.tradeFeeRate,\n openTime: startTime.toString(),\n programId: programId.toString(),\n price: initPrice.toNumber(),\n config: {\n id: ammConfig.id.toString(),\n index: ammConfig.index,\n protocolFeeRate: ammConfig.protocolFeeRate,\n tradeFeeRate: ammConfig.tradeFeeRate,\n tickSpacing: ammConfig.tickSpacing,\n fundFeeRate: ammConfig.fundFeeRate,\n description: ammConfig.description,\n defaultRange: 0,\n defaultRangePoint: [],\n },\n ...mockV3CreatePoolInfo,\n },\n forerunCreate,\n },\n }) as Promise<MakeTxData<T, { mockPoolInfo: ApiV3PoolInfoConcentratedItem; address: ClmmKeys }>>;\n }\n\n public async openPositionFromBase<T extends TxVersion>({\n poolInfo,\n poolKeys: propPoolKeys,\n ownerInfo,\n tickLower,\n tickUpper,\n base,\n baseAmount,\n otherAmountMax,\n associatedOnly = true,\n checkCreateATAOwner = false,\n withMetadata = \"create\",\n getEphemeralSigners,\n computeBudgetConfig,\n txVersion,\n }: OpenPositionFromBase<T>): Promise<MakeTxData<T, OpenPositionFromBaseExtInfo>> {\n if (this.scope.availability.addConcentratedPosition === false)\n this.logAndCreateError(\"add position feature disabled in your region\");\n\n this.scope.checkOwner();\n const txBuilder = this.createTxBuilder();\n\n let ownerTokenAccountA: PublicKey | null = null;\n let ownerTokenAccountB: PublicKey | null = null;\n const mintAUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintA.address === WSOLMint.toString();\n const mintBUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintB.address === WSOLMint.toString();\n const [amountA, amountB] = base === \"MintA\" ? [baseAmount, otherAmountMax] : [otherAmountMax, baseAmount];\n\n const { account: _ownerTokenAccountA, instructionParams: _tokenAccountAInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintA.programId,\n mint: new PublicKey(poolInfo.mintA.address),\n owner: this.scope.ownerPubKey,\n\n createInfo:\n mintAUseSOLBalance || amountA.isZero()\n ? {\n payer: this.scope.ownerPubKey,\n amount: amountA,\n }\n : undefined,\n skipCloseAccount: !mintAUseSOLBalance,\n notUseTokenAccount: mintAUseSOLBalance,\n associatedOnly: mintAUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n if (_ownerTokenAccountA) ownerTokenAccountA = _ownerTokenAccountA;\n txBuilder.addInstruction(_tokenAccountAInstruction || {});\n\n const { account: _ownerTokenAccountB, instructionParams: _tokenAccountBInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintB.programId,\n mint: new PublicKey(poolInfo.mintB.address),\n owner: this.scope.ownerPubKey,\n\n createInfo:\n mintBUseSOLBalance || amountB.isZero()\n ? {\n payer: this.scope.ownerPubKey!,\n amount: amountB,\n }\n : undefined,\n skipCloseAccount: !mintBUseSOLBalance,\n notUseTokenAccount: mintBUseSOLBalance,\n associatedOnly: mintBUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n if (_ownerTokenAccountB) ownerTokenAccountB = _ownerTokenAccountB;\n txBuilder.addInstruction(_tokenAccountBInstruction || {});\n\n if (!ownerTokenAccountA || !ownerTokenAccountB)\n this.logAndCreateError(\"cannot found target token accounts\", \"tokenAccounts\", {\n ownerTokenAccountA: ownerTokenAccountA?.toBase58(),\n ownerTokenAccountB: ownerTokenAccountB?.toBase58(),\n });\n\n const poolKeys = propPoolKeys || (await this.getClmmPoolKeys(poolInfo.id));\n const insInfo = await ClmmInstrument.openPositionFromBaseInstructions({\n poolInfo,\n poolKeys,\n ownerInfo: {\n ...ownerInfo,\n feePayer: this.scope.ownerPubKey,\n wallet: this.scope.ownerPubKey,\n tokenAccountA: ownerTokenAccountA!,\n tokenAccountB: ownerTokenAccountB!,\n },\n tickLower,\n tickUpper,\n base,\n baseAmount,\n otherAmountMax,\n withMetadata,\n getEphemeralSigners,\n });\n\n txBuilder.addInstruction(insInfo);\n txBuilder.addCustomComputeBudget(computeBudgetConfig);\n return txBuilder.versionBuild<OpenPositionFromBaseExtInfo>({\n txVersion,\n extInfo: { ...insInfo.address },\n }) as Promise<MakeTxData<T, OpenPositionFromBaseExtInfo>>;\n }\n\n public async openPositionFromLiquidity<T extends TxVersion>({\n poolInfo,\n poolKeys: propPoolKeys,\n ownerInfo,\n amountMaxA,\n amountMaxB,\n tickLower,\n tickUpper,\n liquidity,\n associatedOnly = true,\n checkCreateATAOwner = false,\n withMetadata = \"create\",\n txVersion,\n computeBudgetConfig,\n getEphemeralSigners,\n }: OpenPositionFromLiquidity<T>): Promise<MakeTxData<T, OpenPositionFromLiquidityExtInfo>> {\n if (this.scope.availability.createConcentratedPosition === false)\n this.logAndCreateError(\"open position feature disabled in your region\");\n const txBuilder = this.createTxBuilder();\n\n let ownerTokenAccountA: PublicKey | null = null;\n let ownerTokenAccountB: PublicKey | null = null;\n const mintAUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintA.address === WSOLMint.toBase58();\n const mintBUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintB.address === WSOLMint.toBase58();\n\n const { account: _ownerTokenAccountA, instructionParams: _tokenAccountAInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintA.programId,\n mint: new PublicKey(poolInfo.mintA.address),\n owner: this.scope.ownerPubKey,\n\n createInfo:\n mintAUseSOLBalance || amountMaxA.isZero()\n ? {\n payer: this.scope.ownerPubKey,\n amount: amountMaxA,\n }\n : undefined,\n\n skipCloseAccount: !mintAUseSOLBalance,\n notUseTokenAccount: mintAUseSOLBalance,\n associatedOnly: mintAUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n if (_ownerTokenAccountA) ownerTokenAccountA = _ownerTokenAccountA;\n txBuilder.addInstruction(_tokenAccountAInstruction || {});\n\n const { account: _ownerTokenAccountB, instructionParams: _tokenAccountBInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintB.programId,\n mint: new PublicKey(poolInfo.mintB.address),\n owner: this.scope.ownerPubKey,\n\n createInfo:\n mintBUseSOLBalance || amountMaxB.isZero()\n ? {\n payer: this.scope.ownerPubKey!,\n amount: amountMaxB,\n }\n : undefined,\n skipCloseAccount: !mintBUseSOLBalance,\n notUseTokenAccount: mintBUseSOLBalance,\n associatedOnly: mintBUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n if (_ownerTokenAccountB) ownerTokenAccountB = _ownerTokenAccountB;\n txBuilder.addInstruction(_tokenAccountBInstruction || {});\n\n if (ownerTokenAccountA === undefined || ownerTokenAccountB === undefined)\n this.logAndCreateError(\"cannot found target token accounts\", \"tokenAccounts\", this.scope.account.tokenAccounts);\n\n const poolKeys = propPoolKeys || (await this.getClmmPoolKeys(poolInfo.id));\n\n const makeOpenPositionInstructions = await ClmmInstrument.openPositionFromLiquidityInstructions({\n poolInfo,\n poolKeys,\n ownerInfo: {\n wallet: this.scope.ownerPubKey,\n tokenAccountA: ownerTokenAccountA!,\n tokenAccountB: ownerTokenAccountB!,\n },\n tickLower,\n tickUpper,\n liquidity,\n amountMaxA,\n amountMaxB,\n withMetadata,\n getEphemeralSigners,\n });\n txBuilder.addInstruction(makeOpenPositionInstructions);\n txBuilder.addCustomComputeBudget(computeBudgetConfig);\n\n return txBuilder.versionBuild<OpenPositionFromLiquidityExtInfo>({\n txVersion,\n extInfo: { address: makeOpenPositionInstructions.address },\n }) as Promise<MakeTxData<T, OpenPositionFromLiquidityExtInfo>>;\n }\n\n public async increasePositionFromLiquidity<T extends TxVersion>(\n props: IncreasePositionFromLiquidity<T>,\n ): Promise<MakeTxData<T, ManipulateLiquidityExtInfo>> {\n const {\n poolInfo,\n poolKeys: propPoolKeys,\n ownerPosition,\n amountMaxA,\n amountMaxB,\n liquidity,\n ownerInfo,\n associatedOnly = true,\n checkCreateATAOwner = false,\n computeBudgetConfig,\n txVersion,\n } = props;\n const txBuilder = this.createTxBuilder();\n\n let ownerTokenAccountA: PublicKey | undefined = undefined;\n let ownerTokenAccountB: PublicKey | undefined = undefined;\n\n const mintAUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintA.address === WSOLMint.toString();\n const mintBUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintB.address === WSOLMint.toString();\n const { account: _ownerTokenAccountA, instructionParams: _tokenAccountAInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintA.programId,\n mint: new PublicKey(poolInfo.mintA.address),\n notUseTokenAccount: mintAUseSOLBalance,\n owner: this.scope.ownerPubKey,\n\n createInfo:\n mintAUseSOLBalance || amountMaxA.isZero()\n ? {\n payer: this.scope.ownerPubKey,\n amount: amountMaxA,\n }\n : undefined,\n skipCloseAccount: !mintAUseSOLBalance,\n associatedOnly: mintAUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n if (_ownerTokenAccountA) ownerTokenAccountA = _ownerTokenAccountA;\n txBuilder.addInstruction(_tokenAccountAInstruction || {});\n const { account: _ownerTokenAccountB, instructionParams: _tokenAccountBInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintB.programId,\n mint: new PublicKey(poolInfo.mintB.address),\n owner: this.scope.ownerPubKey,\n\n createInfo:\n mintBUseSOLBalance || amountMaxB.isZero()\n ? {\n payer: this.scope.ownerPubKey!,\n amount: amountMaxB,\n }\n : undefined,\n notUseTokenAccount: mintBUseSOLBalance,\n skipCloseAccount: !mintBUseSOLBalance,\n associatedOnly: mintBUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n if (_ownerTokenAccountB) ownerTokenAccountB = _ownerTokenAccountB;\n txBuilder.addInstruction(_tokenAccountBInstruction || {});\n\n if (!ownerTokenAccountA && !ownerTokenAccountB)\n this.logAndCreateError(\"cannot found target token accounts\", \"tokenAccounts\", this.scope.account.tokenAccounts);\n const poolKeys = propPoolKeys ?? (await this.getClmmPoolKeys(poolInfo.id));\n const ins = ClmmInstrument.increasePositionFromLiquidityInstructions({\n poolInfo,\n poolKeys,\n ownerPosition,\n ownerInfo: {\n wallet: this.scope.ownerPubKey,\n tokenAccountA: ownerTokenAccountA!,\n tokenAccountB: ownerTokenAccountB!,\n },\n liquidity,\n amountMaxA,\n amountMaxB,\n });\n txBuilder.addInstruction(ins);\n txBuilder.addCustomComputeBudget(computeBudgetConfig);\n return txBuilder.versionBuild<ManipulateLiquidityExtInfo>({\n txVersion,\n extInfo: { address: ins.address },\n }) as Promise<MakeTxData<T, ManipulateLiquidityExtInfo>>;\n }\n\n public async increasePositionFromBase<T extends TxVersion>(\n props: IncreasePositionFromBase<T>,\n ): Promise<MakeTxData<T, ManipulateLiquidityExtInfo>> {\n const {\n poolInfo,\n ownerPosition,\n base,\n baseAmount,\n otherAmountMax,\n ownerInfo,\n associatedOnly = true,\n checkCreateATAOwner = false,\n computeBudgetConfig,\n txVersion,\n } = props;\n const txBuilder = this.createTxBuilder();\n\n let ownerTokenAccountA: PublicKey | undefined = undefined;\n let ownerTokenAccountB: PublicKey | undefined = undefined;\n const mintAUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintA.address === WSOLMint.toString();\n const mintBUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintB.address === WSOLMint.toString();\n\n const { account: _ownerTokenAccountA, instructionParams: _tokenAccountAInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintA.programId,\n mint: new PublicKey(poolInfo.mintA.address),\n notUseTokenAccount: mintAUseSOLBalance,\n owner: this.scope.ownerPubKey,\n\n createI