@meteora-ag/dlmm 1.3.1-sam.0 → 1.3.3
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 +21 -14
- package/dist/index.js +532 -141
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +518 -127
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -7588,6 +7588,8 @@ import {
|
|
|
7588
7588
|
MintLayout,
|
|
7589
7589
|
NATIVE_MINT as NATIVE_MINT2,
|
|
7590
7590
|
TOKEN_PROGRAM_ID as TOKEN_PROGRAM_ID2,
|
|
7591
|
+
createAssociatedTokenAccountInstruction as createAssociatedTokenAccountInstruction2,
|
|
7592
|
+
createTransferInstruction,
|
|
7591
7593
|
getAssociatedTokenAddressSync as getAssociatedTokenAddressSync2
|
|
7592
7594
|
} from "@solana/spl-token";
|
|
7593
7595
|
|
|
@@ -7975,14 +7977,49 @@ var DLMM = class {
|
|
|
7975
7977
|
opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
|
|
7976
7978
|
provider
|
|
7977
7979
|
);
|
|
7978
|
-
const positionsV2 = await
|
|
7979
|
-
|
|
7980
|
-
|
|
7981
|
-
|
|
7982
|
-
|
|
7980
|
+
const [positions, positionsV2] = await Promise.all([
|
|
7981
|
+
program.account.position.all([
|
|
7982
|
+
{
|
|
7983
|
+
memcmp: {
|
|
7984
|
+
bytes: bs58.encode(userPubKey.toBuffer()),
|
|
7985
|
+
offset: 8 + 32
|
|
7986
|
+
}
|
|
7983
7987
|
}
|
|
7984
|
-
|
|
7988
|
+
]),
|
|
7989
|
+
program.account.positionV2.all([
|
|
7990
|
+
{
|
|
7991
|
+
memcmp: {
|
|
7992
|
+
bytes: bs58.encode(userPubKey.toBuffer()),
|
|
7993
|
+
offset: 8 + 32
|
|
7994
|
+
}
|
|
7995
|
+
}
|
|
7996
|
+
])
|
|
7985
7997
|
]);
|
|
7998
|
+
const binArrayPubkeySet = /* @__PURE__ */ new Set();
|
|
7999
|
+
const lbPairSet = /* @__PURE__ */ new Set();
|
|
8000
|
+
positions.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
|
|
8001
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
8002
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
|
|
8003
|
+
const [lowerBinArrayPubKey] = deriveBinArray(
|
|
8004
|
+
lbPair,
|
|
8005
|
+
lowerBinArrayIndex,
|
|
8006
|
+
program.programId
|
|
8007
|
+
);
|
|
8008
|
+
const [upperBinArrayPubKey] = deriveBinArray(
|
|
8009
|
+
lbPair,
|
|
8010
|
+
upperBinArrayIndex,
|
|
8011
|
+
program.programId
|
|
8012
|
+
);
|
|
8013
|
+
binArrayPubkeySet.add(lowerBinArrayPubKey.toBase58());
|
|
8014
|
+
binArrayPubkeySet.add(upperBinArrayPubKey.toBase58());
|
|
8015
|
+
lbPairSet.add(lbPair.toBase58());
|
|
8016
|
+
});
|
|
8017
|
+
const binArrayPubkeyArray = Array.from(binArrayPubkeySet).map(
|
|
8018
|
+
(pubkey) => new PublicKey6(pubkey)
|
|
8019
|
+
);
|
|
8020
|
+
const lbPairArray = Array.from(lbPairSet).map(
|
|
8021
|
+
(pubkey) => new PublicKey6(pubkey)
|
|
8022
|
+
);
|
|
7986
8023
|
const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
|
|
7987
8024
|
const lbPairSetV2 = /* @__PURE__ */ new Set();
|
|
7988
8025
|
positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
|
|
@@ -8010,12 +8047,46 @@ var DLMM = class {
|
|
|
8010
8047
|
);
|
|
8011
8048
|
const [clockAccInfo, ...binArraysAccInfo] = await chunkedGetMultipleAccountInfos(connection, [
|
|
8012
8049
|
SYSVAR_CLOCK_PUBKEY,
|
|
8050
|
+
...binArrayPubkeyArray,
|
|
8051
|
+
...lbPairArray,
|
|
8013
8052
|
...binArrayPubkeyArrayV2,
|
|
8014
8053
|
...lbPairArrayV2
|
|
8015
8054
|
]);
|
|
8055
|
+
const positionBinArraysMap = /* @__PURE__ */ new Map();
|
|
8056
|
+
for (let i = 0; i < binArrayPubkeyArray.length; i++) {
|
|
8057
|
+
const binArrayPubkey = binArrayPubkeyArray[i];
|
|
8058
|
+
const binArrayAccInfoBuffer = binArraysAccInfo[i];
|
|
8059
|
+
if (!binArrayAccInfoBuffer)
|
|
8060
|
+
throw new Error(
|
|
8061
|
+
`Bin Array account ${binArrayPubkey.toBase58()} not found`
|
|
8062
|
+
);
|
|
8063
|
+
const binArrayAccInfo = program.coder.accounts.decode(
|
|
8064
|
+
"binArray",
|
|
8065
|
+
binArrayAccInfoBuffer.data
|
|
8066
|
+
);
|
|
8067
|
+
positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo);
|
|
8068
|
+
}
|
|
8069
|
+
const lbPairArraysMap = /* @__PURE__ */ new Map();
|
|
8070
|
+
for (let i = binArrayPubkeyArray.length; i < binArrayPubkeyArray.length + lbPairArray.length; i++) {
|
|
8071
|
+
const lbPairPubkey = lbPairArray[i - binArrayPubkeyArray.length];
|
|
8072
|
+
const lbPairAccInfoBuffer = binArraysAccInfo[i];
|
|
8073
|
+
if (!lbPairAccInfoBuffer)
|
|
8074
|
+
throw new Error(`LB Pair account ${lbPairPubkey.toBase58()} not found`);
|
|
8075
|
+
const lbPairAccInfo = program.coder.accounts.decode(
|
|
8076
|
+
"lbPair",
|
|
8077
|
+
lbPairAccInfoBuffer.data
|
|
8078
|
+
);
|
|
8079
|
+
lbPairArraysMap.set(lbPairPubkey.toBase58(), lbPairAccInfo);
|
|
8080
|
+
}
|
|
8081
|
+
const reservePublicKeys = Array.from(lbPairArraysMap.values()).map(({ reserveX, reserveY, tokenXMint, tokenYMint }) => [
|
|
8082
|
+
reserveX,
|
|
8083
|
+
reserveY,
|
|
8084
|
+
tokenXMint,
|
|
8085
|
+
tokenYMint
|
|
8086
|
+
]).flat();
|
|
8016
8087
|
const positionBinArraysMapV2 = /* @__PURE__ */ new Map();
|
|
8017
|
-
for (let i =
|
|
8018
|
-
const binArrayPubkey = binArrayPubkeyArrayV2[i];
|
|
8088
|
+
for (let i = binArrayPubkeyArray.length + lbPairArray.length; i < binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length; i++) {
|
|
8089
|
+
const binArrayPubkey = binArrayPubkeyArrayV2[i - (binArrayPubkeyArray.length + lbPairArray.length)];
|
|
8019
8090
|
const binArrayAccInfoBufferV2 = binArraysAccInfo[i];
|
|
8020
8091
|
if (!binArrayAccInfoBufferV2)
|
|
8021
8092
|
throw new Error(
|
|
@@ -8028,8 +8099,8 @@ var DLMM = class {
|
|
|
8028
8099
|
positionBinArraysMapV2.set(binArrayPubkey.toBase58(), binArrayAccInfo);
|
|
8029
8100
|
}
|
|
8030
8101
|
const lbPairArraysMapV2 = /* @__PURE__ */ new Map();
|
|
8031
|
-
for (let i = binArrayPubkeyArrayV2.length; i < binArraysAccInfo.length; i++) {
|
|
8032
|
-
const lbPairPubkey = lbPairArrayV2[i - binArrayPubkeyArrayV2.length];
|
|
8102
|
+
for (let i = binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length; i < binArraysAccInfo.length; i++) {
|
|
8103
|
+
const lbPairPubkey = lbPairArrayV2[i - (binArrayPubkeyArray.length + lbPairArray.length + binArrayPubkeyArrayV2.length)];
|
|
8033
8104
|
const lbPairAccInfoBufferV2 = binArraysAccInfo[i];
|
|
8034
8105
|
if (!lbPairAccInfoBufferV2)
|
|
8035
8106
|
throw new Error(`LB Pair account ${lbPairPubkey.toBase58()} not found`);
|
|
@@ -8047,14 +8118,43 @@ var DLMM = class {
|
|
|
8047
8118
|
]).flat();
|
|
8048
8119
|
const reserveAccountsInfo = await chunkedGetMultipleAccountInfos(
|
|
8049
8120
|
program.provider.connection,
|
|
8050
|
-
reservePublicKeysV2
|
|
8121
|
+
[...reservePublicKeys, ...reservePublicKeysV2]
|
|
8051
8122
|
);
|
|
8123
|
+
const lbPairReserveMap = /* @__PURE__ */ new Map();
|
|
8124
|
+
const lbPairMintMap = /* @__PURE__ */ new Map();
|
|
8125
|
+
lbPairArray.forEach((lbPair, idx) => {
|
|
8126
|
+
const index = idx * 4;
|
|
8127
|
+
const reserveAccBufferX = reserveAccountsInfo[index];
|
|
8128
|
+
const reserveAccBufferY = reserveAccountsInfo[index + 1];
|
|
8129
|
+
if (!reserveAccBufferX || !reserveAccBufferY)
|
|
8130
|
+
throw new Error(
|
|
8131
|
+
`Reserve account for LB Pair ${lbPair.toBase58()} not found`
|
|
8132
|
+
);
|
|
8133
|
+
const reserveAccX = AccountLayout.decode(reserveAccBufferX.data);
|
|
8134
|
+
const reserveAccY = AccountLayout.decode(reserveAccBufferY.data);
|
|
8135
|
+
lbPairReserveMap.set(lbPair.toBase58(), {
|
|
8136
|
+
reserveX: reserveAccX.amount,
|
|
8137
|
+
reserveY: reserveAccY.amount
|
|
8138
|
+
});
|
|
8139
|
+
const mintXBuffer = reserveAccountsInfo[index + 2];
|
|
8140
|
+
const mintYBuffer = reserveAccountsInfo[index + 3];
|
|
8141
|
+
if (!mintXBuffer || !mintYBuffer)
|
|
8142
|
+
throw new Error(
|
|
8143
|
+
`Mint account for LB Pair ${lbPair.toBase58()} not found`
|
|
8144
|
+
);
|
|
8145
|
+
const mintX = MintLayout.decode(mintXBuffer.data);
|
|
8146
|
+
const mintY = MintLayout.decode(mintYBuffer.data);
|
|
8147
|
+
lbPairMintMap.set(lbPair.toBase58(), {
|
|
8148
|
+
mintXDecimal: mintX.decimals,
|
|
8149
|
+
mintYDecimal: mintY.decimals
|
|
8150
|
+
});
|
|
8151
|
+
});
|
|
8052
8152
|
const lbPairReserveMapV2 = /* @__PURE__ */ new Map();
|
|
8053
8153
|
const lbPairMintMapV2 = /* @__PURE__ */ new Map();
|
|
8054
8154
|
lbPairArrayV2.forEach((lbPair, idx) => {
|
|
8055
8155
|
const index = idx * 4;
|
|
8056
|
-
const reserveAccBufferXV2 = reserveAccountsInfo[index];
|
|
8057
|
-
const reserveAccBufferYV2 = reserveAccountsInfo[index + 1];
|
|
8156
|
+
const reserveAccBufferXV2 = reserveAccountsInfo[reservePublicKeys.length + index];
|
|
8157
|
+
const reserveAccBufferYV2 = reserveAccountsInfo[reservePublicKeys.length + index + 1];
|
|
8058
8158
|
if (!reserveAccBufferXV2 || !reserveAccBufferYV2)
|
|
8059
8159
|
throw new Error(
|
|
8060
8160
|
`Reserve account for LB Pair ${lbPair.toBase58()} not found`
|
|
@@ -8065,8 +8165,8 @@ var DLMM = class {
|
|
|
8065
8165
|
reserveX: reserveAccX.amount,
|
|
8066
8166
|
reserveY: reserveAccY.amount
|
|
8067
8167
|
});
|
|
8068
|
-
const mintXBufferV2 = reserveAccountsInfo[index + 2];
|
|
8069
|
-
const mintYBufferV2 = reserveAccountsInfo[index + 3];
|
|
8168
|
+
const mintXBufferV2 = reserveAccountsInfo[reservePublicKeys.length + index + 2];
|
|
8169
|
+
const mintYBufferV2 = reserveAccountsInfo[reservePublicKeys.length + index + 3];
|
|
8070
8170
|
if (!mintXBufferV2 || !mintYBufferV2)
|
|
8071
8171
|
throw new Error(
|
|
8072
8172
|
`Mint account for LB Pair ${lbPair.toBase58()} not found`
|
|
@@ -8082,6 +8182,74 @@ var DLMM = class {
|
|
|
8082
8182
|
clockAccInfo.data.readBigInt64LE(32).toString()
|
|
8083
8183
|
).toNumber();
|
|
8084
8184
|
const positionsMap = /* @__PURE__ */ new Map();
|
|
8185
|
+
for (let position of positions) {
|
|
8186
|
+
const { account, publicKey: positionPubKey } = position;
|
|
8187
|
+
const { upperBinId, lowerBinId, lbPair } = account;
|
|
8188
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
8189
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
|
|
8190
|
+
const [lowerBinArrayPubKey] = deriveBinArray(
|
|
8191
|
+
lbPair,
|
|
8192
|
+
lowerBinArrayIndex,
|
|
8193
|
+
program.programId
|
|
8194
|
+
);
|
|
8195
|
+
const [upperBinArrayPubKey] = deriveBinArray(
|
|
8196
|
+
lbPair,
|
|
8197
|
+
upperBinArrayIndex,
|
|
8198
|
+
program.programId
|
|
8199
|
+
);
|
|
8200
|
+
const lowerBinArray = positionBinArraysMap.get(
|
|
8201
|
+
lowerBinArrayPubKey.toBase58()
|
|
8202
|
+
);
|
|
8203
|
+
const upperBinArray = positionBinArraysMap.get(
|
|
8204
|
+
upperBinArrayPubKey.toBase58()
|
|
8205
|
+
);
|
|
8206
|
+
const lbPairAcc = lbPairArraysMap.get(lbPair.toBase58());
|
|
8207
|
+
const { mintXDecimal, mintYDecimal } = lbPairMintMap.get(
|
|
8208
|
+
lbPair.toBase58()
|
|
8209
|
+
);
|
|
8210
|
+
const reserveXBalance = lbPairReserveMap.get(lbPair.toBase58())?.reserveX ?? BigInt(0);
|
|
8211
|
+
const reserveYBalance = lbPairReserveMap.get(lbPair.toBase58())?.reserveY ?? BigInt(0);
|
|
8212
|
+
const tokenX = {
|
|
8213
|
+
publicKey: lbPairAcc.tokenXMint,
|
|
8214
|
+
reserve: lbPairAcc.reserveX,
|
|
8215
|
+
amount: reserveXBalance,
|
|
8216
|
+
decimal: mintXDecimal
|
|
8217
|
+
};
|
|
8218
|
+
const tokenY = {
|
|
8219
|
+
publicKey: lbPairAcc.tokenYMint,
|
|
8220
|
+
reserve: lbPairAcc.reserveY,
|
|
8221
|
+
amount: reserveYBalance,
|
|
8222
|
+
decimal: mintYDecimal
|
|
8223
|
+
};
|
|
8224
|
+
const positionData = await DLMM.processPosition(
|
|
8225
|
+
program,
|
|
8226
|
+
0 /* V1 */,
|
|
8227
|
+
lbPairAcc,
|
|
8228
|
+
onChainTimestamp,
|
|
8229
|
+
account,
|
|
8230
|
+
mintXDecimal,
|
|
8231
|
+
mintYDecimal,
|
|
8232
|
+
lowerBinArray,
|
|
8233
|
+
upperBinArray,
|
|
8234
|
+
PublicKey6.default
|
|
8235
|
+
);
|
|
8236
|
+
if (positionData) {
|
|
8237
|
+
positionsMap.set(lbPair.toBase58(), {
|
|
8238
|
+
publicKey: lbPair,
|
|
8239
|
+
lbPair: lbPairAcc,
|
|
8240
|
+
tokenX,
|
|
8241
|
+
tokenY,
|
|
8242
|
+
lbPairPositionsData: [
|
|
8243
|
+
...positionsMap.get(lbPair.toBase58())?.lbPairPositionsData ?? [],
|
|
8244
|
+
{
|
|
8245
|
+
publicKey: positionPubKey,
|
|
8246
|
+
positionData,
|
|
8247
|
+
version: 0 /* V1 */
|
|
8248
|
+
}
|
|
8249
|
+
]
|
|
8250
|
+
});
|
|
8251
|
+
}
|
|
8252
|
+
}
|
|
8085
8253
|
for (let position of positionsV2) {
|
|
8086
8254
|
const { account, publicKey: positionPubKey } = position;
|
|
8087
8255
|
const { upperBinId, lowerBinId, lbPair, feeOwner } = account;
|
|
@@ -8153,6 +8321,56 @@ var DLMM = class {
|
|
|
8153
8321
|
}
|
|
8154
8322
|
return positionsMap;
|
|
8155
8323
|
}
|
|
8324
|
+
static async migratePosition(connection, positions, newPositions, walletPubkey, opt) {
|
|
8325
|
+
const cluster = opt?.cluster || "mainnet-beta";
|
|
8326
|
+
const provider = new AnchorProvider2(
|
|
8327
|
+
connection,
|
|
8328
|
+
{},
|
|
8329
|
+
AnchorProvider2.defaultOptions()
|
|
8330
|
+
);
|
|
8331
|
+
const program = new Program2(
|
|
8332
|
+
IDL,
|
|
8333
|
+
opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
|
|
8334
|
+
provider
|
|
8335
|
+
);
|
|
8336
|
+
const positionsState = await program.account.position.fetchMultiple(
|
|
8337
|
+
positions
|
|
8338
|
+
);
|
|
8339
|
+
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash("confirmed");
|
|
8340
|
+
return Promise.all(
|
|
8341
|
+
positionsState.map(async ({ lbPair, lowerBinId }, idx) => {
|
|
8342
|
+
const position = positions[idx];
|
|
8343
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
8344
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
|
|
8345
|
+
const [lowerBinArrayPubKey] = deriveBinArray(
|
|
8346
|
+
lbPair,
|
|
8347
|
+
lowerBinArrayIndex,
|
|
8348
|
+
program.programId
|
|
8349
|
+
);
|
|
8350
|
+
const [upperBinArrayPubKey] = deriveBinArray(
|
|
8351
|
+
lbPair,
|
|
8352
|
+
upperBinArrayIndex,
|
|
8353
|
+
program.programId
|
|
8354
|
+
);
|
|
8355
|
+
const migrateTx = await program.methods.migratePosition().accounts({
|
|
8356
|
+
binArrayLower: lowerBinArrayPubKey,
|
|
8357
|
+
binArrayUpper: upperBinArrayPubKey,
|
|
8358
|
+
lbPair,
|
|
8359
|
+
owner: walletPubkey,
|
|
8360
|
+
positionV1: position,
|
|
8361
|
+
positionV2: newPositions[idx],
|
|
8362
|
+
program: program.programId,
|
|
8363
|
+
rentReceiver: walletPubkey,
|
|
8364
|
+
systemProgram: SystemProgram2.programId
|
|
8365
|
+
}).transaction();
|
|
8366
|
+
return new Transaction({
|
|
8367
|
+
blockhash,
|
|
8368
|
+
lastValidBlockHeight,
|
|
8369
|
+
feePayer: walletPubkey
|
|
8370
|
+
}).add(migrateTx);
|
|
8371
|
+
})
|
|
8372
|
+
);
|
|
8373
|
+
}
|
|
8156
8374
|
static getPricePerLamport(tokenXDecimal, tokenYDecimal, price) {
|
|
8157
8375
|
return new Decimal4(price).mul(new Decimal4(10 ** (tokenYDecimal - tokenXDecimal))).toString();
|
|
8158
8376
|
}
|
|
@@ -8644,6 +8862,20 @@ var DLMM = class {
|
|
|
8644
8862
|
async getPositionsByUserAndLbPair(userPubKey) {
|
|
8645
8863
|
const promiseResults = await Promise.all([
|
|
8646
8864
|
this.getActiveBin(),
|
|
8865
|
+
userPubKey && this.program.account.position.all([
|
|
8866
|
+
{
|
|
8867
|
+
memcmp: {
|
|
8868
|
+
bytes: bs58.encode(userPubKey.toBuffer()),
|
|
8869
|
+
offset: 8 + 32
|
|
8870
|
+
}
|
|
8871
|
+
},
|
|
8872
|
+
{
|
|
8873
|
+
memcmp: {
|
|
8874
|
+
bytes: bs58.encode(this.pubkey.toBuffer()),
|
|
8875
|
+
offset: 8
|
|
8876
|
+
}
|
|
8877
|
+
}
|
|
8878
|
+
]),
|
|
8647
8879
|
userPubKey && this.program.account.positionV2.all([
|
|
8648
8880
|
{
|
|
8649
8881
|
memcmp: {
|
|
@@ -8659,7 +8891,7 @@ var DLMM = class {
|
|
|
8659
8891
|
}
|
|
8660
8892
|
])
|
|
8661
8893
|
]);
|
|
8662
|
-
const [activeBin, positionsV2] = promiseResults;
|
|
8894
|
+
const [activeBin, positions, positionsV2] = promiseResults;
|
|
8663
8895
|
if (!activeBin) {
|
|
8664
8896
|
throw new Error("Error fetching active bin");
|
|
8665
8897
|
}
|
|
@@ -8669,9 +8901,29 @@ var DLMM = class {
|
|
|
8669
8901
|
userPositions: []
|
|
8670
8902
|
};
|
|
8671
8903
|
}
|
|
8672
|
-
if (!positionsV2) {
|
|
8904
|
+
if (!positions || !positionsV2) {
|
|
8673
8905
|
throw new Error("Error fetching positions");
|
|
8674
8906
|
}
|
|
8907
|
+
const binArrayPubkeySet = /* @__PURE__ */ new Set();
|
|
8908
|
+
positions.forEach(({ account: { upperBinId, lowerBinId } }) => {
|
|
8909
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
8910
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
|
|
8911
|
+
const [lowerBinArrayPubKey] = deriveBinArray(
|
|
8912
|
+
this.pubkey,
|
|
8913
|
+
lowerBinArrayIndex,
|
|
8914
|
+
this.program.programId
|
|
8915
|
+
);
|
|
8916
|
+
const [upperBinArrayPubKey] = deriveBinArray(
|
|
8917
|
+
this.pubkey,
|
|
8918
|
+
upperBinArrayIndex,
|
|
8919
|
+
this.program.programId
|
|
8920
|
+
);
|
|
8921
|
+
binArrayPubkeySet.add(lowerBinArrayPubKey.toBase58());
|
|
8922
|
+
binArrayPubkeySet.add(upperBinArrayPubKey.toBase58());
|
|
8923
|
+
});
|
|
8924
|
+
const binArrayPubkeyArray = Array.from(binArrayPubkeySet).map(
|
|
8925
|
+
(pubkey) => new PublicKey6(pubkey)
|
|
8926
|
+
);
|
|
8675
8927
|
const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
|
|
8676
8928
|
positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
|
|
8677
8929
|
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
@@ -8697,13 +8949,28 @@ var DLMM = class {
|
|
|
8697
8949
|
[
|
|
8698
8950
|
this.pubkey,
|
|
8699
8951
|
SYSVAR_CLOCK_PUBKEY,
|
|
8952
|
+
...binArrayPubkeyArray,
|
|
8700
8953
|
...binArrayPubkeyArrayV2
|
|
8701
8954
|
]
|
|
8702
8955
|
);
|
|
8703
8956
|
const [lbPairAccInfo, clockAccInfo, ...binArraysAccInfo] = lbPairAndBinArrays;
|
|
8957
|
+
const positionBinArraysMap = /* @__PURE__ */ new Map();
|
|
8958
|
+
for (let i = 0; i < binArrayPubkeyArray.length; i++) {
|
|
8959
|
+
const binArrayPubkey = binArrayPubkeyArray[i];
|
|
8960
|
+
const binArrayAccBuffer = binArraysAccInfo[i];
|
|
8961
|
+
if (!binArrayAccBuffer)
|
|
8962
|
+
throw new Error(
|
|
8963
|
+
`Bin Array account ${binArrayPubkey.toBase58()} not found`
|
|
8964
|
+
);
|
|
8965
|
+
const binArrayAccInfo = this.program.coder.accounts.decode(
|
|
8966
|
+
"binArray",
|
|
8967
|
+
binArrayAccBuffer.data
|
|
8968
|
+
);
|
|
8969
|
+
positionBinArraysMap.set(binArrayPubkey.toBase58(), binArrayAccInfo);
|
|
8970
|
+
}
|
|
8704
8971
|
const positionBinArraysMapV2 = /* @__PURE__ */ new Map();
|
|
8705
|
-
for (let i =
|
|
8706
|
-
const binArrayPubkey = binArrayPubkeyArrayV2[i];
|
|
8972
|
+
for (let i = binArrayPubkeyArray.length; i < binArraysAccInfo.length; i++) {
|
|
8973
|
+
const binArrayPubkey = binArrayPubkeyArrayV2[i - binArrayPubkeyArray.length];
|
|
8707
8974
|
const binArrayAccBufferV2 = binArraysAccInfo[i];
|
|
8708
8975
|
if (!binArrayAccBufferV2)
|
|
8709
8976
|
throw new Error(
|
|
@@ -8720,6 +8987,45 @@ var DLMM = class {
|
|
|
8720
8987
|
const onChainTimestamp = new BN9(
|
|
8721
8988
|
clockAccInfo.data.readBigInt64LE(32).toString()
|
|
8722
8989
|
).toNumber();
|
|
8990
|
+
const userPositions = await Promise.all(
|
|
8991
|
+
positions.map(async ({ publicKey, account }) => {
|
|
8992
|
+
const { lowerBinId, upperBinId } = account;
|
|
8993
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
8994
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
|
|
8995
|
+
const [lowerBinArrayPubKey] = deriveBinArray(
|
|
8996
|
+
this.pubkey,
|
|
8997
|
+
lowerBinArrayIndex,
|
|
8998
|
+
this.program.programId
|
|
8999
|
+
);
|
|
9000
|
+
const [upperBinArrayPubKey] = deriveBinArray(
|
|
9001
|
+
this.pubkey,
|
|
9002
|
+
upperBinArrayIndex,
|
|
9003
|
+
this.program.programId
|
|
9004
|
+
);
|
|
9005
|
+
const lowerBinArray = positionBinArraysMap.get(
|
|
9006
|
+
lowerBinArrayPubKey.toBase58()
|
|
9007
|
+
);
|
|
9008
|
+
const upperBinArray = positionBinArraysMap.get(
|
|
9009
|
+
upperBinArrayPubKey.toBase58()
|
|
9010
|
+
);
|
|
9011
|
+
return {
|
|
9012
|
+
publicKey,
|
|
9013
|
+
positionData: await DLMM.processPosition(
|
|
9014
|
+
this.program,
|
|
9015
|
+
0 /* V1 */,
|
|
9016
|
+
this.lbPair,
|
|
9017
|
+
onChainTimestamp,
|
|
9018
|
+
account,
|
|
9019
|
+
this.tokenX.decimal,
|
|
9020
|
+
this.tokenY.decimal,
|
|
9021
|
+
lowerBinArray,
|
|
9022
|
+
upperBinArray,
|
|
9023
|
+
PublicKey6.default
|
|
9024
|
+
),
|
|
9025
|
+
version: 0 /* V1 */
|
|
9026
|
+
};
|
|
9027
|
+
})
|
|
9028
|
+
);
|
|
8723
9029
|
const userPositionsV2 = await Promise.all(
|
|
8724
9030
|
positionsV2.map(async ({ publicKey, account }) => {
|
|
8725
9031
|
const { lowerBinId, upperBinId, feeOwner } = account;
|
|
@@ -8761,7 +9067,7 @@ var DLMM = class {
|
|
|
8761
9067
|
);
|
|
8762
9068
|
return {
|
|
8763
9069
|
activeBin,
|
|
8764
|
-
userPositions: userPositionsV2
|
|
9070
|
+
userPositions: [...userPositions, ...userPositionsV2]
|
|
8765
9071
|
};
|
|
8766
9072
|
}
|
|
8767
9073
|
async quoteCreatePosition({ strategy }) {
|
|
@@ -8817,72 +9123,6 @@ var DLMM = class {
|
|
|
8817
9123
|
feePayer: user
|
|
8818
9124
|
}).add(setComputeUnitLimitIx, createPositionIx, ...createBinArrayIxs);
|
|
8819
9125
|
}
|
|
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
|
-
}
|
|
8886
9126
|
/**
|
|
8887
9127
|
* The function `initializePositionAndAddLiquidityByStrategy` function is used to initializes a position and adds liquidity
|
|
8888
9128
|
* @param {TInitializePositionAndAddLiquidityParamsByStrategy}
|
|
@@ -9561,40 +9801,9 @@ var DLMM = class {
|
|
|
9561
9801
|
bps,
|
|
9562
9802
|
shouldClaimAndClose = false
|
|
9563
9803
|
}) {
|
|
9564
|
-
const
|
|
9565
|
-
const
|
|
9566
|
-
const
|
|
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));
|
|
9804
|
+
const { lbPair, lowerBinId, owner, feeOwner } = await this.program.account.positionV2.fetch(position);
|
|
9805
|
+
const { reserveX, reserveY, tokenXMint, tokenYMint } = await this.program.account.lbPair.fetch(lbPair);
|
|
9806
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
9598
9807
|
const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
|
|
9599
9808
|
const [binArrayLower] = deriveBinArray(
|
|
9600
9809
|
lbPair,
|
|
@@ -9710,11 +9919,13 @@ var DLMM = class {
|
|
|
9710
9919
|
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
9711
9920
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
9712
9921
|
}
|
|
9713
|
-
const
|
|
9714
|
-
const
|
|
9922
|
+
const minBinId = Math.min(...binIds);
|
|
9923
|
+
const maxBinId = Math.max(...binIds);
|
|
9924
|
+
const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
|
|
9925
|
+
const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
|
|
9715
9926
|
const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
|
|
9716
9927
|
const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
|
|
9717
|
-
const removeLiquidityTx = await this.program.methods.removeLiquidityByRange(
|
|
9928
|
+
const removeLiquidityTx = await this.program.methods.removeLiquidityByRange(minBinId, maxBinId, bps.toNumber()).accounts({
|
|
9718
9929
|
position,
|
|
9719
9930
|
lbPair,
|
|
9720
9931
|
userTokenX,
|
|
@@ -10027,6 +10238,7 @@ var DLMM = class {
|
|
|
10027
10238
|
}) {
|
|
10028
10239
|
const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = await this.program.account.lbPair.fetch(lbPair);
|
|
10029
10240
|
const preInstructions = [computeBudgetIx()];
|
|
10241
|
+
const postInstructions = [];
|
|
10030
10242
|
const [
|
|
10031
10243
|
{ ataPubKey: userTokenIn, ix: createInTokenAccountIx },
|
|
10032
10244
|
{ ataPubKey: userTokenOut, ix: createOutTokenAccountIx }
|
|
@@ -10051,8 +10263,9 @@ var DLMM = class {
|
|
|
10051
10263
|
BigInt(maxInAmount.toString())
|
|
10052
10264
|
);
|
|
10053
10265
|
preInstructions.push(...wrapSOLIx);
|
|
10266
|
+
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
10267
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
10054
10268
|
}
|
|
10055
|
-
const postInstructions = [];
|
|
10056
10269
|
if (outToken.equals(NATIVE_MINT2)) {
|
|
10057
10270
|
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
10058
10271
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
@@ -10112,6 +10325,7 @@ var DLMM = class {
|
|
|
10112
10325
|
}) {
|
|
10113
10326
|
const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = await this.program.account.lbPair.fetch(lbPair);
|
|
10114
10327
|
const preInstructions = [computeBudgetIx()];
|
|
10328
|
+
const postInstructions = [];
|
|
10115
10329
|
const [
|
|
10116
10330
|
{ ataPubKey: userTokenIn, ix: createInTokenAccountIx },
|
|
10117
10331
|
{ ataPubKey: userTokenOut, ix: createOutTokenAccountIx }
|
|
@@ -10136,8 +10350,9 @@ var DLMM = class {
|
|
|
10136
10350
|
BigInt(inAmount.toString())
|
|
10137
10351
|
);
|
|
10138
10352
|
preInstructions.push(...wrapSOLIx);
|
|
10353
|
+
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
10354
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
10139
10355
|
}
|
|
10140
|
-
const postInstructions = [];
|
|
10141
10356
|
if (outToken.equals(NATIVE_MINT2)) {
|
|
10142
10357
|
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
10143
10358
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
@@ -10201,6 +10416,7 @@ var DLMM = class {
|
|
|
10201
10416
|
}) {
|
|
10202
10417
|
const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = await this.program.account.lbPair.fetch(lbPair);
|
|
10203
10418
|
const preInstructions = [computeBudgetIx()];
|
|
10419
|
+
const postInstructions = [];
|
|
10204
10420
|
const [
|
|
10205
10421
|
{ ataPubKey: userTokenIn, ix: createInTokenAccountIx },
|
|
10206
10422
|
{ ataPubKey: userTokenOut, ix: createOutTokenAccountIx }
|
|
@@ -10225,8 +10441,9 @@ var DLMM = class {
|
|
|
10225
10441
|
BigInt(inAmount.toString())
|
|
10226
10442
|
);
|
|
10227
10443
|
preInstructions.push(...wrapSOLIx);
|
|
10444
|
+
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
10445
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
10228
10446
|
}
|
|
10229
|
-
const postInstructions = [];
|
|
10230
10447
|
if (outToken.equals(NATIVE_MINT2)) {
|
|
10231
10448
|
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
10232
10449
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
@@ -10660,6 +10877,175 @@ var DLMM = class {
|
|
|
10660
10877
|
addLiquidityIxs
|
|
10661
10878
|
};
|
|
10662
10879
|
}
|
|
10880
|
+
/**
|
|
10881
|
+
* The `seedLiquidity` function create multiple grouped instructions. The grouped instructions will be either [initialize bin array + initialize position instructions] or [deposit instruction] combination.
|
|
10882
|
+
* @param
|
|
10883
|
+
* - `payer`: The public key of the tx payer.
|
|
10884
|
+
* - `base`: Base key
|
|
10885
|
+
* - `seedAmount`: Token X lamport amount to be seeded to the pool.
|
|
10886
|
+
* - `price`: TokenX/TokenY Price in UI format
|
|
10887
|
+
* - `roundingUp`: Whether to round up the price
|
|
10888
|
+
* - `positionOwner`: The owner of the position
|
|
10889
|
+
* - `feeOwner`: Position fee owner
|
|
10890
|
+
* - `operator`: Operator of the position. Operator able to manage the position on behalf of the position owner. However, liquidity withdrawal issue by the operator can only send to the position owner.
|
|
10891
|
+
* - `lockReleasePoint`: The lock release point of the position.
|
|
10892
|
+
* - `shouldSeedPositionOwner` (optional): Whether to send 1 lamport amount of token X to the position owner to prove ownership.
|
|
10893
|
+
*
|
|
10894
|
+
* The returned instructions need to be executed sequentially if it was separated into multiple transactions.
|
|
10895
|
+
* @returns {Promise<TransactionInstruction[]>}
|
|
10896
|
+
*/
|
|
10897
|
+
async seedLiquiditySingleBin(payer, base, seedAmount, price, roundingUp, positionOwner, feeOwner, operator, lockReleasePoint, shouldSeedPositionOwner = false) {
|
|
10898
|
+
const pricePerLamport = DLMM.getPricePerLamport(
|
|
10899
|
+
this.tokenX.decimal,
|
|
10900
|
+
this.tokenY.decimal,
|
|
10901
|
+
price
|
|
10902
|
+
);
|
|
10903
|
+
const binIdNumber = DLMM.getBinIdFromPrice(
|
|
10904
|
+
pricePerLamport,
|
|
10905
|
+
this.lbPair.binStep,
|
|
10906
|
+
!roundingUp
|
|
10907
|
+
);
|
|
10908
|
+
const binId = new BN9(binIdNumber);
|
|
10909
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(binId);
|
|
10910
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
|
|
10911
|
+
const [lowerBinArray] = deriveBinArray(this.pubkey, lowerBinArrayIndex, this.program.programId);
|
|
10912
|
+
const [upperBinArray] = deriveBinArray(this.pubkey, upperBinArrayIndex, this.program.programId);
|
|
10913
|
+
const [positionPda] = derivePosition(this.pubkey, base, binId, new BN9(1), this.program.programId);
|
|
10914
|
+
const preInstructions = [];
|
|
10915
|
+
const [
|
|
10916
|
+
{ ataPubKey: userTokenX, ix: createPayerTokenXIx },
|
|
10917
|
+
{ ataPubKey: userTokenY, ix: createPayerTokenYIx }
|
|
10918
|
+
] = await Promise.all([
|
|
10919
|
+
getOrCreateATAInstruction(
|
|
10920
|
+
this.program.provider.connection,
|
|
10921
|
+
this.tokenX.publicKey,
|
|
10922
|
+
operator,
|
|
10923
|
+
payer
|
|
10924
|
+
),
|
|
10925
|
+
getOrCreateATAInstruction(
|
|
10926
|
+
this.program.provider.connection,
|
|
10927
|
+
this.tokenY.publicKey,
|
|
10928
|
+
operator,
|
|
10929
|
+
payer
|
|
10930
|
+
)
|
|
10931
|
+
]);
|
|
10932
|
+
createPayerTokenXIx && preInstructions.push(createPayerTokenXIx);
|
|
10933
|
+
createPayerTokenYIx && preInstructions.push(createPayerTokenYIx);
|
|
10934
|
+
let [binArrayBitmapExtension] = deriveBinArrayBitmapExtension(
|
|
10935
|
+
this.pubkey,
|
|
10936
|
+
this.program.programId
|
|
10937
|
+
);
|
|
10938
|
+
const accounts = await this.program.provider.connection.getMultipleAccountsInfo([
|
|
10939
|
+
lowerBinArray,
|
|
10940
|
+
upperBinArray,
|
|
10941
|
+
positionPda,
|
|
10942
|
+
binArrayBitmapExtension
|
|
10943
|
+
]);
|
|
10944
|
+
if (isOverflowDefaultBinArrayBitmap(lowerBinArrayIndex)) {
|
|
10945
|
+
const bitmapExtensionAccount = accounts[3];
|
|
10946
|
+
if (!bitmapExtensionAccount) {
|
|
10947
|
+
preInstructions.push(await this.program.methods.initializeBinArrayBitmapExtension().accounts({
|
|
10948
|
+
binArrayBitmapExtension,
|
|
10949
|
+
funder: payer,
|
|
10950
|
+
lbPair: this.pubkey
|
|
10951
|
+
}).instruction());
|
|
10952
|
+
}
|
|
10953
|
+
} else {
|
|
10954
|
+
binArrayBitmapExtension = this.program.programId;
|
|
10955
|
+
}
|
|
10956
|
+
const operatorTokenX = getAssociatedTokenAddressSync2(
|
|
10957
|
+
this.lbPair.tokenXMint,
|
|
10958
|
+
operator,
|
|
10959
|
+
true
|
|
10960
|
+
);
|
|
10961
|
+
const positionOwnerTokenX = getAssociatedTokenAddressSync2(
|
|
10962
|
+
this.lbPair.tokenXMint,
|
|
10963
|
+
positionOwner,
|
|
10964
|
+
true
|
|
10965
|
+
);
|
|
10966
|
+
if (shouldSeedPositionOwner) {
|
|
10967
|
+
const positionOwnerTokenXAccount = await this.program.provider.connection.getAccountInfo(positionOwnerTokenX);
|
|
10968
|
+
if (positionOwnerTokenXAccount) {
|
|
10969
|
+
const account = AccountLayout.decode(positionOwnerTokenXAccount.data);
|
|
10970
|
+
if (account.amount == BigInt(0)) {
|
|
10971
|
+
const transferIx = createTransferInstruction(operatorTokenX, positionOwnerTokenX, payer, 1);
|
|
10972
|
+
preInstructions.push(transferIx);
|
|
10973
|
+
}
|
|
10974
|
+
} else {
|
|
10975
|
+
const createPositionOwnerTokenXIx = createAssociatedTokenAccountInstruction2(payer, positionOwnerTokenX, positionOwner, this.lbPair.tokenXMint);
|
|
10976
|
+
preInstructions.push(createPositionOwnerTokenXIx);
|
|
10977
|
+
const transferIx = createTransferInstruction(operatorTokenX, positionOwnerTokenX, payer, 1);
|
|
10978
|
+
preInstructions.push(transferIx);
|
|
10979
|
+
}
|
|
10980
|
+
}
|
|
10981
|
+
const lowerBinArrayAccount = accounts[0];
|
|
10982
|
+
const upperBinArrayAccount = accounts[1];
|
|
10983
|
+
const positionAccount = accounts[2];
|
|
10984
|
+
if (!lowerBinArrayAccount) {
|
|
10985
|
+
preInstructions.push(
|
|
10986
|
+
await this.program.methods.initializeBinArray(lowerBinArrayIndex).accounts({
|
|
10987
|
+
binArray: lowerBinArray,
|
|
10988
|
+
funder: payer,
|
|
10989
|
+
lbPair: this.pubkey
|
|
10990
|
+
}).instruction()
|
|
10991
|
+
);
|
|
10992
|
+
}
|
|
10993
|
+
if (!upperBinArrayAccount) {
|
|
10994
|
+
preInstructions.push(
|
|
10995
|
+
await this.program.methods.initializeBinArray(upperBinArrayIndex).accounts({
|
|
10996
|
+
binArray: upperBinArray,
|
|
10997
|
+
funder: payer,
|
|
10998
|
+
lbPair: this.pubkey
|
|
10999
|
+
}).instruction()
|
|
11000
|
+
);
|
|
11001
|
+
}
|
|
11002
|
+
if (!positionAccount) {
|
|
11003
|
+
preInstructions.push(
|
|
11004
|
+
await this.program.methods.initializePositionByOperator(
|
|
11005
|
+
binId.toNumber(),
|
|
11006
|
+
1,
|
|
11007
|
+
feeOwner,
|
|
11008
|
+
lockReleasePoint
|
|
11009
|
+
).accounts({
|
|
11010
|
+
payer,
|
|
11011
|
+
base,
|
|
11012
|
+
position: positionPda,
|
|
11013
|
+
lbPair: this.pubkey,
|
|
11014
|
+
owner: positionOwner,
|
|
11015
|
+
operator,
|
|
11016
|
+
operatorTokenX,
|
|
11017
|
+
ownerTokenX: positionOwnerTokenX
|
|
11018
|
+
}).instruction()
|
|
11019
|
+
);
|
|
11020
|
+
}
|
|
11021
|
+
const binLiquidityDist = {
|
|
11022
|
+
binId: binIdNumber,
|
|
11023
|
+
distributionX: BASIS_POINT_MAX,
|
|
11024
|
+
distributionY: 0
|
|
11025
|
+
};
|
|
11026
|
+
const addLiquidityParams = {
|
|
11027
|
+
amountX: seedAmount,
|
|
11028
|
+
amountY: new BN9(0),
|
|
11029
|
+
binLiquidityDist: [binLiquidityDist]
|
|
11030
|
+
};
|
|
11031
|
+
const depositLiquidityIx = await this.program.methods.addLiquidity(addLiquidityParams).accounts({
|
|
11032
|
+
position: positionPda,
|
|
11033
|
+
lbPair: this.pubkey,
|
|
11034
|
+
binArrayBitmapExtension,
|
|
11035
|
+
userTokenX,
|
|
11036
|
+
userTokenY,
|
|
11037
|
+
reserveX: this.lbPair.reserveX,
|
|
11038
|
+
reserveY: this.lbPair.reserveY,
|
|
11039
|
+
tokenXMint: this.lbPair.tokenXMint,
|
|
11040
|
+
tokenYMint: this.lbPair.tokenYMint,
|
|
11041
|
+
binArrayLower: lowerBinArray,
|
|
11042
|
+
binArrayUpper: upperBinArray,
|
|
11043
|
+
sender: operator,
|
|
11044
|
+
tokenXProgram: TOKEN_PROGRAM_ID2,
|
|
11045
|
+
tokenYProgram: TOKEN_PROGRAM_ID2
|
|
11046
|
+
}).instruction();
|
|
11047
|
+
return [...preInstructions, depositLiquidityIx];
|
|
11048
|
+
}
|
|
10663
11049
|
/**
|
|
10664
11050
|
* Initializes bin arrays for the given bin array indexes if it wasn't initialized.
|
|
10665
11051
|
*
|
|
@@ -11268,7 +11654,12 @@ var DLMM = class {
|
|
|
11268
11654
|
let totalYAmount = new Decimal4(0);
|
|
11269
11655
|
bins.forEach((bin, idx) => {
|
|
11270
11656
|
const binSupply = new Decimal4(bin.supply.toString());
|
|
11271
|
-
|
|
11657
|
+
let posShare;
|
|
11658
|
+
if (bin.version === 1 && version === 0 /* V1 */) {
|
|
11659
|
+
posShare = new Decimal4(posShares[idx].shln(64).toString());
|
|
11660
|
+
} else {
|
|
11661
|
+
posShare = new Decimal4(posShares[idx].toString());
|
|
11662
|
+
}
|
|
11272
11663
|
const positionXAmount = binSupply.eq(new Decimal4("0")) ? new Decimal4("0") : posShare.mul(bin.xAmount.toString()).div(binSupply);
|
|
11273
11664
|
const positionYAmount = binSupply.eq(new Decimal4("0")) ? new Decimal4("0") : posShare.mul(bin.yAmount.toString()).div(binSupply);
|
|
11274
11665
|
totalXAmount = totalXAmount.add(positionXAmount);
|