@meteora-ag/dlmm 1.3.0 → 1.3.1-sam.0

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
@@ -7975,49 +7975,14 @@ var DLMM = class {
7975
7975
  opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
7976
7976
  provider
7977
7977
  );
7978
- const [positions, positionsV2] = await Promise.all([
7979
- program.account.position.all([
7980
- {
7981
- memcmp: {
7982
- bytes: bs58.encode(userPubKey.toBuffer()),
7983
- offset: 8 + 32
7984
- }
7985
- }
7986
- ]),
7987
- program.account.positionV2.all([
7988
- {
7989
- memcmp: {
7990
- bytes: bs58.encode(userPubKey.toBuffer()),
7991
- offset: 8 + 32
7992
- }
7978
+ const positionsV2 = await program.account.positionV2.all([
7979
+ {
7980
+ memcmp: {
7981
+ bytes: bs58.encode(userPubKey.toBuffer()),
7982
+ offset: 8 + 32
7993
7983
  }
7994
- ])
7984
+ }
7995
7985
  ]);
7996
- const binArrayPubkeySet = /* @__PURE__ */ new Set();
7997
- const lbPairSet = /* @__PURE__ */ new Set();
7998
- positions.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
7999
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8000
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8001
- const [lowerBinArrayPubKey] = deriveBinArray(
8002
- lbPair,
8003
- lowerBinArrayIndex,
8004
- program.programId
8005
- );
8006
- const [upperBinArrayPubKey] = deriveBinArray(
8007
- lbPair,
8008
- upperBinArrayIndex,
8009
- program.programId
8010
- );
8011
- binArrayPubkeySet.add(lowerBinArrayPubKey.toBase58());
8012
- binArrayPubkeySet.add(upperBinArrayPubKey.toBase58());
8013
- lbPairSet.add(lbPair.toBase58());
8014
- });
8015
- const binArrayPubkeyArray = Array.from(binArrayPubkeySet).map(
8016
- (pubkey) => new PublicKey6(pubkey)
8017
- );
8018
- const lbPairArray = Array.from(lbPairSet).map(
8019
- (pubkey) => new PublicKey6(pubkey)
8020
- );
8021
7986
  const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
8022
7987
  const lbPairSetV2 = /* @__PURE__ */ new Set();
8023
7988
  positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
@@ -8045,46 +8010,12 @@ var DLMM = class {
8045
8010
  );
8046
8011
  const [clockAccInfo, ...binArraysAccInfo] = await chunkedGetMultipleAccountInfos(connection, [
8047
8012
  SYSVAR_CLOCK_PUBKEY,
8048
- ...binArrayPubkeyArray,
8049
- ...lbPairArray,
8050
8013
  ...binArrayPubkeyArrayV2,
8051
8014
  ...lbPairArrayV2
8052
8015
  ]);
8053
- const positionBinArraysMap = /* @__PURE__ */ new Map();
8054
- for (let i = 0; i < binArrayPubkeyArray.length; i++) {
8055
- const binArrayPubkey = binArrayPubkeyArray[i];
8056
- const binArrayAccInfoBuffer = binArraysAccInfo[i];
8057
- if (!binArrayAccInfoBuffer)
8058
- throw new Error(
8059
- `Bin Array account ${binArrayPubkey.toBase58()} not found`
8060
- );
8061
- const binArrayAccInfo = program.coder.accounts.decode(
8062
- "binArray",
8063
- binArrayAccInfoBuffer.data
8064
- );
8065
- positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo);
8066
- }
8067
- const lbPairArraysMap = /* @__PURE__ */ new Map();
8068
- for (let i = binArrayPubkeyArray.length; i < binArrayPubkeyArray.length + lbPairArray.length; i++) {
8069
- const lbPairPubkey = lbPairArray[i - binArrayPubkeyArray.length];
8070
- const lbPairAccInfoBuffer = binArraysAccInfo[i];
8071
- if (!lbPairAccInfoBuffer)
8072
- throw new Error(`LB Pair account ${lbPairPubkey.toBase58()} not found`);
8073
- const lbPairAccInfo = program.coder.accounts.decode(
8074
- "lbPair",
8075
- lbPairAccInfoBuffer.data
8076
- );
8077
- lbPairArraysMap.set(lbPairPubkey.toBase58(), lbPairAccInfo);
8078
- }
8079
- const reservePublicKeys = Array.from(lbPairArraysMap.values()).map(({ reserveX, reserveY, tokenXMint, tokenYMint }) => [
8080
- reserveX,
8081
- reserveY,
8082
- tokenXMint,
8083
- tokenYMint
8084
- ]).flat();
8085
8016
  const positionBinArraysMapV2 = /* @__PURE__ */ new Map();
