@meteora-ag/dlmm 1.0.51 → 1.0.52-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -2048,6 +2048,73 @@ var IDL = {
2048
2048
  ],
2049
2049
  "args": []
2050
2050
  },
2051
+ {
2052
+ "name": "removeLiquiditySingleSide",
2053
+ "accounts": [
2054
+ {
2055
+ "name": "position",
2056
+ "isMut": true,
2057
+ "isSigner": false
2058
+ },
2059
+ {
2060
+ "name": "lbPair",
2061
+ "isMut": true,
2062
+ "isSigner": false
2063
+ },
2064
+ {
2065
+ "name": "binArrayBitmapExtension",
2066
+ "isMut": true,
2067
+ "isSigner": false,
2068
+ "isOptional": true
2069
+ },
2070
+ {
2071
+ "name": "userToken",
2072
+ "isMut": true,
2073
+ "isSigner": false
2074
+ },
2075
+ {
2076
+ "name": "reserve",
2077
+ "isMut": true,
2078
+ "isSigner": false
2079
+ },
2080
+ {
2081
+ "name": "tokenMint",
2082
+ "isMut": false,
2083
+ "isSigner": false
2084
+ },
2085
+ {
2086
+ "name": "binArrayLower",
2087
+ "isMut": true,
2088
+ "isSigner": false
2089
+ },
2090
+ {
2091
+ "name": "binArrayUpper",
2092
+ "isMut": true,
2093
+ "isSigner": false
2094
+ },
2095
+ {
2096
+ "name": "sender",
2097
+ "isMut": false,
2098
+ "isSigner": true
2099
+ },
2100
+ {
2101
+ "name": "tokenProgram",
2102
+ "isMut": false,
2103
+ "isSigner": false
2104
+ },
2105
+ {
2106
+ "name": "eventAuthority",
2107
+ "isMut": false,
2108
+ "isSigner": false
2109
+ },
2110
+ {
2111
+ "name": "program",
2112
+ "isMut": false,
2113
+ "isSigner": false
2114
+ }
2115
+ ],
2116
+ "args": []
2117
+ },
2051
2118
  {
2052
2119
  "name": "togglePairStatus",
2053
2120
  "accounts": [
@@ -3724,38 +3791,38 @@ var IDL = {
3724
3791
  }
3725
3792
  },
3726
3793
  {
3727
- "name": "BinLiquidityReduction",
3794
+ "name": "FeeParameter",
3728
3795
  "type": {
3729
3796
  "kind": "struct",
3730
3797
  "fields": [
3731
3798
  {
3732
- "name": "binId",
3733
- "type": "i32"
3799
+ "name": "protocolShare",
3800
+ "docs": [
3801
+ "Portion of swap fees retained by the protocol by controlling protocol_share parameter. protocol_swap_fee = protocol_share * total_swap_fee"
3802
+ ],
3803
+ "type": "u16"
3734
3804
  },
3735
3805
  {
3736
- "name": "bpsToRemove",
3806
+ "name": "baseFactor",
3807
+ "docs": [
3808
+ "Base factor for base fee rate"
3809
+ ],
3737
3810
  "type": "u16"
3738
3811
  }
3739
3812
  ]
3740
3813
  }
3741
3814
  },
3742
3815
  {
3743
- "name": "FeeParameter",
3816
+ "name": "BinLiquidityReduction",
3744
3817
  "type": {
3745
3818
  "kind": "struct",
3746
3819
  "fields": [
3747
3820
  {
3748
- "name": "protocolShare",
3749
- "docs": [
3750
- "Portion of swap fees retained by the protocol by controlling protocol_share parameter. protocol_swap_fee = protocol_share * total_swap_fee"
3751
- ],
3752
- "type": "u16"
3821
+ "name": "binId",
3822
+ "type": "i32"
3753
3823
  },
3754
3824
  {
3755
- "name": "baseFactor",
3756
- "docs": [
3757
- "Base factor for base fee rate"
3758
- ],
3825
+ "name": "bpsToRemove",
3759
3826
  "type": "u16"
3760
3827
  }
3761
3828
  ]
@@ -9604,6 +9671,76 @@ var DLMM = class {
9604
9671
  }).add(removeLiquidityTx);
9605
9672
  }
9606
9673
  }
