Skip to content

Commit

Permalink
fix: swap, claim reward, add and remove liquidity read token mint owner
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithgun committed Jun 11, 2024
1 parent c1d109e commit 06fc86c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 30 deletions.
151 changes: 121 additions & 30 deletions ts-client/src/dlmm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SYSVAR_RENT_PUBKEY,
SystemProgram,
SYSVAR_CLOCK_PUBKEY,
AccountInfo,
} from "@solana/web3.js";
import { IDL } from "./idl";
import {
Expand Down Expand Up @@ -63,6 +64,7 @@ import {
CompressedBinDepositAmounts,
PositionV2,
SeedLiquidityResponse,
RewardMintInfo,
} from "./types";
import { AnchorProvider, BN, Program } from "@coral-xyz/anchor";
import {
Expand Down Expand Up @@ -131,6 +133,7 @@ export class DLMM {
public binArrayBitmapExtension: BinArrayBitmapExtensionAccount | null,
public tokenX: TokenReserve,
public tokenY: TokenReserve,
public rewardMintInfo: RewardMintInfo[],
private opt?: Opt
) {}

Expand Down Expand Up @@ -267,13 +270,18 @@ 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,
[
lbPairAccInfo.reserveX,
lbPairAccInfo.reserveY,
lbPairAccInfo.tokenXMint,
lbPairAccInfo.tokenYMint,
...rewardPubkeys,
]
);
let binArrayBitmapExtension: BinArrayBitmapExtensionAccount | null;
Expand All @@ -290,13 +298,33 @@ 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,
amount: reserveXBalance.amount,
decimal: tokenXDecimal,
owner: mintXAccountInfo.owner,
};

const tokenY = {
publicKey: lbPairAccInfo.tokenYMint,
reserve: lbPairAccInfo.reserveY,
Expand All @@ -312,6 +340,7 @@ export class DLMM {
binArrayBitmapExtension,
tokenX,
tokenY,
rewardInfos,
opt
);
}
Expand Down Expand Up @@ -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<PublicKey, AccountInfo<Buffer>>()
);

const lbClmmImpl = await Promise.all(
dlmmList.map(async (lbPair, index) => {
const lbPairState = lbPairArraysMap.get(lbPair.toBase58());
Expand Down Expand Up @@ -437,33 +487,54 @@ 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,
amount: reserveXBalance.amount,
decimal: tokenXDecimal,
owner: tokenXMintAccountInfo.owner,
};

const tokenY = {
publicKey: lbPairState.tokenYMint,
reserve: lbPairState.reserveY,
amount: reserveYBalance.amount,
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,
lbPairState,
binArrayBitmapExtension,
tokenX,
tokenY,
rewardInfos,
opt
);
})
Expand Down Expand Up @@ -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 programMethod =
Expand Down Expand Up @@ -2243,8 +2314,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 = {
Expand All @@ -2254,21 +2325,31 @@ 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,
lbPair: this.pubkey,
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();
Expand Down Expand Up @@ -2473,8 +2554,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 programMethod =
Expand Down Expand Up @@ -2667,8 +2748,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 = {
Expand All @@ -2678,21 +2759,31 @@ 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,
lbPair: this.pubkey,
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();
Expand Down Expand Up @@ -2891,7 +2982,7 @@ export class DLMM {
binArrayUpper,
rewardVault: rewardInfo.vault,
rewardMint: rewardInfo.mint,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: this.rewardMintInfo[i].owner,
userTokenAccount: ataPubKey,
})
.instruction();
Expand Down Expand Up @@ -2954,8 +3045,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)
Expand Down Expand Up @@ -3252,8 +3343,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,
Expand Down Expand Up @@ -4873,7 +4964,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 : [])
Expand Down
5 changes: 5 additions & 0 deletions ts-client/src/dlmm/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface TokenReserve {
owner: PublicKey;
}

export interface RewardMintInfo {
publicKey: PublicKey;
owner: PublicKey;
}

export type ClmmProgram = Program<LbClmm>;

export type LbPair = IdlAccounts<LbClmm>["lbPair"];
Expand Down

0 comments on commit 06fc86c

Please sign in to comment.