8086
- for (let i = binArrayPubkeyArray.length + lbPairArray.length; i < binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length; i++) {
8087
- const binArrayPubkey = binArrayPubkeyArrayV2[i - (binArrayPubkeyArray.length + lbPairArray.length)];
8017
+ for (let i = 0; i < binArrayPubkeyArrayV2.length; i++) {
8018
+ const binArrayPubkey = binArrayPubkeyArrayV2[i];
8088
8019
  const binArrayAccInfoBufferV2 = binArraysAccInfo[i];
8089
8020
  if (!binArrayAccInfoBufferV2)
8090
8021
  throw new Error(
@@ -8097,8 +8028,8 @@ var DLMM = class {
8097
8028
  positionBinArraysMapV2.set(binArrayPubkey.toBase58(), binArrayAccInfo);
8098
8029
  }
8099
8030
  const lbPairArraysMapV2 = /* @__PURE__ */ new Map();
8100
- for (let i = binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length; i < binArraysAccInfo.length; i++) {
8101
- const lbPairPubkey = lbPairArrayV2[i - (binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length)];
8031
+ for (let i = binArrayPubkeyArrayV2.length; i < binArraysAccInfo.length; i++) {
8032
+ const lbPairPubkey = lbPairArrayV2[i - binArrayPubkeyArrayV2.length];
8102
8033
  const lbPairAccInfoBufferV2 = binArraysAccInfo[i];
8103
8034
  if (!lbPairAccInfoBufferV2)
8104
8035
  throw new Error(`LB Pair account ${lbPairPubkey.toBase58()} not found`);
@@ -8116,43 +8047,14 @@ var DLMM = class {
8116
8047
  ]).flat();
8117
8048
  const reserveAccountsInfo = await chunkedGetMultipleAccountInfos(
8118
8049
  program.provider.connection,
8119
- [...reservePublicKeys, ...reservePublicKeysV2]
8050
+ reservePublicKeysV2
8120
8051
  );
8121
- const lbPairReserveMap = /* @__PURE__ */ new Map();
8122
- const lbPairMintMap = /* @__PURE__ */ new Map();
8123
- lbPairArray.forEach((lbPair, idx) => {
8124
- const index = idx * 4;
8125
- const reserveAccBufferX = reserveAccountsInfo[index];
8126
- const reserveAccBufferY = reserveAccountsInfo[index + 1];
8127
- if (!reserveAccBufferX || !reserveAccBufferY)
8128
- throw new Error(
8129
- `Reserve account for LB Pair ${lbPair.toBase58()} not found`
8130
- );
8131
- const reserveAccX = AccountLayout.decode(reserveAccBufferX.data);
8132
- const reserveAccY = AccountLayout.decode(reserveAccBufferY.data);
8133
- lbPairReserveMap.set(lbPair.toBase58(), {
8134
- reserveX: reserveAccX.amount,
8135
- reserveY: reserveAccY.amount
8136
- });
8137
- const mintXBuffer = reserveAccountsInfo[index + 2];
8138
- const mintYBuffer = reserveAccountsInfo[index + 3];
8139
- if (!mintXBuffer || !mintYBuffer)
8140
- throw new Error(
8141
- `Mint account for LB Pair ${lbPair.toBase58()} not found`
8142
- );
8143
- const mintX = MintLayout.decode(mintXBuffer.data);
8144
- const mintY = MintLayout.decode(mintYBuffer.data);
8145
- lbPairMintMap.set(lbPair.toBase58(), {
8146
- mintXDecimal: mintX.decimals,
8147
- mintYDecimal: mintY.decimals
8148
- });
8149
- });
8150
8052
  const lbPairReserveMapV2 = /* @__PURE__ */ new Map();
8151
8053
  const lbPairMintMapV2 = /* @__PURE__ */ new Map();
8152
8054
  lbPairArrayV2.forEach((lbPair, idx) => {
8153
8055
  const index = idx * 4;
8154
- const reserveAccBufferXV2 = reserveAccountsInfo[reservePublicKeys.length + index];
8155
- const reserveAccBufferYV2 = reserveAccountsInfo[reservePublicKeys.length + index + 1];
8056
+ const reserveAccBufferXV2 = reserveAccountsInfo[index];
8057
+ const reserveAccBufferYV2 = reserveAccountsInfo[index + 1];
8156
8058
  if (!reserveAccBufferXV2 || !reserveAccBufferYV2)
8157
8059
  throw new Error(
8158
8060
  `Reserve account for LB Pair ${lbPair.toBase58()} not found`
@@ -8163,8 +8065,8 @@ var DLMM = class {
8163
8065
  reserveX: reserveAccX.amount,
8164
8066
  reserveY: reserveAccY.amount
8165
8067
  });
8166
- const mintXBufferV2 = reserveAccountsInfo[reservePublicKeys.length + index + 2];
8167
- const mintYBufferV2 = reserveAccountsInfo[reservePublicKeys.length + index + 3];
8068
+ const mintXBufferV2 = reserveAccountsInfo[index + 2];
8069
+ const mintYBufferV2 = reserveAccountsInfo[index + 3];
8168
8070
  if (!mintXBufferV2 || !mintYBufferV2)
8169
8071
  throw new Error(
8170
8072
  `Mint account for LB Pair ${lbPair.toBase58()} not found`
@@ -8180,74 +8082,6 @@ var DLMM = class {
8180
8082
  clockAccInfo.data.readBigInt64LE(32).toString()
8181
8083
  ).toNumber();
8182
8084
  const positionsMap = /* @__PURE__ */ new Map();
8183
- for (let position of positions) {
8184
- const { account, publicKey: positionPubKey } = position;
8185
- const { upperBinId, lowerBinId, lbPair } = account;
8186
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8187
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8188
- const [lowerBinArrayPubKey] = deriveBinArray(
8189
- lbPair,
8190
- lowerBinArrayIndex,
8191
- program.programId
8192
- );
8193
- const [upperBinArrayPubKey] = deriveBinArray(
8194
- lbPair,
8195
- upperBinArrayIndex,
8196
- program.programId
8197
- );
8198
- const lowerBinArray = positionBinArraysMap.get(
8199
- lowerBinArrayPubKey.toBase58()
8200
- );
8201
- const upperBinArray = positionBinArraysMap.get(
8202
- upperBinArrayPubKey.toBase58()
8203
- );
8204
- const lbPairAcc = lbPairArraysMap.get(lbPair.toBase58());
8205
- const { mintXDecimal, mintYDecimal } = lbPairMintMap.get(
8206
- lbPair.toBase58()
8207
- );
8208
- const reserveXBalance = lbPairReserveMap.get(lbPair.toBase58())?.reserveX ?? BigInt(0);
8209
- const reserveYBalance = lbPairReserveMap.get(lbPair.toBase58())?.reserveY ?? BigInt(0);
8210
- const tokenX = {
8211
- publicKey: lbPairAcc.tokenXMint,
8212
- reserve: lbPairAcc.reserveX,
8213
- amount: reserveXBalance,
8214
- decimal: mintXDecimal
8215
- };
8216
- const tokenY = {
8217
- publicKey: lbPairAcc.tokenYMint,
8218
- reserve: lbPairAcc.reserveY,
8219
- amount: reserveYBalance,
8220
- decimal: mintYDecimal
8221
- };
8222
- const positionData = await DLMM.processPosition(
8223
- program,
8224
- 0 /* V1 */,
8225
- lbPairAcc,
8226
- onChainTimestamp,
8227
- account,
8228
- mintXDecimal,
8229
- mintYDecimal,
8230
- lowerBinArray,
8231
- upperBinArray,
8232
- PublicKey6.default
8233
- );
8234
- if (positionData) {
8235
- positionsMap.set(lbPair.toBase58(), {
8236
- publicKey: lbPair,
8237
- lbPair: lbPairAcc,
8238
- tokenX,
8239
- tokenY,
8240
- lbPairPositionsData: [
8241
- ...positionsMap.get(lbPair.toBase58())?.lbPairPositionsData ?? [],
8242
- {
8243
- publicKey: positionPubKey,
8244
- positionData,
8245
- version: 0 /* V1 */
8246
- }
8247
- ]
8248
- });
8249
- }
8250
- }
8251
8085
  for (let position of positionsV2) {
8252
8086
  const { account, publicKey: positionPubKey } = position;
8253
8087
  const { upperBinId, lowerBinId, lbPair, feeOwner } = account;
@@ -8319,56 +8153,6 @@ var DLMM = class {
8319
8153
  }
8320
8154
  return positionsMap;
8321
8155
  }
8322
- static async migratePosition(connection, positions, newPositions, walletPubkey, opt) {
8323
- const cluster = opt?.cluster || "mainnet-beta";
8324
- const provider = new AnchorProvider2(
8325
- connection,
8326
- {},
8327
- AnchorProvider2.defaultOptions()
8328
- );
8329
- const program = new Program2(
8330
- IDL,
8331
- opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
8332
- provider
8333
- );
8334
- const positionsState = await program.account.position.fetchMultiple(
8335
- positions
8336
- );
8337
- const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash("confirmed");
8338
- return Promise.all(
8339
- positionsState.map(async ({ lbPair, lowerBinId }, idx) => {
8340
- const position = positions[idx];
8341
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8342
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
8343
- const [lowerBinArrayPubKey] = deriveBinArray(
8344
- lbPair,
8345
- lowerBinArrayIndex,
8346
- program.programId
8347
- );
8348
- const [upperBinArrayPubKey] = deriveBinArray(
8349
- lbPair,
8350
- upperBinArrayIndex,
8351
- program.programId
8352
- );
8353
- const migrateTx = await program.methods.migratePosition().accounts({
8354
- binArrayLower: lowerBinArrayPubKey,
8355
- binArrayUpper: upperBinArrayPubKey,
8356
- lbPair,
8357
- owner: walletPubkey,
8358
- positionV1: position,
8359
- positionV2: newPositions[idx],
8360
- program: program.programId,
8361
- rentReceiver: walletPubkey,
8362
- systemProgram: SystemProgram2.programId
8363
- }).transaction();
8364
- return new Transaction({
8365
- blockhash,
8366
- lastValidBlockHeight,
8367
- feePayer: walletPubkey
8368
- }).add(migrateTx);
8369
- })
8370
- );
8371
- }
8372
8156
  static getPricePerLamport(tokenXDecimal, tokenYDecimal, price) {
8373
8157
  return new Decimal4(price).mul(new Decimal4(10 ** (tokenYDecimal - tokenXDecimal))).toString();
8374
8158
  }
@@ -8860,20 +8644,6 @@ var DLMM = class {
8860
8644
  async getPositionsByUserAndLbPair(userPubKey) {
8861
8645
  const promiseResults = await Promise.all([
8862
8646
  this.getActiveBin(),
8863
- userPubKey && this.program.account.position.all([
8864
- {
8865
- memcmp: {
8866
- bytes: bs58.encode(userPubKey.toBuffer()),
8867
- offset: 8 + 32
8868
- }
8869
- },
8870
- {
8871
- memcmp: {
8872
- bytes: bs58.encode(this.pubkey.toBuffer()),
8873
- offset: 8
8874
- }
8875
- }
8876
- ]),
8877
8647
  userPubKey && this.program.account.positionV2.all([
8878
8648
  {
8879
8649
  memcmp: {
@@ -8889,7 +8659,7 @@ var DLMM = class {
8889
8659
  }
8890
8660
  ])
8891
8661
  ]);
8892
- const [activeBin, positions, positionsV2] = promiseResults;
8662
+ const [activeBin, positionsV2] = promiseResults;
8893
8663
  if (!activeBin) {
8894
8664
  throw new Error("Error fetching active bin");
8895
8665
  }
@@ -8899,29 +8669,9 @@ var DLMM = class {
8899
8669
  userPositions: []
8900
8670
  };
8901
8671
  }
8902
- if (!positions || !positionsV2) {
8672
+ if (!positionsV2) {
8903
8673
  throw new Error("Error fetching positions");
8904
8674
  }
8905
- const binArrayPubkeySet = /* @__PURE__ */ new Set();
8906
- positions.forEach(({ account: { upperBinId, lowerBinId } }) => {
8907
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8908
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8909
- const [lowerBinArrayPubKey] = deriveBinArray(
8910
- this.pubkey,
8911
- lowerBinArrayIndex,
8912
- this.program.programId
8913
- );
8914
- const [upperBinArrayPubKey] = deriveBinArray(
8915
- this.pubkey,
8916
- upperBinArrayIndex,
8917
- this.program.programId
8918
- );
8919
- binArrayPubkeySet.add(lowerBinArrayPubKey.toBase58());
8920
- binArrayPubkeySet.add(upperBinArrayPubKey.toBase58());
8921
- });
8922
- const binArrayPubkeyArray = Array.from(binArrayPubkeySet).map(
8923
- (pubkey) => new PublicKey6(pubkey)
8924
- );
8925
8675
  const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
8926
8676
  positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
8927
8677
  const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
@@ -8947,28 +8697,13 @@ var DLMM = class {
8947
8697
  [
8948
8698
  this.pubkey,
8949
8699
  SYSVAR_CLOCK_PUBKEY,
8950
- ...binArrayPubkeyArray,
8951
8700
  ...binArrayPubkeyArrayV2
8952
8701
  ]
8953
8702
  );
8954
8703
  const [lbPairAccInfo, clockAccInfo, ...binArraysAccInfo] = lbPairAndBinArrays;
8955
- const positionBinArraysMap = /* @__PURE__ */ new Map();
8956
- for (let i = 0; i < binArrayPubkeyArray.length; i++) {
8957
- const binArrayPubkey = binArrayPubkeyArray[i];
8958
- const binArrayAccBuffer = binArraysAccInfo[i];
8959
- if (!binArrayAccBuffer)
8960
- throw new Error(
8961
- `Bin Array account ${binArrayPubkey.toBase58()} not found`
8962
- );
8963
- const binArrayAccInfo = this.program.coder.accounts.decode(
8964
- "binArray",
8965
- binArrayAccBuffer.data
8966
- );
8967
- positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo);
8968
- }
8969
8704
  const positionBinArraysMapV2 = /* @__PURE__ */ new Map();
8970
- for (let i = binArrayPubkeyArray.length; i < binArraysAccInfo.length; i++) {
8971
- const binArrayPubkey = binArrayPubkeyArrayV2[i - binArrayPubkeyArray.length];
8705
+ for (let i = 0; i < binArraysAccInfo.length; i++) {
8706
+ const binArrayPubkey = binArrayPubkeyArrayV2[i];
8972
8707
  const binArrayAccBufferV2 = binArraysAccInfo[i];
8973
8708
  if (!binArrayAccBufferV2)
8974
8709
  throw new Error(
@@ -8985,45 +8720,6 @@ var DLMM = class {
8985
8720
  const onChainTimestamp = new BN9(
8986
8721
  clockAccInfo.data.readBigInt64LE(32).toString()
8987
8722
  ).toNumber();
8988
- const userPositions = await Promise.all(
8989
- positions.map(async ({ publicKey, account }) => {
8990
- const { lowerBinId, upperBinId } = account;
8991
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8992
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8993
- const [lowerBinArrayPubKey] = deriveBinArray(
8994
- this.pubkey,
8995
- lowerBinArrayIndex,
8996
- this.program.programId
8997
- );
8998
- const [upperBinArrayPubKey] = deriveBinArray(
8999
- this.pubkey,
9000
- upperBinArrayIndex,
9001
- this.program.programId
9002
- );
9003
- const lowerBinArray = positionBinArraysMap.get(
9004
- lowerBinArrayPubKey.toBase58()
9005
- );
9006
- const upperBinArray = positionBinArraysMap.get(
9007
- upperBinArrayPubKey.toBase58()
9008
- );
9009
- return {
9010
- publicKey,
9011
- positionData: await DLMM.processPosition(
9012
- this.program,
9013
- 0 /* V1 */,
9014
- this.lbPair,
9015
- onChainTimestamp,
9016
- account,
9017
- this.tokenX.decimal,
9018
- this.tokenY.decimal,
9019
- lowerBinArray,
9020
- upperBinArray,
9021
- PublicKey6.default
9022
- ),
9023
- version: 0 /* V1 */
9024
- };
9025
- })
9026
- );
9027
8723
  const userPositionsV2 = await Promise.all(
9028
8724
  positionsV2.map(async ({ publicKey, account }) => {
9029
8725
  const { lowerBinId, upperBinId, feeOwner } = account;
@@ -9065,7 +8761,7 @@ var DLMM = class {
9065
8761
  );
9066
8762
  return {
9067
8763
  activeBin,
9068
- userPositions: [...userPositions, ...userPositionsV2]
8764
+ userPositions: userPositionsV2
9069
8765
  };
9070
8766
  }
9071
8767
  async quoteCreatePosition({ strategy }) {
@@ -9121,6 +8817,72 @@ var DLMM = class {
9121
8817
  feePayer: user
9122
8818
  }).add(setComputeUnitLimitIx, createPositionIx, ...createBinArrayIxs);
9123
8819
  }
8820
+ /**
8821
+ * The function `getPosition` retrieves position information for a given public key and processes it
8822
+ * using various data to return a `LbPosition` object.
8823
+ * @param {PublicKey} positionPubKey - The `getPosition` function you provided is an asynchronous
8824
+ * function that fetches position information based on a given public key. Here's a breakdown of the
8825
+ * parameters used in the function:
8826
+ * @returns The `getPosition` function returns a Promise that resolves to an object of type
8827
+ * `LbPosition`. The object contains the following properties:
8828
+ * - `publicKey`: The public key of the position account
8829
+ * - `positionData`: Position Object
8830
+ * - `version`: The version of the position (in this case, `Position.V2`)
8831
+ */
8832
+ async getPosition(positionPubKey) {
8833
+ const positionAccountInfo = await this.program.account.positionV2.fetch(positionPubKey);
8834
+ if (!positionAccountInfo) {
8835
+ throw new Error(`Position account ${positionPubKey.toBase58()} not found`);
8836
+ }
8837
+ const { lowerBinId, upperBinId, feeOwner } = positionAccountInfo;
8838
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8839
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8840
+ const [lowerBinArrayPubKey] = deriveBinArray(
8841
+ this.pubkey,
8842
+ lowerBinArrayIndex,
8843
+ this.program.programId
8844
+ );
8845
+ const [upperBinArrayPubKey] = deriveBinArray(
8846
+ this.pubkey,
8847
+ upperBinArrayIndex,
8848
+ this.program.programId
8849
+ );
8850
+ const [clockAccInfo, lowerBinArrayAccInfo, upperBinArrayAccInfo] = await chunkedGetMultipleAccountInfos(
8851
+ this.program.provider.connection,
8852
+ [
8853
+ SYSVAR_CLOCK_PUBKEY,
8854
+ lowerBinArrayPubKey,
8855
+ upperBinArrayPubKey
8856
+ ]
8857
+ );
8858
+ const onChainTimestamp = new BN9(
8859
+ clockAccInfo.data.readBigInt64LE(32).toString()
8860
+ ).toNumber();
8861
+ const lowerBinArray = this.program.coder.accounts.decode(
8862
+ "binArray",
8863
+ lowerBinArrayAccInfo.data
8864
+ );
8865
+ const upperBinArray = this.program.coder.accounts.decode(
8866
+ "binArray",
8867
+ upperBinArrayAccInfo.data
8868
+ );
8869
+ return {
8870
+ publicKey: positionPubKey,
8871
+ positionData: await DLMM.processPosition(
8872
+ this.program,
8873
+ 1 /* V2 */,
8874
+ this.lbPair,
8875
+ onChainTimestamp,
8876
+ positionAccountInfo,
8877
+ this.tokenX.decimal,
8878
+ this.tokenY.decimal,
8879
+ lowerBinArray,
8880
+ upperBinArray,
8881
+ feeOwner
8882
+ ),
8883
+ version: 1 /* V2 */
8884
+ };
8885
+ }
9124
8886
  /**
9125
8887
  * The function `initializePositionAndAddLiquidityByStrategy` function is used to initializes a position and adds liquidity
9126
8888
  * @param {TInitializePositionAndAddLiquidityParamsByStrategy}
@@ -9799,9 +9561,40 @@ var DLMM = class {
9799
9561
  bps,
9800
9562
  shouldClaimAndClose = false
9801
9563
  }) {
9802
- const { lbPair, lowerBinId, owner, feeOwner } = await this.program.account.positionV2.fetch(position);
9803
- const { reserveX, reserveY, tokenXMint, tokenYMint } = await this.program.account.lbPair.fetch(lbPair);
9804
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
9564
+ const lowerBinIdToRemove = Math.min(...binIds);
9565
+ const upperBinIdToRemove = Math.max(...binIds);
9566
+ const [lowerBinArrayPubKey] = deriveBinArray(this.pubkey, binIdToBinArrayIndex(new BN9(binIds[0])), this.program.programId);
9567
+ const [upperBinArrayPubKey] = deriveBinArray(this.pubkey, binIdToBinArrayIndex(new BN9(binIds[binIds.length - 1])), this.program.programId);
9568
+ const [positionAccInfo, lbPairAccInfo, upperBinArrayInfoAcc, lowerBinArrayInfoAcc] = await this.program.provider.connection.getMultipleAccountsInfo([
9569
+ position,
9570
+ this.pubkey,
9571
+ upperBinArrayPubKey,
9572
+ lowerBinArrayPubKey
9573
+ ]);
9574
+ const { lbPair, owner, feeOwner, upperBinId: positionUpperBinId, lowerBinId: positionLowerBinId } = this.program.coder.accounts.decode(
9575
+ "positionV2",
9576
+ positionAccInfo.data
9577
+ );
9578
+ const { reserveX, reserveY, tokenXMint, tokenYMint } = this.program.coder.accounts.decode(
9579
+ "lbPair",
9580
+ lbPairAccInfo.data
9581
+ );
9582
+ const upperBinArray = this.program.coder.accounts.decode("binArray", upperBinArrayInfoAcc.data);
9583
+ const lowerBinArray = this.program.coder.accounts.decode("binArray", lowerBinArrayInfoAcc.data);
9584
+ const bins = await this.getBins(
9585
+ this.pubkey,
9586
+ positionLowerBinId,
9587
+ positionUpperBinId,
9588
+ this.tokenX.decimal,
9589
+ this.tokenY.decimal,
9590
+ lowerBinArray,
9591
+ upperBinArray
9592
+ );
9593
+ const positionHasNoLiquidity = bins.every(({ supply }) => supply.isZero());
9594
+ if (positionHasNoLiquidity) {
9595
+ throw new Error("No liquidity to remove");
9596
+ }
9597
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(positionLowerBinId));
9805
9598
  const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
9806
9599
  const [binArrayLower] = deriveBinArray(
9807
9600
  lbPair,
@@ -9917,13 +9710,11 @@ var DLMM = class {
9917
9710
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
9918
9711
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
9919
9712
  }
9920
- const minBinId = Math.min(...binIds);
9921
- const maxBinId = Math.max(...binIds);
9922
- const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9923
- const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
9713
+ const minBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinIdToRemove));
9714
+ const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinIdToRemove));
9924
9715
  const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
9925
9716
  const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
9926
- const removeLiquidityTx = await this.program.methods.removeLiquidityByRange(minBinId, maxBinId, bps.toNumber()).accounts({
9717
+ const removeLiquidityTx = await this.program.methods.removeLiquidityByRange(lowerBinIdToRemove, upperBinIdToRemove, bps.toNumber()).accounts({
9927
9718
  position,
9928
9719
  lbPair,
9929
9720
  userTokenX,
@@ -11477,12 +11268,7 @@ var DLMM = class {
11477
11268
  let totalYAmount = new Decimal4(0);
11478
11269
  bins.forEach((bin, idx) => {
11479
11270
  const binSupply = new Decimal4(bin.supply.toString());
11480
- let posShare;
11481
- if (bin.version === 1 && version === 0 /* V1 */) {
11482
- posShare = new Decimal4(posShares[idx].shln(64).toString());
11483
- } else {
11484
- posShare = new Decimal4(posShares[idx].toString());
11485
- }
11271
+ const posShare = new Decimal4(posShares[idx].toString());
11486
11272
  const positionXAmount = binSupply.eq(new Decimal4("0")) ? new Decimal4("0") : posShare.mul(bin.xAmount.toString()).div(binSupply);
11487
11273
  const positionYAmount = binSupply.eq(new Decimal4("0")) ? new Decimal4("0") : posShare.mul(bin.yAmount.toString()).div(binSupply);
11488
11274
  totalXAmount = totalXAmount.add(positionXAmount);