From a590d77f7c9b56ec864e5bacce9ba2a7a7897bac Mon Sep 17 00:00:00 2001 From: kampung-tech Date: Fri, 3 Jan 2025 12:13:22 +0800 Subject: [PATCH] fix: quote end price (#143) * fix: quote end price * fix: remove unused account fetch in swap * fix: remove unused account fetch in swapWithPriceImpact --- CHANGELOG.md | 10 ++++++++++ ts-client/package.json | 2 +- ts-client/src/dlmm/index.ts | 37 +++++++++++++++---------------------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c1ccf04..7aa14c96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security +## @meteora-ag/dlmm [1.3.7] - PR #143 + +### Fixed + +- Fix `swapQuote` end price + ## @meteora-ag/dlmm [1.3.6] - PR #116 ### Changed @@ -26,6 +32,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Refactored; remove `position(V1)` interaction from SDK - Throw error in `removeLiquidity` function if position doesn't have any liquidity +### Fixed + +- Removed unused rpc call in `swap` + ### Added - Function `getPosition` to retrieve a single position data diff --git a/ts-client/package.json b/ts-client/package.json index f8de4811..8f76340c 100644 --- a/ts-client/package.json +++ b/ts-client/package.json @@ -1,6 +1,6 @@ { "name": "@meteora-ag/dlmm", - "version": "1.3.6", + "version": "1.3.7", "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 4251b098..e5a1198c 100644 --- a/ts-client/src/dlmm/index.ts +++ b/ts-client/src/dlmm/index.ts @@ -3157,6 +3157,7 @@ export class DLMM { let actualOutAmount: BN = new BN(0); let feeAmount: BN = new BN(0); let protocolFeeAmount: BN = new BN(0); + let lastFilledActiveBinId = activeId; while (!inAmountLeft.isZero()) { let binArrayAccountToSwap = findNextBinArrayWithLiquidity( @@ -3211,6 +3212,8 @@ export class DLMM { if (!startBin) { startBin = bin; } + + lastFilledActiveBinId = activeId; } } @@ -3249,8 +3252,9 @@ export class DLMM { const minOutAmount = actualOutAmount .mul(new BN(BASIS_POINT_MAX).sub(allowedSlippage)) .div(new BN(BASIS_POINT_MAX)); + const endPrice = getPriceOfBinByBinId( - activeId.toNumber(), + lastFilledActiveBinId.toNumber(), this.lbPair.binStep ); @@ -3389,9 +3393,6 @@ export class DLMM { priceImpact, binArraysPubkey, }: SwapWithPriceImpactParams): Promise { - const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = - await this.program.account.lbPair.fetch(lbPair); - const preInstructions: TransactionInstruction[] = []; const postInstructions: Array = []; @@ -3430,9 +3431,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 { @@ -3450,10 +3448,10 @@ export class DLMM { ) .accounts({ lbPair, - reserveX, - reserveY, - tokenXMint, - tokenYMint, + reserveX: this.lbPair.reserveX, + reserveY: this.lbPair.reserveY, + tokenXMint: this.lbPair.tokenXMint, + tokenYMint: this.lbPair.tokenYMint, tokenXProgram: TOKEN_PROGRAM_ID, tokenYProgram: TOKEN_PROGRAM_ID, user, @@ -3462,7 +3460,7 @@ export class DLMM { binArrayBitmapExtension: this.binArrayBitmapExtension ? this.binArrayBitmapExtension.publicKey : null, - oracle, + oracle: this.lbPair.oracle, hostFeeIn: null, }) .remainingAccounts(binArrays) @@ -3508,9 +3506,6 @@ export class DLMM { user, binArraysPubkey, }: SwapParams): Promise { - const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = - await this.program.account.lbPair.fetch(lbPair); - const preInstructions: TransactionInstruction[] = []; const postInstructions: Array = []; @@ -3549,8 +3544,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) => { @@ -3565,10 +3558,10 @@ export class DLMM { .swap(inAmount, minOutAmount) .accounts({ lbPair, - reserveX, - reserveY, - tokenXMint, - tokenYMint, + reserveX: this.lbPair.reserveX, + reserveY: this.lbPair.reserveY, + tokenXMint: this.lbPair.tokenXMint, + tokenYMint: this.lbPair.tokenYMint, tokenXProgram: TOKEN_PROGRAM_ID, // dont use 2022 first; lack familiarity tokenYProgram: TOKEN_PROGRAM_ID, // dont use 2022 first; lack familiarity user, @@ -3577,7 +3570,7 @@ export class DLMM { binArrayBitmapExtension: this.binArrayBitmapExtension ? this.binArrayBitmapExtension.publicKey : null, - oracle, + oracle: this.lbPair.oracle, hostFeeIn: null, }) .remainingAccounts(binArrays)