@meteora-ag/dlmm 1.3.5 → 1.3.6-rc.1

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.js CHANGED
@@ -8066,49 +8066,14 @@ var DLMM = class {
8066
8066
  _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _46 => _46.programId]), () => ( LBCLMM_PROGRAM_IDS[cluster])),
8067
8067
  provider
8068
8068
  );
8069
- const [positions, positionsV2] = await Promise.all([
8070
- program.account.position.all([
8071
- {
8072
- memcmp: {
8073
- bytes: _bytes.bs58.encode(userPubKey.toBuffer()),
8074
- offset: 8 + 32
8075
- }
8076
- }
8077
- ]),
8078
- program.account.positionV2.all([
8079
- {
8080
- memcmp: {
8081
- bytes: _bytes.bs58.encode(userPubKey.toBuffer()),
8082
- offset: 8 + 32
8083
- }
8069
+ const positionsV2 = await program.account.positionV2.all([
8070
+ {
8071
+ memcmp: {
8072
+ bytes: _bytes.bs58.encode(userPubKey.toBuffer()),
8073
+ offset: 8 + 32
8084
8074
  }
8085
- ])
8075
+ }
8086
8076
  ]);
8087
- const binArrayPubkeySet = /* @__PURE__ */ new Set();
8088
- const lbPairSet = /* @__PURE__ */ new Set();
8089
- positions.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
8090
- const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
8091
- const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(upperBinId));
8092
- const [lowerBinArrayPubKey] = deriveBinArray(
8093
- lbPair,
8094
- lowerBinArrayIndex,
8095
- program.programId
8096
- );
8097
- const [upperBinArrayPubKey] = deriveBinArray(
8098
- lbPair,
8099
- upperBinArrayIndex,
8100
- program.programId
8101
- );
8102
- binArrayPubkeySet.add(lowerBinArrayPubKey.toBase58());
8103
- binArrayPubkeySet.add(upperBinArrayPubKey.toBase58());
8104
- lbPairSet.add(lbPair.toBase58());
8105
- });
8106
- const binArrayPubkeyArray = Array.from(binArrayPubkeySet).map(
8107
- (pubkey) => new (0, _web3js.PublicKey)(pubkey)
8108
- );
8109
- const lbPairArray = Array.from(lbPairSet).map(
8110
- (pubkey) => new (0, _web3js.PublicKey)(pubkey)
8111
- );
8112
8077
  const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
8113
8078
  const lbPairSetV2 = /* @__PURE__ */ new Set();
8114
8079
  positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
@@ -8136,60 +8101,24 @@ var DLMM = class {
8136
8101
  );
8137
8102
  const [clockAccInfo, ...binArraysAccInfo] = await chunkedGetMultipleAccountInfos(connection, [
8138
8103
  _web3js.SYSVAR_CLOCK_PUBKEY,
8139
- ...binArrayPubkeyArray,
8140
- ...lbPairArray,
8141
8104
  ...binArrayPubkeyArrayV2,
8142
8105
  ...lbPairArrayV2
8143
8106
  ]);
8144
- const positionBinArraysMap = /* @__PURE__ */ new Map();
8145
- for (let i = 0; i < binArrayPubkeyArray.length; i++) {
8146
- const binArrayPubkey = binArrayPubkeyArray[i];
8147
- const binArrayAccInfoBuffer = binArraysAccInfo[i];
8148
- if (!binArrayAccInfoBuffer)
8149
- throw new Error(
8150
- `Bin Array account ${binArrayPubkey.toBase58()} not found`
8151
- );
8152
- const binArrayAccInfo = program.coder.accounts.decode(
8153
- "binArray",
8154
- binArrayAccInfoBuffer.data
8155
- );
8156
- positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo);
8157
- }
8158
- const lbPairArraysMap = /* @__PURE__ */ new Map();
8159
- for (let i = binArrayPubkeyArray.length; i < binArrayPubkeyArray.length + lbPairArray.length; i++) {
8160
- const lbPairPubkey = lbPairArray[i - binArrayPubkeyArray.length];
8161
- const lbPairAccInfoBuffer = binArraysAccInfo[i];
8162
- if (!lbPairAccInfoBuffer)
8163
- throw new Error(`LB Pair account ${lbPairPubkey.toBase58()} not found`);
8164
- const lbPairAccInfo = program.coder.accounts.decode(
8165
- "lbPair",
8166
- lbPairAccInfoBuffer.data
8167
- );
8168
- lbPairArraysMap.set(lbPairPubkey.toBase58(), lbPairAccInfo);
8169
- }
8170
- const reservePublicKeys = Array.from(lbPairArraysMap.values()).map(({ reserveX, reserveY, tokenXMint, tokenYMint }) => [
8171
- reserveX,
8172
- reserveY,
8173
- tokenXMint,
8174
- tokenYMint
8175
- ]).flat();
8176
8107
  const positionBinArraysMapV2 = /* @__PURE__ */ new Map();
