UNPKG

test-raydium-sdk-v2

Version:

An SDK for building applications on top of Raydium.

1 lines 721 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/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/account/util.ts","../../../src/raydium/account/layout.ts","../../../node_modules/@noble/hashes/src/_assert.ts","../../../node_modules/@noble/hashes/src/utils.ts","../../../node_modules/@noble/hashes/src/_md.ts","../../../node_modules/@noble/hashes/src/sha256.ts","../../../src/raydium/liquidity/instruction.ts","../../../src/raydium/liquidity/layout.ts","../../../src/raydium/liquidity/stable.ts"],"sourcesContent":["import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from \"@solana/spl-token\";\nimport { PublicKey, TransactionInstruction, SystemProgram, AccountMeta } from \"@solana/web3.js\";\nimport BN from \"bn.js\";\n\nimport { ClmmInstrument, ONE, MIN_SQRT_PRICE_X64, MAX_SQRT_PRICE_X64, getPdaExBitmapAccount } from \"../clmm\";\nimport { InstructionType, jsonInfo2PoolKeys, MEMO_PROGRAM_ID } from \"@/common\";\nimport { struct, u64, u8 } from \"@/marshmallow\";\nimport { makeAMMSwapInstruction } from \"../liquidity/instruction\";\n\nimport { ApiV3PoolInfoItem, ApiV3PoolInfoConcentratedItem, PoolKeys, ClmmKeys, AmmV4Keys, AmmV5Keys } from \"@/api/type\";\nimport { ComputeAmountOutLayout, ReturnTypeMakeSwapInstruction } from \"./type\";\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\nfunction makeInnerInsKey(\n itemPool: ApiV3PoolInfoItem,\n itemPoolKey: PoolKeys,\n inMint: string,\n userInAccount: PublicKey,\n userOutAccount: PublicKey,\n remainingAccount: PublicKey[] | undefined,\n): AccountMeta[] {\n if (itemPool.pooltype.includes(\"StablePool\")) {\n const poolKey = jsonInfo2PoolKeys(itemPoolKey as AmmV5Keys);\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.type === \"Concentrated\") {\n const pool = itemPool as ApiV3PoolInfoConcentratedItem;\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 }, // to do\n { pubkey: poolKey.id, 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 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: false },\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 }\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\n poolInfoA: ApiV3PoolInfoItem,\n poolInfoB: ApiV3PoolInfoItem,\n\n poolKeyA: PoolKeys,\n poolKeyB: PoolKeys,\n\n amountIn: BN,\n amountOut: BN,\n\n remainingAccounts: (PublicKey[] | undefined)[],\n): TransactionInstruction {\n const dataLayout = struct([u8(\"instruction\"), u64(\"amountIn\"), u64(\"amountOut\")]);\n\n const keys: { pubkey: PublicKey; isSigner: boolean; isWritable: boolean }[] = [\n { pubkey: wallet, isSigner: true, isWritable: false },\n { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },\n ];\n\n keys.push(...makeInnerInsKey(poolInfoA, poolKeyA, inputMint, userSourceToken, userRouteToken, remainingAccounts[0]));\n\n keys.push(\n ...makeInnerInsKey(poolInfoB, poolKeyB, routeMint, userRouteToken, userDestinationToken, remainingAccounts[1]),\n );\n\n const data = Buffer.alloc(dataLayout.span);\n dataLayout.encode(\n {\n instruction: 8,\n amountIn,\n amountOut,\n },\n data,\n );\n\n return new TransactionInstruction({\n keys,\n programId,\n data,\n });\n}\n\ntype MakeSwapInstructionParam = {\n ownerInfo: {\n wallet: PublicKey;\n // tokenAccountA: PublicKey\n // tokenAccountB: PublicKey\n\n sourceToken: PublicKey;\n routeToken?: PublicKey;\n destinationToken: PublicKey;\n };\n\n inputMint: PublicKey;\n routeProgram: PublicKey;\n\n swapInfo: ComputeAmountOutLayout;\n};\n\nexport async function makeSwapInstruction({\n routeProgram,\n ownerInfo,\n inputMint,\n swapInfo,\n}: MakeSwapInstructionParam): Promise<ReturnTypeMakeSwapInstruction> {\n if (swapInfo.routeType === \"amm\") {\n if (swapInfo.poolInfo[0].type === \"Concentrated\") {\n const _poolKey = jsonInfo2PoolKeys(swapInfo.poolKey[0] as ClmmKeys);\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 await ClmmInstrument.makeSwapBaseInInstructions({\n poolInfo: _poolKey as any,\n poolKeys: _poolKey as any,\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 {\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.minMiddleAmountFee!.token.mint.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, getTransferAmountFee } from \"@/common\";\nimport { Percent } from \"@/module/percent\";\nimport { ApiV3PoolInfoConcentratedItem, ClmmKeys } from \"@/api/type\";\nimport { MakeTxData, MakeMultiTxData } from \"@/common/txTool/txTool\";\nimport { TxVersion } from \"@/common/txTool/txType\";\nimport { getATAAddress } from \"@/common\";\nimport ModuleBase, { ModuleBaseProps } from \"../moduleBase\";\nimport { mockV3CreatePoolInfo, MAX_SQRT_PRICE_X64, MIN_SQRT_PRICE_X64, ONE } from \"./utils/constants\";\nimport { SqrtPriceMath } from \"./utils/math\";\nimport { PoolUtils } from \"./utils/pool\";\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 ReturnTypeComputeAmountOutBaseOut,\n OpenPositionFromLiquidityExtInfo,\n OpenPositionFromBaseExtInfo,\n ClosePositionExtInfo,\n InitRewardExtInfo,\n HarvestAllRewardsParams,\n} from \"./type\";\nimport { ClmmInstrument } from \"./instrument\";\nimport { LoadParams, MakeTransaction, ReturnTypeFetchMultipleMintInfos } from \"../type\";\nimport { MathUtil } from \"./utils/math\";\nimport { TickArray } from \"./utils/tick\";\nimport { getPdaOperationAccount } from \"./utils/pda\";\nimport { ClmmPositionLayout, OperationLayout } from \"./layout\";\nimport BN from \"bn.js\";\n\nexport class Clmm extends ModuleBase {\n constructor(params: ModuleBaseProps) {\n super(params);\n }\n\n public async load(params?: LoadParams): Promise<void> {\n await this.scope.token.load(params);\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 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,\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.toNumber(),\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 id: insInfo.address.poolId.toString(),\n mintA,\n mintB,\n feeRate: ammConfig.tradeFeeRate,\n openTime: startTime.toNumber(),\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\", this.scope.account.tokenAccounts);\n\n const poolKeys = propPoolKeys || ((await this.scope.api.fetchPoolKeysById({ id: poolInfo.id })) as ClmmKeys);\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>({ txVersion, extInfo: insInfo.address }) as Promise<\n MakeTxData<T, OpenPositionFromBaseExtInfo>\n >;\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 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: mintAUseSOLBalance\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: mintBUseSOLBalance\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.scope.api.fetchPoolKeysById({ id: poolInfo.id })) as ClmmKeys);\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\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 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\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: mintAUseSOLBalance\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\n const { account: _ownerTokenAccountB, instructionParams: _tokenAccountBInstruction } =\n await this.scope.account.getOrCreateTokenAccount({\n mint: new PublicKey(poolInfo.mintB.address),\n owner: this.scope.ownerPubKey,\n\n createInfo: mintBUseSOLBalance\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 = (await this.scope.api.fetchPoolKeysById({ id: poolInfo.id })) as ClmmKeys;\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\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 createInfo: mintAUseSOLBalance\n ? {\n payer: this.scope.ownerPubKey,\n amount: base === \"MintA\" ? baseAmount : otherAmountMax,\n }\n : undefined,\n skipCloseAccount: !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 mint: new PublicKey(poolInfo.mintB.address),\n owner: this.scope.ownerPubKey,\n\n createInfo: mintBUseSOLBalance\n ? {\n payer: this.scope.ownerPubKey!,\n amount: base === \"MintA\" ? otherAmountMax : baseAmount,\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 if (!ownerTokenAccountA && !ownerTokenAccountB)\n this.logAndCreateError(\"cannot found target token accounts\", \"tokenAccounts\", this.scope.account.tokenAccounts);\n\n const poolKeys = (await this.scope.api.fetchPoolKeysById({ id: poolInfo.id })) as ClmmKeys;\n const ins = ClmmInstrument.increasePositionFromBaseInstructions({\n poolInfo,\n poolKeys,\n ownerPosition,\n ownerInfo: {\n wallet: this.scope.ownerPubKey,\n tokenAccountA: ownerTokenAccountA!,\n tokenAccountB: ownerTokenAccountB!,\n },\n base,\n baseAmount,\n otherAmountMax,\n });\n txBuilder.addInstruction(ins);\n txBuilder.addCustomComputeBudget(computeBudgetConfig);\n\n return txBuilder.versionBuild<ManipulateLiquidityExtInfo>({\n txVersion,\n extInfo: { address: ins.address },\n }) as Promise<MakeTxData<T, ManipulateLiquidityExtInfo>>;\n }\n\n public async decreaseLiquidity<T extends TxVersion>(\n props: DecreaseLiquidity<T>,\n ): Promise<MakeTxData<T, ManipulateLiquidityExtInfo & Partial<ClosePositionExtInfo>>> {\n const {\n poolInfo,\n ownerPosition,\n ownerInfo,\n amountMinA,\n amountMinB,\n liquidity,\n associatedOnly = true,\n checkCreateATAOwner = false,\n computeBudgetConfig,\n txVersion,\n } = props;\n if (this.scope.availability.removeConcentratedPosition === false)\n this.logAndCreateError(\"remove position feature disabled in your region\");\n const txBuilder = this.createTxBuilder();\n\n const mintAUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintA.address === WSOLMint.toString();\n const mintBUseSOLBalance = ownerInfo.useSOLBalance && poolInfo.mintB.address === WSOLMint.toString();\n\n let ownerTokenAccountA: PublicKey | undefined = undefined;\n let ownerTokenAccountB: PublicKey | undefined = undefined;\n const { account: _ownerTokenAccountA, instructionParams: accountAInstructions } =\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 createInfo: {\n payer: this.scope.ownerPubKey,\n amount: 0,\n },\n skipCloseAccount: !mintAUseSOLBalance,\n associatedOnly: mintAUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n ownerTokenAccountA = _ownerTokenAccountA;\n accountAInstructions && txBuilder.addInstruction(accountAInstructions);\n\n const { account: _ownerTokenAccountB, instructionParams: accountBInstructions } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: poolInfo.mintB.programId,\n mint: new PublicKey(poolInfo.mintB.address),\n notUseTokenAccount: mintBUseSOLBalance,\n owner: this.scope.ownerPubKey,\n createInfo: {\n payer: this.scope.ownerPubKey,\n amount: 0,\n },\n skipCloseAccount: !mintBUseSOLBalance,\n associatedOnly: mintBUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n ownerTokenAccountB = _ownerTokenAccountB;\n accountBInstructions && txBuilder.addInstruction(accountBInstructions);\n\n const rewardAccounts: PublicKey[] = [];\n for (const itemReward of poolInfo.rewardDefaultInfos) {\n const rewardUseSOLBalance = ownerInfo.useSOLBalance && itemReward.mint.address === WSOLMint.toString();\n\n let ownerRewardAccount: PublicKey | undefined;\n\n if (itemReward.mint.address === poolInfo.mintA.address) ownerRewardAccount = ownerTokenAccountA;\n else if (itemReward.mint.address === poolInfo.mintB.address) ownerRewardAccount = ownerTokenAccountB;\n else {\n const { account: _ownerRewardAccount, instructionParams: ownerRewardAccountInstructions } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: new PublicKey(itemReward.mint.programId),\n mint: new PublicKey(itemReward.mint.address),\n notUseTokenAccount: rewardUseSOLBalance,\n owner: this.scope.ownerPubKey,\n createInfo: {\n payer: this.scope.ownerPubKey,\n amount: 0,\n },\n skipCloseAccount: !rewardUseSOLBalance,\n associatedOnly: rewardUseSOLBalance ? false : associatedOnly,\n checkCreateATAOwner,\n });\n ownerRewardAccount = _ownerRewardAccount;\n ownerRewardAccountInstructions && txBuilder.addInstruction(ownerRewardAccountInstructions);\n }\n\n rewardAccounts.push(ownerRewardAccount!);\n }\n\n if (!ownerTokenAccountA && !ownerTokenAccountB)\n this.logAndCreateError(\n \"cannot found target token accounts\",\n \"tokenAccounts\",\n this.scope.account.tokenAccountRawInfos,\n );\n\n const poolKeys = (await this.scope.api.fetchPoolKeysById({ id: poolInfo.id })) as ClmmKeys;\n\n const decreaseInsInfo = await ClmmInstrument.decreaseLiquidityInstructions({\n poolInfo,\n poolKeys,\n ownerPosition,\n ownerInfo: {\n wallet: this.scope.ownerPubKey,\n tokenAccountA: ownerTokenAccountA!,\n tokenAccountB: ownerTokenAccountB!,\n rewardAccounts,\n },\n liquidity,\n amountMinA,\n amountMinB,\n });\n\n txBuilder.addInstruction({\n instructions: decreaseInsInfo.instructions,\n instructionTypes: [InstructionType.ClmmDecreasePosition],\n });\n\n let extInfo = { ...decreaseInsInfo.address };\n if (ownerInfo.closePosition) {\n const closeInsInfo = await ClmmInstrument.closePositionInstructions({\n poolInfo,\n poolKeys,\n ownerInfo: { wallet: this.scope.ownerPubKey },\n ownerPosition,\n });\n txBuilder.addInstruction({\n endInstructions: closeInsInfo.instructions,\n endInstructionTypes: closeInsInfo.instructionTypes,\n });\n extInfo = { ...extInfo, ...closeInsInfo.address };\n }\n txBuilder.addCustomComputeBudget(computeBudgetConfig);\n\n return txBuilder.versionBuild<ManipulateLiquidityExtInfo>({\n txVersion,\n extInfo: { address: extInfo },\n }) as Promise<MakeTxData<T, ManipulateLiquidityExtInfo>>;\n }\n\n public async closePosition<T extends TxVersion>({\n poolInfo,\n ownerPosition,\n txVersion,\n }: {\n poolInfo: ApiV3PoolInfoConcentratedItem;\n ownerPosition: ClmmPositionLayout;\n txVersion: T;\n }): Promise<MakeTxData<T, ClosePositionExtInfo>> {\n if (this.scope.availability.removeConcentratedPosition === false)\n this.logAndCreateError(\"remove position feature disabled in your region\");\n const txBuilder = this.createTxBuilder();\n const poolKeys = (await this.scope.api.fetchPoolKeysById({ id: poolInfo.id })) as ClmmKeys;\n const ins = ClmmInstrument.closePositionInstructions({\n poolInfo,\n poolKeys,\n ownerInfo: { wallet: this.scope.ownerPubKey },\n ownerPosition,\n });\n\n return txBuilder.addInstruction(ins).versionBuild<ClosePositionExtInfo>({\n txVersion,\n extInfo: { address: ins.address },\n }) as Promise<MakeTxData<T, ClosePositionExtInfo>>;\n }\n\n public async initReward<T extends TxVersion>({\n poolInfo,\n ownerInfo,\n rewardInfo,\n associatedOnly = true,\n checkCreateATAOwner = false,\n computeBudgetConfig,\n txVersion,\n }: InitRewardParams<T>): Promise<MakeTxData<T, InitRewardExtInfo>> {\n if (rewardInfo.endTime <= rewardInfo.openTime)\n this.logAndCreateError(\"reward time error\", \"rewardInfo\", rewardInfo);\n\n const txBuilder = this.createTxBuilder();\n\n const rewardMintUseSOLBalance =\n ownerInfo.useSOLBalance && rewardInfo.mint.address.toString() === WSOLMint.toString();\n const _baseRewardAmount = rewardInfo.perSecond.mul(rewardInfo.endTime - rewardInfo.openTime);\n\n const { account: ownerRewardAccount, instructionParams: ownerRewardAccountIns } =\n await this.scope.account.getOrCreateTokenAccount({\n tokenProgram: new PublicKey(rewardInfo.mint.address),\n mint: new PublicKey(rewardInfo.mint.address),\n notUseTokenAccount: !!rewardMintUseSOLBalance,\n skipCloseAccount: !rewardMintUseSOLBalance,\n owner: this.scope.ownerPubKey,\n createInfo: rewardMintUseSOLBalance\n ? {\n payer: ownerInfo.feePayer || this.scope.ownerPubKey,\n amount: new BN(\n new Decimal(_baseRewardAmount.toFixed(0)).gte(_baseRewardAmount)\n ? _baseRewardAmount.toFixed(0)\n