@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.js
CHANGED
|
@@ -7591,6 +7591,8 @@ var _bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
|
|
|
7591
7591
|
|
|
7592
7592
|
|
|
7593
7593
|
|
|
7594
|
+
|
|
7595
|
+
|
|
7594
7596
|
// src/dlmm/error.ts
|
|
7595
7597
|
|
|
7596
7598
|
var DLMMError = class extends Error {
|
|
@@ -7975,14 +7977,49 @@ var DLMM = class {
|
|
|
7975
7977
|
_nullishCoalesce(_optionalChain([opt, 'optionalAccess', _46 => _46.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: _bytes.bs58.encode(userPubKey.toBuffer()),
|
|
7985
|
+
offset: 8 + 32
|
|
7986
|
+
}
|
|
7983
7987
|
}
|
|
7984
|
-
|
|
7988
|
+
]),
|
|
7989
|
+
program.account.positionV2.all([
|
|
7990
|
+
{
|
|
7991
|
+
memcmp: {
|
|
7992
|
+
bytes: _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 (0, _anchor.BN)(lowerBinId));
|
|
8002
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(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 (0, _web3js.PublicKey)(pubkey)
|
|
8019
|
+
);
|
|
8020
|
+
const lbPairArray = Array.from(lbPairSet).map(
|
|
8021
|
+
(pubkey) => new (0, _web3js.PublicKey)(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
|
_web3js.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 = _spltoken.AccountLayout.decode(reserveAccBufferX.data);
|
|
8134
|
+
const reserveAccY = _spltoken.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 = _spltoken.MintLayout.decode(mintXBuffer.data);
|
|
8146
|
+
const mintY = _spltoken.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 (0, _anchor.BN)(lowerBinId));
|
|
8189
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(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 = _nullishCoalesce(_optionalChain([lbPairReserveMap, 'access', _47 => _47.get, 'call', _48 => _48(lbPair.toBase58()), 'optionalAccess', _49 => _49.reserveX]), () => ( BigInt(0)));
|
|
8211
|
+
const reserveYBalance = _nullishCoalesce(_optionalChain([lbPairReserveMap, 'access', _50 => _50.get, 'call', _51 => _51(lbPair.toBase58()), 'optionalAccess', _52 => _52.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
|
+
_web3js.PublicKey.default
|
|
8235
|
+
);
|
|
8236
|
+
if (positionData) {
|
|
8237
|
+
positionsMap.set(lbPair.toBase58(), {
|
|
8238
|
+
publicKey: lbPair,
|
|
8239
|
+
lbPair: lbPairAcc,
|
|
8240
|
+
tokenX,
|
|
8241
|
+
tokenY,
|
|
8242
|
+
lbPairPositionsData: [
|
|
8243
|
+
..._nullishCoalesce(_optionalChain([positionsMap, 'access', _53 => _53.get, 'call', _54 => _54(lbPair.toBase58()), 'optionalAccess', _55 => _55.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;
|
|
@@ -8108,8 +8276,8 @@ var DLMM = class {
|
|
|
8108
8276
|
getTokenDecimals(program.provider.connection, lbPairAcc.tokenXMint),
|
|
8109
8277
|
getTokenDecimals(program.provider.connection, lbPairAcc.tokenYMint)
|
|
8110
8278
|
]);
|
|
8111
|
-
const reserveXBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access',
|
|
8112
|
-
const reserveYBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access',
|
|
8279
|
+
const reserveXBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access', _56 => _56.get, 'call', _57 => _57(lbPair.toBase58()), 'optionalAccess', _58 => _58.reserveX]), () => ( BigInt(0)));
|
|
8280
|
+
const reserveYBalance = _nullishCoalesce(_optionalChain([lbPairReserveMapV2, 'access', _59 => _59.get, 'call', _60 => _60(lbPair.toBase58()), 'optionalAccess', _61 => _61.reserveY]), () => ( BigInt(0)));
|
|
8113
8281
|
const tokenX = {
|
|
8114
8282
|
publicKey: lbPairAcc.tokenXMint,
|
|
8115
8283
|
reserve: lbPairAcc.reserveX,
|
|
@@ -8141,7 +8309,7 @@ var DLMM = class {
|
|
|
8141
8309
|
tokenX,
|
|
8142
8310
|
tokenY,
|
|
8143
8311
|
lbPairPositionsData: [
|
|
8144
|
-
..._nullishCoalesce(_optionalChain([positionsMap, 'access',
|
|
8312
|
+
..._nullishCoalesce(_optionalChain([positionsMap, 'access', _62 => _62.get, 'call', _63 => _63(lbPair.toBase58()), 'optionalAccess', _64 => _64.lbPairPositionsData]), () => ( [])),
|
|
8145
8313
|
{
|
|
8146
8314
|
publicKey: positionPubKey,
|
|
8147
8315
|
positionData,
|
|
@@ -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 = _optionalChain([opt, 'optionalAccess', _65 => _65.cluster]) || "mainnet-beta";
|
|
8326
|
+
const provider = new (0, _anchor.AnchorProvider)(
|
|
8327
|
+
connection,
|
|
8328
|
+
{},
|
|
8329
|
+
_anchor.AnchorProvider.defaultOptions()
|
|
8330
|
+
);
|
|
8331
|
+
const program = new (0, _anchor.Program)(
|
|
8332
|
+
IDL,
|
|
8333
|
+
_nullishCoalesce(_optionalChain([opt, 'optionalAccess', _66 => _66.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 (0, _anchor.BN)(lowerBinId));
|
|
8344
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _anchor.BN)(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: _web3js.SystemProgram.programId
|
|
8365
|
+
}).transaction();
|
|
8366
|
+
return new (0, _web3js.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 (0, _decimaljs2.default)(price).mul(new (0, _decimaljs2.default)(10 ** (tokenYDecimal - tokenXDecimal))).toString();
|
|
8158
8376
|
}
|
|
@@ -8170,7 +8388,7 @@ var DLMM = class {
|
|
|
8170
8388
|
);
|
|
8171
8389
|
const program = new (0, _anchor.Program)(
|
|
8172
8390
|
IDL,
|
|
8173
|
-
_nullishCoalesce(_optionalChain([opt, 'optionalAccess',
|
|
8391
|
+
_nullishCoalesce(_optionalChain([opt, 'optionalAccess', _67 => _67.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
|
|
8174
8392
|
provider
|
|
8175
8393
|
);
|
|
8176
8394
|
const [lbPair] = derivePermissionLbPair(
|
|
@@ -8219,7 +8437,7 @@ var DLMM = class {
|
|
|
8219
8437
|
);
|
|
8220
8438
|
const program = new (0, _anchor.Program)(
|
|
8221
8439
|
IDL,
|
|
8222
|
-
_nullishCoalesce(_optionalChain([opt, 'optionalAccess',
|
|
8440
|
+
_nullishCoalesce(_optionalChain([opt, 'optionalAccess', _68 => _68.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
|
|
8223
8441
|
provider
|
|
8224
8442
|
);
|
|
8225
8443
|
const [lbPair] = deriveCustomizablePermissionlessLbPair(
|
|
@@ -8267,7 +8485,7 @@ var DLMM = class {
|
|
|
8267
8485
|
);
|
|
8268
8486
|
const program = new (0, _anchor.Program)(
|
|
8269
8487
|
IDL,
|
|
8270
|
-
_nullishCoalesce(_optionalChain([opt, 'optionalAccess',
|
|
8488
|
+
_nullishCoalesce(_optionalChain([opt, 'optionalAccess', _69 => _69.programId]), () => ( LBCLMM_PROGRAM_IDS[opt.cluster])),
|
|
8271
8489
|
provider
|
|
8272
8490
|
);
|
|
8273
8491
|
const existsPool = await this.getPairPubkeyIfExists(
|
|
@@ -8403,7 +8621,7 @@ var DLMM = class {
|
|
|
8403
8621
|
swapForY,
|
|
8404
8622
|
new (0, _anchor.BN)(activeIdToLoop),
|
|
8405
8623
|
this.lbPair,
|
|
8406
|
-
_nullishCoalesce(_optionalChain([this, 'access',
|
|
8624
|
+
_nullishCoalesce(_optionalChain([this, 'access', _70 => _70.binArrayBitmapExtension, 'optionalAccess', _71 => _71.account]), () => ( null))
|
|
8407
8625
|
);
|
|
8408
8626
|
if (binArrayIndex === null)
|
|
8409
8627
|
shouldStop = true;
|
|
@@ -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: _bytes.bs58.encode(userPubKey.toBuffer()),
|
|
8869
|
+
offset: 8 + 32
|
|
8870
|
+
}
|
|
8871
|
+
},
|
|
8872
|
+
{
|
|
8873
|
+
memcmp: {
|
|
8874
|
+
bytes: _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 (0, _anchor.BN)(lowerBinId));
|
|
8910
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(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 (0, _web3js.PublicKey)(pubkey)
|
|
8926
|
+
);
|
|
8675
8927
|
const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
|
|
8676
8928
|
positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
|
|
8677
8929
|
const lowerBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(lowerBinId));
|
|
@@ -8697,13 +8949,28 @@ var DLMM = class {
|
|
|
8697
8949
|
[
|
|
8698
8950
|
this.pubkey,
|
|
8699
8951
|
_web3js.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 (0, _anchor.BN)(
|
|
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 (0, _anchor.BN)(lowerBinId));
|
|
8994
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(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
|
+
_web3js.PublicKey.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 (0, _anchor.BN)(lowerBinId));
|
|
8839
|
-
const upperBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(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
|
-
_web3js.SYSVAR_CLOCK_PUBKEY,
|
|
8854
|
-
lowerBinArrayPubKey,
|
|
8855
|
-
upperBinArrayPubKey
|
|
8856
|
-
]
|
|
8857
|
-
);
|
|
8858
|
-
const onChainTimestamp = new (0, _anchor.BN)(
|
|
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 (0, _anchor.BN)(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 (0, _anchor.BN)(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 (0, _anchor.BN)(lowerBinId));
|
|
9598
9807
|
const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _anchor.BN)(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 (0, _anchor.BN)(minBinId));
|
|
9925
|
+
const maxBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(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,
|
|
@@ -9831,7 +10042,7 @@ var DLMM = class {
|
|
|
9831
10042
|
swapForY,
|
|
9832
10043
|
activeId,
|
|
9833
10044
|
this.lbPair,
|
|
9834
|
-
_nullishCoalesce(_optionalChain([this, 'access',
|
|
10045
|
+
_nullishCoalesce(_optionalChain([this, 'access', _72 => _72.binArrayBitmapExtension, 'optionalAccess', _73 => _73.account]), () => ( null)),
|
|
9835
10046
|
binArrays
|
|
9836
10047
|
);
|
|
9837
10048
|
if (binArrayAccountToSwap == null) {
|
|
@@ -9935,7 +10146,7 @@ var DLMM = class {
|
|
|
9935
10146
|
swapForY,
|
|
9936
10147
|
activeId,
|
|
9937
10148
|
this.lbPair,
|
|
9938
|
-
_nullishCoalesce(_optionalChain([this, 'access',
|
|
10149
|
+
_nullishCoalesce(_optionalChain([this, 'access', _74 => _74.binArrayBitmapExtension, 'optionalAccess', _75 => _75.account]), () => ( null)),
|
|
9939
10150
|
binArrays
|
|
9940
10151
|
);
|
|
9941
10152
|
if (binArrayAccountToSwap == null) {
|
|
@@ -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(_spltoken.NATIVE_MINT)) {
|
|
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(_spltoken.NATIVE_MINT)) {
|
|
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(_spltoken.NATIVE_MINT)) {
|
|
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 (0, _anchor.BN)(binIdNumber);
|
|
10909
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(binId);
|
|
10910
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _anchor.BN)(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 (0, _anchor.BN)(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 = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
10957
|
+
this.lbPair.tokenXMint,
|
|
10958
|
+
operator,
|
|
10959
|
+
true
|
|
10960
|
+
);
|
|
10961
|
+
const positionOwnerTokenX = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
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 = _spltoken.AccountLayout.decode(positionOwnerTokenXAccount.data);
|
|
10970
|
+
if (account.amount == BigInt(0)) {
|
|
10971
|
+
const transferIx = _spltoken.createTransferInstruction.call(void 0, operatorTokenX, positionOwnerTokenX, payer, 1);
|
|
10972
|
+
preInstructions.push(transferIx);
|
|
10973
|
+
}
|
|
10974
|
+
} else {
|
|
10975
|
+
const createPositionOwnerTokenXIx = _spltoken.createAssociatedTokenAccountInstruction.call(void 0, payer, positionOwnerTokenX, positionOwner, this.lbPair.tokenXMint);
|
|
10976
|
+
preInstructions.push(createPositionOwnerTokenXIx);
|
|
10977
|
+
const transferIx = _spltoken.createTransferInstruction.call(void 0, 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 (0, _anchor.BN)(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: _spltoken.TOKEN_PROGRAM_ID,
|
|
11045
|
+
tokenYProgram: _spltoken.TOKEN_PROGRAM_ID
|
|
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
|
*
|
|
@@ -10869,7 +11255,7 @@ var DLMM = class {
|
|
|
10869
11255
|
swapForY,
|
|
10870
11256
|
new (0, _anchor.BN)(activeBinId),
|
|
10871
11257
|
this.lbPair,
|
|
10872
|
-
_nullishCoalesce(_optionalChain([this, 'access',
|
|
11258
|
+
_nullishCoalesce(_optionalChain([this, 'access', _76 => _76.binArrayBitmapExtension, 'optionalAccess', _77 => _77.account]), () => ( null))
|
|
10873
11259
|
);
|
|
10874
11260
|
if (toBinArrayIndex === null)
|
|
10875
11261
|
return true;
|
|
@@ -10906,7 +11292,7 @@ var DLMM = class {
|
|
|
10906
11292
|
swapForY,
|
|
10907
11293
|
new (0, _anchor.BN)(activeBinId),
|
|
10908
11294
|
this.lbPair,
|
|
10909
|
-
_nullishCoalesce(_optionalChain([this, 'access',
|
|
11295
|
+
_nullishCoalesce(_optionalChain([this, 'access', _78 => _78.binArrayBitmapExtension, 'optionalAccess', _79 => _79.account]), () => ( null))
|
|
10910
11296
|
);
|
|
10911
11297
|
const accountsToFetch = [];
|
|
10912
11298
|
const [binArrayBitMapExtensionPubkey] = deriveBinArrayBitmapExtension(
|
|
@@ -10937,13 +11323,13 @@ var DLMM = class {
|
|
|
10937
11323
|
let fromBinArray = null;
|
|
10938
11324
|
let toBinArray = null;
|
|
10939
11325
|
let binArrayBitmapExtension = null;
|
|
10940
|
-
if (!!_optionalChain([binArrayAccounts, 'optionalAccess',
|
|
11326
|
+
if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _80 => _80[0]])) {
|
|
10941
11327
|
binArrayBitmapExtension = binArrayBitMapExtensionPubkey;
|
|
10942
11328
|
}
|
|
10943
|
-
if (!!_optionalChain([binArrayAccounts, 'optionalAccess',
|
|
11329
|
+
if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _81 => _81[1]])) {
|
|
10944
11330
|
fromBinArray = fromBinArrayPubkey;
|
|
10945
11331
|
}
|
|
10946
|
-
if (!!_optionalChain([binArrayAccounts, 'optionalAccess',
|
|
11332
|
+
if (!!_optionalChain([binArrayAccounts, 'optionalAccess', _82 => _82[2]]) && !!toBinArrayIndex) {
|
|
10947
11333
|
toBinArray = toBinArrayPubkey;
|
|
10948
11334
|
}
|
|
10949
11335
|
const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
|
|
@@ -11268,7 +11654,12 @@ var DLMM = class {
|
|
|
11268
11654
|
let totalYAmount = new (0, _decimaljs2.default)(0);
|
|
11269
11655
|
bins.forEach((bin, idx) => {
|
|
11270
11656
|
const binSupply = new (0, _decimaljs2.default)(bin.supply.toString());
|
|
11271
|
-
|
|
11657
|
+
let posShare;
|
|
11658
|
+
if (bin.version === 1 && version === 0 /* V1 */) {
|
|
11659
|
+
posShare = new (0, _decimaljs2.default)(posShares[idx].shln(64).toString());
|
|
11660
|
+
} else {
|
|
11661
|
+
posShare = new (0, _decimaljs2.default)(posShares[idx].toString());
|
|
11662
|
+
}
|
|
11272
11663
|
const positionXAmount = binSupply.eq(new (0, _decimaljs2.default)("0")) ? new (0, _decimaljs2.default)("0") : posShare.mul(bin.xAmount.toString()).div(binSupply);
|
|
11273
11664
|
const positionYAmount = binSupply.eq(new (0, _decimaljs2.default)("0")) ? new (0, _decimaljs2.default)("0") : posShare.mul(bin.yAmount.toString()).div(binSupply);
|
|
11274
11665
|
totalXAmount = totalXAmount.add(positionXAmount);
|