8177
- for (let i = binArrayPubkeyArray.length + lbPairArray.length; i < binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length; i++) {
8178
- const binArrayPubkey = binArrayPubkeyArrayV2[i - (binArrayPubkeyArray.length + lbPairArray.length)];
8108
+ for (let i = 0; i < binArrayPubkeyArrayV2.length; i++) {
8109
+ const binArrayPubkey = binArrayPubkeyArrayV2[i];
8179
8110
  const binArrayAccInfoBufferV2 = binArraysAccInfo[i];
8180
- if (!binArrayAccInfoBufferV2)
8181
- throw new Error(
8182
- `Bin Array account ${binArrayPubkey.toBase58()} not found`
8111
+ if (binArrayAccInfoBufferV2) {
8112
+ const binArrayAccInfo = program.coder.accounts.decode(
8113
+ "binArray",
8114
+ binArrayAccInfoBufferV2.data
8183
8115
  );
8184
- const binArrayAccInfo = program.coder.accounts.decode(
8185
- "binArray",
8186
- binArrayAccInfoBufferV2.data
8187
- );
8188
- positionBinArraysMapV2.set(binArrayPubkey.toBase58(), binArrayAccInfo);
8116
+ positionBinArraysMapV2.set(binArrayPubkey.toBase58(), binArrayAccInfo);
8117
+ }
8189
8118
  }
8190
8119
  const lbPairArraysMapV2 = /* @__PURE__ */ new Map();
8191
- for (let i = binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length; i < binArraysAccInfo.length; i++) {
8192
- const lbPairPubkey = lbPairArrayV2[i - (binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length)];
8120
+ for (let i = binArrayPubkeyArrayV2.length; i < binArraysAccInfo.length; i++) {
8121
+ const lbPairPubkey = lbPairArrayV2[i - binArrayPubkeyArrayV2.length];
8193
8122
  const lbPairAccInfoBufferV2 = binArraysAccInfo[i];
8194
8123
  if (!lbPairAccInfoBufferV2)
8195
8124
  throw new Error(`LB Pair account ${lbPairPubkey.toBase58()} not found`);
@@ -8207,43 +8136,14 @@ var DLMM = class {
8207
8136
  ]).flat();
8208
8137
  const reserveAccountsInfo = await chunkedGetMultipleAccountInfos(
8209
8138
  program.provider.connection,
8210
- [...reservePublicKeys, ...reservePublicKeysV2]
8139
+ reservePublicKeysV2
8211
8140
  );
8212
- const lbPairReserveMap = /* @__PURE__ */ new Map();
8213
- const lbPairMintMap = /* @__PURE__ */ new Map();
8214
- lbPairArray.forEach((lbPair, idx) => {
8215
- const index = idx * 4;
8216
- const reserveAccBufferX = reserveAccountsInfo[index];
8217
- const reserveAccBufferY = reserveAccountsInfo[index + 1];
8218
- if (!reserveAccBufferX || !reserveAccBufferY)
8219
- throw new Error(
8220
- `Reserve account for LB Pair ${lbPair.toBase58()} not found`
8221
- );
8222
- const reserveAccX = _spltoken.AccountLayout.decode(reserveAccBufferX.data);
8223
- const reserveAccY = _spltoken.AccountLayout.decode(reserveAccBufferY.data);
8224
- lbPairReserveMap.set(lbPair.toBase58(), {
8225
- reserveX: reserveAccX.amount,
8226
- reserveY: reserveAccY.amount
8227
- });
8228
- const mintXBuffer = reserveAccountsInfo[index + 2];
8229
- const mintYBuffer = reserveAccountsInfo[index + 3];
8230
- if (!mintXBuffer || !mintYBuffer)
8231
- throw new Error(
8232
- `Mint account for LB Pair ${lbPair.toBase58()} not found`
8233
- );
8234
- const mintX = _spltoken.MintLayout.decode(mintXBuffer.data);
8235
- const mintY = _spltoken.MintLayout.decode(mintYBuffer.data);
8236
- lbPairMintMap.set(lbPair.toBase58(), {
8237
- mintXDecimal: mintX.decimals,
8238
- mintYDecimal: mintY.decimals
8239
- });
8240
- });
8241
8141
  const lbPairReserveMapV2 = /* @__PURE__ */ new Map();
8242
8142
  const lbPairMintMapV2 = /* @__PURE__ */ new Map();
8243
8143
  lbPairArrayV2.forEach((lbPair, idx) => {
8244
8144
  const index = idx * 4;
8245
- const reserveAccBufferXV2 = reserveAccountsInfo[reservePublicKeys.length + index];
8246
- const reserveAccBufferYV2 = reserveAccountsInfo[reservePublicKeys.length + index + 1];
8145
+ const reserveAccBufferXV2 = reserveAccountsInfo[index];
8146
+ const reserveAccBufferYV2 = reserveAccountsInfo[index + 1];
8247
8147
  if (!reserveAccBufferXV2 || !reserveAccBufferYV2)
8248
8148
  throw new Error(
8249
8149
  `Reserve account for LB Pair ${lbPair.toBase58()} not found`
@@ -8254,8 +8154,8 @@ var DLMM = class {
8254
8154
  reserveX: reserveAccX.amount,
8255
8155
  reserveY: reserveAccY.amount
8256
8156
  });
8257
- const mintXBufferV2 = reserveAccountsInfo[reservePublicKeys.length + index + 2];
8258
- const mintYBufferV2 = reserveAccountsInfo[reservePublicKeys.length + index + 3];
8157
+ const mintXBufferV2 = reserveAccountsInfo[index + 2];
8158
+ const mintYBufferV2 = reserveAccountsInfo[index + 3];
8259
8159
  if (!mintXBufferV2 || !mintYBufferV2)
8260
8160
  throw new Error(
8261
8161
  `Mint account for LB Pair ${lbPair.toBase58()} not found`
@@ -8271,74 +8171,6 @@ var DLMM = class {
8271
8171
  clockAccInfo.data.readBigInt64LE(32).toString()
8272
8172
  ).toNumber();
8273
8173
  const positionsMap = /* @__PURE__ */ new Map();
8274
- for (let position of positions) {
8275
- const { account, publicKey: positionPubKey } = position;
8276
- const { upperBinId, lowerBinId, lbPair } = account;
8277
- const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
8278
- const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(upperBinId));
8279
- const [lowerBinArrayPubKey] = deriveBinArray(
8280
- lbPair,
8281
- lowerBinArrayIndex,
8282
- program.programId
8283
- );
8284
- const [upperBinArrayPubKey] = deriveBinArray(
8285
- lbPair,
8286
- upperBinArrayIndex,
8287
- program.programId
8288
- );
8289
- const lowerBinArray = positionBinArraysMap.get(
8290
- lowerBinArrayPubKey.toBase58()
8291
- );
8292
- const upperBinArray = positionBinArraysMap.get(
8293
- upperBinArrayPubKey.toBase58()
8294
- );
8295
- const lbPairAcc = lbPairArraysMap.get(lbPair.toBase58());
8296
- const { mintXDecimal, mintYDecimal } = lbPairMintMap.get(
8297
- lbPair.toBase58()
8298
- );
8299
- const reserveXBalance = _nullishCoalesce(_optionalChain([lbPairReserveMap, 'access', _47 => _47.get, 'call', _48 => _48(lbPair.toBase58()), 'optionalAccess', _49 => _49.reserveX]), () => ( BigInt(0)));
8300
- const reserveYBalance = _nullishCoalesce(_optionalChain([lbPairReserveMap, 'access', _50 => _50.get, 'call', _51 => _51(lbPair.toBase58()), 'optionalAccess', _52 => _52.reserveY]), () => ( BigInt(0)));
8301
- const tokenX = {
8302
- publicKey: lbPairAcc.tokenXMint,
8303
- reserve: lbPairAcc.reserveX,
8304
- amount: reserveXBalance,
8305
- decimal: mintXDecimal
8306
- };
8307
- const tokenY = {
8308
- publicKey: lbPairAcc.tokenYMint,
8309
- reserve: lbPairAcc.reserveY,
8310
- amount: reserveYBalance,
8311
- decimal: mintYDecimal
8312
- };
8313
- const positionData = await DLMM.processPosition(
8314
- program,
8315
- 0 /* V1 */,
8316
- lbPairAcc,
8317
- onChainTimestamp,
8318
- account,
8319
- mintXDecimal,
8320
- mintYDecimal,
8321
- lowerBinArray,
8322
- upperBinArray,
8323
- _web3js.PublicKey.default
8324
- );
8325
- if (positionData) {
8326
- positionsMap.set(lbPair.toBase58(), {
8327
- publicKey: lbPair,
8328
- lbPair: lbPairAcc,
8329
- tokenX,
8330
- tokenY,
8331
- lbPairPositionsData: [
8332
- ..._nullishCoalesce(_optionalChain([positionsMap, 'access', _53 => _53.get, 'call', _54 => _54(lbPair.toBase58()), 'optionalAccess', _55 => _55.lbPairPositionsData]), () => ( [])),
8333
- {
8334
- publicKey: positionPubKey,
8335
- positionData,
8336
- version: 0 /* V1 */
8337
- }
8338
- ]
8339
- });
8340
- }
8341
- }
8342
8174
  for (let position of positionsV2) {
8343
8175
  const { account, publicKey: positionPubKey } = position;
8344
8176
  const { upperBinId, lowerBinId, lbPair, feeOwner } = account;
@@ -8365,8 +8197,8 @@ var DLMM = class {
8365
8197
  getTokenDecimals(program.provider.connection, lbPairAcc.tokenXMint),
8366
8198
  getTokenDecimals(program.provider.connection, lbPairAcc.tokenYMint)
8367
8199
  ]);
8368
- const reserveXBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access', _56 => _56.get, 'call', _57 => _57(lbPair.toBase58()), 'optionalAccess', _58 => _58.reserveX]), () => ( BigInt(0)));
8369
- const reserveYBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access', _59 => _59.get, 'call', _60 => _60(lbPair.toBase58()), 'optionalAccess', _61 => _61.reserveY]), () => ( BigInt(0)));
8200
+ const reserveXBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access', _47 => _47.get, 'call', _48 => _48(lbPair.toBase58()), 'optionalAccess', _49 => _49.reserveX]), () => ( BigInt(0)));
8201
+ const reserveYBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access', _50 => _50.get, 'call', _51 => _51(lbPair.toBase58()), 'optionalAccess', _52 => _52.reserveY]), () => ( BigInt(0)));
8370
8202
  const tokenX = {
8371
8203
  publicKey: lbPairAcc.tokenXMint,
8372
8204
  reserve: lbPairAcc.reserveX,
@@ -8379,7 +8211,7 @@ var DLMM = class {
8379
8211
  amount: reserveYBalance,
8380
8212
  decimal: quoteTokenDecimal
8381
8213
  };
8382
- const positionData = await DLMM.processPosition(
8214
+ const positionData = !!lowerBinArray && !!upperBinArray ? await DLMM.processPosition(
8383
8215
  program,
8384
8216
  1 /* V2 */,
8385
8217
  lbPairAcc,
@@ -8390,7 +8222,21 @@ var DLMM = class {
8390
8222
  lowerBinArray,
8391
8223
  upperBinArray,
8392
8224
  feeOwner
8393
- );
8225
+ ) : {
8226
+ totalXAmount: "0",
8227
+ totalYAmount: "0",
8228
+ positionBinData: [],
8229
+ lastUpdatedAt: new (0, _anchor.BN)(0),
8230
+ upperBinId,
8231
+ lowerBinId,
8232
+ feeX: new (0, _anchor.BN)(0),
8233
+ feeY: new (0, _anchor.BN)(0),
8234
+ rewardOne: new (0, _anchor.BN)(0),
8235
+ rewardTwo: new (0, _anchor.BN)(0),
8236
+ feeOwner,
8237
+ totalClaimedFeeXAmount: new (0, _anchor.BN)(0),
8238
+ totalClaimedFeeYAmount: new (0, _anchor.BN)(0)
8239
+ };
8394
8240
  if (positionData) {
8395
8241
  positionsMap.set(lbPair.toBase58(), {
8396
8242
  publicKey: lbPair,
@@ -8398,7 +8244,7 @@ var DLMM = class {
8398
8244
  tokenX,
8399
8245
  tokenY,
8400
8246
  lbPairPositionsData: [
8401
- ..._nullishCoalesce(_optionalChain([positionsMap, 'access', _62 => _62.get, 'call', _63 => _63(lbPair.toBase58()), 'optionalAccess', _64 => _64.lbPairPositionsData]), () => ( [])),
8247
+ ..._nullishCoalesce(_optionalChain([positionsMap, 'access', _53 => _53.get, 'call', _54 => _54(lbPair.toBase58()), 'optionalAccess', _55 => _55.lbPairPositionsData]), () => ( [])),
8402
8248
  {
8403
8249
  publicKey: positionPubKey,
8404
8250
  positionData,
@@ -8410,56 +8256,6 @@ var DLMM = class {
8410
8256
  }
8411
8257
  return positionsMap;
8412
8258
  }
8413
- static async migratePosition(connection, positions, newPositions, walletPubkey, opt) {
8414
- const cluster = _optionalChain([opt, 'optionalAccess', _65 => _65.cluster]) || "mainnet-beta";
8415
- const provider = new (0, _anchor.AnchorProvider)(
8416
- connection,
8417
- {},
8418
- _anchor.AnchorProvider.defaultOptions()
8419
- );
8420
- const program = new (0, _anchor.Program)(
8421
- IDL,
8422
- _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _66 => _66.programId]), () => ( LBCLMM_PROGRAM_IDS[cluster])),
8423
- provider
8424
- );
8425
- const positionsState = await program.account.position.fetchMultiple(
8426
- positions
8427
- );
8428
- const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash("confirmed");
8429
- return Promise.all(
8430
- positionsState.map(async ({ lbPair, lowerBinId }, idx) => {
8431
- const position = positions[idx];
8432
- const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
8433
- const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _anchor.BN)(1));
8434
- const [lowerBinArrayPubKey] = deriveBinArray(
8435
- lbPair,
8436
- lowerBinArrayIndex,
8437
- program.programId
8438
- );
8439
- const [upperBinArrayPubKey] = deriveBinArray(
8440
- lbPair,
8441
- upperBinArrayIndex,
8442
- program.programId
8443
- );
8444
- const migrateTx = await program.methods.migratePosition().accounts({
8445
- binArrayLower: lowerBinArrayPubKey,
8446
- binArrayUpper: upperBinArrayPubKey,
8447
- lbPair,
8448
- owner: walletPubkey,
8449
- positionV1: position,
8450
- positionV2: newPositions[idx],
8451
- program: program.programId,
8452
- rentReceiver: walletPubkey,
8453
- systemProgram: _web3js.SystemProgram.programId
8454
- }).transaction();
8455
- return new (0, _web3js.Transaction)({
8456
- blockhash,
8457
- lastValidBlockHeight,
8458
- feePayer: walletPubkey
8459
- }).add(migrateTx);
8460
- })
8461
- );
8462
- }
8463
8259
  static getPricePerLamport(tokenXDecimal, tokenYDecimal, price) {
8464
8260
  return new (0, _decimaljs2.default)(price).mul(new (0, _decimaljs2.default)(10 ** (tokenYDecimal - tokenXDecimal))).toString();
8465
8261
  }
@@ -8477,7 +8273,7 @@ var DLMM = class {
8477
8273
  );
8478
8274
  const program = new (0, _anchor.Program)(
8479
8275
  IDL,
8480
- _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _67 => _67.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
8276
+ _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _56 => _56.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
8481
8277
  provider
8482
8278
  );
8483
8279
  const [lbPair] = derivePermissionLbPair(
@@ -8526,7 +8322,7 @@ var DLMM = class {
8526
8322
  );
8527
8323
  const program = new (0, _anchor.Program)(
8528
8324
  IDL,
8529
- _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _68 => _68.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
8325
+ _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _57 => _57.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
8530
8326
  provider
8531
8327
  );
8532
8328
  const [lbPair] = deriveCustomizablePermissionlessLbPair(
@@ -8574,7 +8370,7 @@ var DLMM = class {
8574
8370
  );
8575
8371
  const program = new (0, _anchor.Program)(
8576
8372
  IDL,
8577
- _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _69 => _69.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
8373
+ _nullishCoalesce(_optionalChain([opt, 'optionalAccess', _58 => _58.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
8578
8374
  provider
8579
8375
  );
8580
8376
  const existsPool = await this.getPairPubkeyIfExists(
@@ -8710,7 +8506,7 @@ var DLMM = class {
8710
8506
  swapForY,
8711
8507
  new (0, _anchor.BN)(activeIdToLoop),
8712
8508
  this.lbPair,
8713
- _nullishCoalesce(_optionalChain([this, 'access', _70 => _70.binArrayBitmapExtension, 'optionalAccess', _71 => _71.account]), () => ( null))
8509
+ _nullishCoalesce(_optionalChain([this, 'access', _59 => _59.binArrayBitmapExtension, 'optionalAccess', _60 => _60.account]), () => ( null))
8714
8510
  );
8715
8511
  if (binArrayIndex === null)
8716
8512
  shouldStop = true;
@@ -8951,20 +8747,6 @@ var DLMM = class {
8951
8747
  async getPositionsByUserAndLbPair(userPubKey) {
8952
8748
  const promiseResults = await Promise.all([
8953
8749
  this.getActiveBin(),
8954
- userPubKey && this.program.account.position.all([
8955
- {
8956
- memcmp: {
8957
- bytes: _bytes.bs58.encode(userPubKey.toBuffer()),
8958
- offset: 8 + 32
8959
- }
8960
- },
8961
- {
8962
- memcmp: {
8963
- bytes: _bytes.bs58.encode(this.pubkey.toBuffer()),
8964
- offset: 8
8965
- }
8966
- }
8967
- ]),
8968
8750
  userPubKey && this.program.account.positionV2.all([
8969
8751
  {
8970
8752
  memcmp: {
@@ -8980,7 +8762,7 @@ var DLMM = class {
8980
8762
  }
8981
8763
  ])
8982
8764
  ]);
8983
- const [activeBin, positions, positionsV2] = promiseResults;
8765
+ const [activeBin, positionsV2] = promiseResults;
8984
8766
  if (!activeBin) {
8985
8767
  throw new Error("Error fetching active bin");
8986
8768
  }
@@ -8990,29 +8772,9 @@ var DLMM = class {
8990
8772
  userPositions: []
8991
8773
  };
8992
8774
  }
8993
- if (!positions || !positionsV2) {
8775
+ if (!positionsV2) {
8994
8776
  throw new Error("Error fetching positions");
8995
8777
  }
8996
- const binArrayPubkeySet = /* @__PURE__ */ new Set();
8997
- positions.forEach(({ account: { upperBinId, lowerBinId } }) => {
8998
- const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
8999
- const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(upperBinId));
9000
- const [lowerBinArrayPubKey] = deriveBinArray(
9001
- this.pubkey,
9002
- lowerBinArrayIndex,
9003
- this.program.programId
9004
- );
9005
- const [upperBinArrayPubKey] = deriveBinArray(
9006
- this.pubkey,
9007
- upperBinArrayIndex,
9008
- this.program.programId
9009
- );
9010
- binArrayPubkeySet.add(lowerBinArrayPubKey.toBase58());
9011
- binArrayPubkeySet.add(upperBinArrayPubKey.toBase58());
9012
- });
9013
- const binArrayPubkeyArray = Array.from(binArrayPubkeySet).map(
9014
- (pubkey) => new (0, _web3js.PublicKey)(pubkey)
9015
- );
9016
8778
  const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
9017
8779
  positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
9018
8780
  const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
@@ -9038,28 +8800,13 @@ var DLMM = class {
9038
8800
  [
9039
8801
  this.pubkey,
9040
8802
  _web3js.SYSVAR_CLOCK_PUBKEY,
9041
- ...binArrayPubkeyArray,
9042
8803
  ...binArrayPubkeyArrayV2
9043
8804
  ]
9044
8805
  );
9045
8806
  const [lbPairAccInfo, clockAccInfo, ...binArraysAccInfo] = lbPairAndBinArrays;
9046
- const positionBinArraysMap = /* @__PURE__ */ new Map();
9047
- for (let i = 0; i < binArrayPubkeyArray.length; i++) {
9048
- const binArrayPubkey = binArrayPubkeyArray[i];
9049
- const binArrayAccBuffer = binArraysAccInfo[i];
9050
- if (!binArrayAccBuffer)
9051
- throw new Error(
9052
- `Bin Array account ${binArrayPubkey.toBase58()} not found`
9053
- );
9054
- const binArrayAccInfo = this.program.coder.accounts.decode(
9055
- "binArray",
9056
- binArrayAccBuffer.data
9057
- );
9058
- positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo);
9059
- }
9060
8807
  const positionBinArraysMapV2 = /* @__PURE__ */ new Map();
9061
- for (let i = binArrayPubkeyArray.length; i < binArraysAccInfo.length; i++) {
9062
- const binArrayPubkey = binArrayPubkeyArrayV2[i - binArrayPubkeyArray.length];
8808
+ for (let i = 0; i < binArraysAccInfo.length; i++) {
8809
+ const binArrayPubkey = binArrayPubkeyArrayV2[i];
9063
8810
  const binArrayAccBufferV2 = binArraysAccInfo[i];
9064
8811
  if (!binArrayAccBufferV2)
9065
8812
  throw new Error(
@@ -9076,45 +8823,6 @@ var DLMM = class {
9076
8823
  const onChainTimestamp = new (0, _anchor.BN)(
9077
8824
  clockAccInfo.data.readBigInt64LE(32).toString()
9078
8825
  ).toNumber();
9079
- const userPositions = await Promise.all(
9080
- positions.map(async ({ publicKey, account }) => {
9081
- const { lowerBinId, upperBinId } = account;
9082
- const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
9083
- const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(upperBinId));
9084
- const [lowerBinArrayPubKey] = deriveBinArray(
9085
- this.pubkey,
9086
- lowerBinArrayIndex,
9087
- this.program.programId
9088
- );
9089
- const [upperBinArrayPubKey] = deriveBinArray(
9090
- this.pubkey,
9091
- upperBinArrayIndex,
9092
- this.program.programId
9093
- );
9094
- const lowerBinArray = positionBinArraysMap.get(
9095
- lowerBinArrayPubKey.toBase58()
9096
- );
9097
- const upperBinArray = positionBinArraysMap.get(
9098
- upperBinArrayPubKey.toBase58()
9099
- );
9100
- return {
9101
- publicKey,
9102
- positionData: await DLMM.processPosition(
9103
- this.program,
9104
- 0 /* V1 */,
9105
- this.lbPair,
9106
- onChainTimestamp,
9107
- account,
9108
- this.tokenX.decimal,
9109
- this.tokenY.decimal,
9110
- lowerBinArray,
9111
- upperBinArray,
9112
- _web3js.PublicKey.default
9113
- ),
9114
- version: 0 /* V1 */
9115
- };
9116
- })
9117
- );
9118
8826
  const userPositionsV2 = await Promise.all(
9119
8827
  positionsV2.map(async ({ publicKey, account }) => {
9120
8828
  const { lowerBinId, upperBinId, feeOwner } = account;
@@ -9156,7 +8864,7 @@ var DLMM = class {
9156
8864
  );
9157
8865
  return {
9158
8866
  activeBin,
9159
- userPositions: [...userPositions, ...userPositionsV2]
8867
+ userPositions: userPositionsV2
9160
8868
  };
9161
8869
  }
9162
8870
  async quoteCreatePosition({ strategy }) {
@@ -9217,6 +8925,93 @@ var DLMM = class {
9217
8925
  feePayer: user
9218
8926
  }).add(setCUIx, ...instructions);
9219
8927
  }
8928
+ /**
8929
+ * The function `getPosition` retrieves position information for a given public key and processes it
8930
+ * using various data to return a `LbPosition` object.
8931
+ * @param {PublicKey} positionPubKey - The `getPosition` function you provided is an asynchronous
8932
+ * function that fetches position information based on a given public key. Here's a breakdown of the
8933
+ * parameters used in the function:
8934
+ * @returns The `getPosition` function returns a Promise that resolves to an object of type
8935
+ * `LbPosition`. The object contains the following properties:
8936
+ * - `publicKey`: The public key of the position account
8937
+ * - `positionData`: Position Object
8938
+ * - `version`: The version of the position (in this case, `Position.V2`)
8939
+ */
8940
+ async getPosition(positionPubKey) {
8941
+ const positionAccountInfo = await this.program.account.positionV2.fetch(positionPubKey);
8942
+ if (!positionAccountInfo) {
8943
+ throw new Error(`Position account ${positionPubKey.toBase58()} not found`);
8944
+ }
8945
+ const { lowerBinId, upperBinId, feeOwner } = positionAccountInfo;
8946
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
8947
+ const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(upperBinId));
8948
+ const [lowerBinArrayPubKey] = deriveBinArray(
8949
+ this.pubkey,
8950
+ lowerBinArrayIndex,
8951
+ this.program.programId
8952
+ );
8953
+ const [upperBinArrayPubKey] = deriveBinArray(
8954
+ this.pubkey,
8955
+ upperBinArrayIndex,
8956
+ this.program.programId
8957
+ );
8958
+ const [clockAccInfo, lowerBinArrayAccInfo, upperBinArrayAccInfo] = await chunkedGetMultipleAccountInfos(
8959
+ this.program.provider.connection,
8960
+ [
8961
+ _web3js.SYSVAR_CLOCK_PUBKEY,
8962
+ lowerBinArrayPubKey,
8963
+ upperBinArrayPubKey
8964
+ ]
8965
+ );
8966
+ if (!lowerBinArrayAccInfo || !upperBinArrayAccInfo) {
8967
+ return {
8968
+ publicKey: positionPubKey,
8969
+ positionData: {
8970
+ totalXAmount: "0",
8971
+ totalYAmount: "0",
8972
+ positionBinData: [],
8973
+ lastUpdatedAt: new (0, _anchor.BN)(0),
8974
+ upperBinId,
8975
+ lowerBinId,
8976
+ feeX: new (0, _anchor.BN)(0),
8977
+ feeY: new (0, _anchor.BN)(0),
8978
+ rewardOne: new (0, _anchor.BN)(0),
8979
+ rewardTwo: new (0, _anchor.BN)(0),
8980
+ feeOwner,
8981
+ totalClaimedFeeXAmount: new (0, _anchor.BN)(0),
8982
+ totalClaimedFeeYAmount: new (0, _anchor.BN)(0)
8983
+ },
8984
+ version: 1 /* V2 */
8985
+ };
8986
+ }
8987
+ const onChainTimestamp = new (0, _anchor.BN)(
8988
+ clockAccInfo.data.readBigInt64LE(32).toString()
8989
+ ).toNumber();
8990
+ const lowerBinArray = this.program.coder.accounts.decode(
8991
+ "binArray",
8992
+ lowerBinArrayAccInfo.data
8993
+ );
8994
+ const upperBinArray = this.program.coder.accounts.decode(
8995
+ "binArray",
8996
+ upperBinArrayAccInfo.data
8997
+ );
8998
+ return {
8999
+ publicKey: positionPubKey,
9000
+ positionData: await DLMM.processPosition(
9001
+ this.program,
9002
+ 1 /* V2 */,
9003
+ this.lbPair,
9004
+ onChainTimestamp,
9005
+ positionAccountInfo,
9006
+ this.tokenX.decimal,
9007
+ this.tokenY.decimal,
9008
+ lowerBinArray,
9009
+ upperBinArray,
9010
+ feeOwner
9011
+ ),
9012
+ version: 1 /* V2 */
9013
+ };
9014
+ }
9220
9015
  /**
9221
9016
  * The function `initializePositionAndAddLiquidityByStrategy` function is used to initializes a position and adds liquidity
9222
9017
  * @param {TInitializePositionAndAddLiquidityParamsByStrategy}
@@ -9940,9 +9735,13 @@ var DLMM = class {
9940
9735
  bps,
9941
9736
  shouldClaimAndClose = false
9942
9737
  }) {
9943
- const { lbPair, lowerBinId, owner, feeOwner } = await this.program.account.positionV2.fetch(position);
9944
- const { reserveX, reserveY, tokenXMint, tokenYMint } = await this.program.account.lbPair.fetch(lbPair);
9945
- const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
9738
+ const lowerBinIdToRemove = Math.min(...binIds);
9739
+ const upperBinIdToRemove = Math.max(...binIds);
9740
+ const { lbPair, owner, feeOwner, lowerBinId: positionLowerBinId, liquidityShares } = await this.program.account.positionV2.fetch(position);
9741
+ if (liquidityShares.every((share) => share.isZero())) {
9742
+ throw new Error("No liquidity to remove");
9743
+ }
9744
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(positionLowerBinId));
9946
9745
  const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _anchor.BN)(1));
9947
9746
  const [binArrayLower] = deriveBinArray(
9948
9747
  lbPair,
@@ -10002,8 +9801,8 @@ var DLMM = class {
10002
9801
  lbPair: this.pubkey,
10003
9802
  sender: user,
10004
9803
  position,
10005
- reserveX,
10006
- reserveY,
9804
+ reserveX: this.lbPair.reserveX,
9805
+ reserveY: this.lbPair.reserveY,
10007
9806
  tokenProgram: _spltoken.TOKEN_PROGRAM_ID,
10008
9807
  tokenXMint: this.tokenX.publicKey,
10009
9808
  tokenYMint: this.tokenY.publicKey,
@@ -10056,21 +9855,19 @@ var DLMM = class {
10056
9855
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
10057
9856
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
10058
9857
  }
10059
- const minBinId = Math.min(...binIds);
10060
- const maxBinId = Math.max(...binIds);
10061
- const minBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(minBinId));
10062
- const maxBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(maxBinId));
9858
+ const minBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinIdToRemove));
9859
+ const maxBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(upperBinIdToRemove));
10063
9860
  const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
10064
9861
  const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
10065
- const removeLiquidityIx = await this.program.methods.removeLiquidityByRange(minBinId, maxBinId, bps.toNumber()).accounts({
9862
+ const removeLiquidityTx = await this.program.methods.removeLiquidityByRange(lowerBinIdToRemove, upperBinIdToRemove, bps.toNumber()).accounts({
10066
9863
  position,
10067
9864
  lbPair,
10068
9865
  userTokenX,
10069
9866
  userTokenY,
10070
- reserveX,
10071
- reserveY,
10072
- tokenXMint,
10073
- tokenYMint,
9867
+ reserveX: this.lbPair.reserveX,
9868
+ reserveY: this.lbPair.reserveY,
9869
+ tokenXMint: this.tokenX.publicKey,
9870
+ tokenYMint: this.tokenY.publicKey,
10074
9871
  binArrayLower,
10075
9872
  binArrayUpper,
10076
9873
  binArrayBitmapExtension,
@@ -10080,7 +9877,7 @@ var DLMM = class {
10080
9877
  }).instruction();
10081
9878
  const instructions = [
10082
9879
  ...preInstructions,
10083
- removeLiquidityIx,
9880
+ removeLiquidityTx,
10084
9881
  ...postInstructions
10085
9882
  ];
10086
9883
  const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
@@ -10196,7 +9993,7 @@ var DLMM = class {
10196
9993
  swapForY,
10197
9994
  activeId,
10198
9995
  this.lbPair,
10199
- _nullishCoalesce(_optionalChain([this, 'access', _72 => _72.binArrayBitmapExtension, 'optionalAccess', _73 => _73.account]), () => ( null)),
9996
+ _nullishCoalesce(_optionalChain([this, 'access', _61 => _61.binArrayBitmapExtension, 'optionalAccess', _62 => _62.account]), () => ( null)),
10200
9997
  binArrays
10201
9998
  );
10202
9999
  if (binArrayAccountToSwap == null) {
@@ -10300,7 +10097,7 @@ var DLMM = class {
10300
10097
  swapForY,
10301
10098
  activeId,
10302
10099
  this.lbPair,
10303
- _nullishCoalesce(_optionalChain([this, 'access', _74 => _74.binArrayBitmapExtension, 'optionalAccess', _75 => _75.account]), () => ( null)),
10100
+ _nullishCoalesce(_optionalChain([this, 'access', _63 => _63.binArrayBitmapExtension, 'optionalAccess', _64 => _64.account]), () => ( null)),
10304
10101
  binArrays
10305
10102
  );
10306
10103
  if (binArrayAccountToSwap == null) {
@@ -11486,7 +11283,7 @@ var DLMM = class {
11486
11283
  swapForY,
11487
11284
  new (0, _anchor.BN)(activeBinId),
11488
11285
  this.lbPair,
11489
- _nullishCoalesce(_optionalChain([this, 'access', _76 => _76.binArrayBitmapExtension, 'optionalAccess', _77 => _77.account]), () => ( null))
11286
+ _nullishCoalesce(_optionalChain([this, 'access', _65 => _65.binArrayBitmapExtension, 'optionalAccess', _66 => _66.account]), () => ( null))
11490
11287
  );
11491
11288
  if (toBinArrayIndex === null)
11492
11289
  return true;
@@ -11523,7 +11320,7 @@ var DLMM = class {
11523
11320
  swapForY,
11524
11321
  new (0, _anchor.BN)(activeBinId),
11525
11322
  this.lbPair,
11526
- _nullishCoalesce(_optionalChain([this, 'access', _78 => _78.binArrayBitmapExtension, 'optionalAccess', _79 => _79.account]), () => ( null))
11323
+ _nullishCoalesce(_optionalChain([this, 'access', _67 => _67.binArrayBitmapExtension, 'optionalAccess', _68 => _68.account]), () => ( null))
11527
11324
  );
11528
11325
  const accountsToFetch = [];
11529
11326
  const [binArrayBitMapExtensionPubkey] = deriveBinArrayBitmapExtension(
@@ -11554,13 +11351,13 @@ var DLMM = class {
11554
11351
  let fromBinArray = null;
11555
11352
  let toBinArray = null;
11556
11353
  let binArrayBitmapExtension = null;
11557
- if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _80 => _80[0]])) {
11354
+ if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _69 => _69[0]])) {
11558
11355
  binArrayBitmapExtension = binArrayBitMapExtensionPubkey;
11559
11356
  }
11560
- if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _81 => _81[1]])) {
11357
+ if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _70 => _70[1]])) {
11561
11358
  fromBinArray = fromBinArrayPubkey;
11562
11359
  }
11563
- if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _82 => _82[2]]) && !!toBinArrayIndex) {
11360
+ if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _71 => _71[2]]) && !!toBinArrayIndex) {
11564
11361
  toBinArray = toBinArrayPubkey;
11565
11362
  }
11566
11363
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
@@ -11885,12 +11682,7 @@ var DLMM = class {
11885
11682
  let totalYAmount = new (0, _decimaljs2.default)(0);
11886
11683
  bins.forEach((bin, idx) => {
11887
11684
  const binSupply = new (0, _decimaljs2.default)(bin.supply.toString());
11888
- let posShare;
11889
- if (bin.version === 1 && version === 0 /* V1 */) {
11890
- posShare = new (0, _decimaljs2.default)(posShares[idx].shln(64).toString());
11891
- } else {
11892
- posShare = new (0, _decimaljs2.default)(posShares[idx].toString());
11893
- }
11685
+ const posShare = new (0, _decimaljs2.default)(posShares[idx].toString());
11894
11686
  const positionXAmount = binSupply.eq(new (0, _decimaljs2.default)("0")) ? new (0, _decimaljs2.default)("0") : posShare.mul(bin.xAmount.toString()).div(binSupply);
11895
11687
  const positionYAmount = binSupply.eq(new (0, _decimaljs2.default)("0")) ? new (0, _decimaljs2.default)("0") : posShare.mul(bin.yAmount.toString()).div(binSupply);
11896
11688
  totalXAmount = totalXAmount.add(positionXAmount);
@@ -12041,7 +11833,7 @@ var DLMM = class {
12041
11833
  const [lowerBinId2] = getBinArrayLowerUpperBinId(index);
12042
11834
  return bins.map((b, i) => [lowerBinId2.toNumber() + i, b]);
12043
11835
  }));
12044
- const version = _nullishCoalesce(_optionalChain([binArrays, 'access', _83 => _83.find, 'call', _84 => _84((binArray) => binArray != null), 'optionalAccess', _85 => _85.version]), () => ( 1));
11836
+ const version = _nullishCoalesce(_optionalChain([binArrays, 'access', _72 => _72.find, 'call', _73 => _73((binArray) => binArray != null), 'optionalAccess', _74 => _74.version]), () => ( 1));
12045
11837
  return Array.from(enumerateBins(
12046
11838
  binsById,
12047
11839
  lowerBinId,