diff --git a/ts-client/src/dlmm/index.ts b/ts-client/src/dlmm/index.ts index 8f3fa54..1bc2fe5 100644 --- a/ts-client/src/dlmm/index.ts +++ b/ts-client/src/dlmm/index.ts @@ -8,6 +8,7 @@ import { SYSVAR_RENT_PUBKEY, SystemProgram, SYSVAR_CLOCK_PUBKEY, + AccountInfo, } from "@solana/web3.js"; import { IDL } from "./idl"; import { @@ -63,6 +64,7 @@ import { CompressedBinDepositAmounts, PositionV2, SeedLiquidityResponse, + RewardMintInfo, } from "./types"; import { AnchorProvider, BN, Program } from "@coral-xyz/anchor"; import { @@ -131,6 +133,7 @@ export class DLMM { public binArrayBitmapExtension: BinArrayBitmapExtensionAccount | null, public tokenX: TokenReserve, public tokenY: TokenReserve, + public rewardMintInfo: RewardMintInfo[], private opt?: Opt ) {} @@ -267,6 +270,10 @@ export class DLMM { ); } + const rewardPubkeys = lbPairAccInfo.rewardInfos + .filter((info) => !info.mint.equals(PublicKey.default)) + .map((info) => info.mint); + const reserveAccountsInfo = await chunkedGetMultipleAccountInfos( program.provider.connection, [ @@ -274,6 +281,7 @@ export class DLMM { lbPairAccInfo.reserveY, lbPairAccInfo.tokenXMint, lbPairAccInfo.tokenYMint, + ...rewardPubkeys, ] ); let binArrayBitmapExtension: BinArrayBitmapExtensionAccount | null; @@ -290,6 +298,25 @@ export class DLMM { const mintYAccountInfo = reserveAccountsInfo[3]; const tokenXDecimal = MintLayout.decode(mintXAccountInfo.data).decimals; const tokenYDecimal = MintLayout.decode(mintYAccountInfo.data).decimals; + + let rewardInfos: RewardMintInfo[] = []; + + const rewardMintAccountsInfo = reserveAccountsInfo.slice(4); + + for (const reward of lbPairAccInfo.rewardInfos) { + if (reward.mint.equals(PublicKey.default)) { + rewardInfos.push({ + publicKey: PublicKey.default, + owner: SystemProgram.programId, + }); + } else { + rewardInfos.push({ + publicKey: reward.mint, + owner: rewardMintAccountsInfo.shift().owner, + }); + } + } + const tokenX = { publicKey: lbPairAccInfo.tokenXMint, reserve: lbPairAccInfo.reserveX, @@ -297,6 +324,7 @@ export class DLMM { decimal: tokenXDecimal, owner: mintXAccountInfo.owner, }; + const tokenY = { publicKey: lbPairAccInfo.tokenYMint, reserve: lbPairAccInfo.reserveY, @@ -312,6 +340,7 @@ export class DLMM { binArrayBitmapExtension, tokenX, tokenY, + rewardInfos, opt ); } @@ -400,6 +429,27 @@ export class DLMM { ...tokenMintPublicKeys, ]); + const rewardMintPublicKeys = Array.from(lbPairArraysMap.values()).flatMap( + ({ rewardInfos }) => { + return rewardInfos + .filter((reward) => !reward.mint.equals(PublicKey.default)) + .map((reward) => reward.mint); + } + ); + + const rewardMintAccountsInfo = await chunkedGetMultipleAccountInfos( + program.provider.connection, + rewardMintPublicKeys + ); + + const rewardMintAccountInfoMap = rewardMintPublicKeys.reduce( + (map, key, idx) => { + map.set(key, rewardMintAccountsInfo[idx]); + return map; + }, + new Map>() + ); + const lbClmmImpl = await Promise.all( dlmmList.map(async (lbPair, index) => { const lbPairState = lbPairArraysMap.get(lbPair.toBase58()); @@ -437,12 +487,14 @@ export class DLMM { const reserveXBalance = AccountLayout.decode(reserveXAccountInfo.data); const reserveYBalance = AccountLayout.decode(reserveYAccountInfo.data); + const tokenXDecimal = MintLayout.decode( tokenXMintAccountInfo.data ).decimals; const tokenYDecimal = MintLayout.decode( tokenYMintAccountInfo.data ).decimals; + const tokenX = { publicKey: lbPairState.tokenXMint, reserve: lbPairState.reserveX, @@ -450,6 +502,7 @@ export class DLMM { decimal: tokenXDecimal, owner: tokenXMintAccountInfo.owner, }; + const tokenY = { publicKey: lbPairState.tokenYMint, reserve: lbPairState.reserveY, @@ -457,6 +510,23 @@ export class DLMM { decimal: tokenYDecimal, owner: tokenYMintAccountInfo.owner, }; + + let rewardInfos: RewardMintInfo[] = []; + + for (const reward of lbPairState.rewardInfos) { + if (reward.mint.equals(PublicKey.default)) { + rewardInfos.push({ + publicKey: PublicKey.default, + owner: SystemProgram.programId, + }); + } else { + rewardInfos.push({ + publicKey: reward.mint, + owner: rewardMintAccountInfoMap.get(reward.mint).owner, + }); + } + } + return new DLMM( lbPair, program, @@ -464,6 +534,7 @@ export class DLMM { binArrayBitmapExtension, tokenX, tokenY, + rewardInfos, opt ); }) @@ -2046,8 +2117,8 @@ export class DLMM { binArrayUpper, binArrayBitmapExtension, sender: user, - tokenXProgram: TOKEN_PROGRAM_ID, - tokenYProgram: TOKEN_PROGRAM_ID, + tokenXProgram: this.tokenX.owner, + tokenYProgram: this.tokenY.owner, }; const oneSideLiquidityParams: LiquidityParameterByStrategyOneSide = { @@ -2057,6 +2128,20 @@ export class DLMM { strategyParameters, }; + const [reserve, tokenMint, tokenProgram, userToken] = totalXAmount.isZero() + ? [ + this.lbPair.reserveY, + this.lbPair.tokenYMint, + this.tokenY.owner, + userTokenY, + ] + : [ + this.lbPair.reserveX, + this.lbPair.tokenXMint, + this.tokenX.owner, + userTokenX, + ]; + const oneSideAddLiquidityAccounts = { binArrayLower, binArrayUpper, @@ -2064,14 +2149,10 @@ export class DLMM { binArrayBitmapExtension: null, sender: user, position: positionPubKey, - reserve: totalXAmount.isZero() - ? this.lbPair.reserveY - : this.lbPair.reserveX, - tokenMint: totalXAmount.isZero() - ? this.lbPair.tokenYMint - : this.lbPair.tokenXMint, - tokenProgram: TOKEN_PROGRAM_ID, - userToken: totalXAmount.isZero() ? userTokenY : userTokenX, + reserve, + tokenMint, + tokenProgram, + userToken, }; const isOneSideDeposit = totalXAmount.isZero() || totalYAmount.isZero(); @@ -2273,8 +2354,8 @@ export class DLMM { binArrayUpper, binArrayBitmapExtension, sender: user, - tokenXProgram: TOKEN_PROGRAM_ID, - tokenYProgram: TOKEN_PROGRAM_ID, + tokenXProgram: this.tokenX.owner, + tokenYProgram: this.tokenY.owner, }; const oneSideLiquidityParams: LiquidityOneSideParameter = { @@ -2284,6 +2365,20 @@ export class DLMM { binLiquidityDist, }; + const [reserve, tokenMint, tokenProgram, userToken] = totalXAmount.isZero() + ? [ + this.lbPair.reserveY, + this.lbPair.tokenYMint, + this.tokenY.owner, + userTokenY, + ] + : [ + this.lbPair.reserveX, + this.lbPair.tokenXMint, + this.tokenX.owner, + userTokenX, + ]; + const oneSideAddLiquidityAccounts = { binArrayLower, binArrayUpper, @@ -2291,14 +2386,10 @@ export class DLMM { binArrayBitmapExtension: null, sender: user, position: positionPubKey, - reserve: totalXAmount.isZero() - ? this.lbPair.reserveY - : this.lbPair.reserveX, - tokenMint: totalXAmount.isZero() - ? this.lbPair.tokenYMint - : this.lbPair.tokenXMint, - tokenProgram: TOKEN_PROGRAM_ID, - userToken: totalXAmount.isZero() ? userTokenY : userTokenX, + reserve, + tokenMint, + tokenProgram, + userToken, }; const isOneSideDeposit = totalXAmount.isZero() || totalYAmount.isZero(); @@ -2503,8 +2594,8 @@ export class DLMM { binArrayUpper, binArrayBitmapExtension, sender: user, - tokenXProgram: TOKEN_PROGRAM_ID, - tokenYProgram: TOKEN_PROGRAM_ID, + tokenXProgram: this.tokenX.owner, + tokenYProgram: this.tokenY.owner, }; const oneSideLiquidityParams: LiquidityParameterByStrategyOneSide = { @@ -2514,6 +2605,20 @@ export class DLMM { strategyParameters, }; + const [reserve, tokenMint, tokenProgram, userToken] = totalXAmount.isZero() + ? [ + this.lbPair.reserveY, + this.lbPair.tokenYMint, + this.tokenY.owner, + userTokenY, + ] + : [ + this.lbPair.reserveX, + this.lbPair.tokenXMint, + this.tokenX.owner, + userTokenX, + ]; + const oneSideAddLiquidityAccounts = { binArrayLower, binArrayUpper, @@ -2521,14 +2626,10 @@ export class DLMM { binArrayBitmapExtension: null, sender: user, position: positionPubKey, - reserve: totalXAmount.isZero() - ? this.lbPair.reserveY - : this.lbPair.reserveX, - tokenMint: totalXAmount.isZero() - ? this.lbPair.tokenYMint - : this.lbPair.tokenXMint, - tokenProgram: TOKEN_PROGRAM_ID, - userToken: totalXAmount.isZero() ? userTokenY : userTokenX, + reserve, + tokenMint, + tokenProgram, + userToken, }; const isOneSideDeposit = totalXAmount.isZero() || totalYAmount.isZero(); @@ -2727,8 +2828,8 @@ export class DLMM { binArrayUpper, binArrayBitmapExtension, sender: user, - tokenXProgram: TOKEN_PROGRAM_ID, - tokenYProgram: TOKEN_PROGRAM_ID, + tokenXProgram: this.tokenX.owner, + tokenYProgram: this.tokenY.owner, }; const oneSideLiquidityParams: LiquidityOneSideParameter = { @@ -2738,6 +2839,20 @@ export class DLMM { binLiquidityDist, }; + const [reserve, tokenMint, tokenProgram, userToken] = totalXAmount.isZero() + ? [ + this.lbPair.reserveY, + this.lbPair.tokenYMint, + this.tokenY.owner, + userTokenY, + ] + : [ + this.lbPair.reserveX, + this.lbPair.tokenXMint, + this.tokenX.owner, + userTokenX, + ]; + const oneSideAddLiquidityAccounts = { binArrayLower, binArrayUpper, @@ -2745,14 +2860,10 @@ export class DLMM { binArrayBitmapExtension: null, sender: user, position: positionPubKey, - reserve: totalXAmount.isZero() - ? this.lbPair.reserveY - : this.lbPair.reserveX, - tokenMint: totalXAmount.isZero() - ? this.lbPair.tokenYMint - : this.lbPair.tokenXMint, - tokenProgram: TOKEN_PROGRAM_ID, - userToken: totalXAmount.isZero() ? userTokenY : userTokenX, + reserve, + tokenMint, + tokenProgram, + userToken, }; const isOneSideDeposit = totalXAmount.isZero() || totalYAmount.isZero(); @@ -2951,7 +3062,7 @@ export class DLMM { binArrayUpper, rewardVault: rewardInfo.vault, rewardMint: rewardInfo.mint, - tokenProgram: TOKEN_PROGRAM_ID, + tokenProgram: this.rewardMintInfo[i].owner, userTokenAccount: ataPubKey, }) .instruction(); @@ -3014,8 +3125,8 @@ export class DLMM { binArrayLower, binArrayUpper, binArrayBitmapExtension, - tokenXProgram: TOKEN_PROGRAM_ID, - tokenYProgram: TOKEN_PROGRAM_ID, + tokenXProgram: this.tokenX.owner, + tokenYProgram: this.tokenY.owner, sender: user, }) .preInstructions(preInstructions) @@ -3312,8 +3423,8 @@ export class DLMM { reserveY, tokenXMint, tokenYMint, - tokenXProgram: TOKEN_PROGRAM_ID, // dont use 2022 first; lack familiarity - tokenYProgram: TOKEN_PROGRAM_ID, // dont use 2022 first; lack familiarity + tokenXProgram: this.tokenX.owner, + tokenYProgram: this.tokenY.owner, user, userTokenIn, userTokenOut, @@ -4933,7 +5044,7 @@ export class DLMM { binArrayUpper, rewardVault: rewardInfo.vault, rewardMint: rewardInfo.mint, - tokenProgram: TOKEN_PROGRAM_ID, + tokenProgram: this.rewardMintInfo[i].owner, userTokenAccount: ataPubKey, }) .preInstructions(shouldIncludePreIx ? preInstructions : []) diff --git a/ts-client/src/dlmm/types/index.ts b/ts-client/src/dlmm/types/index.ts index 99ae08c..befb790 100644 --- a/ts-client/src/dlmm/types/index.ts +++ b/ts-client/src/dlmm/types/index.ts @@ -29,6 +29,11 @@ export interface TokenReserve { owner: PublicKey; } +export interface RewardMintInfo { + publicKey: PublicKey; + owner: PublicKey; +} + export type ClmmProgram = Program; export type LbPair = IdlAccounts["lbPair"];