Skip to content

Commit

Permalink
fix: process position bin amount remove floor
Browse files Browse the repository at this point in the history
  • Loading branch information
McSam94 committed Feb 6, 2024
1 parent 3b1885e commit 2ce7ac6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
78 changes: 62 additions & 16 deletions ts-client/src/dlmm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export class DLMM {
positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo);
}

const lbPairArraysMap = new Map();
const lbPairArraysMap = new Map<string, LbPair>();
for (
let i = binArrayPubkeyArray.length;
i < binArrayPubkeyArray.length + lbPairArray.length;
Expand All @@ -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();
Expand Down Expand Up @@ -551,7 +556,7 @@ export class DLMM {
positionBinArraysMapV2.set(binArrayPubkey.toBase58(), binArrayAccInfo);
}

const lbPairArraysMapV2 = new Map();
const lbPairArraysMapV2 = new Map<string, LbPair>();
for (
let i =
binArrayPubkeyArray.length +
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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 =
Expand All @@ -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(
Expand Down Expand Up @@ -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 =
Expand All @@ -683,22 +729,22 @@ 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,
PositionVersion.V1,
lbPairAcc,
onChainTimestamp,
account,
baseTokenDecimal,
quoteTokenDecimal,
mintXDecimal,
mintYDecimal,
lowerBinArray,
upperBinArray
);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2ce7ac6

Please sign in to comment.