9674
+ async removeLiquiditySingleSide({
9675
+ user,
9676
+ position,
9677
+ binIds,
9678
+ removeLiquidityForY = false
9679
+ }) {
9680
+ const { lbPair, lowerBinId, owner, feeOwner } = await this.program.account.positionV2.fetch(position);
9681
+ const { reserveX, reserveY, tokenXMint, tokenYMint } = await this.program.account.lbPair.fetch(lbPair);
9682
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
9683
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
9684
+ const [binArrayLower] = deriveBinArray(
9685
+ lbPair,
9686
+ lowerBinArrayIndex,
9687
+ this.program.programId
9688
+ );
9689
+ const [binArrayUpper] = deriveBinArray(
9690
+ lbPair,
9691
+ upperBinArrayIndex,
9692
+ this.program.programId
9693
+ );
9694
+ const preInstructions = [];
9695
+ const setComputeUnitLimitIx = computeBudgetIx();
9696
+ preInstructions.push(setComputeUnitLimitIx);
9697
+ const { ataPubKey: userToken, ix: createPayerTokenIx } = removeLiquidityForY ? await getOrCreateATAInstruction(
9698
+ this.program.provider.connection,
9699
+ this.tokenY.publicKey,
9700
+ owner,
9701
+ user
9702
+ ) : await getOrCreateATAInstruction(
9703
+ this.program.provider.connection,
9704
+ this.tokenX.publicKey,
9705
+ owner,
9706
+ user
9707
+ );
9708
+ createPayerTokenIx && preInstructions.push(createPayerTokenIx);
9709
+ const postInstructions = [];
9710
+ if ([
9711
+ this.tokenX.publicKey.toBase58(),
9712
+ this.tokenY.publicKey.toBase58()
9713
+ ].includes(NATIVE_MINT2.toBase58())) {
9714
+ const closeWrappedSOLIx = await unwrapSOLInstruction(user);
9715
+ closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
9716
+ }
9717
+ const minBinId = Math.min(...binIds);
9718
+ const maxBinId = Math.max(...binIds);
9719
+ const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9720
+ const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
9721
+ const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
9722
+ const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
9723
+ const reserve = removeLiquidityForY ? reserveY : reserveX;
9724
+ const tokenMint = removeLiquidityForY ? tokenYMint : tokenXMint;
9725
+ const removeLiquiditySingleSideTx = await this.program.methods.removeLiquiditySingleSide().accounts({
9726
+ position,
9727
+ lbPair,
9728
+ binArrayBitmapExtension,
9729
+ userToken,
9730
+ reserve,
9731
+ tokenMint,
9732
+ binArrayLower,
9733
+ binArrayUpper,
9734
+ sender: user,
9735
+ tokenProgram: TOKEN_PROGRAM_ID2
9736
+ }).preInstructions(preInstructions).transaction();
9737
+ const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9738
+ return new Transaction({
9739
+ blockhash,
9740
+ lastValidBlockHeight,
9741
+ feePayer: user
9742
+ }).add(removeLiquiditySingleSideTx);
9743
+ }
9607
9744
  /**
9608
9745
  * The `closePosition` function closes a position
9609
9746
  * @param
@@ -10823,6 +10960,108 @@ var DLMM = class {
10823
10960
  Number(binPriceWithLastLiquidity) / (2 ** 64 - 1)
10824
10961
  );
10825
10962
  }
10963
+ getAmountOutWithdrawSingleSide(maxLiquidityShare, price, bin, isWithdrawForY) {
10964
+ const amountX = mulDiv(
10965
+ maxLiquidityShare,
10966
+ bin.amountX,
10967
+ bin.liquiditySupply,
10968
+ 1 /* Down */
10969
+ );
10970
+ const amountY = mulDiv(
10971
+ maxLiquidityShare,
10972
+ bin.amountY,
10973
+ bin.liquiditySupply,
10974
+ 1 /* Down */
10975
+ );
10976
+ const amount0 = isWithdrawForY ? amountX : amountY;
10977
+ const amount1 = isWithdrawForY ? amountY : amountX;
10978
+ const remainAmountX = bin.amountX.sub(amountX);
10979
+ const remainAmountY = bin.amountY.sub(amountY);
10980
+ if (amount0.eq(new BN9(0))) {
10981
+ return {
10982
+ withdrawAmount: amount1
10983
+ };
10984
+ }
10985
+ let maxAmountOut = isWithdrawForY ? remainAmountY : remainAmountX;
10986
+ let maxAmountIn = isWithdrawForY ? shlDiv(remainAmountY, price, SCALE_OFFSET, 0 /* Up */) : mulShr(remainAmountX, price, SCALE_OFFSET, 0 /* Up */);
10987
+ let maxFee = computeFee(
10988
+ this.lbPair.binStep,
10989
+ this.lbPair.parameters,
10990
+ this.lbPair.vParameters,
10991
+ maxAmountIn
10992
+ );
10993
+ maxAmountIn = maxAmountIn.add(maxFee);
10994
+ if (amount0.gt(maxAmountIn)) {
10995
+ return {
10996
+ withdrawAmount: maxAmountOut
10997
+ };
10998
+ }
10999
+ const fee = computeFeeFromAmount(
11000
+ this.lbPair.binStep,
11001
+ this.lbPair.parameters,
11002
+ this.lbPair.vParameters,
11003
+ amount0
11004
+ );
11005
+ const amount0AfterFee = amount0.sub(fee);
11006
+ const amountOut = isWithdrawForY ? mulShr(price, amount0AfterFee, SCALE_OFFSET, 1 /* Down */) : shlDiv(amount0AfterFee, price, SCALE_OFFSET, 1 /* Down */);
11007
+ return {
11008
+ withdrawAmount: amount1.add(amountOut > maxAmountOut ? maxAmountOut : amountOut)
11009
+ };
11010
+ }
11011
+ async getWithdrawSingleSideAmount(positionPubkey, isWithdrawForY) {
11012
+ let totalWithdrawAmount = new BN9(0);
11013
+ let _lowerBinArray;
11014
+ let _upperBinArray;
11015
+ const position = await this.program.account.positionV2.fetch(
11016
+ positionPubkey
11017
+ );
11018
+ const lowerBinArrayIdx = binIdToBinArrayIndex(new BN9(position.lowerBinId));
11019
+ const [lowerBinArray] = deriveBinArray(
11020
+ position.lbPair,
11021
+ lowerBinArrayIdx,
11022
+ this.program.programId
11023
+ );
11024
+ const upperBinArrayIdx = lowerBinArrayIdx.add(new BN9(1));
11025
+ const [upperBinArray] = deriveBinArray(
11026
+ position.lbPair,
11027
+ upperBinArrayIdx,
11028
+ this.program.programId
11029
+ );
11030
+ [_lowerBinArray, _upperBinArray] = await this.program.account.binArray.fetchMultiple([
11031
+ lowerBinArray,
11032
+ upperBinArray
11033
+ ]);
11034
+ if (!_lowerBinArray || !_upperBinArray)
11035
+ throw new Error("BinArray not found");
11036
+ for (let idx = 0; idx < position.liquidityShares.length; idx++) {
11037
+ const shareToRemove = position.liquidityShares[idx];
11038
+ if (shareToRemove.eq(new BN9(0))) {
11039
+ continue;
11040
+ }
11041
+ const binId = new BN9(position.lowerBinId).add(new BN9(idx));
11042
+ const binArrayIndex = binIdToBinArrayIndex(binId);
11043
+ const binArray = binArrayIndex.eq(lowerBinArrayIdx) ? _lowerBinArray : _upperBinArray;
11044
+ const bin = getBinFromBinArray(binId.toNumber(), binArray);
11045
+ if (isWithdrawForY) {
11046
+ if (binId.gt(new BN9(this.lbPair.activeId))) {
11047
+ break;
11048
+ }
11049
+ } else {
11050
+ if (binId.lt(new BN9(this.lbPair.activeId))) {
11051
+ continue;
11052
+ }
11053
+ }
11054
+ const price = getQPriceFromId(binId, new BN9(this.lbPair.binStep));
11055
+ const { withdrawAmount } = this.getAmountOutWithdrawSingleSide(
11056
+ shareToRemove,
11057
+ price,
11058
+ bin,
11059
+ isWithdrawForY
11060
+ );
11061
+ totalWithdrawAmount = totalWithdrawAmount.add(withdrawAmount);
11062
+ }
11063
+ return totalWithdrawAmount;
11064
+ }
10826
11065
  /** Private static method */
10827
11066
  static async getBinArrays(program, lbPairPubkey) {
10828
11067
  return program.account.binArray.all([