Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithgun committed Jan 3, 2025
1 parent aeab3ea commit bb47854
Show file tree
Hide file tree
Showing 3 changed files with 824 additions and 38 deletions.
11 changes: 11 additions & 0 deletions ts-client/src/dlmm/helpers/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,14 @@ export function deriveEventAuthority(programId: PublicKey) {
programId
);
}

export function deriveRewardVault(
lbPair: PublicKey,
rewardIndex: BN,
programId: PublicKey
) {
return PublicKey.findProgramAddressSync(
[lbPair.toBuffer(), rewardIndex.toArrayLike(Buffer, "le", 8)],
programId
);
}
73 changes: 47 additions & 26 deletions ts-client/src/dlmm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2458,8 +2458,8 @@ export class DLMM {
tokenYMint: this.lbPair.tokenYMint,
binArrayBitmapExtension,
sender: user,
tokenXProgram: TOKEN_PROGRAM_ID,
tokenYProgram: TOKEN_PROGRAM_ID,
tokenXProgram: this.tokenX.owner,
tokenYProgram: this.tokenY.owner,
memoProgram: MEMO_PROGRAM_ID,
};

Expand Down Expand Up @@ -4020,10 +4020,6 @@ export class DLMM {
.div(new Decimal(outAmountWithoutSlippage.toString()))
.mul(new Decimal(100));

const minOutAmount = totalOutAmount
.mul(new BN(BASIS_POINT_MAX).sub(allowedSlippage))
.div(new BN(BASIS_POINT_MAX));

const endPrice = getPriceOfBinByBinId(
activeId.toNumber(),
this.lbPair.binStep
Expand All @@ -4035,6 +4031,10 @@ export class DLMM {
this.clock.epoch.toNumber()
).amount;

const minOutAmount = transferFeeExcludedAmountOut
.mul(new BN(BASIS_POINT_MAX).sub(allowedSlippage))
.div(new BN(BASIS_POINT_MAX));

return {
consumedInAmount: transferFeeIncludedInAmount,
outAmount: transferFeeExcludedAmountOut,
Expand Down Expand Up @@ -4215,9 +4215,6 @@ export class DLMM {
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
}

let swapForY = true;
if (outToken.equals(tokenXMint)) swapForY = false;

// TODO: needs some refinement in case binArray not yet initialized
const binArrays: AccountMeta[] = binArraysPubkey.map((pubkey) => {
return {
Expand Down Expand Up @@ -4299,12 +4296,15 @@ export class DLMM {
user,
binArraysPubkey,
}: SwapParams): Promise<Transaction> {
const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } =
await this.program.account.lbPair.fetch(lbPair);

const preInstructions: TransactionInstruction[] = [];
const postInstructions: Array<TransactionInstruction> = [];

const [inTokenProgram, outTokenProgram] = inToken.equals(
this.lbPair.tokenXMint
)
? [this.tokenX.owner, this.tokenY.owner]
: [this.tokenY.owner, this.tokenX.owner];

const [
{ ataPubKey: userTokenIn, ix: createInTokenAccountIx },
{ ataPubKey: userTokenOut, ix: createOutTokenAccountIx },
Expand All @@ -4313,13 +4313,13 @@ export class DLMM {
this.program.provider.connection,
inToken,
user,
this.tokenX.owner
inTokenProgram
),
getOrCreateATAInstruction(
this.program.provider.connection,
outToken,
user,
this.tokenY.owner
outTokenProgram
),
]);
createInTokenAccountIx && preInstructions.push(createInTokenAccountIx);
Expand All @@ -4342,9 +4342,6 @@ export class DLMM {
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
}

let swapForY = true;
if (outToken.equals(tokenXMint)) swapForY = false;

// TODO: needs some refinement in case binArray not yet initialized
const binArrays: AccountMeta[] = binArraysPubkey.map((pubkey) => {
return {
Expand All @@ -4361,10 +4358,10 @@ export class DLMM {
.swap2(inAmount, minOutAmount, { slices })
.accounts({
lbPair,
reserveX,
reserveY,
tokenXMint,
tokenYMint,
reserveX: this.lbPair.reserveX,
reserveY: this.lbPair.reserveY,
tokenXMint: this.lbPair.tokenXMint,
tokenYMint: this.lbPair.tokenYMint,
tokenXProgram: this.tokenX.owner,
tokenYProgram: this.tokenY.owner,
user,
Expand All @@ -4373,7 +4370,7 @@ export class DLMM {
binArrayBitmapExtension: this.binArrayBitmapExtension
? this.binArrayBitmapExtension.publicKey
: null,
oracle,
oracle: this.lbPair.oracle,
hostFeeIn: null,
memoProgram: MEMO_PROGRAM_ID,
})
Expand Down Expand Up @@ -4523,19 +4520,39 @@ export class DLMM {
public async claimSwapFee({
owner,
position,
binRange,
}: {
owner: PublicKey;
position: LbPosition;
binRange?: {
minBinId: BN;
maxBinId: BN;
};
}): Promise<Transaction> {
const claimFeeTx = await this.createClaimSwapFeeMethod({ owner, position });
const claimFeeTx = await this.createClaimSwapFeeMethod({
owner,
position,
shouldIncludePretIx: true,
shouldIncludePostIx: true,
binRange,
});

const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
this.program.provider.connection,
claimFeeTx.instructions,
owner
);

const instructions = [setCUIx, ...claimFeeTx.instructions];

const { blockhash, lastValidBlockHeight } =
await this.program.provider.connection.getLatestBlockhash("confirmed");

return new Transaction({
blockhash,
lastValidBlockHeight,
feePayer: owner,
}).add(claimFeeTx);
}).add(...instructions);
}

/**
Expand All @@ -4548,7 +4565,8 @@ export class DLMM {
public async claimAllSwapFee({
owner,
positions,
}: {
}: // todo: range
{
owner: PublicKey;
positions: LbPosition[];
}): Promise<Transaction[]> {
Expand Down Expand Up @@ -5519,6 +5537,7 @@ export class DLMM {
});
});

// TODO: Merge it into above iteration and allow quote chunk swap fee
const { feeX, feeY } = await this.getClaimableSwapFee(
program,
position,
Expand Down Expand Up @@ -5900,7 +5919,7 @@ export class DLMM {
position: position.publicKey,
rewardVault: rewardInfo.vault,
rewardMint: rewardInfo.mint,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: this.rewards[i].owner,
userTokenAccount: ataPubKey,
memoProgram: MEMO_PROGRAM_ID,
})
Expand Down Expand Up @@ -5956,12 +5975,14 @@ export class DLMM {
this.program.provider.connection,
this.tokenX.publicKey,
walletToReceiveFee,
this.tokenX.owner,
owner
),
getOrCreateATAInstruction(
this.program.provider.connection,
this.tokenY.publicKey,
walletToReceiveFee,
this.tokenY.owner,
owner
),
]);
Expand Down
Loading

0 comments on commit bb47854

Please sign in to comment.