@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.d.ts +13 -1
- package/dist/index.js +162 -370
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +147 -355
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -8066,49 +8066,14 @@ var DLMM = class {
|
|
|
8066
8066
|
opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
|
|
8067
8067
|
provider
|
|
8068
8068
|
);
|
|
8069
|
-
const
|
|
8070
|
-
|
|
8071
|
-
{
|
|
8072
|
-
|
|
8073
|
-
|
|
8074
|
-
offset: 8 + 32
|
|
8075
|
-
}
|
|
8076
|
-
}
|
|
8077
|
-
]),
|
|
8078
|
-
program.account.positionV2.all([
|
|
8079
|
-
{
|
|
8080
|
-
memcmp: {
|
|
8081
|
-
bytes: bs58.encode(userPubKey.toBuffer()),
|
|
8082
|
-
offset: 8 + 32
|
|
8083
|
-
}
|
|
8069
|
+
const positionsV2 = await program.account.positionV2.all([
|
|
8070
|
+
{
|
|
8071
|
+
memcmp: {
|
|
8072
|
+
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 BN10(lowerBinId));
|
|
8091
|
-
const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(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 PublicKey6(pubkey)
|
|
8108
|
-
);
|
|
8109
|
-
const lbPairArray = Array.from(lbPairSet).map(
|
|
8110
|
-
(pubkey) => new PublicKey6(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
|
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 =
|
|
8178
|
-
const binArrayPubkey = binArrayPubkeyArrayV2[i
|
|
8108
|
+
for (let i = 0; i < binArrayPubkeyArrayV2.length; i++) {
|
|
8109
|
+
const binArrayPubkey = binArrayPubkeyArrayV2[i];
|
|
8179
8110
|
const binArrayAccInfoBufferV2 = binArraysAccInfo[i];
|
|
8180
|
-
if (
|
|
8181
|
-
|
|
8182
|
-
|
|
8111
|
+
if (binArrayAccInfoBufferV2) {
|
|
8112
|
+
const binArrayAccInfo = program.coder.accounts.decode(
|
|
8113
|
+
"binArray",
|
|
8114
|
+
binArrayAccInfoBufferV2.data
|
|
8183
8115
|
);
|
|
8184
|
-
|
|
8185
|
-
|
|
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 =
|
|
8192
|
-
const lbPairPubkey = lbPairArrayV2[i -
|
|
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
|
-
|
|
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 = AccountLayout.decode(reserveAccBufferX.data);
|
|
8223
|
-
const reserveAccY = 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 = MintLayout.decode(mintXBuffer.data);
|
|
8235
|
-
const mintY = 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[
|
|
8246
|
-
const reserveAccBufferYV2 = reserveAccountsInfo[
|
|
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[
|
|
8258
|
-
const mintYBufferV2 = reserveAccountsInfo[
|
|
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 BN10(lowerBinId));
|
|
8278
|
-
const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(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 = lbPairReserveMap.get(lbPair.toBase58())?.reserveX ?? BigInt(0);
|
|
8300
|
-
const reserveYBalance = lbPairReserveMap.get(lbPair.toBase58())?.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
|
-
PublicKey6.default
|
|
8324
|
-
);
|
|
8325
|
-
if (positionData) {
|
|
8326
|
-
positionsMap.set(lbPair.toBase58(), {
|
|
8327
|
-
publicKey: lbPair,
|
|
8328
|
-
lbPair: lbPairAcc,
|
|
8329
|
-
tokenX,
|
|
8330
|
-
tokenY,
|
|
8331
|
-
lbPairPositionsData: [
|
|
8332
|
-
...positionsMap.get(lbPair.toBase58())?.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;
|
|
@@ -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 BN10(0),
|
|
8230
|
+
upperBinId,
|
|
8231
|
+
lowerBinId,
|
|
8232
|
+
feeX: new BN10(0),
|
|
8233
|
+
feeY: new BN10(0),
|
|
8234
|
+
rewardOne: new BN10(0),
|
|
8235
|
+
rewardTwo: new BN10(0),
|
|
8236
|
+
feeOwner,
|
|
8237
|
+
totalClaimedFeeXAmount: new BN10(0),
|
|
8238
|
+
totalClaimedFeeYAmount: new BN10(0)
|
|
8239
|
+
};
|
|
8394
8240
|
if (positionData) {
|
|
8395
8241
|
positionsMap.set(lbPair.toBase58(), {
|
|
8396
8242
|
publicKey: lbPair,
|
|
@@ -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 = opt?.cluster || "mainnet-beta";
|
|
8415
|
-
const provider = new AnchorProvider2(
|
|
8416
|
-
connection,
|
|
8417
|
-
{},
|
|
8418
|
-
AnchorProvider2.defaultOptions()
|
|
8419
|
-
);
|
|
8420
|
-
const program = new Program3(
|
|
8421
|
-
IDL,
|
|
8422
|
-
opt?.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 BN10(lowerBinId));
|
|
8433
|
-
const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(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: SystemProgram2.programId
|
|
8454
|
-
}).transaction();
|
|
8455
|
-
return new 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 Decimal5(price).mul(new Decimal5(10 ** (tokenYDecimal - tokenXDecimal))).toString();
|
|
8465
8261
|
}
|
|
@@ -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: bs58.encode(userPubKey.toBuffer()),
|
|
8958
|
-
offset: 8 + 32
|
|
8959
|
-
}
|
|
8960
|
-
},
|
|
8961
|
-
{
|
|
8962
|
-
memcmp: {
|
|
8963
|
-
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,
|
|
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 (!
|
|
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 BN10(lowerBinId));
|
|
8999
|
-
const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(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 PublicKey6(pubkey)
|
|
9015
|
-
);
|
|
9016
8778
|
const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
|
|
9017
8779
|
positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
|
|
9018
8780
|
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
|
|
@@ -9038,28 +8800,13 @@ var DLMM = class {
|
|
|
9038
8800
|
[
|
|
9039
8801
|
this.pubkey,
|
|
9040
8802
|
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 =
|
|
9062
|
-
const binArrayPubkey = binArrayPubkeyArrayV2[i
|
|
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 BN10(
|
|
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 BN10(lowerBinId));
|
|
9083
|
-
const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(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
|
-
PublicKey6.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:
|
|
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 BN10(lowerBinId));
|
|
8947
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(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
|
+
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 BN10(0),
|
|
8974
|
+
upperBinId,
|
|
8975
|
+
lowerBinId,
|
|
8976
|
+
feeX: new BN10(0),
|
|
8977
|
+
feeY: new BN10(0),
|
|
8978
|
+
rewardOne: new BN10(0),
|
|
8979
|
+
rewardTwo: new BN10(0),
|
|
8980
|
+
feeOwner,
|
|
8981
|
+
totalClaimedFeeXAmount: new BN10(0),
|
|
8982
|
+
totalClaimedFeeYAmount: new BN10(0)
|
|
8983
|
+
},
|
|
8984
|
+
version: 1 /* V2 */
|
|
8985
|
+
};
|
|
8986
|
+
}
|
|
8987
|
+
const onChainTimestamp = new BN10(
|
|
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
|
|
9944
|
-
const
|
|
9945
|
-
const
|
|
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 BN10(positionLowerBinId));
|
|
9946
9745
|
const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(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: TOKEN_PROGRAM_ID2,
|
|
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
|
|
10060
|
-
const
|
|
10061
|
-
const minBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
|
|
10062
|
-
const maxBinArrayIndex = binIdToBinArrayIndex(new BN10(maxBinId));
|
|
9858
|
+
const minBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinIdToRemove));
|
|
9859
|
+
const maxBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinIdToRemove));
|
|
10063
9860
|
const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
|
|
10064
9861
|
const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
|
|
10065
|
-
const
|
|
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
|
-
|
|
9880
|
+
removeLiquidityTx,
|
|
10084
9881
|
...postInstructions
|
|
10085
9882
|
];
|
|
10086
9883
|
const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
|
|
@@ -11885,12 +11682,7 @@ var DLMM = class {
|
|
|
11885
11682
|
let totalYAmount = new Decimal5(0);
|
|
11886
11683
|
bins.forEach((bin, idx) => {
|
|
11887
11684
|
const binSupply = new Decimal5(bin.supply.toString());
|
|
11888
|
-
|
|
11889
|
-
if (bin.version === 1 && version === 0 /* V1 */) {
|
|
11890
|
-
posShare = new Decimal5(posShares[idx].shln(64).toString());
|
|
11891
|
-
} else {
|
|
11892
|
-
posShare = new Decimal5(posShares[idx].toString());
|
|
11893
|
-
}
|
|
11685
|
+
const posShare = new Decimal5(posShares[idx].toString());
|
|
11894
11686
|
const positionXAmount = binSupply.eq(new Decimal5("0")) ? new Decimal5("0") : posShare.mul(bin.xAmount.toString()).div(binSupply);
|
|
11895
11687
|
const positionYAmount = binSupply.eq(new Decimal5("0")) ? new Decimal5("0") : posShare.mul(bin.yAmount.toString()).div(binSupply);
|
|
11896
11688
|
totalXAmount = totalXAmount.add(positionXAmount);
|