diff --git a/ts-client/package.json b/ts-client/package.json index 72e2ce25..a266d13b 100644 --- a/ts-client/package.json +++ b/ts-client/package.json @@ -1,6 +1,6 @@ { "name": "@meteora-ag/dlmm", - "version": "1.0.8", + "version": "1.0.9", "description": "", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/ts-client/src/dlmm/index.ts b/ts-client/src/dlmm/index.ts index b620d28a..09dea1ac 100644 --- a/ts-client/src/dlmm/index.ts +++ b/ts-client/src/dlmm/index.ts @@ -505,7 +505,7 @@ export class DLMM { positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo); } - const lbPairArraysMap = new Map(); + const lbPairArraysMap = new Map(); for ( let i = binArrayPubkeyArray.length; i < binArrayPubkeyArray.length + lbPairArray.length; @@ -523,7 +523,12 @@ export class DLMM { } const reservePublicKeys = Array.from(lbPairArraysMap.values()) - .map(({ reserveX, reserveY }) => [reserveX, reserveY]) + .map(({ reserveX, reserveY, tokenXMint, tokenYMint }) => [ + reserveX, + reserveY, + tokenXMint, + tokenYMint, + ]) .flat(); const positionBinArraysMapV2 = new Map(); @@ -551,7 +556,7 @@ export class DLMM { positionBinArraysMapV2.set(binArrayPubkey.toBase58(), binArrayAccInfo); } - const lbPairArraysMapV2 = new Map(); + const lbPairArraysMapV2 = new Map(); for ( let i = binArrayPubkeyArray.length + @@ -578,7 +583,12 @@ export class DLMM { } const reservePublicKeysV2 = Array.from(lbPairArraysMapV2.values()) - .map(({ reserveX, reserveY }) => [reserveX, reserveY]) + .map(({ reserveX, reserveY, tokenXMint, tokenYMint }) => [ + reserveX, + reserveY, + tokenXMint, + tokenYMint, + ]) .flat(); const reserveAccountsInfo = await chunkedGetMultipleAccountInfos( @@ -590,8 +600,12 @@ export class DLMM { string, { reserveX: bigint; reserveY: bigint } >(); + const lbPairMintMap = new Map< + string, + { mintXDecimal: number; mintYDecimal: number } + >(); lbPairArray.forEach((lbPair, idx) => { - const index = idx * 2; + const index = idx * 4; const reserveAccBufferX = reserveAccountsInfo[index]; const reserveAccBufferY = reserveAccountsInfo[index + 1]; if (!reserveAccBufferX || !reserveAccBufferY) @@ -605,14 +619,32 @@ export class DLMM { reserveX: reserveAccX.amount, reserveY: reserveAccY.amount, }); + + const mintXBuffer = reserveAccountsInfo[index + 2]; + const mintYBuffer = reserveAccountsInfo[index + 3]; + if (!mintXBuffer || !mintYBuffer) + throw new Error( + `Mint account for LB Pair ${lbPair.toBase58()} not found` + ); + const mintX = MintLayout.decode(mintXBuffer.data); + const mintY = MintLayout.decode(mintYBuffer.data); + + lbPairMintMap.set(lbPair.toBase58(), { + mintXDecimal: mintX.decimals, + mintYDecimal: mintY.decimals, + }); }); const lbPairReserveMapV2 = new Map< string, { reserveX: bigint; reserveY: bigint } >(); + const lbPairMintMapV2 = new Map< + string, + { mintXDecimal: number; mintYDecimal: number } + >(); lbPairArrayV2.forEach((lbPair, idx) => { - const index = idx * 2; + const index = idx * 4; const reserveAccBufferXV2 = reserveAccountsInfo[reservePublicKeys.length + index]; const reserveAccBufferYV2 = @@ -628,6 +660,21 @@ export class DLMM { reserveX: reserveAccX.amount, reserveY: reserveAccY.amount, }); + + const mintXBufferV2 = + reserveAccountsInfo[reservePublicKeys.length + index + 2]; + const mintYBufferV2 = + reserveAccountsInfo[reservePublicKeys.length + index + 3]; + if (!mintXBufferV2 || !mintYBufferV2) + throw new Error( + `Mint account for LB Pair ${lbPair.toBase58()} not found` + ); + const mintX = MintLayout.decode(mintXBufferV2.data); + const mintY = MintLayout.decode(mintYBufferV2.data); + lbPairMintMapV2.set(lbPair.toBase58(), { + mintXDecimal: mintX.decimals, + mintYDecimal: mintY.decimals, + }); }); const onChainTimestamp = new BN( @@ -671,10 +718,9 @@ export class DLMM { upperBinArrayPubKey.toBase58() ); const lbPairAcc = lbPairArraysMap.get(lbPair.toBase58()); - const [baseTokenDecimal, quoteTokenDecimal] = await Promise.all([ - getTokenDecimals(program.provider.connection, lbPairAcc.tokenXMint), - getTokenDecimals(program.provider.connection, lbPairAcc.tokenYMint), - ]); + const { mintXDecimal, mintYDecimal } = lbPairMintMap.get( + lbPair.toBase58() + ); const reserveXBalance = lbPairReserveMap.get(lbPair.toBase58())?.reserveX ?? BigInt(0); const reserveYBalance = @@ -683,13 +729,13 @@ export class DLMM { publicKey: lbPairAcc.tokenXMint, reserve: lbPairAcc.reserveX, amount: reserveXBalance, - decimal: baseTokenDecimal, + decimal: mintXDecimal, }; const tokenY = { publicKey: lbPairAcc.tokenYMint, reserve: lbPairAcc.reserveY, amount: reserveYBalance, - decimal: quoteTokenDecimal, + decimal: mintYDecimal, }; const positionData = await DLMM.processPosition( program, @@ -697,8 +743,8 @@ export class DLMM { lbPairAcc, onChainTimestamp, account, - baseTokenDecimal, - quoteTokenDecimal, + mintXDecimal, + mintYDecimal, lowerBinArray, upperBinArray ); @@ -3210,10 +3256,10 @@ export class DLMM { } const positionXAmount = binSupply.eq(new Decimal("0")) ? new Decimal("0") - : posShare.mul(bin.xAmount.toString()).div(binSupply).floor(); + : posShare.mul(bin.xAmount.toString()).div(binSupply); const positionYAmount = binSupply.eq(new Decimal("0")) ? new Decimal("0") - : posShare.mul(bin.yAmount.toString()).div(binSupply).floor(); + : posShare.mul(bin.yAmount.toString()).div(binSupply); totalXAmount = totalXAmount.add(positionXAmount); totalYAmount = totalYAmount.add(positionYAmount);