@meteora-ag/dlmm 1.6.0-rc.20 → 1.6.0-rc.22
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 +214 -94
- package/dist/index.js +2105 -1831
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2444 -2170
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -9219,7 +9219,7 @@ var ILM_BASE = new (0, _web3js.PublicKey)(
|
|
|
9219
9219
|
var MAX_EXTRA_BIN_ARRAYS = 3;
|
|
9220
9220
|
var U64_MAX = new (0, _anchor.BN)("18446744073709551615");
|
|
9221
9221
|
var MAX_BINS_PER_POSITION = new (0, _anchor.BN)(
|
|
9222
|
-
_nullishCoalesce(_optionalChain([CONSTANTS, 'access', _26 => _26.find, 'call', _27 => _27(([k, v]) => v.name == "
|
|
9222
|
+
_nullishCoalesce(_optionalChain([CONSTANTS, 'access', _26 => _26.find, 'call', _27 => _27(([k, v]) => v.name == "POSITION_MAX_LENGTH"), 'optionalAccess', _28 => _28[1], 'access', _29 => _29.value]), () => ( 0))
|
|
9223
9223
|
);
|
|
9224
9224
|
|
|
9225
9225
|
// src/dlmm/error.ts
|
|
@@ -9285,6 +9285,116 @@ var DlmmSdkError = class extends Error {
|
|
|
9285
9285
|
|
|
9286
9286
|
|
|
9287
9287
|
|
|
9288
|
+
// src/dlmm/types/index.ts
|
|
9289
|
+
|
|
9290
|
+
|
|
9291
|
+
|
|
9292
|
+
|
|
9293
|
+
|
|
9294
|
+
|
|
9295
|
+
|
|
9296
|
+
var _borsh = require('@coral-xyz/borsh');
|
|
9297
|
+
var PositionVersion = /* @__PURE__ */ ((PositionVersion3) => {
|
|
9298
|
+
PositionVersion3[PositionVersion3["V1"] = 0] = "V1";
|
|
9299
|
+
PositionVersion3[PositionVersion3["V2"] = 1] = "V2";
|
|
9300
|
+
return PositionVersion3;
|
|
9301
|
+
})(PositionVersion || {});
|
|
9302
|
+
var PairType = /* @__PURE__ */ ((PairType2) => {
|
|
9303
|
+
PairType2[PairType2["Permissionless"] = 0] = "Permissionless";
|
|
9304
|
+
PairType2[PairType2["Permissioned"] = 1] = "Permissioned";
|
|
9305
|
+
return PairType2;
|
|
9306
|
+
})(PairType || {});
|
|
9307
|
+
var Strategy = {
|
|
9308
|
+
SpotBalanced: { spotBalanced: {} },
|
|
9309
|
+
CurveBalanced: { curveBalanced: {} },
|
|
9310
|
+
BidAskBalanced: { bidAskBalanced: {} },
|
|
9311
|
+
SpotImBalanced: { spotImBalanced: {} },
|
|
9312
|
+
CurveImBalanced: { curveImBalanced: {} },
|
|
9313
|
+
BidAskImBalanced: { bidAskImBalanced: {} }
|
|
9314
|
+
};
|
|
9315
|
+
var StrategyType = /* @__PURE__ */ ((StrategyType3) => {
|
|
9316
|
+
StrategyType3[StrategyType3["Spot"] = 0] = "Spot";
|
|
9317
|
+
StrategyType3[StrategyType3["Curve"] = 1] = "Curve";
|
|
9318
|
+
StrategyType3[StrategyType3["BidAsk"] = 2] = "BidAsk";
|
|
9319
|
+
return StrategyType3;
|
|
9320
|
+
})(StrategyType || {});
|
|
9321
|
+
var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
|
|
9322
|
+
ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
|
|
9323
|
+
ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
|
|
9324
|
+
return ActivationType2;
|
|
9325
|
+
})(ActivationType || {});
|
|
9326
|
+
var POSITION_MIN_SIZE = 8112;
|
|
9327
|
+
var POSITION_BIN_DATA_SIZE = 112;
|
|
9328
|
+
var BinLiquidity;
|
|
9329
|
+
((BinLiquidity3) => {
|
|
9330
|
+
function fromBin(bin, binId, binStep, baseTokenDecimal, quoteTokenDecimal, version) {
|
|
9331
|
+
const pricePerLamport = getPriceOfBinByBinId(binId, binStep).toString();
|
|
9332
|
+
return {
|
|
9333
|
+
binId,
|
|
9334
|
+
xAmount: bin.amountX,
|
|
9335
|
+
yAmount: bin.amountY,
|
|
9336
|
+
supply: bin.liquiditySupply,
|
|
9337
|
+
price: pricePerLamport,
|
|
9338
|
+
version,
|
|
9339
|
+
pricePerToken: new (0, _decimaljs2.default)(pricePerLamport).mul(new (0, _decimaljs2.default)(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString(),
|
|
9340
|
+
feeAmountXPerTokenStored: bin.feeAmountXPerTokenStored,
|
|
9341
|
+
feeAmountYPerTokenStored: bin.feeAmountYPerTokenStored,
|
|
9342
|
+
rewardPerTokenStored: bin.rewardPerTokenStored
|
|
9343
|
+
};
|
|
9344
|
+
}
|
|
9345
|
+
BinLiquidity3.fromBin = fromBin;
|
|
9346
|
+
function empty(binId, binStep, baseTokenDecimal, quoteTokenDecimal, version) {
|
|
9347
|
+
const pricePerLamport = getPriceOfBinByBinId(binId, binStep).toString();
|
|
9348
|
+
return {
|
|
9349
|
+
binId,
|
|
9350
|
+
xAmount: new (0, _anchor.BN)(0),
|
|
9351
|
+
yAmount: new (0, _anchor.BN)(0),
|
|
9352
|
+
supply: new (0, _anchor.BN)(0),
|
|
9353
|
+
price: pricePerLamport,
|
|
9354
|
+
version,
|
|
9355
|
+
pricePerToken: new (0, _decimaljs2.default)(pricePerLamport).mul(new (0, _decimaljs2.default)(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString(),
|
|
9356
|
+
feeAmountXPerTokenStored: new (0, _anchor.BN)(0),
|
|
9357
|
+
feeAmountYPerTokenStored: new (0, _anchor.BN)(0),
|
|
9358
|
+
rewardPerTokenStored: [new (0, _anchor.BN)(0), new (0, _anchor.BN)(0)]
|
|
9359
|
+
};
|
|
9360
|
+
}
|
|
9361
|
+
BinLiquidity3.empty = empty;
|
|
9362
|
+
})(BinLiquidity || (BinLiquidity = exports.BinLiquidity = {}));
|
|
9363
|
+
var BitmapType = /* @__PURE__ */ ((BitmapType2) => {
|
|
9364
|
+
BitmapType2[BitmapType2["U1024"] = 0] = "U1024";
|
|
9365
|
+
BitmapType2[BitmapType2["U512"] = 1] = "U512";
|
|
9366
|
+
return BitmapType2;
|
|
9367
|
+
})(BitmapType || {});
|
|
9368
|
+
var ClockLayout = _borsh.struct.call(void 0, [
|
|
9369
|
+
_borsh.u64.call(void 0, "slot"),
|
|
9370
|
+
_borsh.i64.call(void 0, "epochStartTimestamp"),
|
|
9371
|
+
_borsh.u64.call(void 0, "epoch"),
|
|
9372
|
+
_borsh.u64.call(void 0, "leaderScheduleEpoch"),
|
|
9373
|
+
_borsh.i64.call(void 0, "unixTimestamp")
|
|
9374
|
+
]);
|
|
9375
|
+
var PairStatus = /* @__PURE__ */ ((PairStatus2) => {
|
|
9376
|
+
PairStatus2[PairStatus2["Enabled"] = 0] = "Enabled";
|
|
9377
|
+
PairStatus2[PairStatus2["Disabled"] = 1] = "Disabled";
|
|
9378
|
+
return PairStatus2;
|
|
9379
|
+
})(PairStatus || {});
|
|
9380
|
+
var ActionType = /* @__PURE__ */ ((ActionType2) => {
|
|
9381
|
+
ActionType2[ActionType2["Liquidity"] = 0] = "Liquidity";
|
|
9382
|
+
ActionType2[ActionType2["Reward"] = 1] = "Reward";
|
|
9383
|
+
return ActionType2;
|
|
9384
|
+
})(ActionType || {});
|
|
9385
|
+
var ResizeSide = /* @__PURE__ */ ((ResizeSide2) => {
|
|
9386
|
+
ResizeSide2[ResizeSide2["Lower"] = 0] = "Lower";
|
|
9387
|
+
ResizeSide2[ResizeSide2["Upper"] = 1] = "Upper";
|
|
9388
|
+
return ResizeSide2;
|
|
9389
|
+
})(ResizeSide || {});
|
|
9390
|
+
var MEMO_PROGRAM_ID = new (0, _web3js.PublicKey)(
|
|
9391
|
+
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
|
|
9392
|
+
);
|
|
9393
|
+
|
|
9394
|
+
// src/dlmm/helpers/binArray.ts
|
|
9395
|
+
|
|
9396
|
+
|
|
9397
|
+
|
|
9288
9398
|
// src/dlmm/helpers/math.ts
|
|
9289
9399
|
|
|
9290
9400
|
|
|
@@ -10479,169 +10589,7 @@ function generateBinAmount(amount, binStep, binId, tokenXDecimal, tokenYDecimal,
|
|
|
10479
10589
|
return new (0, _anchor.BN)(c1.sub(c0).floor().toString());
|
|
10480
10590
|
}
|
|
10481
10591
|
|
|
10482
|
-
// src/dlmm/helpers/
|
|
10483
|
-
|
|
10484
|
-
|
|
10485
|
-
|
|
10486
|
-
|
|
10487
|
-
|
|
10488
|
-
|
|
10489
|
-
|
|
10490
|
-
// src/dlmm/types/index.ts
|
|
10491
|
-
|
|
10492
|
-
|
|
10493
|
-
|
|
10494
|
-
|
|
10495
|
-
|
|
10496
|
-
|
|
10497
|
-
|
|
10498
|
-
var _borsh = require('@coral-xyz/borsh');
|
|
10499
|
-
var PositionVersion = /* @__PURE__ */ ((PositionVersion3) => {
|
|
10500
|
-
PositionVersion3[PositionVersion3["V1"] = 0] = "V1";
|
|
10501
|
-
PositionVersion3[PositionVersion3["V2"] = 1] = "V2";
|
|
10502
|
-
return PositionVersion3;
|
|
10503
|
-
})(PositionVersion || {});
|
|
10504
|
-
var PairType = /* @__PURE__ */ ((PairType2) => {
|
|
10505
|
-
PairType2[PairType2["Permissionless"] = 0] = "Permissionless";
|
|
10506
|
-
PairType2[PairType2["Permissioned"] = 1] = "Permissioned";
|
|
10507
|
-
return PairType2;
|
|
10508
|
-
})(PairType || {});
|
|
10509
|
-
var Strategy = {
|
|
10510
|
-
SpotBalanced: { spotBalanced: {} },
|
|
10511
|
-
CurveBalanced: { curveBalanced: {} },
|
|
10512
|
-
BidAskBalanced: { bidAskBalanced: {} },
|
|
10513
|
-
SpotImBalanced: { spotImBalanced: {} },
|
|
10514
|
-
CurveImBalanced: { curveImBalanced: {} },
|
|
10515
|
-
BidAskImBalanced: { bidAskImBalanced: {} }
|
|
10516
|
-
};
|
|
10517
|
-
var StrategyType = /* @__PURE__ */ ((StrategyType3) => {
|
|
10518
|
-
StrategyType3[StrategyType3["Spot"] = 0] = "Spot";
|
|
10519
|
-
StrategyType3[StrategyType3["Curve"] = 1] = "Curve";
|
|
10520
|
-
StrategyType3[StrategyType3["BidAsk"] = 2] = "BidAsk";
|
|
10521
|
-
return StrategyType3;
|
|
10522
|
-
})(StrategyType || {});
|
|
10523
|
-
var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
|
|
10524
|
-
ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
|
|
10525
|
-
ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
|
|
10526
|
-
return ActivationType2;
|
|
10527
|
-
})(ActivationType || {});
|
|
10528
|
-
var POSITION_MIN_SIZE = 8112;
|
|
10529
|
-
var POSITION_BIN_DATA_SIZE = 112;
|
|
10530
|
-
var BinLiquidity;
|
|
10531
|
-
((BinLiquidity3) => {
|
|
10532
|
-
function fromBin(bin, binId, binStep, baseTokenDecimal, quoteTokenDecimal, version) {
|
|
10533
|
-
const pricePerLamport = getPriceOfBinByBinId(binId, binStep).toString();
|
|
10534
|
-
return {
|
|
10535
|
-
binId,
|
|
10536
|
-
xAmount: bin.amountX,
|
|
10537
|
-
yAmount: bin.amountY,
|
|
10538
|
-
supply: bin.liquiditySupply,
|
|
10539
|
-
price: pricePerLamport,
|
|
10540
|
-
version,
|
|
10541
|
-
pricePerToken: new (0, _decimaljs2.default)(pricePerLamport).mul(new (0, _decimaljs2.default)(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString(),
|
|
10542
|
-
feeAmountXPerTokenStored: bin.feeAmountXPerTokenStored,
|
|
10543
|
-
feeAmountYPerTokenStored: bin.feeAmountYPerTokenStored,
|
|
10544
|
-
rewardPerTokenStored: bin.rewardPerTokenStored
|
|
10545
|
-
};
|
|
10546
|
-
}
|
|
10547
|
-
BinLiquidity3.fromBin = fromBin;
|
|
10548
|
-
function empty(binId, binStep, baseTokenDecimal, quoteTokenDecimal, version) {
|
|
10549
|
-
const pricePerLamport = getPriceOfBinByBinId(binId, binStep).toString();
|
|
10550
|
-
return {
|
|
10551
|
-
binId,
|
|
10552
|
-
xAmount: new (0, _anchor.BN)(0),
|
|
10553
|
-
yAmount: new (0, _anchor.BN)(0),
|
|
10554
|
-
supply: new (0, _anchor.BN)(0),
|
|
10555
|
-
price: pricePerLamport,
|
|
10556
|
-
version,
|
|
10557
|
-
pricePerToken: new (0, _decimaljs2.default)(pricePerLamport).mul(new (0, _decimaljs2.default)(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString(),
|
|
10558
|
-
feeAmountXPerTokenStored: new (0, _anchor.BN)(0),
|
|
10559
|
-
feeAmountYPerTokenStored: new (0, _anchor.BN)(0),
|
|
10560
|
-
rewardPerTokenStored: [new (0, _anchor.BN)(0), new (0, _anchor.BN)(0)]
|
|
10561
|
-
};
|
|
10562
|
-
}
|
|
10563
|
-
BinLiquidity3.empty = empty;
|
|
10564
|
-
})(BinLiquidity || (BinLiquidity = exports.BinLiquidity = {}));
|
|
10565
|
-
var BitmapType = /* @__PURE__ */ ((BitmapType2) => {
|
|
10566
|
-
BitmapType2[BitmapType2["U1024"] = 0] = "U1024";
|
|
10567
|
-
BitmapType2[BitmapType2["U512"] = 1] = "U512";
|
|
10568
|
-
return BitmapType2;
|
|
10569
|
-
})(BitmapType || {});
|
|
10570
|
-
var ClockLayout = _borsh.struct.call(void 0, [
|
|
10571
|
-
_borsh.u64.call(void 0, "slot"),
|
|
10572
|
-
_borsh.i64.call(void 0, "epochStartTimestamp"),
|
|
10573
|
-
_borsh.u64.call(void 0, "epoch"),
|
|
10574
|
-
_borsh.u64.call(void 0, "leaderScheduleEpoch"),
|
|
10575
|
-
_borsh.i64.call(void 0, "unixTimestamp")
|
|
10576
|
-
]);
|
|
10577
|
-
var PairStatus = /* @__PURE__ */ ((PairStatus2) => {
|
|
10578
|
-
PairStatus2[PairStatus2["Enabled"] = 0] = "Enabled";
|
|
10579
|
-
PairStatus2[PairStatus2["Disabled"] = 1] = "Disabled";
|
|
10580
|
-
return PairStatus2;
|
|
10581
|
-
})(PairStatus || {});
|
|
10582
|
-
var ActionType = /* @__PURE__ */ ((ActionType2) => {
|
|
10583
|
-
ActionType2[ActionType2["Liquidity"] = 0] = "Liquidity";
|
|
10584
|
-
ActionType2[ActionType2["Reward"] = 1] = "Reward";
|
|
10585
|
-
return ActionType2;
|
|
10586
|
-
})(ActionType || {});
|
|
10587
|
-
var ResizeSide = /* @__PURE__ */ ((ResizeSide2) => {
|
|
10588
|
-
ResizeSide2[ResizeSide2["Lower"] = 0] = "Lower";
|
|
10589
|
-
ResizeSide2[ResizeSide2["Upper"] = 1] = "Upper";
|
|
10590
|
-
return ResizeSide2;
|
|
10591
|
-
})(ResizeSide || {});
|
|
10592
|
-
var MEMO_PROGRAM_ID = new (0, _web3js.PublicKey)(
|
|
10593
|
-
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
|
|
10594
|
-
);
|
|
10595
|
-
|
|
10596
|
-
// src/dlmm/helpers/computeUnit.ts
|
|
10597
|
-
var DEFAULT_ADD_LIQUIDITY_CU = 1e6;
|
|
10598
|
-
var DEFAULT_EXTEND_POSITION_HIGH_CU = 1e6;
|
|
10599
|
-
var DEFAULT_EXTEND_POSITION_LOW_CU = 3e4;
|
|
10600
|
-
var DEFAULT_INIT_POSITION_CU = 3e4;
|
|
10601
|
-
var DEFAULT_INIT_BIN_ARRAY_CU = 35e4;
|
|
10602
|
-
var MIN_CU_BUFFER = 5e4;
|
|
10603
|
-
var MAX_CU_BUFFER = 2e5;
|
|
10604
|
-
var getDefaultExtendPositionCU = (side) => {
|
|
10605
|
-
switch (side) {
|
|
10606
|
-
case 0 /* Lower */:
|
|
10607
|
-
return DEFAULT_EXTEND_POSITION_HIGH_CU;
|
|
10608
|
-
case 1 /* Upper */:
|
|
10609
|
-
return DEFAULT_EXTEND_POSITION_LOW_CU;
|
|
10610
|
-
}
|
|
10611
|
-
};
|
|
10612
|
-
var getSimulationComputeUnits = async (connection, instructions, payer, lookupTables, commitment = "confirmed") => {
|
|
10613
|
-
const testInstructions = [
|
|
10614
|
-
// Set an arbitrarily high number in simulation
|
|
10615
|
-
// so we can be sure the transaction will succeed
|
|
10616
|
-
// and get the real compute units used
|
|
10617
|
-
_web3js.ComputeBudgetProgram.setComputeUnitLimit({ units: 14e5 }),
|
|
10618
|
-
...instructions
|
|
10619
|
-
];
|
|
10620
|
-
const testTransaction = new (0, _web3js.VersionedTransaction)(
|
|
10621
|
-
new (0, _web3js.TransactionMessage)({
|
|
10622
|
-
instructions: testInstructions,
|
|
10623
|
-
payerKey: payer,
|
|
10624
|
-
// RecentBlockhash can by any public key during simulation
|
|
10625
|
-
// since 'replaceRecentBlockhash' is set to 'true' below
|
|
10626
|
-
recentBlockhash: _web3js.PublicKey.default.toString()
|
|
10627
|
-
}).compileToV0Message(lookupTables)
|
|
10628
|
-
);
|
|
10629
|
-
const rpcResponse = await connection.simulateTransaction(testTransaction, {
|
|
10630
|
-
replaceRecentBlockhash: true,
|
|
10631
|
-
sigVerify: false,
|
|
10632
|
-
commitment
|
|
10633
|
-
});
|
|
10634
|
-
if (_optionalChain([rpcResponse, 'optionalAccess', _33 => _33.value, 'optionalAccess', _34 => _34.err])) {
|
|
10635
|
-
const logs = _optionalChain([rpcResponse, 'access', _35 => _35.value, 'access', _36 => _36.logs, 'optionalAccess', _37 => _37.join, 'call', _38 => _38("\n \u2022 ")]) || "No logs available";
|
|
10636
|
-
throw new Error(
|
|
10637
|
-
`Transaction simulation failed:
|
|
10638
|
-
\u2022${logs}` + JSON.stringify(_optionalChain([rpcResponse, 'optionalAccess', _39 => _39.value, 'optionalAccess', _40 => _40.err]))
|
|
10639
|
-
);
|
|
10640
|
-
}
|
|
10641
|
-
return rpcResponse.value.unitsConsumed || null;
|
|
10642
|
-
};
|
|
10643
|
-
|
|
10644
|
-
// src/dlmm/helpers/derive.ts
|
|
10592
|
+
// src/dlmm/helpers/derive.ts
|
|
10645
10593
|
|
|
10646
10594
|
function sortTokenMints(tokenX, tokenY) {
|
|
10647
10595
|
const [minKey, maxKey] = tokenX.toBuffer().compare(tokenY.toBuffer()) == 1 ? [tokenY, tokenX] : [tokenX, tokenY];
|
|
@@ -10788,10 +10736,15 @@ function deriveRewardVault(lbPair, rewardIndex, programId) {
|
|
|
10788
10736
|
programId
|
|
10789
10737
|
);
|
|
10790
10738
|
}
|
|
10739
|
+
function derivePlaceHolderAccountMeta(programId) {
|
|
10740
|
+
return {
|
|
10741
|
+
pubkey: programId,
|
|
10742
|
+
isWritable: false,
|
|
10743
|
+
isSigner: false
|
|
10744
|
+
};
|
|
10745
|
+
}
|
|
10791
10746
|
|
|
10792
10747
|
// src/dlmm/helpers/binArray.ts
|
|
10793
|
-
|
|
10794
|
-
|
|
10795
10748
|
function internalBitmapRange() {
|
|
10796
10749
|
const lowerBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.neg();
|
|
10797
10750
|
const upperBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.sub(new (0, _anchor.BN)(1));
|
|
@@ -11100,784 +11053,514 @@ function updateBinArray(activeId, clock, allRewardInfos, binArray) {
|
|
|
11100
11053
|
return binArrayClone;
|
|
11101
11054
|
}
|
|
11102
11055
|
|
|
11103
|
-
// src/dlmm/helpers/
|
|
11056
|
+
// src/dlmm/helpers/computeUnit.ts
|
|
11104
11057
|
|
|
11105
|
-
|
|
11106
|
-
|
|
11107
|
-
|
|
11108
|
-
|
|
11109
|
-
|
|
11110
|
-
|
|
11111
|
-
|
|
11112
|
-
|
|
11058
|
+
|
|
11059
|
+
|
|
11060
|
+
|
|
11061
|
+
|
|
11062
|
+
|
|
11063
|
+
var DEFAULT_ADD_LIQUIDITY_CU = 1e6;
|
|
11064
|
+
var DEFAULT_EXTEND_POSITION_HIGH_CU = 1e6;
|
|
11065
|
+
var DEFAULT_EXTEND_POSITION_LOW_CU = 3e4;
|
|
11066
|
+
var DEFAULT_INIT_POSITION_CU = 3e4;
|
|
11067
|
+
var DEFAULT_INIT_BIN_ARRAY_CU = 35e4;
|
|
11068
|
+
var MIN_CU_BUFFER = 5e4;
|
|
11069
|
+
var MAX_CU_BUFFER = 2e5;
|
|
11070
|
+
var MAX_CU = 14e5;
|
|
11071
|
+
var getDefaultExtendPositionCU = (side) => {
|
|
11072
|
+
switch (side) {
|
|
11073
|
+
case 0 /* Lower */:
|
|
11074
|
+
return DEFAULT_EXTEND_POSITION_HIGH_CU;
|
|
11075
|
+
case 1 /* Upper */:
|
|
11076
|
+
return DEFAULT_EXTEND_POSITION_LOW_CU;
|
|
11113
11077
|
}
|
|
11114
|
-
|
|
11115
|
-
|
|
11116
|
-
|
|
11117
|
-
|
|
11118
|
-
|
|
11078
|
+
};
|
|
11079
|
+
var getSimulationComputeUnits = async (connection, instructions, payer, lookupTables, commitment = "confirmed") => {
|
|
11080
|
+
const testInstructions = [
|
|
11081
|
+
// Set an arbitrarily high number in simulation
|
|
11082
|
+
// so we can be sure the transaction will succeed
|
|
11083
|
+
// and get the real compute units used
|
|
11084
|
+
_web3js.ComputeBudgetProgram.setComputeUnitLimit({ units: 14e5 }),
|
|
11085
|
+
...instructions
|
|
11086
|
+
];
|
|
11087
|
+
const testTransaction = new (0, _web3js.VersionedTransaction)(
|
|
11088
|
+
new (0, _web3js.TransactionMessage)({
|
|
11089
|
+
instructions: testInstructions,
|
|
11090
|
+
payerKey: payer,
|
|
11091
|
+
// RecentBlockhash can by any public key during simulation
|
|
11092
|
+
// since 'replaceRecentBlockhash' is set to 'true' below
|
|
11093
|
+
recentBlockhash: _web3js.PublicKey.default.toString()
|
|
11094
|
+
}).compileToV0Message(lookupTables)
|
|
11119
11095
|
);
|
|
11120
|
-
|
|
11121
|
-
|
|
11122
|
-
|
|
11123
|
-
|
|
11124
|
-
|
|
11125
|
-
|
|
11126
|
-
|
|
11127
|
-
|
|
11128
|
-
|
|
11129
|
-
|
|
11130
|
-
|
|
11131
|
-
function computeProtocolFee(feeAmount, sParameter) {
|
|
11132
|
-
return feeAmount.mul(new (0, _anchor.BN)(sParameter.protocolShare)).div(new (0, _anchor.BN)(BASIS_POINT_MAX));
|
|
11133
|
-
}
|
|
11134
|
-
function swapExactOutQuoteAtBin(bin, binStep, sParameter, vParameter, outAmount, swapForY) {
|
|
11135
|
-
if (swapForY && bin.amountY.isZero()) {
|
|
11136
|
-
return {
|
|
11137
|
-
amountIn: new (0, _anchor.BN)(0),
|
|
11138
|
-
amountOut: new (0, _anchor.BN)(0),
|
|
11139
|
-
fee: new (0, _anchor.BN)(0),
|
|
11140
|
-
protocolFee: new (0, _anchor.BN)(0)
|
|
11141
|
-
};
|
|
11142
|
-
}
|
|
11143
|
-
if (!swapForY && bin.amountX.isZero()) {
|
|
11144
|
-
return {
|
|
11145
|
-
amountIn: new (0, _anchor.BN)(0),
|
|
11146
|
-
amountOut: new (0, _anchor.BN)(0),
|
|
11147
|
-
fee: new (0, _anchor.BN)(0),
|
|
11148
|
-
protocolFee: new (0, _anchor.BN)(0)
|
|
11149
|
-
};
|
|
11096
|
+
const rpcResponse = await connection.simulateTransaction(testTransaction, {
|
|
11097
|
+
replaceRecentBlockhash: true,
|
|
11098
|
+
sigVerify: false,
|
|
11099
|
+
commitment
|
|
11100
|
+
});
|
|
11101
|
+
if (_optionalChain([rpcResponse, 'optionalAccess', _33 => _33.value, 'optionalAccess', _34 => _34.err])) {
|
|
11102
|
+
const logs = _optionalChain([rpcResponse, 'access', _35 => _35.value, 'access', _36 => _36.logs, 'optionalAccess', _37 => _37.join, 'call', _38 => _38("\n \u2022 ")]) || "No logs available";
|
|
11103
|
+
throw new Error(
|
|
11104
|
+
`Transaction simulation failed:
|
|
11105
|
+
\u2022${logs}` + JSON.stringify(_optionalChain([rpcResponse, 'optionalAccess', _39 => _39.value, 'optionalAccess', _40 => _40.err]))
|
|
11106
|
+
);
|
|
11150
11107
|
}
|
|
11151
|
-
|
|
11152
|
-
|
|
11153
|
-
|
|
11154
|
-
|
|
11155
|
-
|
|
11156
|
-
|
|
11157
|
-
|
|
11158
|
-
|
|
11108
|
+
return rpcResponse.value.unitsConsumed || null;
|
|
11109
|
+
};
|
|
11110
|
+
|
|
11111
|
+
// src/dlmm/helpers/positions/index.ts
|
|
11112
|
+
|
|
11113
|
+
|
|
11114
|
+
// src/dlmm/helpers/positions/wrapper.ts
|
|
11115
|
+
|
|
11116
|
+
function combineBaseAndExtendedPositionBinData(base, extended) {
|
|
11117
|
+
const combinedLiquidityShares = base.liquidityShares;
|
|
11118
|
+
const combinedRewardInfos = base.rewardInfos;
|
|
11119
|
+
const combinedFeeInfos = base.feeInfos;
|
|
11120
|
+
for (const binData of extended) {
|
|
11121
|
+
combinedLiquidityShares.push(binData.liquidityShare);
|
|
11122
|
+
combinedRewardInfos.push(binData.rewardInfo);
|
|
11123
|
+
combinedFeeInfos.push(binData.feeInfo);
|
|
11159
11124
|
}
|
|
11160
|
-
|
|
11161
|
-
|
|
11162
|
-
|
|
11163
|
-
|
|
11164
|
-
|
|
11165
|
-
|
|
11166
|
-
|
|
11167
|
-
|
|
11168
|
-
|
|
11125
|
+
return {
|
|
11126
|
+
liquidityShares: combinedLiquidityShares,
|
|
11127
|
+
rewardInfos: combinedRewardInfos,
|
|
11128
|
+
feeInfos: combinedFeeInfos
|
|
11129
|
+
};
|
|
11130
|
+
}
|
|
11131
|
+
function wrapPosition(program, key, account) {
|
|
11132
|
+
const disc = account.data.subarray(0, 8);
|
|
11133
|
+
if (disc.equals(Buffer.from(getAccountDiscriminator("positionV2")))) {
|
|
11134
|
+
const state = decodeAccount(
|
|
11135
|
+
program,
|
|
11136
|
+
"positionV2",
|
|
11137
|
+
account.data
|
|
11138
|
+
);
|
|
11139
|
+
const extended = decodeExtendedPosition(
|
|
11140
|
+
state,
|
|
11141
|
+
program,
|
|
11142
|
+
account.data.subarray(8 + POSITION_MIN_SIZE)
|
|
11143
|
+
);
|
|
11144
|
+
const combinedPositionBinData = combineBaseAndExtendedPositionBinData(
|
|
11145
|
+
state,
|
|
11146
|
+
extended
|
|
11147
|
+
);
|
|
11148
|
+
return new PositionV2Wrapper(key, state, extended, combinedPositionBinData);
|
|
11169
11149
|
} else {
|
|
11170
|
-
|
|
11171
|
-
const fee = computeFee(binStep, sParameter, vParameter, amountIn);
|
|
11172
|
-
const protocolFee = computeProtocolFee(fee, sParameter);
|
|
11173
|
-
return {
|
|
11174
|
-
amountIn,
|
|
11175
|
-
amountOut: outAmount,
|
|
11176
|
-
fee,
|
|
11177
|
-
protocolFee
|
|
11178
|
-
};
|
|
11150
|
+
throw new Error("Unknown position account");
|
|
11179
11151
|
}
|
|
11180
11152
|
}
|
|
11181
|
-
|
|
11182
|
-
|
|
11183
|
-
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
|
|
11187
|
-
protocolFee: new (0, _anchor.BN)(0)
|
|
11188
|
-
};
|
|
11153
|
+
var PositionV2Wrapper = class {
|
|
11154
|
+
constructor(positionAddress, inner, extended, combinedPositionBinData) {
|
|
11155
|
+
this.positionAddress = positionAddress;
|
|
11156
|
+
this.inner = inner;
|
|
11157
|
+
this.extended = extended;
|
|
11158
|
+
this.combinedPositionBinData = combinedPositionBinData;
|
|
11189
11159
|
}
|
|
11190
|
-
|
|
11191
|
-
return
|
|
11192
|
-
amountIn: new (0, _anchor.BN)(0),
|
|
11193
|
-
amountOut: new (0, _anchor.BN)(0),
|
|
11194
|
-
fee: new (0, _anchor.BN)(0),
|
|
11195
|
-
protocolFee: new (0, _anchor.BN)(0)
|
|
11196
|
-
};
|
|
11160
|
+
address() {
|
|
11161
|
+
return this.positionAddress;
|
|
11197
11162
|
}
|
|
11198
|
-
|
|
11199
|
-
|
|
11200
|
-
if (swapForY) {
|
|
11201
|
-
maxAmountOut = bin.amountY;
|
|
11202
|
-
maxAmountIn = shlDiv(bin.amountY, bin.price, SCALE_OFFSET, 0 /* Up */);
|
|
11203
|
-
} else {
|
|
11204
|
-
maxAmountOut = bin.amountX;
|
|
11205
|
-
maxAmountIn = mulShr(bin.amountX, bin.price, SCALE_OFFSET, 0 /* Up */);
|
|
11163
|
+
totalClaimedRewards() {
|
|
11164
|
+
return this.inner.totalClaimedRewards;
|
|
11206
11165
|
}
|
|
11207
|
-
|
|
11208
|
-
|
|
11209
|
-
let amountInWithFees;
|
|
11210
|
-
let amountOut;
|
|
11211
|
-
let fee;
|
|
11212
|
-
let protocolFee;
|
|
11213
|
-
if (inAmount.gt(maxAmountIn)) {
|
|
11214
|
-
amountInWithFees = maxAmountIn;
|
|
11215
|
-
amountOut = maxAmountOut;
|
|
11216
|
-
fee = maxFee;
|
|
11217
|
-
protocolFee = computeProtocolFee(maxFee, sParameter);
|
|
11218
|
-
} else {
|
|
11219
|
-
fee = computeFeeFromAmount(binStep, sParameter, vParameter, inAmount);
|
|
11220
|
-
const amountInAfterFee = inAmount.sub(fee);
|
|
11221
|
-
const computedOutAmount = getOutAmount(bin, amountInAfterFee, swapForY);
|
|
11222
|
-
amountOut = computedOutAmount.gt(maxAmountOut) ? maxAmountOut : computedOutAmount;
|
|
11223
|
-
protocolFee = computeProtocolFee(fee, sParameter);
|
|
11224
|
-
amountInWithFees = inAmount;
|
|
11166
|
+
feeOwner() {
|
|
11167
|
+
return this.inner.feeOwner;
|
|
11225
11168
|
}
|
|
11226
|
-
|
|
11227
|
-
|
|
11228
|
-
amountOut,
|
|
11229
|
-
fee,
|
|
11230
|
-
protocolFee
|
|
11231
|
-
};
|
|
11232
|
-
}
|
|
11233
|
-
function getAmountIn(amountOut, price, swapForY) {
|
|
11234
|
-
if (swapForY) {
|
|
11235
|
-
return shlDiv(amountOut, price, SCALE_OFFSET, 0 /* Up */);
|
|
11236
|
-
} else {
|
|
11237
|
-
return mulShr(amountOut, price, SCALE_OFFSET, 0 /* Up */);
|
|
11169
|
+
lockReleasePoint() {
|
|
11170
|
+
return this.inner.lockReleasePoint;
|
|
11238
11171
|
}
|
|
11239
|
-
|
|
11240
|
-
|
|
11241
|
-
// src/dlmm/helpers/strategy.ts
|
|
11242
|
-
|
|
11243
|
-
var DEFAULT_MAX_WEIGHT = 2e3;
|
|
11244
|
-
var DEFAULT_MIN_WEIGHT = 200;
|
|
11245
|
-
function toWeightSpotBalanced(minBinId, maxBinId) {
|
|
11246
|
-
let distributions = [];
|
|
11247
|
-
for (let i = minBinId; i <= maxBinId; i++) {
|
|
11248
|
-
distributions.push({
|
|
11249
|
-
binId: i,
|
|
11250
|
-
weight: 1
|
|
11251
|
-
});
|
|
11172
|
+
operator() {
|
|
11173
|
+
return this.inner.operator;
|
|
11252
11174
|
}
|
|
11253
|
-
|
|
11254
|
-
|
|
11255
|
-
function toWeightDescendingOrder(minBinId, maxBinId) {
|
|
11256
|
-
let distributions = [];
|
|
11257
|
-
for (let i = minBinId; i <= maxBinId; i++) {
|
|
11258
|
-
distributions.push({
|
|
11259
|
-
binId: i,
|
|
11260
|
-
weight: maxBinId - i + 1
|
|
11261
|
-
});
|
|
11175
|
+
totalClaimedFeeYAmount() {
|
|
11176
|
+
return this.inner.totalClaimedFeeYAmount;
|
|
11262
11177
|
}
|
|
11263
|
-
|
|
11264
|
-
|
|
11265
|
-
function toWeightAscendingOrder(minBinId, maxBinId) {
|
|
11266
|
-
let distributions = [];
|
|
11267
|
-
for (let i = minBinId; i <= maxBinId; i++) {
|
|
11268
|
-
distributions.push({
|
|
11269
|
-
binId: i,
|
|
11270
|
-
weight: i - minBinId + 1
|
|
11271
|
-
});
|
|
11178
|
+
totalClaimedFeeXAmount() {
|
|
11179
|
+
return this.inner.totalClaimedFeeXAmount;
|
|
11272
11180
|
}
|
|
11273
|
-
|
|
11274
|
-
|
|
11275
|
-
function toWeightCurve(minBinId, maxBinId, activeId) {
|
|
11276
|
-
if (activeId < minBinId || activeId > maxBinId) {
|
|
11277
|
-
throw "Invalid strategy params";
|
|
11181
|
+
lbPair() {
|
|
11182
|
+
return this.inner.lbPair;
|
|
11278
11183
|
}
|
|
11279
|
-
|
|
11280
|
-
|
|
11281
|
-
let diffWeight = maxWeight - minWeight;
|
|
11282
|
-
let diffMinWeight = activeId > minBinId ? Math.floor(diffWeight / (activeId - minBinId)) : 0;
|
|
11283
|
-
let diffMaxWeight = maxBinId > activeId ? Math.floor(diffWeight / (maxBinId - activeId)) : 0;
|
|
11284
|
-
let distributions = [];
|
|
11285
|
-
for (let i = minBinId; i <= maxBinId; i++) {
|
|
11286
|
-
if (i < activeId) {
|
|
11287
|
-
distributions.push({
|
|
11288
|
-
binId: i,
|
|
11289
|
-
weight: maxWeight - (activeId - i) * diffMinWeight
|
|
11290
|
-
});
|
|
11291
|
-
} else if (i > activeId) {
|
|
11292
|
-
distributions.push({
|
|
11293
|
-
binId: i,
|
|
11294
|
-
weight: maxWeight - (i - activeId) * diffMaxWeight
|
|
11295
|
-
});
|
|
11296
|
-
} else {
|
|
11297
|
-
distributions.push({
|
|
11298
|
-
binId: i,
|
|
11299
|
-
weight: maxWeight
|
|
11300
|
-
});
|
|
11301
|
-
}
|
|
11184
|
+
lowerBinId() {
|
|
11185
|
+
return new (0, _bnjs2.default)(this.inner.lowerBinId);
|
|
11302
11186
|
}
|
|
11303
|
-
|
|
11304
|
-
|
|
11305
|
-
function toWeightBidAsk(minBinId, maxBinId, activeId) {
|
|
11306
|
-
if (activeId < minBinId || activeId > maxBinId) {
|
|
11307
|
-
throw "Invalid strategy params";
|
|
11187
|
+
upperBinId() {
|
|
11188
|
+
return new (0, _bnjs2.default)(this.inner.upperBinId);
|
|
11308
11189
|
}
|
|
11309
|
-
|
|
11310
|
-
|
|
11311
|
-
|
|
11312
|
-
|
|
11313
|
-
|
|
11314
|
-
|
|
11315
|
-
|
|
11316
|
-
|
|
11317
|
-
|
|
11318
|
-
|
|
11319
|
-
|
|
11320
|
-
|
|
11321
|
-
|
|
11322
|
-
|
|
11323
|
-
|
|
11324
|
-
|
|
11325
|
-
});
|
|
11190
|
+
liquidityShares() {
|
|
11191
|
+
return this.combinedPositionBinData.liquidityShares;
|
|
11192
|
+
}
|
|
11193
|
+
rewardInfos() {
|
|
11194
|
+
return this.combinedPositionBinData.rewardInfos;
|
|
11195
|
+
}
|
|
11196
|
+
feeInfos() {
|
|
11197
|
+
return this.combinedPositionBinData.feeInfos;
|
|
11198
|
+
}
|
|
11199
|
+
lastUpdatedAt() {
|
|
11200
|
+
return this.inner.lastUpdatedAt;
|
|
11201
|
+
}
|
|
11202
|
+
getBinArrayIndexesCoverage() {
|
|
11203
|
+
const isExtended = this.extended.length > 0;
|
|
11204
|
+
if (isExtended) {
|
|
11205
|
+
return getBinArrayIndexesCoverage(this.lowerBinId(), this.upperBinId());
|
|
11326
11206
|
} else {
|
|
11327
|
-
|
|
11328
|
-
|
|
11329
|
-
|
|
11330
|
-
});
|
|
11207
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(this.lowerBinId());
|
|
11208
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _bnjs2.default)(1));
|
|
11209
|
+
return [lowerBinArrayIndex, upperBinArrayIndex];
|
|
11331
11210
|
}
|
|
11332
11211
|
}
|
|
11333
|
-
|
|
11212
|
+
getBinArrayKeysCoverage(programId) {
|
|
11213
|
+
return this.getBinArrayIndexesCoverage().map(
|
|
11214
|
+
(index) => deriveBinArray(this.lbPair(), index, programId)[0]
|
|
11215
|
+
);
|
|
11216
|
+
}
|
|
11217
|
+
version() {
|
|
11218
|
+
return 1 /* V2 */;
|
|
11219
|
+
}
|
|
11220
|
+
owner() {
|
|
11221
|
+
return this.inner.owner;
|
|
11222
|
+
}
|
|
11223
|
+
width() {
|
|
11224
|
+
return this.upperBinId().sub(this.lowerBinId()).add(new (0, _bnjs2.default)(1));
|
|
11225
|
+
}
|
|
11226
|
+
};
|
|
11227
|
+
|
|
11228
|
+
// src/dlmm/helpers/positions/index.ts
|
|
11229
|
+
function getBinArrayIndexesCoverage(lowerBinId, upperBinId) {
|
|
11230
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(lowerBinId);
|
|
11231
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(upperBinId);
|
|
11232
|
+
const binArrayIndexes = [];
|
|
11233
|
+
for (let i = lowerBinArrayIndex.toNumber(); i <= upperBinArrayIndex.toNumber(); i++) {
|
|
11234
|
+
binArrayIndexes.push(new (0, _bnjs2.default)(i));
|
|
11235
|
+
}
|
|
11236
|
+
return binArrayIndexes;
|
|
11334
11237
|
}
|
|
11335
|
-
function
|
|
11336
|
-
const
|
|
11337
|
-
|
|
11338
|
-
|
|
11339
|
-
|
|
11340
|
-
|
|
11341
|
-
|
|
11342
|
-
|
|
11343
|
-
|
|
11344
|
-
|
|
11345
|
-
|
|
11346
|
-
|
|
11347
|
-
|
|
11348
|
-
|
|
11349
|
-
mintX,
|
|
11350
|
-
mintY,
|
|
11351
|
-
clock
|
|
11352
|
-
);
|
|
11353
|
-
}
|
|
11354
|
-
const amountsInBin = [];
|
|
11355
|
-
if (!isSingleSideX) {
|
|
11356
|
-
if (minBinId <= activeId) {
|
|
11357
|
-
const weights = toWeightSpotBalanced(minBinId, activeId);
|
|
11358
|
-
const amounts = toAmountBidSide(
|
|
11359
|
-
activeId,
|
|
11360
|
-
amountY,
|
|
11361
|
-
weights,
|
|
11362
|
-
mintY,
|
|
11363
|
-
clock
|
|
11364
|
-
);
|
|
11365
|
-
for (let bin of amounts) {
|
|
11366
|
-
amountsInBin.push({
|
|
11367
|
-
binId: bin.binId,
|
|
11368
|
-
amountX: new (0, _anchor.BN)(0),
|
|
11369
|
-
amountY: bin.amount
|
|
11370
|
-
});
|
|
11371
|
-
}
|
|
11372
|
-
}
|
|
11373
|
-
if (activeId < maxBinId) {
|
|
11374
|
-
const weights = toWeightSpotBalanced(activeId + 1, maxBinId);
|
|
11375
|
-
const amounts = toAmountAskSide(
|
|
11376
|
-
activeId,
|
|
11377
|
-
binStep,
|
|
11378
|
-
amountX,
|
|
11379
|
-
weights,
|
|
11380
|
-
mintX,
|
|
11381
|
-
clock
|
|
11382
|
-
);
|
|
11383
|
-
for (let bin of amounts) {
|
|
11384
|
-
amountsInBin.push({
|
|
11385
|
-
binId: bin.binId,
|
|
11386
|
-
amountX: bin.amount,
|
|
11387
|
-
amountY: new (0, _anchor.BN)(0)
|
|
11388
|
-
});
|
|
11389
|
-
}
|
|
11390
|
-
}
|
|
11391
|
-
} else {
|
|
11392
|
-
if (minBinId < activeId) {
|
|
11393
|
-
const weights = toWeightSpotBalanced(minBinId, activeId - 1);
|
|
11394
|
-
const amountsIntoBidSide = toAmountBidSide(
|
|
11395
|
-
activeId,
|
|
11396
|
-
amountY,
|
|
11397
|
-
weights,
|
|
11398
|
-
mintY,
|
|
11399
|
-
clock
|
|
11400
|
-
);
|
|
11401
|
-
for (let bin of amountsIntoBidSide) {
|
|
11402
|
-
amountsInBin.push({
|
|
11403
|
-
binId: bin.binId,
|
|
11404
|
-
amountX: new (0, _anchor.BN)(0),
|
|
11405
|
-
amountY: bin.amount
|
|
11406
|
-
});
|
|
11407
|
-
}
|
|
11408
|
-
}
|
|
11409
|
-
if (activeId <= maxBinId) {
|
|
11410
|
-
const weights = toWeightSpotBalanced(activeId, maxBinId);
|
|
11411
|
-
const amountsIntoAskSide = toAmountAskSide(
|
|
11412
|
-
activeId,
|
|
11413
|
-
binStep,
|
|
11414
|
-
amountX,
|
|
11415
|
-
weights,
|
|
11416
|
-
mintX,
|
|
11417
|
-
clock
|
|
11418
|
-
);
|
|
11419
|
-
for (let bin of amountsIntoAskSide) {
|
|
11420
|
-
amountsInBin.push({
|
|
11421
|
-
binId: bin.binId,
|
|
11422
|
-
amountX: bin.amount,
|
|
11423
|
-
amountY: new (0, _anchor.BN)(0)
|
|
11424
|
-
});
|
|
11425
|
-
}
|
|
11426
|
-
}
|
|
11427
|
-
}
|
|
11428
|
-
return amountsInBin;
|
|
11238
|
+
function getBinArrayKeysCoverage2(lowerBinId, upperBinId, lbPair, programId) {
|
|
11239
|
+
const binArrayIndexes = getBinArrayIndexesCoverage(lowerBinId, upperBinId);
|
|
11240
|
+
return binArrayIndexes.map((index) => {
|
|
11241
|
+
return deriveBinArray(lbPair, index, programId)[0];
|
|
11242
|
+
});
|
|
11243
|
+
}
|
|
11244
|
+
function getBinArrayAccountMetasCoverage(lowerBinId, upperBinId, lbPair, programId) {
|
|
11245
|
+
return getBinArrayKeysCoverage2(lowerBinId, upperBinId, lbPair, programId).map(
|
|
11246
|
+
(key) => {
|
|
11247
|
+
return {
|
|
11248
|
+
pubkey: key,
|
|
11249
|
+
isSigner: false,
|
|
11250
|
+
isWritable: true
|
|
11251
|
+
};
|
|
11429
11252
|
}
|
|
11430
|
-
|
|
11431
|
-
if (activeId < minBinId) {
|
|
11432
|
-
let weights = toWeightDescendingOrder(minBinId, maxBinId);
|
|
11433
|
-
return toAmountBothSide(
|
|
11434
|
-
activeId,
|
|
11435
|
-
binStep,
|
|
11436
|
-
amountX,
|
|
11437
|
-
amountY,
|
|
11438
|
-
amountXInActiveBin,
|
|
11439
|
-
amountYInActiveBin,
|
|
11440
|
-
weights,
|
|
11441
|
-
mintX,
|
|
11442
|
-
mintY,
|
|
11443
|
-
clock
|
|
11444
|
-
);
|
|
11445
|
-
}
|
|
11446
|
-
if (activeId > maxBinId) {
|
|
11447
|
-
const weights = toWeightAscendingOrder(minBinId, maxBinId);
|
|
11448
|
-
return toAmountBothSide(
|
|
11449
|
-
activeId,
|
|
11450
|
-
binStep,
|
|
11451
|
-
amountX,
|
|
11452
|
-
amountY,
|
|
11453
|
-
amountXInActiveBin,
|
|
11454
|
-
amountYInActiveBin,
|
|
11455
|
-
weights,
|
|
11456
|
-
mintX,
|
|
11457
|
-
mintY,
|
|
11458
|
-
clock
|
|
11459
|
-
);
|
|
11460
|
-
}
|
|
11461
|
-
const amountsInBin = [];
|
|
11462
|
-
if (!isSingleSideX) {
|
|
11463
|
-
if (minBinId <= activeId) {
|
|
11464
|
-
const weights = toWeightAscendingOrder(minBinId, activeId);
|
|
11465
|
-
const amounts = toAmountBidSide(
|
|
11466
|
-
activeId,
|
|
11467
|
-
amountY,
|
|
11468
|
-
weights,
|
|
11469
|
-
mintY,
|
|
11470
|
-
clock
|
|
11471
|
-
);
|
|
11472
|
-
for (let bin of amounts) {
|
|
11473
|
-
amountsInBin.push({
|
|
11474
|
-
binId: bin.binId,
|
|
11475
|
-
amountX: new (0, _anchor.BN)(0),
|
|
11476
|
-
amountY: bin.amount
|
|
11477
|
-
});
|
|
11478
|
-
}
|
|
11479
|
-
}
|
|
11480
|
-
if (activeId < maxBinId) {
|
|
11481
|
-
const weights = toWeightDescendingOrder(activeId + 1, maxBinId);
|
|
11482
|
-
const amounts = toAmountAskSide(
|
|
11483
|
-
activeId,
|
|
11484
|
-
binStep,
|
|
11485
|
-
amountX,
|
|
11486
|
-
weights,
|
|
11487
|
-
mintX,
|
|
11488
|
-
clock
|
|
11489
|
-
);
|
|
11490
|
-
for (let bin of amounts) {
|
|
11491
|
-
amountsInBin.push({
|
|
11492
|
-
binId: bin.binId,
|
|
11493
|
-
amountX: bin.amount,
|
|
11494
|
-
amountY: new (0, _anchor.BN)(0)
|
|
11495
|
-
});
|
|
11496
|
-
}
|
|
11497
|
-
}
|
|
11498
|
-
} else {
|
|
11499
|
-
if (minBinId < activeId) {
|
|
11500
|
-
const weights = toWeightAscendingOrder(minBinId, activeId - 1);
|
|
11501
|
-
const amountsIntoBidSide = toAmountBidSide(
|
|
11502
|
-
activeId,
|
|
11503
|
-
amountY,
|
|
11504
|
-
weights,
|
|
11505
|
-
mintY,
|
|
11506
|
-
clock
|
|
11507
|
-
);
|
|
11508
|
-
for (let bin of amountsIntoBidSide) {
|
|
11509
|
-
amountsInBin.push({
|
|
11510
|
-
binId: bin.binId,
|
|
11511
|
-
amountX: new (0, _anchor.BN)(0),
|
|
11512
|
-
amountY: bin.amount
|
|
11513
|
-
});
|
|
11514
|
-
}
|
|
11515
|
-
}
|
|
11516
|
-
if (activeId <= maxBinId) {
|
|
11517
|
-
const weights = toWeightDescendingOrder(activeId, maxBinId);
|
|
11518
|
-
const amountsIntoAskSide = toAmountAskSide(
|
|
11519
|
-
activeId,
|
|
11520
|
-
binStep,
|
|
11521
|
-
amountX,
|
|
11522
|
-
weights,
|
|
11523
|
-
mintX,
|
|
11524
|
-
clock
|
|
11525
|
-
);
|
|
11526
|
-
for (let bin of amountsIntoAskSide) {
|
|
11527
|
-
amountsInBin.push({
|
|
11528
|
-
binId: bin.binId,
|
|
11529
|
-
amountX: bin.amount,
|
|
11530
|
-
amountY: new (0, _anchor.BN)(0)
|
|
11531
|
-
});
|
|
11532
|
-
}
|
|
11533
|
-
}
|
|
11534
|
-
}
|
|
11535
|
-
return amountsInBin;
|
|
11536
|
-
}
|
|
11537
|
-
case 2 /* BidAsk */: {
|
|
11538
|
-
if (activeId < minBinId) {
|
|
11539
|
-
const weights = toWeightAscendingOrder(minBinId, maxBinId);
|
|
11540
|
-
return toAmountBothSide(
|
|
11541
|
-
activeId,
|
|
11542
|
-
binStep,
|
|
11543
|
-
amountX,
|
|
11544
|
-
amountY,
|
|
11545
|
-
amountXInActiveBin,
|
|
11546
|
-
amountYInActiveBin,
|
|
11547
|
-
weights,
|
|
11548
|
-
mintX,
|
|
11549
|
-
mintY,
|
|
11550
|
-
clock
|
|
11551
|
-
);
|
|
11552
|
-
}
|
|
11553
|
-
if (activeId > maxBinId) {
|
|
11554
|
-
const weights = toWeightDescendingOrder(minBinId, maxBinId);
|
|
11555
|
-
return toAmountBothSide(
|
|
11556
|
-
activeId,
|
|
11557
|
-
binStep,
|
|
11558
|
-
amountX,
|
|
11559
|
-
amountY,
|
|
11560
|
-
amountXInActiveBin,
|
|
11561
|
-
amountYInActiveBin,
|
|
11562
|
-
weights,
|
|
11563
|
-
mintX,
|
|
11564
|
-
mintY,
|
|
11565
|
-
clock
|
|
11566
|
-
);
|
|
11567
|
-
}
|
|
11568
|
-
const amountsInBin = [];
|
|
11569
|
-
if (!isSingleSideX) {
|
|
11570
|
-
if (minBinId <= activeId) {
|
|
11571
|
-
const weights = toWeightDescendingOrder(minBinId, activeId);
|
|
11572
|
-
const amounts = toAmountBidSide(
|
|
11573
|
-
activeId,
|
|
11574
|
-
amountY,
|
|
11575
|
-
weights,
|
|
11576
|
-
mintY,
|
|
11577
|
-
clock
|
|
11578
|
-
);
|
|
11579
|
-
for (let bin of amounts) {
|
|
11580
|
-
amountsInBin.push({
|
|
11581
|
-
binId: bin.binId,
|
|
11582
|
-
amountX: new (0, _anchor.BN)(0),
|
|
11583
|
-
amountY: bin.amount
|
|
11584
|
-
});
|
|
11585
|
-
}
|
|
11586
|
-
}
|
|
11587
|
-
if (activeId < maxBinId) {
|
|
11588
|
-
const weights = toWeightAscendingOrder(activeId + 1, maxBinId);
|
|
11589
|
-
const amounts = toAmountAskSide(
|
|
11590
|
-
activeId,
|
|
11591
|
-
binStep,
|
|
11592
|
-
amountX,
|
|
11593
|
-
weights,
|
|
11594
|
-
mintX,
|
|
11595
|
-
clock
|
|
11596
|
-
);
|
|
11597
|
-
for (let bin of amounts) {
|
|
11598
|
-
amountsInBin.push({
|
|
11599
|
-
binId: bin.binId,
|
|
11600
|
-
amountX: bin.amount,
|
|
11601
|
-
amountY: new (0, _anchor.BN)(0)
|
|
11602
|
-
});
|
|
11603
|
-
}
|
|
11604
|
-
}
|
|
11605
|
-
} else {
|
|
11606
|
-
if (minBinId < activeId) {
|
|
11607
|
-
const weights = toWeightDescendingOrder(minBinId, activeId - 1);
|
|
11608
|
-
const amountsIntoBidSide = toAmountBidSide(
|
|
11609
|
-
activeId,
|
|
11610
|
-
amountY,
|
|
11611
|
-
weights,
|
|
11612
|
-
mintY,
|
|
11613
|
-
clock
|
|
11614
|
-
);
|
|
11615
|
-
for (let bin of amountsIntoBidSide) {
|
|
11616
|
-
amountsInBin.push({
|
|
11617
|
-
binId: bin.binId,
|
|
11618
|
-
amountX: new (0, _anchor.BN)(0),
|
|
11619
|
-
amountY: bin.amount
|
|
11620
|
-
});
|
|
11621
|
-
}
|
|
11622
|
-
}
|
|
11623
|
-
if (activeId <= maxBinId) {
|
|
11624
|
-
const weights = toWeightAscendingOrder(activeId, maxBinId);
|
|
11625
|
-
const amountsIntoAskSide = toAmountAskSide(
|
|
11626
|
-
activeId,
|
|
11627
|
-
binStep,
|
|
11628
|
-
amountX,
|
|
11629
|
-
weights,
|
|
11630
|
-
mintX,
|
|
11631
|
-
clock
|
|
11632
|
-
);
|
|
11633
|
-
for (let bin of amountsIntoAskSide) {
|
|
11634
|
-
amountsInBin.push({
|
|
11635
|
-
binId: bin.binId,
|
|
11636
|
-
amountX: bin.amount,
|
|
11637
|
-
amountY: new (0, _anchor.BN)(0)
|
|
11638
|
-
});
|
|
11639
|
-
}
|
|
11640
|
-
}
|
|
11641
|
-
}
|
|
11642
|
-
return amountsInBin;
|
|
11643
|
-
}
|
|
11644
|
-
}
|
|
11253
|
+
);
|
|
11645
11254
|
}
|
|
11646
|
-
function
|
|
11647
|
-
|
|
11648
|
-
|
|
11649
|
-
|
|
11650
|
-
|
|
11651
|
-
|
|
11652
|
-
|
|
11653
|
-
|
|
11654
|
-
amountXInActiveBin,
|
|
11655
|
-
amountYInActiveBin,
|
|
11656
|
-
weights
|
|
11657
|
-
);
|
|
11658
|
-
}
|
|
11659
|
-
case 1 /* Curve */: {
|
|
11660
|
-
let weights = toWeightCurve(minBinId, maxBinId, activeId);
|
|
11661
|
-
return autoFillYByWeight(
|
|
11662
|
-
activeId,
|
|
11663
|
-
binStep,
|
|
11664
|
-
amountX,
|
|
11665
|
-
amountXInActiveBin,
|
|
11666
|
-
amountYInActiveBin,
|
|
11667
|
-
weights
|
|
11668
|
-
);
|
|
11669
|
-
}
|
|
11670
|
-
case 2 /* BidAsk */: {
|
|
11671
|
-
let weights = toWeightBidAsk(minBinId, maxBinId, activeId);
|
|
11672
|
-
return autoFillYByWeight(
|
|
11673
|
-
activeId,
|
|
11674
|
-
binStep,
|
|
11675
|
-
amountX,
|
|
11676
|
-
amountXInActiveBin,
|
|
11677
|
-
amountYInActiveBin,
|
|
11678
|
-
weights
|
|
11679
|
-
);
|
|
11680
|
-
}
|
|
11681
|
-
}
|
|
11255
|
+
function getPositionLowerUpperBinIdWithLiquidity(position) {
|
|
11256
|
+
const binWithLiquidity = position.positionBinData.filter(
|
|
11257
|
+
(b) => !new (0, _bnjs2.default)(b.binLiquidity).isZero() || !new (0, _bnjs2.default)(b.positionFeeXAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionFeeYAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[0].toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[1].toString()).isZero()
|
|
11258
|
+
);
|
|
11259
|
+
return binWithLiquidity.length > 0 ? {
|
|
11260
|
+
lowerBinId: new (0, _bnjs2.default)(binWithLiquidity[0].binId),
|
|
11261
|
+
upperBinId: new (0, _bnjs2.default)(binWithLiquidity[binWithLiquidity.length - 1].binId)
|
|
11262
|
+
} : null;
|
|
11682
11263
|
}
|
|
11683
|
-
function
|
|
11684
|
-
|
|
11685
|
-
|
|
11686
|
-
|
|
11687
|
-
|
|
11688
|
-
|
|
11689
|
-
|
|
11690
|
-
|
|
11691
|
-
|
|
11692
|
-
|
|
11693
|
-
|
|
11694
|
-
)
|
|
11695
|
-
|
|
11696
|
-
|
|
11697
|
-
|
|
11698
|
-
|
|
11699
|
-
|
|
11700
|
-
|
|
11701
|
-
|
|
11702
|
-
amountXInActiveBin,
|
|
11703
|
-
amountYInActiveBin,
|
|
11704
|
-
weights
|
|
11705
|
-
);
|
|
11706
|
-
}
|
|
11707
|
-
case 2 /* BidAsk */: {
|
|
11708
|
-
let weights = toWeightBidAsk(minBinId, maxBinId, activeId);
|
|
11709
|
-
return autoFillXByWeight(
|
|
11710
|
-
activeId,
|
|
11711
|
-
binStep,
|
|
11712
|
-
amountY,
|
|
11713
|
-
amountXInActiveBin,
|
|
11714
|
-
amountYInActiveBin,
|
|
11715
|
-
weights
|
|
11716
|
-
);
|
|
11717
|
-
}
|
|
11264
|
+
function isPositionNoFee(position) {
|
|
11265
|
+
return position.feeX.isZero() && position.feeY.isZero();
|
|
11266
|
+
}
|
|
11267
|
+
function isPositionNoReward(position) {
|
|
11268
|
+
return position.rewardOne.isZero() && position.rewardTwo.isZero();
|
|
11269
|
+
}
|
|
11270
|
+
function chunkBinRange(minBinId, maxBinId) {
|
|
11271
|
+
const chunkedBinRange = [];
|
|
11272
|
+
let startBinId = minBinId;
|
|
11273
|
+
while (startBinId <= maxBinId) {
|
|
11274
|
+
const endBinId = Math.min(
|
|
11275
|
+
startBinId + DEFAULT_BIN_PER_POSITION.toNumber() - 1,
|
|
11276
|
+
maxBinId
|
|
11277
|
+
);
|
|
11278
|
+
chunkedBinRange.push({
|
|
11279
|
+
lowerBinId: startBinId,
|
|
11280
|
+
upperBinId: endBinId
|
|
11281
|
+
});
|
|
11282
|
+
startBinId += DEFAULT_BIN_PER_POSITION.toNumber();
|
|
11718
11283
|
}
|
|
11284
|
+
return chunkedBinRange;
|
|
11719
11285
|
}
|
|
11720
|
-
function
|
|
11721
|
-
|
|
11722
|
-
|
|
11723
|
-
|
|
11724
|
-
|
|
11725
|
-
}
|
|
11726
|
-
|
|
11727
|
-
|
|
11728
|
-
|
|
11729
|
-
|
|
11730
|
-
|
|
11731
|
-
|
|
11732
|
-
|
|
11733
|
-
|
|
11734
|
-
|
|
11735
|
-
parameteres
|
|
11736
|
-
};
|
|
11737
|
-
}
|
|
11738
|
-
case 1 /* Curve */: {
|
|
11739
|
-
return {
|
|
11740
|
-
minBinId,
|
|
11741
|
-
maxBinId,
|
|
11742
|
-
strategyType: { curveImBalanced: {} },
|
|
11743
|
-
parameteres
|
|
11744
|
-
};
|
|
11745
|
-
}
|
|
11746
|
-
case 2 /* BidAsk */: {
|
|
11747
|
-
return {
|
|
11748
|
-
minBinId,
|
|
11749
|
-
maxBinId,
|
|
11750
|
-
strategyType: { bidAskImBalanced: {} },
|
|
11751
|
-
parameteres
|
|
11752
|
-
};
|
|
11753
|
-
}
|
|
11286
|
+
async function getPositionExpandRentExemption(currentMinBinId, currentMaxBinId, connection, binCountToExpand) {
|
|
11287
|
+
const currentPositionWidth = currentMaxBinId.sub(currentMinBinId).addn(1);
|
|
11288
|
+
const positionWidthAfterExpand = currentPositionWidth.add(binCountToExpand);
|
|
11289
|
+
if (positionWidthAfterExpand.lte(DEFAULT_BIN_PER_POSITION)) {
|
|
11290
|
+
return 0;
|
|
11291
|
+
} else {
|
|
11292
|
+
const binCountInExpandedBytes = positionWidthAfterExpand.sub(
|
|
11293
|
+
DEFAULT_BIN_PER_POSITION
|
|
11294
|
+
);
|
|
11295
|
+
const expandSize = binCountInExpandedBytes.toNumber() * POSITION_BIN_DATA_SIZE;
|
|
11296
|
+
const [minimumLamports, rentExemptionLamports] = await Promise.all([
|
|
11297
|
+
connection.getMinimumBalanceForRentExemption(0),
|
|
11298
|
+
connection.getMinimumBalanceForRentExemption(expandSize)
|
|
11299
|
+
]);
|
|
11300
|
+
return rentExemptionLamports - minimumLamports;
|
|
11754
11301
|
}
|
|
11755
11302
|
}
|
|
11756
|
-
|
|
11757
|
-
|
|
11758
|
-
|
|
11759
|
-
|
|
11760
|
-
async function getTokensMintFromPoolAddress(connection, poolAddress, opt) {
|
|
11761
|
-
const program = createProgram(connection, opt);
|
|
11762
|
-
const poolAccount = await program.account.lbPair.fetchNullable(
|
|
11763
|
-
new (0, _web3js.PublicKey)(poolAddress)
|
|
11764
|
-
);
|
|
11765
|
-
if (!poolAccount)
|
|
11766
|
-
throw new Error("Pool account not found");
|
|
11767
|
-
return {
|
|
11768
|
-
tokenXMint: poolAccount.tokenXMint,
|
|
11769
|
-
tokenYMint: poolAccount.tokenYMint
|
|
11770
|
-
};
|
|
11303
|
+
function getExtendedPositionBinCount(minBinId, maxBinId) {
|
|
11304
|
+
const width = maxBinId.sub(minBinId).addn(1);
|
|
11305
|
+
const extended = width.sub(DEFAULT_BIN_PER_POSITION);
|
|
11306
|
+
return extended.lte(new (0, _bnjs2.default)(0)) ? new (0, _bnjs2.default)(0) : extended;
|
|
11771
11307
|
}
|
|
11772
|
-
function
|
|
11773
|
-
const
|
|
11774
|
-
|
|
11775
|
-
|
|
11776
|
-
|
|
11777
|
-
|
|
11778
|
-
|
|
11779
|
-
|
|
11308
|
+
function decodeExtendedPosition(base, program, bytes) {
|
|
11309
|
+
const width = base.upperBinId - base.lowerBinId + 1;
|
|
11310
|
+
const extendedWidth = width - DEFAULT_BIN_PER_POSITION.toNumber();
|
|
11311
|
+
const extendedPosition = [];
|
|
11312
|
+
for (let i = 0; i < extendedWidth; i++) {
|
|
11313
|
+
const offset = i * POSITION_BIN_DATA_SIZE;
|
|
11314
|
+
const data = bytes.subarray(offset, offset + POSITION_BIN_DATA_SIZE);
|
|
11315
|
+
const decodedPositionBinData = program.coder.types.decode(
|
|
11316
|
+
// TODO: Find a type safe way
|
|
11317
|
+
"positionBinData",
|
|
11318
|
+
data
|
|
11319
|
+
);
|
|
11320
|
+
extendedPosition.push(decodedPositionBinData);
|
|
11321
|
+
}
|
|
11322
|
+
return extendedPosition;
|
|
11780
11323
|
}
|
|
11781
11324
|
|
|
11782
11325
|
// src/dlmm/helpers/rebalance/rebalancePosition.ts
|
|
11783
11326
|
|
|
11784
11327
|
|
|
11785
11328
|
|
|
11786
|
-
|
|
11787
|
-
|
|
11788
|
-
|
|
11789
|
-
|
|
11790
|
-
|
|
11791
|
-
|
|
11792
|
-
|
|
11793
|
-
|
|
11794
|
-
|
|
11795
|
-
|
|
11796
|
-
|
|
11797
|
-
bitFlag |= 4;
|
|
11798
|
-
deltaX = deltaX.neg();
|
|
11799
|
-
}
|
|
11800
|
-
if (deltaY.isNeg()) {
|
|
11801
|
-
bitFlag |= 8;
|
|
11802
|
-
deltaY = deltaY.neg();
|
|
11329
|
+
|
|
11330
|
+
// src/dlmm/helpers/fee.ts
|
|
11331
|
+
|
|
11332
|
+
function getBaseFee(binStep, sParameter) {
|
|
11333
|
+
return new (0, _anchor.BN)(sParameter.baseFactor).mul(new (0, _anchor.BN)(binStep)).mul(new (0, _anchor.BN)(10)).mul(new (0, _anchor.BN)(10).pow(new (0, _anchor.BN)(sParameter.baseFeePowerFactor)));
|
|
11334
|
+
}
|
|
11335
|
+
function getVariableFee(binStep, sParameter, vParameter) {
|
|
11336
|
+
if (sParameter.variableFeeControl > 0) {
|
|
11337
|
+
const square_vfa_bin = new (0, _anchor.BN)(vParameter.volatilityAccumulator).mul(new (0, _anchor.BN)(binStep)).pow(new (0, _anchor.BN)(2));
|
|
11338
|
+
const v_fee = new (0, _anchor.BN)(sParameter.variableFeeControl).mul(square_vfa_bin);
|
|
11339
|
+
return v_fee.add(new (0, _anchor.BN)(99999999999)).div(new (0, _anchor.BN)(1e11));
|
|
11803
11340
|
}
|
|
11804
|
-
return
|
|
11805
|
-
bitFlag,
|
|
11806
|
-
x0,
|
|
11807
|
-
y0,
|
|
11808
|
-
deltaX,
|
|
11809
|
-
deltaY
|
|
11810
|
-
};
|
|
11341
|
+
return new (0, _anchor.BN)(0);
|
|
11811
11342
|
}
|
|
11812
|
-
function
|
|
11813
|
-
|
|
11814
|
-
(
|
|
11815
|
-
binId,
|
|
11816
|
-
price,
|
|
11817
|
-
pricePerToken,
|
|
11818
|
-
positionXAmount,
|
|
11819
|
-
positionYAmount,
|
|
11820
|
-
positionFeeXAmount,
|
|
11821
|
-
positionFeeYAmount,
|
|
11822
|
-
positionRewardAmount
|
|
11823
|
-
}) => {
|
|
11824
|
-
return {
|
|
11825
|
-
binId,
|
|
11826
|
-
price,
|
|
11827
|
-
pricePerToken,
|
|
11828
|
-
amountX: new (0, _bnjs2.default)(positionXAmount),
|
|
11829
|
-
amountY: new (0, _bnjs2.default)(positionYAmount),
|
|
11830
|
-
claimableRewardAmount: positionRewardAmount.map(
|
|
11831
|
-
(amount) => new (0, _bnjs2.default)(amount)
|
|
11832
|
-
),
|
|
11833
|
-
claimableFeeXAmount: new (0, _bnjs2.default)(positionFeeXAmount),
|
|
11834
|
-
claimableFeeYAmount: new (0, _bnjs2.default)(positionFeeYAmount)
|
|
11835
|
-
};
|
|
11836
|
-
}
|
|
11343
|
+
function getTotalFee(binStep, sParameter, vParameter) {
|
|
11344
|
+
const totalFee = getBaseFee(binStep, sParameter).add(
|
|
11345
|
+
getVariableFee(binStep, sParameter, vParameter)
|
|
11837
11346
|
);
|
|
11347
|
+
return totalFee.gt(MAX_FEE_RATE) ? MAX_FEE_RATE : totalFee;
|
|
11838
11348
|
}
|
|
11839
|
-
function
|
|
11840
|
-
const
|
|
11841
|
-
|
|
11842
|
-
|
|
11843
|
-
const maxBinId = activeId.add(maxDeltaId);
|
|
11844
|
-
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
11845
|
-
uniqueBinId.add(binId);
|
|
11846
|
-
}
|
|
11847
|
-
}
|
|
11848
|
-
const binIds = Array.from(uniqueBinId);
|
|
11849
|
-
binIds.sort((a, b) => a - b);
|
|
11850
|
-
return binIds;
|
|
11349
|
+
function computeFee(binStep, sParameter, vParameter, inAmount) {
|
|
11350
|
+
const totalFee = getTotalFee(binStep, sParameter, vParameter);
|
|
11351
|
+
const denominator = FEE_PRECISION.sub(totalFee);
|
|
11352
|
+
return inAmount.mul(totalFee).add(denominator).sub(new (0, _anchor.BN)(1)).div(denominator);
|
|
11851
11353
|
}
|
|
11852
|
-
function
|
|
11853
|
-
|
|
11854
|
-
|
|
11855
|
-
for (const binData of rebalancePositionBinData) {
|
|
11856
|
-
if (binData.amountX.isZero() && binData.amountY.isZero() && binData.claimableFeeXAmount.isZero() && binData.claimableFeeYAmount.isZero() && binData.claimableRewardAmount.every((amount) => amount.isZero())) {
|
|
11857
|
-
continue;
|
|
11858
|
-
}
|
|
11859
|
-
if (minBinId == null || binData.binId < minBinId) {
|
|
11860
|
-
minBinId = binData.binId;
|
|
11861
|
-
}
|
|
11862
|
-
if (maxBinId == null || binData.binId > maxBinId) {
|
|
11863
|
-
maxBinId = binData.binId;
|
|
11864
|
-
}
|
|
11865
|
-
}
|
|
11866
|
-
return [minBinId, maxBinId];
|
|
11354
|
+
function computeFeeFromAmount(binStep, sParameter, vParameter, inAmountWithFees) {
|
|
11355
|
+
const totalFee = getTotalFee(binStep, sParameter, vParameter);
|
|
11356
|
+
return inAmountWithFees.mul(totalFee).add(FEE_PRECISION.sub(new (0, _anchor.BN)(1))).div(FEE_PRECISION);
|
|
11867
11357
|
}
|
|
11868
|
-
function
|
|
11869
|
-
|
|
11870
|
-
return maxDeltaId.lt(new (0, _bnjs2.default)(0));
|
|
11871
|
-
}
|
|
11872
|
-
return maxDeltaId.lte(new (0, _bnjs2.default)(0));
|
|
11358
|
+
function computeProtocolFee(feeAmount, sParameter) {
|
|
11359
|
+
return feeAmount.mul(new (0, _anchor.BN)(sParameter.protocolShare)).div(new (0, _anchor.BN)(BASIS_POINT_MAX));
|
|
11873
11360
|
}
|
|
11874
|
-
function
|
|
11875
|
-
if (
|
|
11876
|
-
return
|
|
11361
|
+
function swapExactOutQuoteAtBin(bin, binStep, sParameter, vParameter, outAmount, swapForY) {
|
|
11362
|
+
if (swapForY && bin.amountY.isZero()) {
|
|
11363
|
+
return {
|
|
11364
|
+
amountIn: new (0, _anchor.BN)(0),
|
|
11365
|
+
amountOut: new (0, _anchor.BN)(0),
|
|
11366
|
+
fee: new (0, _anchor.BN)(0),
|
|
11367
|
+
protocolFee: new (0, _anchor.BN)(0)
|
|
11368
|
+
};
|
|
11877
11369
|
}
|
|
11878
|
-
|
|
11879
|
-
|
|
11880
|
-
|
|
11370
|
+
if (!swapForY && bin.amountX.isZero()) {
|
|
11371
|
+
return {
|
|
11372
|
+
amountIn: new (0, _anchor.BN)(0),
|
|
11373
|
+
amountOut: new (0, _anchor.BN)(0),
|
|
11374
|
+
fee: new (0, _anchor.BN)(0),
|
|
11375
|
+
protocolFee: new (0, _anchor.BN)(0)
|
|
11376
|
+
};
|
|
11377
|
+
}
|
|
11378
|
+
let maxAmountOut;
|
|
11379
|
+
let maxAmountIn;
|
|
11380
|
+
if (swapForY) {
|
|
11381
|
+
maxAmountOut = bin.amountY;
|
|
11382
|
+
maxAmountIn = shlDiv(bin.amountY, bin.price, SCALE_OFFSET, 0 /* Up */);
|
|
11383
|
+
} else {
|
|
11384
|
+
maxAmountOut = bin.amountX;
|
|
11385
|
+
maxAmountIn = mulShr(bin.amountX, bin.price, SCALE_OFFSET, 0 /* Up */);
|
|
11386
|
+
}
|
|
11387
|
+
if (outAmount.gte(maxAmountOut)) {
|
|
11388
|
+
const maxFee = computeFee(binStep, sParameter, vParameter, maxAmountIn);
|
|
11389
|
+
const protocolFee = computeProtocolFee(maxFee, sParameter);
|
|
11390
|
+
return {
|
|
11391
|
+
amountIn: maxAmountIn,
|
|
11392
|
+
amountOut: maxAmountOut,
|
|
11393
|
+
fee: maxFee,
|
|
11394
|
+
protocolFee
|
|
11395
|
+
};
|
|
11396
|
+
} else {
|
|
11397
|
+
const amountIn = getAmountIn(outAmount, bin.price, swapForY);
|
|
11398
|
+
const fee = computeFee(binStep, sParameter, vParameter, amountIn);
|
|
11399
|
+
const protocolFee = computeProtocolFee(fee, sParameter);
|
|
11400
|
+
return {
|
|
11401
|
+
amountIn,
|
|
11402
|
+
amountOut: outAmount,
|
|
11403
|
+
fee,
|
|
11404
|
+
protocolFee
|
|
11405
|
+
};
|
|
11406
|
+
}
|
|
11407
|
+
}
|
|
11408
|
+
function swapExactInQuoteAtBin(bin, binStep, sParameter, vParameter, inAmount, swapForY) {
|
|
11409
|
+
if (swapForY && bin.amountY.isZero()) {
|
|
11410
|
+
return {
|
|
11411
|
+
amountIn: new (0, _anchor.BN)(0),
|
|
11412
|
+
amountOut: new (0, _anchor.BN)(0),
|
|
11413
|
+
fee: new (0, _anchor.BN)(0),
|
|
11414
|
+
protocolFee: new (0, _anchor.BN)(0)
|
|
11415
|
+
};
|
|
11416
|
+
}
|
|
11417
|
+
if (!swapForY && bin.amountX.isZero()) {
|
|
11418
|
+
return {
|
|
11419
|
+
amountIn: new (0, _anchor.BN)(0),
|
|
11420
|
+
amountOut: new (0, _anchor.BN)(0),
|
|
11421
|
+
fee: new (0, _anchor.BN)(0),
|
|
11422
|
+
protocolFee: new (0, _anchor.BN)(0)
|
|
11423
|
+
};
|
|
11424
|
+
}
|
|
11425
|
+
let maxAmountOut;
|
|
11426
|
+
let maxAmountIn;
|
|
11427
|
+
if (swapForY) {
|
|
11428
|
+
maxAmountOut = bin.amountY;
|
|
11429
|
+
maxAmountIn = shlDiv(bin.amountY, bin.price, SCALE_OFFSET, 0 /* Up */);
|
|
11430
|
+
} else {
|
|
11431
|
+
maxAmountOut = bin.amountX;
|
|
11432
|
+
maxAmountIn = mulShr(bin.amountX, bin.price, SCALE_OFFSET, 0 /* Up */);
|
|
11433
|
+
}
|
|
11434
|
+
const maxFee = computeFee(binStep, sParameter, vParameter, maxAmountIn);
|
|
11435
|
+
maxAmountIn = maxAmountIn.add(maxFee);
|
|
11436
|
+
let amountInWithFees;
|
|
11437
|
+
let amountOut;
|
|
11438
|
+
let fee;
|
|
11439
|
+
let protocolFee;
|
|
11440
|
+
if (inAmount.gt(maxAmountIn)) {
|
|
11441
|
+
amountInWithFees = maxAmountIn;
|
|
11442
|
+
amountOut = maxAmountOut;
|
|
11443
|
+
fee = maxFee;
|
|
11444
|
+
protocolFee = computeProtocolFee(maxFee, sParameter);
|
|
11445
|
+
} else {
|
|
11446
|
+
fee = computeFeeFromAmount(binStep, sParameter, vParameter, inAmount);
|
|
11447
|
+
const amountInAfterFee = inAmount.sub(fee);
|
|
11448
|
+
const computedOutAmount = getOutAmount(bin, amountInAfterFee, swapForY);
|
|
11449
|
+
amountOut = computedOutAmount.gt(maxAmountOut) ? maxAmountOut : computedOutAmount;
|
|
11450
|
+
protocolFee = computeProtocolFee(fee, sParameter);
|
|
11451
|
+
amountInWithFees = inAmount;
|
|
11452
|
+
}
|
|
11453
|
+
return {
|
|
11454
|
+
amountIn: amountInWithFees,
|
|
11455
|
+
amountOut,
|
|
11456
|
+
fee,
|
|
11457
|
+
protocolFee
|
|
11458
|
+
};
|
|
11459
|
+
}
|
|
11460
|
+
function getAmountIn(amountOut, price, swapForY) {
|
|
11461
|
+
if (swapForY) {
|
|
11462
|
+
return shlDiv(amountOut, price, SCALE_OFFSET, 0 /* Up */);
|
|
11463
|
+
} else {
|
|
11464
|
+
return mulShr(amountOut, price, SCALE_OFFSET, 0 /* Up */);
|
|
11465
|
+
}
|
|
11466
|
+
}
|
|
11467
|
+
|
|
11468
|
+
// src/dlmm/helpers/rebalance/rebalancePosition.ts
|
|
11469
|
+
function buildBitFlagAndNegateStrategyParameters(x0, y0, deltaX, deltaY) {
|
|
11470
|
+
let bitFlag = 0;
|
|
11471
|
+
if (x0.isNeg()) {
|
|
11472
|
+
bitFlag |= 1;
|
|
11473
|
+
x0 = x0.neg();
|
|
11474
|
+
}
|
|
11475
|
+
if (y0.isNeg()) {
|
|
11476
|
+
bitFlag |= 2;
|
|
11477
|
+
y0 = y0.neg();
|
|
11478
|
+
}
|
|
11479
|
+
if (deltaX.isNeg()) {
|
|
11480
|
+
bitFlag |= 4;
|
|
11481
|
+
deltaX = deltaX.neg();
|
|
11482
|
+
}
|
|
11483
|
+
if (deltaY.isNeg()) {
|
|
11484
|
+
bitFlag |= 8;
|
|
11485
|
+
deltaY = deltaY.neg();
|
|
11486
|
+
}
|
|
11487
|
+
return {
|
|
11488
|
+
bitFlag,
|
|
11489
|
+
x0,
|
|
11490
|
+
y0,
|
|
11491
|
+
deltaX,
|
|
11492
|
+
deltaY
|
|
11493
|
+
};
|
|
11494
|
+
}
|
|
11495
|
+
function toRebalancePositionBinData(positionData) {
|
|
11496
|
+
return positionData.positionBinData.map(
|
|
11497
|
+
({
|
|
11498
|
+
binId,
|
|
11499
|
+
price,
|
|
11500
|
+
pricePerToken,
|
|
11501
|
+
positionXAmount,
|
|
11502
|
+
positionYAmount,
|
|
11503
|
+
positionFeeXAmount,
|
|
11504
|
+
positionFeeYAmount,
|
|
11505
|
+
positionRewardAmount
|
|
11506
|
+
}) => {
|
|
11507
|
+
return {
|
|
11508
|
+
binId,
|
|
11509
|
+
price,
|
|
11510
|
+
pricePerToken,
|
|
11511
|
+
amountX: new (0, _bnjs2.default)(positionXAmount),
|
|
11512
|
+
amountY: new (0, _bnjs2.default)(positionYAmount),
|
|
11513
|
+
claimableRewardAmount: positionRewardAmount.map(
|
|
11514
|
+
(amount) => new (0, _bnjs2.default)(amount)
|
|
11515
|
+
),
|
|
11516
|
+
claimableFeeXAmount: new (0, _bnjs2.default)(positionFeeXAmount),
|
|
11517
|
+
claimableFeeYAmount: new (0, _bnjs2.default)(positionFeeYAmount)
|
|
11518
|
+
};
|
|
11519
|
+
}
|
|
11520
|
+
);
|
|
11521
|
+
}
|
|
11522
|
+
function getDepositBinIds(activeId, deposits) {
|
|
11523
|
+
const uniqueBinId = /* @__PURE__ */ new Set();
|
|
11524
|
+
for (const { minDeltaId, maxDeltaId } of deposits) {
|
|
11525
|
+
const minBinId = activeId.add(minDeltaId);
|
|
11526
|
+
const maxBinId = activeId.add(maxDeltaId);
|
|
11527
|
+
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
11528
|
+
uniqueBinId.add(binId);
|
|
11529
|
+
}
|
|
11530
|
+
}
|
|
11531
|
+
const binIds = Array.from(uniqueBinId);
|
|
11532
|
+
binIds.sort((a, b) => a - b);
|
|
11533
|
+
return binIds;
|
|
11534
|
+
}
|
|
11535
|
+
function findMinMaxBinIdWithLiquidity(rebalancePositionBinData) {
|
|
11536
|
+
let minBinId = null;
|
|
11537
|
+
let maxBinId = null;
|
|
11538
|
+
for (const binData of rebalancePositionBinData) {
|
|
11539
|
+
if (binData.amountX.isZero() && binData.amountY.isZero() && binData.claimableFeeXAmount.isZero() && binData.claimableFeeYAmount.isZero() && binData.claimableRewardAmount.every((amount) => amount.isZero())) {
|
|
11540
|
+
continue;
|
|
11541
|
+
}
|
|
11542
|
+
if (minBinId == null || binData.binId < minBinId) {
|
|
11543
|
+
minBinId = binData.binId;
|
|
11544
|
+
}
|
|
11545
|
+
if (maxBinId == null || binData.binId > maxBinId) {
|
|
11546
|
+
maxBinId = binData.binId;
|
|
11547
|
+
}
|
|
11548
|
+
}
|
|
11549
|
+
return [minBinId, maxBinId];
|
|
11550
|
+
}
|
|
11551
|
+
function onlyDepositToBidSide(maxDeltaId, favorXInActiveBin) {
|
|
11552
|
+
if (favorXInActiveBin) {
|
|
11553
|
+
return maxDeltaId.lt(new (0, _bnjs2.default)(0));
|
|
11554
|
+
}
|
|
11555
|
+
return maxDeltaId.lte(new (0, _bnjs2.default)(0));
|
|
11556
|
+
}
|
|
11557
|
+
function onlyDepositToAskSide(minDeltaId, favorXInActiveBin) {
|
|
11558
|
+
if (favorXInActiveBin) {
|
|
11559
|
+
return minDeltaId.gte(new (0, _bnjs2.default)(0));
|
|
11560
|
+
}
|
|
11561
|
+
return minDeltaId.gt(new (0, _bnjs2.default)(0));
|
|
11562
|
+
}
|
|
11563
|
+
function getAmountInBinsBidSide(activeId, minDeltaId, maxDeltaId, deltaY, y0) {
|
|
11881
11564
|
const amountInBins = [];
|
|
11882
11565
|
const minBinId = activeId.add(minDeltaId);
|
|
11883
11566
|
const maxBinId = activeId.add(maxDeltaId);
|
|
@@ -13026,161 +12709,702 @@ function getAutoFillAmountByRebalancedPosition(rebalancePosition, strategyType)
|
|
|
13026
12709
|
activeIdIndex = idx;
|
|
13027
12710
|
}
|
|
13028
12711
|
}
|
|
13029
|
-
if (liquidityInAskSide.gt(liquidityInBidSide)) {
|
|
13030
|
-
const minBinId = rebalancePosition.rebalancePositionBinData[0].binId;
|
|
13031
|
-
let maxBinId;
|
|
13032
|
-
if (activeIdIndex == -1) {
|
|
13033
|
-
maxBinId = rebalancePosition.rebalancePositionBinData[rebalancePosition.rebalancePositionBinData.length - 1].binId;
|
|
13034
|
-
} else {
|
|
13035
|
-
maxBinId = rebalancePosition.rebalancePositionBinData[favorXInActiveBin ? activeIdIndex - 1 : activeIdIndex].binId;
|
|
12712
|
+
if (liquidityInAskSide.gt(liquidityInBidSide)) {
|
|
12713
|
+
const minBinId = rebalancePosition.rebalancePositionBinData[0].binId;
|
|
12714
|
+
let maxBinId;
|
|
12715
|
+
if (activeIdIndex == -1) {
|
|
12716
|
+
maxBinId = rebalancePosition.rebalancePositionBinData[rebalancePosition.rebalancePositionBinData.length - 1].binId;
|
|
12717
|
+
} else {
|
|
12718
|
+
maxBinId = rebalancePosition.rebalancePositionBinData[favorXInActiveBin ? activeIdIndex - 1 : activeIdIndex].binId;
|
|
12719
|
+
}
|
|
12720
|
+
const minDeltaId = minBinId - lbPair.activeId;
|
|
12721
|
+
const maxDeltaId = maxBinId - lbPair.activeId;
|
|
12722
|
+
const { amountY } = builder.suggestBalancedYParametersFromX(
|
|
12723
|
+
new (0, _bnjs2.default)(lbPair.activeId),
|
|
12724
|
+
new (0, _bnjs2.default)(lbPair.binStep),
|
|
12725
|
+
favorXInActiveBin,
|
|
12726
|
+
new (0, _bnjs2.default)(minDeltaId),
|
|
12727
|
+
new (0, _bnjs2.default)(maxDeltaId),
|
|
12728
|
+
liquidityInAskSide
|
|
12729
|
+
);
|
|
12730
|
+
const [_, positionAmountY] = rebalancePosition.totalAmounts();
|
|
12731
|
+
return {
|
|
12732
|
+
amount: _bnjs2.default.max(amountY.sub(positionAmountY), new (0, _bnjs2.default)(0)),
|
|
12733
|
+
isBidSide: true
|
|
12734
|
+
};
|
|
12735
|
+
} else if (liquidityInAskSide.lt(liquidityInBidSide)) {
|
|
12736
|
+
const maxBinId = rebalancePosition.rebalancePositionBinData[rebalancePosition.rebalancePositionBinData.length - 1].binId;
|
|
12737
|
+
let minBinId;
|
|
12738
|
+
if (activeIdIndex == -1) {
|
|
12739
|
+
minBinId = rebalancePosition.rebalancePositionBinData[0].binId;
|
|
12740
|
+
} else {
|
|
12741
|
+
minBinId = rebalancePosition.rebalancePositionBinData[favorXInActiveBin ? activeIdIndex - 1 : activeIdIndex].binId;
|
|
12742
|
+
}
|
|
12743
|
+
const minDeltaId = minBinId - lbPair.activeId;
|
|
12744
|
+
const maxDeltaId = maxBinId - lbPair.activeId;
|
|
12745
|
+
const { amountX } = builder.suggestBalancedXParametersFromY(
|
|
12746
|
+
new (0, _bnjs2.default)(lbPair.activeId),
|
|
12747
|
+
new (0, _bnjs2.default)(lbPair.binStep),
|
|
12748
|
+
favorXInActiveBin,
|
|
12749
|
+
new (0, _bnjs2.default)(minDeltaId),
|
|
12750
|
+
new (0, _bnjs2.default)(maxDeltaId),
|
|
12751
|
+
liquidityInBidSide
|
|
12752
|
+
);
|
|
12753
|
+
const [positionAmountX] = rebalancePosition.totalAmounts();
|
|
12754
|
+
return {
|
|
12755
|
+
amount: _bnjs2.default.max(amountX.sub(positionAmountX), new (0, _bnjs2.default)(0)),
|
|
12756
|
+
isBidSide: false
|
|
12757
|
+
};
|
|
12758
|
+
} else {
|
|
12759
|
+
return {
|
|
12760
|
+
amount: new (0, _bnjs2.default)(0),
|
|
12761
|
+
isBidSide: false
|
|
12762
|
+
};
|
|
12763
|
+
}
|
|
12764
|
+
}
|
|
12765
|
+
function suggestBalancedYParametersFromX(x0, deltaX, minDeltaId, maxDeltaId, activeId, binStep, favorXInActiveBin, builder) {
|
|
12766
|
+
const startDeltaIdAskSide = favorXInActiveBin ? new (0, _bnjs2.default)(0) : new (0, _bnjs2.default)(1);
|
|
12767
|
+
if (minDeltaId.gte(startDeltaIdAskSide)) {
|
|
12768
|
+
return {
|
|
12769
|
+
base: new (0, _bnjs2.default)(0),
|
|
12770
|
+
delta: new (0, _bnjs2.default)(0),
|
|
12771
|
+
amountY: new (0, _bnjs2.default)(0)
|
|
12772
|
+
};
|
|
12773
|
+
}
|
|
12774
|
+
const minXDeltaId = startDeltaIdAskSide;
|
|
12775
|
+
const maxXDeltaId = maxDeltaId;
|
|
12776
|
+
const amountXInBins = toAmountIntoBins(
|
|
12777
|
+
activeId,
|
|
12778
|
+
minXDeltaId,
|
|
12779
|
+
maxXDeltaId,
|
|
12780
|
+
deltaX,
|
|
12781
|
+
new (0, _bnjs2.default)(0),
|
|
12782
|
+
x0,
|
|
12783
|
+
new (0, _bnjs2.default)(0),
|
|
12784
|
+
binStep,
|
|
12785
|
+
favorXInActiveBin
|
|
12786
|
+
);
|
|
12787
|
+
const totalAmountXInQuote = amountXInBins.reduce((acc, bin) => {
|
|
12788
|
+
const price = getPriceOfBinByBinId(
|
|
12789
|
+
bin.binId.toNumber(),
|
|
12790
|
+
binStep.toNumber()
|
|
12791
|
+
);
|
|
12792
|
+
return acc.add(price.mul(new (0, _decimaljs2.default)(bin.amountX.toString())));
|
|
12793
|
+
}, new (0, _decimaljs2.default)(0));
|
|
12794
|
+
const totalAmountXInQuoteBN = new (0, _bnjs2.default)(totalAmountXInQuote.floor().toString());
|
|
12795
|
+
const minYDeltaId = minDeltaId;
|
|
12796
|
+
const maxYDeltaId = startDeltaIdAskSide.subn(1);
|
|
12797
|
+
return builder.suggestBalancedYParametersFromX(
|
|
12798
|
+
activeId,
|
|
12799
|
+
binStep,
|
|
12800
|
+
favorXInActiveBin,
|
|
12801
|
+
minYDeltaId,
|
|
12802
|
+
maxYDeltaId,
|
|
12803
|
+
totalAmountXInQuoteBN
|
|
12804
|
+
);
|
|
12805
|
+
}
|
|
12806
|
+
function buildLiquidityStrategyParameters(amountX, amountY, minDeltaId, maxDeltaId, binStep, favorXInActiveId, activeId, strategyParameterBuilder) {
|
|
12807
|
+
if (minDeltaId.gt(maxDeltaId)) {
|
|
12808
|
+
return {
|
|
12809
|
+
x0: new (0, _bnjs2.default)(0),
|
|
12810
|
+
y0: new (0, _bnjs2.default)(0),
|
|
12811
|
+
deltaX: new (0, _bnjs2.default)(0),
|
|
12812
|
+
deltaY: new (0, _bnjs2.default)(0)
|
|
12813
|
+
};
|
|
12814
|
+
}
|
|
12815
|
+
const depositOnlyY = maxDeltaId.lt(new (0, _bnjs2.default)(0)) || maxDeltaId.isZero() && !favorXInActiveId;
|
|
12816
|
+
const depositOnlyX = minDeltaId.gt(new (0, _bnjs2.default)(0)) || minDeltaId.isZero() && favorXInActiveId;
|
|
12817
|
+
if (depositOnlyY) {
|
|
12818
|
+
const { base, delta } = strategyParameterBuilder.findYParameters(
|
|
12819
|
+
amountY,
|
|
12820
|
+
minDeltaId,
|
|
12821
|
+
maxDeltaId,
|
|
12822
|
+
activeId
|
|
12823
|
+
);
|
|
12824
|
+
return {
|
|
12825
|
+
x0: new (0, _bnjs2.default)(0),
|
|
12826
|
+
deltaX: new (0, _bnjs2.default)(0),
|
|
12827
|
+
y0: base,
|
|
12828
|
+
deltaY: delta
|
|
12829
|
+
};
|
|
12830
|
+
}
|
|
12831
|
+
if (depositOnlyX) {
|
|
12832
|
+
const { base, delta } = strategyParameterBuilder.findXParameters(
|
|
12833
|
+
amountX,
|
|
12834
|
+
minDeltaId,
|
|
12835
|
+
maxDeltaId,
|
|
12836
|
+
binStep,
|
|
12837
|
+
activeId
|
|
12838
|
+
);
|
|
12839
|
+
return {
|
|
12840
|
+
x0: base,
|
|
12841
|
+
deltaX: delta,
|
|
12842
|
+
y0: new (0, _bnjs2.default)(0),
|
|
12843
|
+
deltaY: new (0, _bnjs2.default)(0)
|
|
12844
|
+
};
|
|
12845
|
+
}
|
|
12846
|
+
const maxDeltaIdBidSide = favorXInActiveId ? new (0, _bnjs2.default)(-1) : new (0, _bnjs2.default)(0);
|
|
12847
|
+
const minDeltaIdAskSide = favorXInActiveId ? new (0, _bnjs2.default)(0) : new (0, _bnjs2.default)(1);
|
|
12848
|
+
const { base: y0, delta: deltaY } = strategyParameterBuilder.findYParameters(
|
|
12849
|
+
amountY,
|
|
12850
|
+
minDeltaId,
|
|
12851
|
+
maxDeltaIdBidSide,
|
|
12852
|
+
activeId
|
|
12853
|
+
);
|
|
12854
|
+
const { base: x0, delta: deltaX } = strategyParameterBuilder.findXParameters(
|
|
12855
|
+
amountX,
|
|
12856
|
+
minDeltaIdAskSide,
|
|
12857
|
+
maxDeltaId,
|
|
12858
|
+
binStep,
|
|
12859
|
+
activeId
|
|
12860
|
+
);
|
|
12861
|
+
return {
|
|
12862
|
+
x0,
|
|
12863
|
+
deltaX,
|
|
12864
|
+
y0,
|
|
12865
|
+
deltaY
|
|
12866
|
+
};
|
|
12867
|
+
}
|
|
12868
|
+
|
|
12869
|
+
// src/dlmm/helpers/lbPair.ts
|
|
12870
|
+
|
|
12871
|
+
|
|
12872
|
+
async function getTokensMintFromPoolAddress(connection, poolAddress, opt) {
|
|
12873
|
+
const program = createProgram(connection, opt);
|
|
12874
|
+
const poolAccount = await program.account.lbPair.fetchNullable(
|
|
12875
|
+
new (0, _web3js.PublicKey)(poolAddress)
|
|
12876
|
+
);
|
|
12877
|
+
if (!poolAccount)
|
|
12878
|
+
throw new Error("Pool account not found");
|
|
12879
|
+
return {
|
|
12880
|
+
tokenXMint: poolAccount.tokenXMint,
|
|
12881
|
+
tokenYMint: poolAccount.tokenYMint
|
|
12882
|
+
};
|
|
12883
|
+
}
|
|
12884
|
+
function getTokenProgramId(lbPairState) {
|
|
12885
|
+
const getTokenProgramIdByFlag = (flag) => {
|
|
12886
|
+
return flag == 0 ? _spltoken.TOKEN_PROGRAM_ID : _spltoken.TOKEN_2022_PROGRAM_ID;
|
|
12887
|
+
};
|
|
12888
|
+
return {
|
|
12889
|
+
tokenXProgram: getTokenProgramIdByFlag(lbPairState.tokenMintXProgramFlag),
|
|
12890
|
+
tokenYProgram: getTokenProgramIdByFlag(lbPairState.tokenMintYProgramFlag)
|
|
12891
|
+
};
|
|
12892
|
+
}
|
|
12893
|
+
|
|
12894
|
+
// src/dlmm/helpers/strategy.ts
|
|
12895
|
+
|
|
12896
|
+
var DEFAULT_MAX_WEIGHT = 2e3;
|
|
12897
|
+
var DEFAULT_MIN_WEIGHT = 200;
|
|
12898
|
+
function toWeightSpotBalanced(minBinId, maxBinId) {
|
|
12899
|
+
let distributions = [];
|
|
12900
|
+
for (let i = minBinId; i <= maxBinId; i++) {
|
|
12901
|
+
distributions.push({
|
|
12902
|
+
binId: i,
|
|
12903
|
+
weight: 1
|
|
12904
|
+
});
|
|
12905
|
+
}
|
|
12906
|
+
return distributions;
|
|
12907
|
+
}
|
|
12908
|
+
function toWeightDescendingOrder(minBinId, maxBinId) {
|
|
12909
|
+
let distributions = [];
|
|
12910
|
+
for (let i = minBinId; i <= maxBinId; i++) {
|
|
12911
|
+
distributions.push({
|
|
12912
|
+
binId: i,
|
|
12913
|
+
weight: maxBinId - i + 1
|
|
12914
|
+
});
|
|
12915
|
+
}
|
|
12916
|
+
return distributions;
|
|
12917
|
+
}
|
|
12918
|
+
function toWeightAscendingOrder(minBinId, maxBinId) {
|
|
12919
|
+
let distributions = [];
|
|
12920
|
+
for (let i = minBinId; i <= maxBinId; i++) {
|
|
12921
|
+
distributions.push({
|
|
12922
|
+
binId: i,
|
|
12923
|
+
weight: i - minBinId + 1
|
|
12924
|
+
});
|
|
12925
|
+
}
|
|
12926
|
+
return distributions;
|
|
12927
|
+
}
|
|
12928
|
+
function toWeightCurve(minBinId, maxBinId, activeId) {
|
|
12929
|
+
if (activeId < minBinId || activeId > maxBinId) {
|
|
12930
|
+
throw "Invalid strategy params";
|
|
12931
|
+
}
|
|
12932
|
+
let maxWeight = DEFAULT_MAX_WEIGHT;
|
|
12933
|
+
let minWeight = DEFAULT_MIN_WEIGHT;
|
|
12934
|
+
let diffWeight = maxWeight - minWeight;
|
|
12935
|
+
let diffMinWeight = activeId > minBinId ? Math.floor(diffWeight / (activeId - minBinId)) : 0;
|
|
12936
|
+
let diffMaxWeight = maxBinId > activeId ? Math.floor(diffWeight / (maxBinId - activeId)) : 0;
|
|
12937
|
+
let distributions = [];
|
|
12938
|
+
for (let i = minBinId; i <= maxBinId; i++) {
|
|
12939
|
+
if (i < activeId) {
|
|
12940
|
+
distributions.push({
|
|
12941
|
+
binId: i,
|
|
12942
|
+
weight: maxWeight - (activeId - i) * diffMinWeight
|
|
12943
|
+
});
|
|
12944
|
+
} else if (i > activeId) {
|
|
12945
|
+
distributions.push({
|
|
12946
|
+
binId: i,
|
|
12947
|
+
weight: maxWeight - (i - activeId) * diffMaxWeight
|
|
12948
|
+
});
|
|
12949
|
+
} else {
|
|
12950
|
+
distributions.push({
|
|
12951
|
+
binId: i,
|
|
12952
|
+
weight: maxWeight
|
|
12953
|
+
});
|
|
12954
|
+
}
|
|
12955
|
+
}
|
|
12956
|
+
return distributions;
|
|
12957
|
+
}
|
|
12958
|
+
function toWeightBidAsk(minBinId, maxBinId, activeId) {
|
|
12959
|
+
if (activeId < minBinId || activeId > maxBinId) {
|
|
12960
|
+
throw "Invalid strategy params";
|
|
12961
|
+
}
|
|
12962
|
+
let maxWeight = DEFAULT_MAX_WEIGHT;
|
|
12963
|
+
let minWeight = DEFAULT_MIN_WEIGHT;
|
|
12964
|
+
let diffWeight = maxWeight - minWeight;
|
|
12965
|
+
let diffMinWeight = activeId > minBinId ? Math.floor(diffWeight / (activeId - minBinId)) : 0;
|
|
12966
|
+
let diffMaxWeight = maxBinId > activeId ? Math.floor(diffWeight / (maxBinId - activeId)) : 0;
|
|
12967
|
+
let distributions = [];
|
|
12968
|
+
for (let i = minBinId; i <= maxBinId; i++) {
|
|
12969
|
+
if (i < activeId) {
|
|
12970
|
+
distributions.push({
|
|
12971
|
+
binId: i,
|
|
12972
|
+
weight: minWeight + (activeId - i) * diffMinWeight
|
|
12973
|
+
});
|
|
12974
|
+
} else if (i > activeId) {
|
|
12975
|
+
distributions.push({
|
|
12976
|
+
binId: i,
|
|
12977
|
+
weight: minWeight + (i - activeId) * diffMaxWeight
|
|
12978
|
+
});
|
|
12979
|
+
} else {
|
|
12980
|
+
distributions.push({
|
|
12981
|
+
binId: i,
|
|
12982
|
+
weight: minWeight
|
|
12983
|
+
});
|
|
12984
|
+
}
|
|
12985
|
+
}
|
|
12986
|
+
return distributions;
|
|
12987
|
+
}
|
|
12988
|
+
function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amountX, amountY, amountXInActiveBin, amountYInActiveBin, strategyType, mintX, mintY, clock) {
|
|
12989
|
+
const isSingleSideX = amountY.isZero();
|
|
12990
|
+
switch (strategyType) {
|
|
12991
|
+
case 0 /* Spot */: {
|
|
12992
|
+
if (activeId < minBinId || activeId > maxBinId) {
|
|
12993
|
+
const weights = toWeightSpotBalanced(minBinId, maxBinId);
|
|
12994
|
+
return toAmountBothSide(
|
|
12995
|
+
activeId,
|
|
12996
|
+
binStep,
|
|
12997
|
+
amountX,
|
|
12998
|
+
amountY,
|
|
12999
|
+
amountXInActiveBin,
|
|
13000
|
+
amountYInActiveBin,
|
|
13001
|
+
weights,
|
|
13002
|
+
mintX,
|
|
13003
|
+
mintY,
|
|
13004
|
+
clock
|
|
13005
|
+
);
|
|
13006
|
+
}
|
|
13007
|
+
const amountsInBin = [];
|
|
13008
|
+
if (!isSingleSideX) {
|
|
13009
|
+
if (minBinId <= activeId) {
|
|
13010
|
+
const weights = toWeightSpotBalanced(minBinId, activeId);
|
|
13011
|
+
const amounts = toAmountBidSide(
|
|
13012
|
+
activeId,
|
|
13013
|
+
amountY,
|
|
13014
|
+
weights,
|
|
13015
|
+
mintY,
|
|
13016
|
+
clock
|
|
13017
|
+
);
|
|
13018
|
+
for (let bin of amounts) {
|
|
13019
|
+
amountsInBin.push({
|
|
13020
|
+
binId: bin.binId,
|
|
13021
|
+
amountX: new (0, _anchor.BN)(0),
|
|
13022
|
+
amountY: bin.amount
|
|
13023
|
+
});
|
|
13024
|
+
}
|
|
13025
|
+
}
|
|
13026
|
+
if (activeId < maxBinId) {
|
|
13027
|
+
const weights = toWeightSpotBalanced(activeId + 1, maxBinId);
|
|
13028
|
+
const amounts = toAmountAskSide(
|
|
13029
|
+
activeId,
|
|
13030
|
+
binStep,
|
|
13031
|
+
amountX,
|
|
13032
|
+
weights,
|
|
13033
|
+
mintX,
|
|
13034
|
+
clock
|
|
13035
|
+
);
|
|
13036
|
+
for (let bin of amounts) {
|
|
13037
|
+
amountsInBin.push({
|
|
13038
|
+
binId: bin.binId,
|
|
13039
|
+
amountX: bin.amount,
|
|
13040
|
+
amountY: new (0, _anchor.BN)(0)
|
|
13041
|
+
});
|
|
13042
|
+
}
|
|
13043
|
+
}
|
|
13044
|
+
} else {
|
|
13045
|
+
if (minBinId < activeId) {
|
|
13046
|
+
const weights = toWeightSpotBalanced(minBinId, activeId - 1);
|
|
13047
|
+
const amountsIntoBidSide = toAmountBidSide(
|
|
13048
|
+
activeId,
|
|
13049
|
+
amountY,
|
|
13050
|
+
weights,
|
|
13051
|
+
mintY,
|
|
13052
|
+
clock
|
|
13053
|
+
);
|
|
13054
|
+
for (let bin of amountsIntoBidSide) {
|
|
13055
|
+
amountsInBin.push({
|
|
13056
|
+
binId: bin.binId,
|
|
13057
|
+
amountX: new (0, _anchor.BN)(0),
|
|
13058
|
+
amountY: bin.amount
|
|
13059
|
+
});
|
|
13060
|
+
}
|
|
13061
|
+
}
|
|
13062
|
+
if (activeId <= maxBinId) {
|
|
13063
|
+
const weights = toWeightSpotBalanced(activeId, maxBinId);
|
|
13064
|
+
const amountsIntoAskSide = toAmountAskSide(
|
|
13065
|
+
activeId,
|
|
13066
|
+
binStep,
|
|
13067
|
+
amountX,
|
|
13068
|
+
weights,
|
|
13069
|
+
mintX,
|
|
13070
|
+
clock
|
|
13071
|
+
);
|
|
13072
|
+
for (let bin of amountsIntoAskSide) {
|
|
13073
|
+
amountsInBin.push({
|
|
13074
|
+
binId: bin.binId,
|
|
13075
|
+
amountX: bin.amount,
|
|
13076
|
+
amountY: new (0, _anchor.BN)(0)
|
|
13077
|
+
});
|
|
13078
|
+
}
|
|
13079
|
+
}
|
|
13080
|
+
}
|
|
13081
|
+
return amountsInBin;
|
|
13082
|
+
}
|
|
13083
|
+
case 1 /* Curve */: {
|
|
13084
|
+
if (activeId < minBinId) {
|
|
13085
|
+
let weights = toWeightDescendingOrder(minBinId, maxBinId);
|
|
13086
|
+
return toAmountBothSide(
|
|
13087
|
+
activeId,
|
|
13088
|
+
binStep,
|
|
13089
|
+
amountX,
|
|
13090
|
+
amountY,
|
|
13091
|
+
amountXInActiveBin,
|
|
13092
|
+
amountYInActiveBin,
|
|
13093
|
+
weights,
|
|
13094
|
+
mintX,
|
|
13095
|
+
mintY,
|
|
13096
|
+
clock
|
|
13097
|
+
);
|
|
13098
|
+
}
|
|
13099
|
+
if (activeId > maxBinId) {
|
|
13100
|
+
const weights = toWeightAscendingOrder(minBinId, maxBinId);
|
|
13101
|
+
return toAmountBothSide(
|
|
13102
|
+
activeId,
|
|
13103
|
+
binStep,
|
|
13104
|
+
amountX,
|
|
13105
|
+
amountY,
|
|
13106
|
+
amountXInActiveBin,
|
|
13107
|
+
amountYInActiveBin,
|
|
13108
|
+
weights,
|
|
13109
|
+
mintX,
|
|
13110
|
+
mintY,
|
|
13111
|
+
clock
|
|
13112
|
+
);
|
|
13113
|
+
}
|
|
13114
|
+
const amountsInBin = [];
|
|
13115
|
+
if (!isSingleSideX) {
|
|
13116
|
+
if (minBinId <= activeId) {
|
|
13117
|
+
const weights = toWeightAscendingOrder(minBinId, activeId);
|
|
13118
|
+
const amounts = toAmountBidSide(
|
|
13119
|
+
activeId,
|
|
13120
|
+
amountY,
|
|
13121
|
+
weights,
|
|
13122
|
+
mintY,
|
|
13123
|
+
clock
|
|
13124
|
+
);
|
|
13125
|
+
for (let bin of amounts) {
|
|
13126
|
+
amountsInBin.push({
|
|
13127
|
+
binId: bin.binId,
|
|
13128
|
+
amountX: new (0, _anchor.BN)(0),
|
|
13129
|
+
amountY: bin.amount
|
|
13130
|
+
});
|
|
13131
|
+
}
|
|
13132
|
+
}
|
|
13133
|
+
if (activeId < maxBinId) {
|
|
13134
|
+
const weights = toWeightDescendingOrder(activeId + 1, maxBinId);
|
|
13135
|
+
const amounts = toAmountAskSide(
|
|
13136
|
+
activeId,
|
|
13137
|
+
binStep,
|
|
13138
|
+
amountX,
|
|
13139
|
+
weights,
|
|
13140
|
+
mintX,
|
|
13141
|
+
clock
|
|
13142
|
+
);
|
|
13143
|
+
for (let bin of amounts) {
|
|
13144
|
+
amountsInBin.push({
|
|
13145
|
+
binId: bin.binId,
|
|
13146
|
+
amountX: bin.amount,
|
|
13147
|
+
amountY: new (0, _anchor.BN)(0)
|
|
13148
|
+
});
|
|
13149
|
+
}
|
|
13150
|
+
}
|
|
13151
|
+
} else {
|
|
13152
|
+
if (minBinId < activeId) {
|
|
13153
|
+
const weights = toWeightAscendingOrder(minBinId, activeId - 1);
|
|
13154
|
+
const amountsIntoBidSide = toAmountBidSide(
|
|
13155
|
+
activeId,
|
|
13156
|
+
amountY,
|
|
13157
|
+
weights,
|
|
13158
|
+
mintY,
|
|
13159
|
+
clock
|
|
13160
|
+
);
|
|
13161
|
+
for (let bin of amountsIntoBidSide) {
|
|
13162
|
+
amountsInBin.push({
|
|
13163
|
+
binId: bin.binId,
|
|
13164
|
+
amountX: new (0, _anchor.BN)(0),
|
|
13165
|
+
amountY: bin.amount
|
|
13166
|
+
});
|
|
13167
|
+
}
|
|
13168
|
+
}
|
|
13169
|
+
if (activeId <= maxBinId) {
|
|
13170
|
+
const weights = toWeightDescendingOrder(activeId, maxBinId);
|
|
13171
|
+
const amountsIntoAskSide = toAmountAskSide(
|
|
13172
|
+
activeId,
|
|
13173
|
+
binStep,
|
|
13174
|
+
amountX,
|
|
13175
|
+
weights,
|
|
13176
|
+
mintX,
|
|
13177
|
+
clock
|
|
13178
|
+
);
|
|
13179
|
+
for (let bin of amountsIntoAskSide) {
|
|
13180
|
+
amountsInBin.push({
|
|
13181
|
+
binId: bin.binId,
|
|
13182
|
+
amountX: bin.amount,
|
|
13183
|
+
amountY: new (0, _anchor.BN)(0)
|
|
13184
|
+
});
|
|
13185
|
+
}
|
|
13186
|
+
}
|
|
13187
|
+
}
|
|
13188
|
+
return amountsInBin;
|
|
13189
|
+
}
|
|
13190
|
+
case 2 /* BidAsk */: {
|
|
13191
|
+
if (activeId < minBinId) {
|
|
13192
|
+
const weights = toWeightAscendingOrder(minBinId, maxBinId);
|
|
13193
|
+
return toAmountBothSide(
|
|
13194
|
+
activeId,
|
|
13195
|
+
binStep,
|
|
13196
|
+
amountX,
|
|
13197
|
+
amountY,
|
|
13198
|
+
amountXInActiveBin,
|
|
13199
|
+
amountYInActiveBin,
|
|
13200
|
+
weights,
|
|
13201
|
+
mintX,
|
|
13202
|
+
mintY,
|
|
13203
|
+
clock
|
|
13204
|
+
);
|
|
13205
|
+
}
|
|
13206
|
+
if (activeId > maxBinId) {
|
|
13207
|
+
const weights = toWeightDescendingOrder(minBinId, maxBinId);
|
|
13208
|
+
return toAmountBothSide(
|
|
13209
|
+
activeId,
|
|
13210
|
+
binStep,
|
|
13211
|
+
amountX,
|
|
13212
|
+
amountY,
|
|
13213
|
+
amountXInActiveBin,
|
|
13214
|
+
amountYInActiveBin,
|
|
13215
|
+
weights,
|
|
13216
|
+
mintX,
|
|
13217
|
+
mintY,
|
|
13218
|
+
clock
|
|
13219
|
+
);
|
|
13220
|
+
}
|
|
13221
|
+
const amountsInBin = [];
|
|
13222
|
+
if (!isSingleSideX) {
|
|
13223
|
+
if (minBinId <= activeId) {
|
|
13224
|
+
const weights = toWeightDescendingOrder(minBinId, activeId);
|
|
13225
|
+
const amounts = toAmountBidSide(
|
|
13226
|
+
activeId,
|
|
13227
|
+
amountY,
|
|
13228
|
+
weights,
|
|
13229
|
+
mintY,
|
|
13230
|
+
clock
|
|
13231
|
+
);
|
|
13232
|
+
for (let bin of amounts) {
|
|
13233
|
+
amountsInBin.push({
|
|
13234
|
+
binId: bin.binId,
|
|
13235
|
+
amountX: new (0, _anchor.BN)(0),
|
|
13236
|
+
amountY: bin.amount
|
|
13237
|
+
});
|
|
13238
|
+
}
|
|
13239
|
+
}
|
|
13240
|
+
if (activeId < maxBinId) {
|
|
13241
|
+
const weights = toWeightAscendingOrder(activeId + 1, maxBinId);
|
|
13242
|
+
const amounts = toAmountAskSide(
|
|
13243
|
+
activeId,
|
|
13244
|
+
binStep,
|
|
13245
|
+
amountX,
|
|
13246
|
+
weights,
|
|
13247
|
+
mintX,
|
|
13248
|
+
clock
|
|
13249
|
+
);
|
|
13250
|
+
for (let bin of amounts) {
|
|
13251
|
+
amountsInBin.push({
|
|
13252
|
+
binId: bin.binId,
|
|
13253
|
+
amountX: bin.amount,
|
|
13254
|
+
amountY: new (0, _anchor.BN)(0)
|
|
13255
|
+
});
|
|
13256
|
+
}
|
|
13257
|
+
}
|
|
13258
|
+
} else {
|
|
13259
|
+
if (minBinId < activeId) {
|
|
13260
|
+
const weights = toWeightDescendingOrder(minBinId, activeId - 1);
|
|
13261
|
+
const amountsIntoBidSide = toAmountBidSide(
|
|
13262
|
+
activeId,
|
|
13263
|
+
amountY,
|
|
13264
|
+
weights,
|
|
13265
|
+
mintY,
|
|
13266
|
+
clock
|
|
13267
|
+
);
|
|
13268
|
+
for (let bin of amountsIntoBidSide) {
|
|
13269
|
+
amountsInBin.push({
|
|
13270
|
+
binId: bin.binId,
|
|
13271
|
+
amountX: new (0, _anchor.BN)(0),
|
|
13272
|
+
amountY: bin.amount
|
|
13273
|
+
});
|
|
13274
|
+
}
|
|
13275
|
+
}
|
|
13276
|
+
if (activeId <= maxBinId) {
|
|
13277
|
+
const weights = toWeightAscendingOrder(activeId, maxBinId);
|
|
13278
|
+
const amountsIntoAskSide = toAmountAskSide(
|
|
13279
|
+
activeId,
|
|
13280
|
+
binStep,
|
|
13281
|
+
amountX,
|
|
13282
|
+
weights,
|
|
13283
|
+
mintX,
|
|
13284
|
+
clock
|
|
13285
|
+
);
|
|
13286
|
+
for (let bin of amountsIntoAskSide) {
|
|
13287
|
+
amountsInBin.push({
|
|
13288
|
+
binId: bin.binId,
|
|
13289
|
+
amountX: bin.amount,
|
|
13290
|
+
amountY: new (0, _anchor.BN)(0)
|
|
13291
|
+
});
|
|
13292
|
+
}
|
|
13293
|
+
}
|
|
13294
|
+
}
|
|
13295
|
+
return amountsInBin;
|
|
13296
|
+
}
|
|
13297
|
+
}
|
|
13298
|
+
}
|
|
13299
|
+
function autoFillYByStrategy(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, minBinId, maxBinId, strategyType) {
|
|
13300
|
+
switch (strategyType) {
|
|
13301
|
+
case 0 /* Spot */: {
|
|
13302
|
+
let weights = toWeightSpotBalanced(minBinId, maxBinId);
|
|
13303
|
+
return autoFillYByWeight(
|
|
13304
|
+
activeId,
|
|
13305
|
+
binStep,
|
|
13306
|
+
amountX,
|
|
13307
|
+
amountXInActiveBin,
|
|
13308
|
+
amountYInActiveBin,
|
|
13309
|
+
weights
|
|
13310
|
+
);
|
|
13036
13311
|
}
|
|
13037
|
-
|
|
13038
|
-
|
|
13039
|
-
|
|
13040
|
-
|
|
13041
|
-
|
|
13042
|
-
|
|
13043
|
-
|
|
13044
|
-
|
|
13045
|
-
|
|
13046
|
-
|
|
13047
|
-
|
|
13048
|
-
|
|
13049
|
-
|
|
13050
|
-
|
|
13051
|
-
|
|
13052
|
-
|
|
13053
|
-
|
|
13054
|
-
|
|
13055
|
-
|
|
13056
|
-
|
|
13057
|
-
|
|
13058
|
-
minBinId = rebalancePosition.rebalancePositionBinData[favorXInActiveBin ? activeIdIndex - 1 : activeIdIndex].binId;
|
|
13312
|
+
case 1 /* Curve */: {
|
|
13313
|
+
let weights = toWeightCurve(minBinId, maxBinId, activeId);
|
|
13314
|
+
return autoFillYByWeight(
|
|
13315
|
+
activeId,
|
|
13316
|
+
binStep,
|
|
13317
|
+
amountX,
|
|
13318
|
+
amountXInActiveBin,
|
|
13319
|
+
amountYInActiveBin,
|
|
13320
|
+
weights
|
|
13321
|
+
);
|
|
13322
|
+
}
|
|
13323
|
+
case 2 /* BidAsk */: {
|
|
13324
|
+
let weights = toWeightBidAsk(minBinId, maxBinId, activeId);
|
|
13325
|
+
return autoFillYByWeight(
|
|
13326
|
+
activeId,
|
|
13327
|
+
binStep,
|
|
13328
|
+
amountX,
|
|
13329
|
+
amountXInActiveBin,
|
|
13330
|
+
amountYInActiveBin,
|
|
13331
|
+
weights
|
|
13332
|
+
);
|
|
13059
13333
|
}
|
|
13060
|
-
const minDeltaId = lbPair.activeId - minBinId;
|
|
13061
|
-
const maxDeltaId = lbPair.activeId - maxBinId;
|
|
13062
|
-
const { amountX } = builder.suggestBalancedXParametersFromY(
|
|
13063
|
-
new (0, _bnjs2.default)(lbPair.activeId),
|
|
13064
|
-
new (0, _bnjs2.default)(lbPair.binStep),
|
|
13065
|
-
favorXInActiveBin,
|
|
13066
|
-
new (0, _bnjs2.default)(minDeltaId),
|
|
13067
|
-
new (0, _bnjs2.default)(maxDeltaId),
|
|
13068
|
-
liquidityInBidSide
|
|
13069
|
-
);
|
|
13070
|
-
const [positionAmountX] = rebalancePosition.totalAmounts();
|
|
13071
|
-
return {
|
|
13072
|
-
amount: _bnjs2.default.max(amountX.sub(positionAmountX), new (0, _bnjs2.default)(0)),
|
|
13073
|
-
isBidSide: false
|
|
13074
|
-
};
|
|
13075
|
-
} else {
|
|
13076
|
-
return {
|
|
13077
|
-
amount: new (0, _bnjs2.default)(0),
|
|
13078
|
-
isBidSide: false
|
|
13079
|
-
};
|
|
13080
13334
|
}
|
|
13081
13335
|
}
|
|
13082
|
-
function
|
|
13083
|
-
|
|
13084
|
-
|
|
13085
|
-
|
|
13086
|
-
|
|
13087
|
-
|
|
13088
|
-
|
|
13089
|
-
|
|
13336
|
+
function autoFillXByStrategy(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, minBinId, maxBinId, strategyType) {
|
|
13337
|
+
switch (strategyType) {
|
|
13338
|
+
case 0 /* Spot */: {
|
|
13339
|
+
let weights = toWeightSpotBalanced(minBinId, maxBinId);
|
|
13340
|
+
return autoFillXByWeight(
|
|
13341
|
+
activeId,
|
|
13342
|
+
binStep,
|
|
13343
|
+
amountY,
|
|
13344
|
+
amountXInActiveBin,
|
|
13345
|
+
amountYInActiveBin,
|
|
13346
|
+
weights
|
|
13347
|
+
);
|
|
13348
|
+
}
|
|
13349
|
+
case 1 /* Curve */: {
|
|
13350
|
+
let weights = toWeightCurve(minBinId, maxBinId, activeId);
|
|
13351
|
+
return autoFillXByWeight(
|
|
13352
|
+
activeId,
|
|
13353
|
+
binStep,
|
|
13354
|
+
amountY,
|
|
13355
|
+
amountXInActiveBin,
|
|
13356
|
+
amountYInActiveBin,
|
|
13357
|
+
weights
|
|
13358
|
+
);
|
|
13359
|
+
}
|
|
13360
|
+
case 2 /* BidAsk */: {
|
|
13361
|
+
let weights = toWeightBidAsk(minBinId, maxBinId, activeId);
|
|
13362
|
+
return autoFillXByWeight(
|
|
13363
|
+
activeId,
|
|
13364
|
+
binStep,
|
|
13365
|
+
amountY,
|
|
13366
|
+
amountXInActiveBin,
|
|
13367
|
+
amountYInActiveBin,
|
|
13368
|
+
weights
|
|
13369
|
+
);
|
|
13370
|
+
}
|
|
13090
13371
|
}
|
|
13091
|
-
const minXDeltaId = startDeltaIdAskSide;
|
|
13092
|
-
const maxXDeltaId = maxDeltaId;
|
|
13093
|
-
const amountXInBins = toAmountIntoBins(
|
|
13094
|
-
activeId,
|
|
13095
|
-
minXDeltaId,
|
|
13096
|
-
maxXDeltaId,
|
|
13097
|
-
deltaX,
|
|
13098
|
-
new (0, _bnjs2.default)(0),
|
|
13099
|
-
x0,
|
|
13100
|
-
new (0, _bnjs2.default)(0),
|
|
13101
|
-
binStep,
|
|
13102
|
-
favorXInActiveBin
|
|
13103
|
-
);
|
|
13104
|
-
const totalAmountXInQuote = amountXInBins.reduce((acc, bin) => {
|
|
13105
|
-
const price = getPriceOfBinByBinId(
|
|
13106
|
-
bin.binId.toNumber(),
|
|
13107
|
-
binStep.toNumber()
|
|
13108
|
-
);
|
|
13109
|
-
return acc.add(price.mul(new (0, _decimaljs2.default)(bin.amountX.toString())));
|
|
13110
|
-
}, new (0, _decimaljs2.default)(0));
|
|
13111
|
-
const totalAmountXInQuoteBN = new (0, _bnjs2.default)(totalAmountXInQuote.floor().toString());
|
|
13112
|
-
const minYDeltaId = minDeltaId;
|
|
13113
|
-
const maxYDeltaId = startDeltaIdAskSide.subn(1);
|
|
13114
|
-
return builder.suggestBalancedYParametersFromX(
|
|
13115
|
-
activeId,
|
|
13116
|
-
binStep,
|
|
13117
|
-
favorXInActiveBin,
|
|
13118
|
-
minYDeltaId,
|
|
13119
|
-
maxYDeltaId,
|
|
13120
|
-
totalAmountXInQuoteBN
|
|
13121
|
-
);
|
|
13122
13372
|
}
|
|
13123
|
-
function
|
|
13124
|
-
|
|
13125
|
-
|
|
13126
|
-
|
|
13127
|
-
|
|
13128
|
-
|
|
13129
|
-
|
|
13130
|
-
|
|
13131
|
-
|
|
13132
|
-
|
|
13133
|
-
|
|
13134
|
-
|
|
13135
|
-
|
|
13136
|
-
|
|
13137
|
-
|
|
13138
|
-
|
|
13139
|
-
|
|
13140
|
-
|
|
13141
|
-
|
|
13142
|
-
|
|
13143
|
-
|
|
13144
|
-
|
|
13145
|
-
|
|
13146
|
-
|
|
13147
|
-
|
|
13148
|
-
|
|
13149
|
-
|
|
13150
|
-
|
|
13151
|
-
|
|
13152
|
-
|
|
13153
|
-
|
|
13154
|
-
|
|
13155
|
-
|
|
13156
|
-
|
|
13157
|
-
|
|
13158
|
-
deltaX: delta,
|
|
13159
|
-
y0: new (0, _bnjs2.default)(0),
|
|
13160
|
-
deltaY: new (0, _bnjs2.default)(0)
|
|
13161
|
-
};
|
|
13162
|
-
}
|
|
13163
|
-
const maxDeltaIdBidSide = favorXInActiveId ? new (0, _bnjs2.default)(-1) : new (0, _bnjs2.default)(0);
|
|
13164
|
-
const minDeltaIdAskSide = favorXInActiveId ? new (0, _bnjs2.default)(0) : new (0, _bnjs2.default)(1);
|
|
13165
|
-
const { base: y0, delta: deltaY } = strategyParameterBuilder.findYParameters(
|
|
13166
|
-
amountY,
|
|
13167
|
-
minDeltaId,
|
|
13168
|
-
maxDeltaIdBidSide,
|
|
13169
|
-
activeId
|
|
13170
|
-
);
|
|
13171
|
-
const { base: x0, delta: deltaX } = strategyParameterBuilder.findXParameters(
|
|
13172
|
-
amountX,
|
|
13173
|
-
minDeltaIdAskSide,
|
|
13174
|
-
maxDeltaId,
|
|
13175
|
-
binStep,
|
|
13176
|
-
activeId
|
|
13177
|
-
);
|
|
13178
|
-
return {
|
|
13179
|
-
x0,
|
|
13180
|
-
deltaX,
|
|
13181
|
-
y0,
|
|
13182
|
-
deltaY
|
|
13183
|
-
};
|
|
13373
|
+
function toStrategyParameters({
|
|
13374
|
+
maxBinId,
|
|
13375
|
+
minBinId,
|
|
13376
|
+
strategyType,
|
|
13377
|
+
singleSidedX
|
|
13378
|
+
}) {
|
|
13379
|
+
const parameteres = new Array(64).fill(0);
|
|
13380
|
+
const favorSide = singleSidedX ? 1 : 0;
|
|
13381
|
+
parameteres[0] = favorSide;
|
|
13382
|
+
switch (strategyType) {
|
|
13383
|
+
case 0 /* Spot */: {
|
|
13384
|
+
return {
|
|
13385
|
+
minBinId,
|
|
13386
|
+
maxBinId,
|
|
13387
|
+
strategyType: { spotImBalanced: {} },
|
|
13388
|
+
parameteres
|
|
13389
|
+
};
|
|
13390
|
+
}
|
|
13391
|
+
case 1 /* Curve */: {
|
|
13392
|
+
return {
|
|
13393
|
+
minBinId,
|
|
13394
|
+
maxBinId,
|
|
13395
|
+
strategyType: { curveImBalanced: {} },
|
|
13396
|
+
parameteres
|
|
13397
|
+
};
|
|
13398
|
+
}
|
|
13399
|
+
case 2 /* BidAsk */: {
|
|
13400
|
+
return {
|
|
13401
|
+
minBinId,
|
|
13402
|
+
maxBinId,
|
|
13403
|
+
strategyType: { bidAskImBalanced: {} },
|
|
13404
|
+
parameteres
|
|
13405
|
+
};
|
|
13406
|
+
}
|
|
13407
|
+
}
|
|
13184
13408
|
}
|
|
13185
13409
|
|
|
13186
13410
|
// src/dlmm/helpers/index.ts
|
|
@@ -13285,359 +13509,403 @@ var unwrapSOLInstruction = async (owner, allowOwnerOffCurve = true) => {
|
|
|
13285
13509
|
);
|
|
13286
13510
|
if (wSolATAAccount) {
|
|
13287
13511
|
const closedWrappedSolInstruction = _spltoken.createCloseAccountInstruction.call(void 0,
|
|
13288
|
-
wSolATAAccount,
|
|
13289
|
-
owner,
|
|
13290
|
-
owner,
|
|
13291
|
-
[],
|
|
13292
|
-
_spltoken.TOKEN_PROGRAM_ID
|
|
13293
|
-
);
|
|
13294
|
-
return closedWrappedSolInstruction;
|
|
13295
|
-
}
|
|
13296
|
-
return null;
|
|
13297
|
-
};
|
|
13298
|
-
async function chunkedGetMultipleAccountInfos(connection, pks, chunkSize = 100) {
|
|
13299
|
-
const accountInfos = (await Promise.all(
|
|
13300
|
-
chunks(pks, chunkSize).map(
|
|
13301
|
-
(chunk) => connection.getMultipleAccountsInfo(chunk)
|
|
13302
|
-
)
|
|
13303
|
-
)).flat();
|
|
13304
|
-
return accountInfos;
|
|
13305
|
-
}
|
|
13306
|
-
var getEstimatedComputeUnitUsageWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
13307
|
-
if (!buffer) {
|
|
13308
|
-
buffer = 0.1;
|
|
13309
|
-
}
|
|
13310
|
-
buffer = Math.max(0, buffer);
|
|
13311
|
-
buffer = Math.min(1, buffer);
|
|
13312
|
-
const estimatedComputeUnitUsage = await getSimulationComputeUnits(
|
|
13313
|
-
connection,
|
|
13314
|
-
instructions,
|
|
13315
|
-
feePayer,
|
|
13316
|
-
[]
|
|
13317
|
-
);
|
|
13318
|
-
let extraComputeUnitBuffer = estimatedComputeUnitUsage * buffer;
|
|
13319
|
-
if (extraComputeUnitBuffer > MAX_CU_BUFFER) {
|
|
13320
|
-
extraComputeUnitBuffer = MAX_CU_BUFFER;
|
|
13321
|
-
} else if (extraComputeUnitBuffer < MIN_CU_BUFFER) {
|
|
13322
|
-
extraComputeUnitBuffer = MIN_CU_BUFFER;
|
|
13323
|
-
}
|
|
13324
|
-
return estimatedComputeUnitUsage + extraComputeUnitBuffer;
|
|
13325
|
-
};
|
|
13326
|
-
var getEstimatedComputeUnitIxWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
13327
|
-
const units = await getEstimatedComputeUnitUsageWithBuffer(
|
|
13328
|
-
connection,
|
|
13329
|
-
instructions,
|
|
13330
|
-
feePayer,
|
|
13331
|
-
buffer
|
|
13332
|
-
).catch((error) => {
|
|
13333
|
-
console.error("Error::getEstimatedComputeUnitUsageWithBuffer", error);
|
|
13334
|
-
return 14e5;
|
|
13335
|
-
});
|
|
13336
|
-
return _web3js.ComputeBudgetProgram.setComputeUnitLimit({ units });
|
|
13337
|
-
};
|
|
13338
|
-
function createProgram(connection, opt) {
|
|
13339
|
-
const cluster = _optionalChain([opt, 'optionalAccess', _43 => _43.cluster]) || "mainnet-beta";
|
|
13340
|
-
const provider = new (0, _anchor.AnchorProvider)(
|
|
13341
|
-
connection,
|
|
13342
|
-
{},
|
|
13343
|
-
_anchor.AnchorProvider.defaultOptions()
|
|
13344
|
-
);
|
|
13345
|
-
return new (0, _anchor.Program)(
|
|
13346
|
-
{ ...dlmm_default, address: LBCLMM_PROGRAM_IDS[cluster] },
|
|
13347
|
-
provider
|
|
13348
|
-
);
|
|
13349
|
-
}
|
|
13350
|
-
function decodeAccount(program, accountName, buffer) {
|
|
13351
|
-
return program.coder.accounts.decode(accountName, buffer);
|
|
13352
|
-
}
|
|
13353
|
-
function getAccountDiscriminator(accountName) {
|
|
13354
|
-
return _optionalChain([dlmm_default, 'access', _44 => _44.accounts, 'access', _45 => _45.find, 'call', _46 => _46(
|
|
13355
|
-
(acc) => acc.name.toLowerCase() === accountName.toLowerCase()
|
|
13356
|
-
), 'optionalAccess', _47 => _47.discriminator]);
|
|
13357
|
-
}
|
|
13358
|
-
function capSlippagePercentage(slippage) {
|
|
13359
|
-
if (slippage > 100) {
|
|
13360
|
-
slippage = 100;
|
|
13361
|
-
}
|
|
13362
|
-
if (slippage < 0) {
|
|
13363
|
-
slippage = 0;
|
|
13364
|
-
}
|
|
13365
|
-
return slippage;
|
|
13366
|
-
}
|
|
13367
|
-
|
|
13368
|
-
// src/dlmm/helpers/accountFilters.ts
|
|
13369
|
-
var _bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
|
|
13370
|
-
var presetParameter2BinStepFilter = (binStep) => {
|
|
13371
|
-
return {
|
|
13372
|
-
memcmp: {
|
|
13373
|
-
bytes: _bytes.bs58.encode(binStep.toArrayLike(Buffer, "le", 2)),
|
|
13374
|
-
offset: 8
|
|
13375
|
-
}
|
|
13376
|
-
};
|
|
13377
|
-
};
|
|
13378
|
-
var presetParameter2BaseFactorFilter = (baseFactor) => {
|
|
13379
|
-
return {
|
|
13380
|
-
memcmp: {
|
|
13381
|
-
bytes: _bytes.bs58.encode(baseFactor.toArrayLike(Buffer, "le", 2)),
|
|
13382
|
-
offset: 8 + 2
|
|
13383
|
-
}
|
|
13384
|
-
};
|
|
13385
|
-
};
|
|
13386
|
-
var presetParameter2BaseFeePowerFactor = (baseFeePowerFactor) => {
|
|
13387
|
-
return {
|
|
13388
|
-
memcmp: {
|
|
13389
|
-
bytes: _bytes.bs58.encode(baseFeePowerFactor.toArrayLike(Buffer, "le", 1)),
|
|
13390
|
-
offset: 8 + 22
|
|
13391
|
-
}
|
|
13392
|
-
};
|
|
13393
|
-
};
|
|
13394
|
-
var binArrayLbPairFilter = (lbPair) => {
|
|
13395
|
-
return {
|
|
13396
|
-
memcmp: {
|
|
13397
|
-
bytes: lbPair.toBase58(),
|
|
13398
|
-
offset: 8 + 16
|
|
13399
|
-
}
|
|
13400
|
-
};
|
|
13401
|
-
};
|
|
13402
|
-
var positionOwnerFilter = (owner) => {
|
|
13403
|
-
return {
|
|
13404
|
-
memcmp: {
|
|
13405
|
-
bytes: owner.toBase58(),
|
|
13406
|
-
offset: 8 + 32
|
|
13407
|
-
}
|
|
13408
|
-
};
|
|
13409
|
-
};
|
|
13410
|
-
var positionLbPairFilter = (lbPair) => {
|
|
13411
|
-
return {
|
|
13412
|
-
memcmp: {
|
|
13413
|
-
bytes: _bytes.bs58.encode(lbPair.toBuffer()),
|
|
13414
|
-
offset: 8
|
|
13415
|
-
}
|
|
13416
|
-
};
|
|
13417
|
-
};
|
|
13418
|
-
var positionV2Filter = () => {
|
|
13419
|
-
return {
|
|
13420
|
-
memcmp: {
|
|
13421
|
-
bytes: _bytes.bs58.encode(Buffer.from(getAccountDiscriminator("positionV2"))),
|
|
13422
|
-
offset: 0
|
|
13423
|
-
}
|
|
13424
|
-
};
|
|
13425
|
-
};
|
|
13426
|
-
|
|
13427
|
-
// src/dlmm/helpers/positions/index.ts
|
|
13428
|
-
|
|
13429
|
-
|
|
13430
|
-
// src/dlmm/helpers/positions/wrapper.ts
|
|
13431
|
-
|
|
13432
|
-
function combineBaseAndExtendedPositionBinData(base, extended) {
|
|
13433
|
-
const combinedLiquidityShares = base.liquidityShares;
|
|
13434
|
-
const combinedRewardInfos = base.rewardInfos;
|
|
13435
|
-
const combinedFeeInfos = base.feeInfos;
|
|
13436
|
-
for (const binData of extended) {
|
|
13437
|
-
combinedLiquidityShares.push(binData.liquidityShare);
|
|
13438
|
-
combinedRewardInfos.push(binData.rewardInfo);
|
|
13439
|
-
combinedFeeInfos.push(binData.feeInfo);
|
|
13440
|
-
}
|
|
13441
|
-
return {
|
|
13442
|
-
liquidityShares: combinedLiquidityShares,
|
|
13443
|
-
rewardInfos: combinedRewardInfos,
|
|
13444
|
-
feeInfos: combinedFeeInfos
|
|
13445
|
-
};
|
|
13446
|
-
}
|
|
13447
|
-
function wrapPosition(program, key, account) {
|
|
13448
|
-
const disc = account.data.subarray(0, 8);
|
|
13449
|
-
if (disc.equals(Buffer.from(getAccountDiscriminator("positionV2")))) {
|
|
13450
|
-
const state = decodeAccount(
|
|
13451
|
-
program,
|
|
13452
|
-
"positionV2",
|
|
13453
|
-
account.data
|
|
13454
|
-
);
|
|
13455
|
-
const extended = decodeExtendedPosition(
|
|
13456
|
-
state,
|
|
13457
|
-
program,
|
|
13458
|
-
account.data.subarray(8 + POSITION_MIN_SIZE)
|
|
13459
|
-
);
|
|
13460
|
-
const combinedPositionBinData = combineBaseAndExtendedPositionBinData(
|
|
13461
|
-
state,
|
|
13462
|
-
extended
|
|
13463
|
-
);
|
|
13464
|
-
return new PositionV2Wrapper(key, state, extended, combinedPositionBinData);
|
|
13465
|
-
} else {
|
|
13466
|
-
throw new Error("Unknown position account");
|
|
13467
|
-
}
|
|
13468
|
-
}
|
|
13469
|
-
var PositionV2Wrapper = class {
|
|
13470
|
-
constructor(positionAddress, inner, extended, combinedPositionBinData) {
|
|
13471
|
-
this.positionAddress = positionAddress;
|
|
13472
|
-
this.inner = inner;
|
|
13473
|
-
this.extended = extended;
|
|
13474
|
-
this.combinedPositionBinData = combinedPositionBinData;
|
|
13475
|
-
}
|
|
13476
|
-
address() {
|
|
13477
|
-
return this.positionAddress;
|
|
13478
|
-
}
|
|
13479
|
-
totalClaimedRewards() {
|
|
13480
|
-
return this.inner.totalClaimedRewards;
|
|
13481
|
-
}
|
|
13482
|
-
feeOwner() {
|
|
13483
|
-
return this.inner.feeOwner;
|
|
13484
|
-
}
|
|
13485
|
-
lockReleasePoint() {
|
|
13486
|
-
return this.inner.lockReleasePoint;
|
|
13487
|
-
}
|
|
13488
|
-
operator() {
|
|
13489
|
-
return this.inner.operator;
|
|
13490
|
-
}
|
|
13491
|
-
totalClaimedFeeYAmount() {
|
|
13492
|
-
return this.inner.totalClaimedFeeYAmount;
|
|
13493
|
-
}
|
|
13494
|
-
totalClaimedFeeXAmount() {
|
|
13495
|
-
return this.inner.totalClaimedFeeXAmount;
|
|
13496
|
-
}
|
|
13497
|
-
lbPair() {
|
|
13498
|
-
return this.inner.lbPair;
|
|
13499
|
-
}
|
|
13500
|
-
lowerBinId() {
|
|
13501
|
-
return new (0, _bnjs2.default)(this.inner.lowerBinId);
|
|
13502
|
-
}
|
|
13503
|
-
upperBinId() {
|
|
13504
|
-
return new (0, _bnjs2.default)(this.inner.upperBinId);
|
|
13505
|
-
}
|
|
13506
|
-
liquidityShares() {
|
|
13507
|
-
return this.combinedPositionBinData.liquidityShares;
|
|
13508
|
-
}
|
|
13509
|
-
rewardInfos() {
|
|
13510
|
-
return this.combinedPositionBinData.rewardInfos;
|
|
13511
|
-
}
|
|
13512
|
-
feeInfos() {
|
|
13513
|
-
return this.combinedPositionBinData.feeInfos;
|
|
13514
|
-
}
|
|
13515
|
-
lastUpdatedAt() {
|
|
13516
|
-
return this.inner.lastUpdatedAt;
|
|
13517
|
-
}
|
|
13518
|
-
getBinArrayIndexesCoverage() {
|
|
13519
|
-
const isExtended = this.extended.length > 0;
|
|
13520
|
-
if (isExtended) {
|
|
13521
|
-
return getBinArrayIndexesCoverage(this.lowerBinId(), this.upperBinId());
|
|
13522
|
-
} else {
|
|
13523
|
-
const lowerBinArrayIndex = binIdToBinArrayIndex(this.lowerBinId());
|
|
13524
|
-
const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _bnjs2.default)(1));
|
|
13525
|
-
return [lowerBinArrayIndex, upperBinArrayIndex];
|
|
13526
|
-
}
|
|
13527
|
-
}
|
|
13528
|
-
getBinArrayKeysCoverage(programId) {
|
|
13529
|
-
return this.getBinArrayIndexesCoverage().map(
|
|
13530
|
-
(index) => deriveBinArray(this.lbPair(), index, programId)[0]
|
|
13512
|
+
wSolATAAccount,
|
|
13513
|
+
owner,
|
|
13514
|
+
owner,
|
|
13515
|
+
[],
|
|
13516
|
+
_spltoken.TOKEN_PROGRAM_ID
|
|
13531
13517
|
);
|
|
13518
|
+
return closedWrappedSolInstruction;
|
|
13532
13519
|
}
|
|
13533
|
-
|
|
13534
|
-
|
|
13535
|
-
|
|
13536
|
-
|
|
13537
|
-
|
|
13520
|
+
return null;
|
|
13521
|
+
};
|
|
13522
|
+
async function chunkedGetMultipleAccountInfos(connection, pks, chunkSize = 100) {
|
|
13523
|
+
const accountInfos = (await Promise.all(
|
|
13524
|
+
chunks(pks, chunkSize).map(
|
|
13525
|
+
(chunk) => connection.getMultipleAccountsInfo(chunk)
|
|
13526
|
+
)
|
|
13527
|
+
)).flat();
|
|
13528
|
+
return accountInfos;
|
|
13529
|
+
}
|
|
13530
|
+
var getEstimatedComputeUnitUsageWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
13531
|
+
if (!buffer) {
|
|
13532
|
+
buffer = 0.1;
|
|
13538
13533
|
}
|
|
13539
|
-
|
|
13540
|
-
|
|
13534
|
+
buffer = Math.max(0, buffer);
|
|
13535
|
+
buffer = Math.min(1, buffer);
|
|
13536
|
+
const estimatedComputeUnitUsage = await getSimulationComputeUnits(
|
|
13537
|
+
connection,
|
|
13538
|
+
instructions,
|
|
13539
|
+
feePayer,
|
|
13540
|
+
[]
|
|
13541
|
+
);
|
|
13542
|
+
let extraComputeUnitBuffer = estimatedComputeUnitUsage * buffer;
|
|
13543
|
+
if (extraComputeUnitBuffer > MAX_CU_BUFFER) {
|
|
13544
|
+
extraComputeUnitBuffer = MAX_CU_BUFFER;
|
|
13545
|
+
} else if (extraComputeUnitBuffer < MIN_CU_BUFFER) {
|
|
13546
|
+
extraComputeUnitBuffer = MIN_CU_BUFFER;
|
|
13541
13547
|
}
|
|
13548
|
+
return estimatedComputeUnitUsage + extraComputeUnitBuffer;
|
|
13542
13549
|
};
|
|
13543
|
-
|
|
13544
|
-
|
|
13545
|
-
|
|
13546
|
-
|
|
13547
|
-
|
|
13548
|
-
|
|
13549
|
-
|
|
13550
|
-
|
|
13551
|
-
|
|
13552
|
-
return binArrayIndexes;
|
|
13553
|
-
}
|
|
13554
|
-
function getBinArrayKeysCoverage2(lowerBinId, upperBinId, lbPair, programId) {
|
|
13555
|
-
const binArrayIndexes = getBinArrayIndexesCoverage(lowerBinId, upperBinId);
|
|
13556
|
-
return binArrayIndexes.map((index) => {
|
|
13557
|
-
return deriveBinArray(lbPair, index, programId)[0];
|
|
13550
|
+
var getEstimatedComputeUnitIxWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
13551
|
+
const units = await getEstimatedComputeUnitUsageWithBuffer(
|
|
13552
|
+
connection,
|
|
13553
|
+
instructions,
|
|
13554
|
+
feePayer,
|
|
13555
|
+
buffer
|
|
13556
|
+
).catch((error) => {
|
|
13557
|
+
console.error("Error::getEstimatedComputeUnitUsageWithBuffer", error);
|
|
13558
|
+
return 14e5;
|
|
13558
13559
|
});
|
|
13559
|
-
}
|
|
13560
|
-
|
|
13561
|
-
|
|
13562
|
-
|
|
13563
|
-
|
|
13564
|
-
|
|
13565
|
-
|
|
13566
|
-
|
|
13567
|
-
};
|
|
13568
|
-
}
|
|
13560
|
+
return _web3js.ComputeBudgetProgram.setComputeUnitLimit({ units });
|
|
13561
|
+
};
|
|
13562
|
+
function createProgram(connection, opt) {
|
|
13563
|
+
const cluster = _optionalChain([opt, 'optionalAccess', _43 => _43.cluster]) || "mainnet-beta";
|
|
13564
|
+
const provider = new (0, _anchor.AnchorProvider)(
|
|
13565
|
+
connection,
|
|
13566
|
+
{},
|
|
13567
|
+
_anchor.AnchorProvider.defaultOptions()
|
|
13569
13568
|
);
|
|
13570
|
-
|
|
13571
|
-
|
|
13572
|
-
|
|
13573
|
-
(b) => !new (0, _bnjs2.default)(b.binLiquidity).isZero() || !new (0, _bnjs2.default)(b.positionFeeXAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionFeeYAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[0].toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[1].toString()).isZero()
|
|
13569
|
+
return new (0, _anchor.Program)(
|
|
13570
|
+
{ ...dlmm_default, address: LBCLMM_PROGRAM_IDS[cluster] },
|
|
13571
|
+
provider
|
|
13574
13572
|
);
|
|
13575
|
-
return binWithLiquidity.length > 0 ? {
|
|
13576
|
-
lowerBinId: new (0, _bnjs2.default)(binWithLiquidity[0].binId),
|
|
13577
|
-
upperBinId: new (0, _bnjs2.default)(binWithLiquidity[binWithLiquidity.length - 1].binId)
|
|
13578
|
-
} : null;
|
|
13579
13573
|
}
|
|
13580
|
-
function
|
|
13581
|
-
return
|
|
13574
|
+
function decodeAccount(program, accountName, buffer) {
|
|
13575
|
+
return program.coder.accounts.decode(accountName, buffer);
|
|
13582
13576
|
}
|
|
13583
|
-
function
|
|
13584
|
-
return
|
|
13577
|
+
function getAccountDiscriminator(accountName) {
|
|
13578
|
+
return _optionalChain([dlmm_default, 'access', _44 => _44.accounts, 'access', _45 => _45.find, 'call', _46 => _46(
|
|
13579
|
+
(acc) => acc.name.toLowerCase() === accountName.toLowerCase()
|
|
13580
|
+
), 'optionalAccess', _47 => _47.discriminator]);
|
|
13585
13581
|
}
|
|
13586
|
-
function
|
|
13587
|
-
|
|
13588
|
-
|
|
13589
|
-
while (startBinId <= maxBinId) {
|
|
13590
|
-
const endBinId = Math.min(
|
|
13591
|
-
startBinId + DEFAULT_BIN_PER_POSITION.toNumber() - 1,
|
|
13592
|
-
maxBinId
|
|
13593
|
-
);
|
|
13594
|
-
chunkedBinRange.push({
|
|
13595
|
-
lowerBinId: startBinId,
|
|
13596
|
-
upperBinId: endBinId
|
|
13597
|
-
});
|
|
13598
|
-
startBinId += DEFAULT_BIN_PER_POSITION.toNumber();
|
|
13582
|
+
function capSlippagePercentage(slippage) {
|
|
13583
|
+
if (slippage > 100) {
|
|
13584
|
+
slippage = 100;
|
|
13599
13585
|
}
|
|
13600
|
-
|
|
13601
|
-
|
|
13602
|
-
async function getPositionExpandRentExemption(currentMinBinId, currentMaxBinId, connection, binCountToExpand) {
|
|
13603
|
-
const currentPositionWidth = currentMaxBinId.sub(currentMinBinId).addn(1);
|
|
13604
|
-
const positionWidthAfterExpand = currentPositionWidth.add(binCountToExpand);
|
|
13605
|
-
if (positionWidthAfterExpand.lte(DEFAULT_BIN_PER_POSITION)) {
|
|
13606
|
-
return 0;
|
|
13607
|
-
} else {
|
|
13608
|
-
const binCountInExpandedBytes = positionWidthAfterExpand.sub(
|
|
13609
|
-
DEFAULT_BIN_PER_POSITION
|
|
13610
|
-
);
|
|
13611
|
-
const expandSize = binCountInExpandedBytes.toNumber() * POSITION_BIN_DATA_SIZE;
|
|
13612
|
-
const [minimumLamports, rentExemptionLamports] = await Promise.all([
|
|
13613
|
-
connection.getMinimumBalanceForRentExemption(0),
|
|
13614
|
-
connection.getMinimumBalanceForRentExemption(expandSize)
|
|
13615
|
-
]);
|
|
13616
|
-
return rentExemptionLamports - minimumLamports;
|
|
13586
|
+
if (slippage < 0) {
|
|
13587
|
+
slippage = 0;
|
|
13617
13588
|
}
|
|
13589
|
+
return slippage;
|
|
13618
13590
|
}
|
|
13619
|
-
function
|
|
13620
|
-
|
|
13621
|
-
const extended = width.sub(DEFAULT_BIN_PER_POSITION);
|
|
13622
|
-
return extended.lte(new (0, _bnjs2.default)(0)) ? new (0, _bnjs2.default)(0) : extended;
|
|
13591
|
+
function getAndCapMaxActiveBinSlippage(slippagePercentage, binStep, maxActiveBinSlippage) {
|
|
13592
|
+
return slippagePercentage ? Math.ceil(slippagePercentage / (binStep / 100)) : maxActiveBinSlippage;
|
|
13623
13593
|
}
|
|
13624
|
-
function
|
|
13625
|
-
|
|
13626
|
-
|
|
13627
|
-
|
|
13628
|
-
|
|
13629
|
-
|
|
13630
|
-
|
|
13631
|
-
|
|
13632
|
-
|
|
13633
|
-
|
|
13634
|
-
|
|
13594
|
+
function getBinCount(minBinId, maxBinId) {
|
|
13595
|
+
return maxBinId - minBinId + 1;
|
|
13596
|
+
}
|
|
13597
|
+
function getSlippageMaxAmount(amount, slippage) {
|
|
13598
|
+
return slippage == 100 ? U64_MAX : amount.muln(100 + slippage).divn(100);
|
|
13599
|
+
}
|
|
13600
|
+
function getSlippageMinAmount(amount, slippage) {
|
|
13601
|
+
return amount.muln(100 - slippage).divn(100);
|
|
13602
|
+
}
|
|
13603
|
+
function getPositionCountByBinCount(binCount) {
|
|
13604
|
+
return Math.ceil(binCount / MAX_BINS_PER_POSITION.toNumber());
|
|
13605
|
+
}
|
|
13606
|
+
function resetUninvolvedLiquidityParams(minDeltaId, maxDeltaId, favorXInActiveId, params) {
|
|
13607
|
+
const endBidSideDeltaId = favorXInActiveId ? new (0, _anchor.BN)(-1) : new (0, _anchor.BN)(0);
|
|
13608
|
+
const startAskSideDeltaId = endBidSideDeltaId.addn(1);
|
|
13609
|
+
let x0 = params.x0;
|
|
13610
|
+
let y0 = params.y0;
|
|
13611
|
+
let deltaX = params.deltaX;
|
|
13612
|
+
let deltaY = params.deltaY;
|
|
13613
|
+
if (maxDeltaId.lte(endBidSideDeltaId)) {
|
|
13614
|
+
deltaX = new (0, _anchor.BN)(0);
|
|
13615
|
+
x0 = new (0, _anchor.BN)(0);
|
|
13616
|
+
}
|
|
13617
|
+
if (minDeltaId.gte(startAskSideDeltaId)) {
|
|
13618
|
+
deltaY = new (0, _anchor.BN)(0);
|
|
13619
|
+
y0 = new (0, _anchor.BN)(0);
|
|
13620
|
+
}
|
|
13621
|
+
return {
|
|
13622
|
+
x0,
|
|
13623
|
+
y0,
|
|
13624
|
+
deltaX,
|
|
13625
|
+
deltaY
|
|
13626
|
+
};
|
|
13627
|
+
}
|
|
13628
|
+
async function chunkDepositWithRebalanceEndpoint(dlmm, strategy, slippagePercentage, maxActiveBinSlippage, position, positionMinBinId, positionMaxBinId, liquidityStrategyParameters, owner, payer, simulateCU) {
|
|
13629
|
+
const { slices, accounts: transferHookAccounts } = dlmm.getPotentialToken2022IxDataAndAccounts(0 /* Liquidity */);
|
|
13630
|
+
const userTokenX = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
13631
|
+
dlmm.lbPair.tokenXMint,
|
|
13632
|
+
owner,
|
|
13633
|
+
true,
|
|
13634
|
+
dlmm.tokenX.owner
|
|
13635
|
+
);
|
|
13636
|
+
const userTokenY = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
13637
|
+
dlmm.lbPair.tokenYMint,
|
|
13638
|
+
owner,
|
|
13639
|
+
true,
|
|
13640
|
+
dlmm.tokenY.owner
|
|
13641
|
+
);
|
|
13642
|
+
const createUserTokenXIx = _spltoken.createAssociatedTokenAccountIdempotentInstruction.call(void 0,
|
|
13643
|
+
payer,
|
|
13644
|
+
userTokenX,
|
|
13645
|
+
owner,
|
|
13646
|
+
dlmm.lbPair.tokenXMint,
|
|
13647
|
+
dlmm.tokenX.owner
|
|
13648
|
+
);
|
|
13649
|
+
const createUserTokenYIx = _spltoken.createAssociatedTokenAccountIdempotentInstruction.call(void 0,
|
|
13650
|
+
payer,
|
|
13651
|
+
userTokenY,
|
|
13652
|
+
owner,
|
|
13653
|
+
dlmm.lbPair.tokenYMint,
|
|
13654
|
+
dlmm.tokenY.owner
|
|
13655
|
+
);
|
|
13656
|
+
const bitmapPubkey = deriveBinArrayBitmapExtension(
|
|
13657
|
+
dlmm.pubkey,
|
|
13658
|
+
dlmm.program.programId
|
|
13659
|
+
)[0];
|
|
13660
|
+
let calculatedAddLiquidityCU = 0;
|
|
13661
|
+
const chunkedAddLiquidityIx = [];
|
|
13662
|
+
const chunkedBinRange = chunkBinRange(positionMinBinId, positionMaxBinId);
|
|
13663
|
+
const binArrayOrBitmapInitTracking = /* @__PURE__ */ new Set();
|
|
13664
|
+
for (let i = 0; i < chunkedBinRange.length; i++) {
|
|
13665
|
+
const chunkMinBinId = chunkedBinRange[i].lowerBinId;
|
|
13666
|
+
const chunkMaxBinId = chunkedBinRange[i].upperBinId;
|
|
13667
|
+
const initBinArrayIxs = [];
|
|
13668
|
+
const initBitmapIxs = [];
|
|
13669
|
+
const binArrayIndexes = getBinArrayIndexesCoverage(
|
|
13670
|
+
new (0, _anchor.BN)(chunkMinBinId),
|
|
13671
|
+
new (0, _anchor.BN)(chunkMaxBinId)
|
|
13635
13672
|
);
|
|
13636
|
-
|
|
13673
|
+
const overflowDefaultBinArrayBitmap = binArrayIndexes.reduce(
|
|
13674
|
+
(acc, binArrayIndex) => acc || isOverflowDefaultBinArrayBitmap(binArrayIndex),
|
|
13675
|
+
false
|
|
13676
|
+
);
|
|
13677
|
+
if (overflowDefaultBinArrayBitmap && !binArrayOrBitmapInitTracking.has(bitmapPubkey.toBase58())) {
|
|
13678
|
+
const initBitmapIx = await dlmm.program.methods.initializeBinArrayBitmapExtension().accountsPartial({
|
|
13679
|
+
binArrayBitmapExtension: bitmapPubkey,
|
|
13680
|
+
lbPair: dlmm.pubkey,
|
|
13681
|
+
funder: payer
|
|
13682
|
+
}).instruction();
|
|
13683
|
+
initBitmapIxs.push(initBitmapIx);
|
|
13684
|
+
binArrayOrBitmapInitTracking.add(bitmapPubkey.toBase58());
|
|
13685
|
+
}
|
|
13686
|
+
const binArrayPubkeys = binArrayIndexes.map(
|
|
13687
|
+
(index) => deriveBinArray(dlmm.pubkey, index, dlmm.program.programId)[0]
|
|
13688
|
+
);
|
|
13689
|
+
for (const [idx, binArrayPubkey] of binArrayPubkeys.entries()) {
|
|
13690
|
+
if (!binArrayOrBitmapInitTracking.has(binArrayPubkey.toBase58())) {
|
|
13691
|
+
const initBinArrayIx = await dlmm.program.methods.initializeBinArray(binArrayIndexes[idx]).accountsPartial({
|
|
13692
|
+
binArray: binArrayPubkey,
|
|
13693
|
+
funder: payer,
|
|
13694
|
+
lbPair: dlmm.pubkey
|
|
13695
|
+
}).instruction();
|
|
13696
|
+
binArrayOrBitmapInitTracking.add(binArrayPubkey.toBase58());
|
|
13697
|
+
initBinArrayIxs.push(initBinArrayIx);
|
|
13698
|
+
calculatedAddLiquidityCU += DEFAULT_INIT_BIN_ARRAY_CU;
|
|
13699
|
+
}
|
|
13700
|
+
}
|
|
13701
|
+
const minDeltaId = new (0, _anchor.BN)(chunkMinBinId - dlmm.lbPair.activeId);
|
|
13702
|
+
const maxDeltaId = new (0, _anchor.BN)(chunkMaxBinId - dlmm.lbPair.activeId);
|
|
13703
|
+
const { bitFlag, ...baseAndDelta } = buildBitFlagAndNegateStrategyParameters(
|
|
13704
|
+
liquidityStrategyParameters.x0,
|
|
13705
|
+
liquidityStrategyParameters.y0,
|
|
13706
|
+
liquidityStrategyParameters.deltaX,
|
|
13707
|
+
liquidityStrategyParameters.deltaY
|
|
13708
|
+
);
|
|
13709
|
+
const { deltaX, deltaY, x0, y0 } = resetUninvolvedLiquidityParams(
|
|
13710
|
+
minDeltaId,
|
|
13711
|
+
maxDeltaId,
|
|
13712
|
+
strategy.singleSidedX,
|
|
13713
|
+
{
|
|
13714
|
+
...baseAndDelta
|
|
13715
|
+
}
|
|
13716
|
+
);
|
|
13717
|
+
const addParam = {
|
|
13718
|
+
minDeltaId: minDeltaId.toNumber(),
|
|
13719
|
+
maxDeltaId: maxDeltaId.toNumber(),
|
|
13720
|
+
x0,
|
|
13721
|
+
y0,
|
|
13722
|
+
deltaX,
|
|
13723
|
+
deltaY,
|
|
13724
|
+
bitFlag,
|
|
13725
|
+
favorXInActiveId: strategy.singleSidedX,
|
|
13726
|
+
padding: Array(36).fill(0)
|
|
13727
|
+
};
|
|
13728
|
+
const { totalXAmount, totalYAmount } = toAmountIntoBins(
|
|
13729
|
+
new (0, _anchor.BN)(dlmm.lbPair.activeId),
|
|
13730
|
+
minDeltaId,
|
|
13731
|
+
maxDeltaId,
|
|
13732
|
+
deltaX,
|
|
13733
|
+
deltaY,
|
|
13734
|
+
x0,
|
|
13735
|
+
y0,
|
|
13736
|
+
new (0, _anchor.BN)(dlmm.lbPair.binStep),
|
|
13737
|
+
strategy.singleSidedX
|
|
13738
|
+
).reduce(
|
|
13739
|
+
(acc, bin) => {
|
|
13740
|
+
return {
|
|
13741
|
+
totalXAmount: acc.totalXAmount.add(bin.amountX),
|
|
13742
|
+
totalYAmount: acc.totalYAmount.add(bin.amountY)
|
|
13743
|
+
};
|
|
13744
|
+
},
|
|
13745
|
+
{
|
|
13746
|
+
totalXAmount: new (0, _anchor.BN)(0),
|
|
13747
|
+
totalYAmount: new (0, _anchor.BN)(0)
|
|
13748
|
+
}
|
|
13749
|
+
);
|
|
13750
|
+
const totalXAmountIncludeTransferFee = calculateTransferFeeIncludedAmount(
|
|
13751
|
+
totalXAmount,
|
|
13752
|
+
dlmm.tokenX.mint,
|
|
13753
|
+
dlmm.clock.epoch.toNumber()
|
|
13754
|
+
).amount;
|
|
13755
|
+
const totalYAmountIncludeTransferFee = calculateTransferFeeIncludedAmount(
|
|
13756
|
+
totalYAmount,
|
|
13757
|
+
dlmm.tokenY.mint,
|
|
13758
|
+
dlmm.clock.epoch.toNumber()
|
|
13759
|
+
).amount;
|
|
13760
|
+
const maxDepositXAmount = getSlippageMaxAmount(
|
|
13761
|
+
totalXAmountIncludeTransferFee,
|
|
13762
|
+
slippagePercentage
|
|
13763
|
+
);
|
|
13764
|
+
const maxDepositYAmount = getSlippageMaxAmount(
|
|
13765
|
+
totalYAmountIncludeTransferFee,
|
|
13766
|
+
slippagePercentage
|
|
13767
|
+
);
|
|
13768
|
+
const rebalanceIx = await dlmm.program.methods.rebalanceLiquidity(
|
|
13769
|
+
{
|
|
13770
|
+
activeId: dlmm.lbPair.activeId,
|
|
13771
|
+
maxActiveBinSlippage,
|
|
13772
|
+
shouldClaimFee: false,
|
|
13773
|
+
shouldClaimReward: false,
|
|
13774
|
+
minWithdrawXAmount: new (0, _anchor.BN)(0),
|
|
13775
|
+
minWithdrawYAmount: new (0, _anchor.BN)(0),
|
|
13776
|
+
maxDepositXAmount,
|
|
13777
|
+
maxDepositYAmount,
|
|
13778
|
+
removes: [],
|
|
13779
|
+
adds: [addParam],
|
|
13780
|
+
padding: Array(32).fill(0)
|
|
13781
|
+
},
|
|
13782
|
+
{
|
|
13783
|
+
slices
|
|
13784
|
+
}
|
|
13785
|
+
).accountsPartial({
|
|
13786
|
+
binArrayBitmapExtension: initBitmapIxs.length > 0 ? bitmapPubkey : dlmm.program.programId,
|
|
13787
|
+
lbPair: dlmm.pubkey,
|
|
13788
|
+
position,
|
|
13789
|
+
owner,
|
|
13790
|
+
tokenXMint: dlmm.lbPair.tokenXMint,
|
|
13791
|
+
tokenYMint: dlmm.lbPair.tokenYMint,
|
|
13792
|
+
userTokenX,
|
|
13793
|
+
userTokenY,
|
|
13794
|
+
tokenXProgram: dlmm.tokenX.owner,
|
|
13795
|
+
tokenYProgram: dlmm.tokenY.owner,
|
|
13796
|
+
rentPayer: payer
|
|
13797
|
+
}).remainingAccounts([
|
|
13798
|
+
...transferHookAccounts,
|
|
13799
|
+
...binArrayPubkeys.map((baPubkey) => ({
|
|
13800
|
+
pubkey: baPubkey,
|
|
13801
|
+
isWritable: true,
|
|
13802
|
+
isSigner: false
|
|
13803
|
+
})),
|
|
13804
|
+
derivePlaceHolderAccountMeta(dlmm.program.programId)
|
|
13805
|
+
]).instruction();
|
|
13806
|
+
calculatedAddLiquidityCU += DEFAULT_ADD_LIQUIDITY_CU;
|
|
13807
|
+
const addLiquidityIxs = [];
|
|
13808
|
+
addLiquidityIxs.push(
|
|
13809
|
+
...initBitmapIxs,
|
|
13810
|
+
...initBinArrayIxs,
|
|
13811
|
+
createUserTokenXIx,
|
|
13812
|
+
createUserTokenYIx
|
|
13813
|
+
);
|
|
13814
|
+
if (dlmm.tokenX.publicKey.equals(_spltoken.NATIVE_MINT) && !totalXAmount.isZero()) {
|
|
13815
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
13816
|
+
owner,
|
|
13817
|
+
userTokenX,
|
|
13818
|
+
BigInt(totalXAmount.toString())
|
|
13819
|
+
);
|
|
13820
|
+
addLiquidityIxs.push(...wrapSOLIx);
|
|
13821
|
+
}
|
|
13822
|
+
if (dlmm.tokenY.publicKey.equals(_spltoken.NATIVE_MINT) && !totalYAmount.isZero()) {
|
|
13823
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
13824
|
+
owner,
|
|
13825
|
+
userTokenY,
|
|
13826
|
+
BigInt(totalYAmount.toString())
|
|
13827
|
+
);
|
|
13828
|
+
addLiquidityIxs.push(...wrapSOLIx);
|
|
13829
|
+
}
|
|
13830
|
+
addLiquidityIxs.push(rebalanceIx);
|
|
13831
|
+
if (simulateCU) {
|
|
13832
|
+
const cuIx = await getEstimatedComputeUnitIxWithBuffer(
|
|
13833
|
+
dlmm.program.provider.connection,
|
|
13834
|
+
addLiquidityIxs,
|
|
13835
|
+
payer
|
|
13836
|
+
);
|
|
13837
|
+
addLiquidityIxs.unshift(cuIx);
|
|
13838
|
+
} else {
|
|
13839
|
+
addLiquidityIxs.unshift(
|
|
13840
|
+
_web3js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
13841
|
+
units: Math.min(calculatedAddLiquidityCU, MAX_CU)
|
|
13842
|
+
})
|
|
13843
|
+
);
|
|
13844
|
+
}
|
|
13845
|
+
chunkedAddLiquidityIx.push(addLiquidityIxs);
|
|
13637
13846
|
}
|
|
13638
|
-
return
|
|
13847
|
+
return chunkedAddLiquidityIx;
|
|
13639
13848
|
}
|
|
13640
13849
|
|
|
13850
|
+
// src/dlmm/helpers/accountFilters.ts
|
|
13851
|
+
var _bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
|
|
13852
|
+
var presetParameter2BinStepFilter = (binStep) => {
|
|
13853
|
+
return {
|
|
13854
|
+
memcmp: {
|
|
13855
|
+
bytes: _bytes.bs58.encode(binStep.toArrayLike(Buffer, "le", 2)),
|
|
13856
|
+
offset: 8
|
|
13857
|
+
}
|
|
13858
|
+
};
|
|
13859
|
+
};
|
|
13860
|
+
var presetParameter2BaseFactorFilter = (baseFactor) => {
|
|
13861
|
+
return {
|
|
13862
|
+
memcmp: {
|
|
13863
|
+
bytes: _bytes.bs58.encode(baseFactor.toArrayLike(Buffer, "le", 2)),
|
|
13864
|
+
offset: 8 + 2
|
|
13865
|
+
}
|
|
13866
|
+
};
|
|
13867
|
+
};
|
|
13868
|
+
var presetParameter2BaseFeePowerFactor = (baseFeePowerFactor) => {
|
|
13869
|
+
return {
|
|
13870
|
+
memcmp: {
|
|
13871
|
+
bytes: _bytes.bs58.encode(baseFeePowerFactor.toArrayLike(Buffer, "le", 1)),
|
|
13872
|
+
offset: 8 + 22
|
|
13873
|
+
}
|
|
13874
|
+
};
|
|
13875
|
+
};
|
|
13876
|
+
var binArrayLbPairFilter = (lbPair) => {
|
|
13877
|
+
return {
|
|
13878
|
+
memcmp: {
|
|
13879
|
+
bytes: lbPair.toBase58(),
|
|
13880
|
+
offset: 8 + 16
|
|
13881
|
+
}
|
|
13882
|
+
};
|
|
13883
|
+
};
|
|
13884
|
+
var positionOwnerFilter = (owner) => {
|
|
13885
|
+
return {
|
|
13886
|
+
memcmp: {
|
|
13887
|
+
bytes: owner.toBase58(),
|
|
13888
|
+
offset: 8 + 32
|
|
13889
|
+
}
|
|
13890
|
+
};
|
|
13891
|
+
};
|
|
13892
|
+
var positionLbPairFilter = (lbPair) => {
|
|
13893
|
+
return {
|
|
13894
|
+
memcmp: {
|
|
13895
|
+
bytes: _bytes.bs58.encode(lbPair.toBuffer()),
|
|
13896
|
+
offset: 8
|
|
13897
|
+
}
|
|
13898
|
+
};
|
|
13899
|
+
};
|
|
13900
|
+
var positionV2Filter = () => {
|
|
13901
|
+
return {
|
|
13902
|
+
memcmp: {
|
|
13903
|
+
bytes: _bytes.bs58.encode(Buffer.from(getAccountDiscriminator("positionV2"))),
|
|
13904
|
+
offset: 0
|
|
13905
|
+
}
|
|
13906
|
+
};
|
|
13907
|
+
};
|
|
13908
|
+
|
|
13641
13909
|
// src/dlmm/helpers/rebalance/strategy/balanced.ts
|
|
13642
13910
|
|
|
13643
13911
|
|
|
@@ -15487,6 +15755,137 @@ var DLMM = class {
|
|
|
15487
15755
|
version: position.version()
|
|
15488
15756
|
};
|
|
15489
15757
|
}
|
|
15758
|
+
/**
|
|
15759
|
+
* Creates multiple positions and adds liquidity by strategy without chainsaw issues.
|
|
15760
|
+
* @param positionKeypairGenerator A function that generates a specified number of keypairs.
|
|
15761
|
+
* @param totalXAmount The total amount of token X to be added.
|
|
15762
|
+
* @param totalYAmount The total amount of token Y to be added.
|
|
15763
|
+
* @param strategy The strategy for adding liquidity.
|
|
15764
|
+
* @param owner The owner of the position.
|
|
15765
|
+
* @param payer The payer of the transaction.
|
|
15766
|
+
* @param slippagePercentage The slippage percentage for adding liquidity.
|
|
15767
|
+
* @returns An object with two properties: `initPositionIxs` and `addLiquidityIxs`.
|
|
15768
|
+
*/
|
|
15769
|
+
async initializeMultiplePositionAndAddLiquidityByStrategy(positionKeypairGenerator, totalXAmount, totalYAmount, strategy, owner, payer, slippagePercentage) {
|
|
15770
|
+
const maxActiveBinSlippage = getAndCapMaxActiveBinSlippage(
|
|
15771
|
+
slippagePercentage,
|
|
15772
|
+
this.lbPair.binStep,
|
|
15773
|
+
MAX_ACTIVE_BIN_SLIPPAGE
|
|
15774
|
+
);
|
|
15775
|
+
const { minBinId, maxBinId } = strategy;
|
|
15776
|
+
const binCount = getBinCount(minBinId, maxBinId);
|
|
15777
|
+
const positionCount = getPositionCountByBinCount(binCount);
|
|
15778
|
+
const positionKeypairs = await positionKeypairGenerator(positionCount);
|
|
15779
|
+
const liquidityStrategyParameters = buildLiquidityStrategyParameters(
|
|
15780
|
+
totalXAmount,
|
|
15781
|
+
totalYAmount,
|
|
15782
|
+
new (0, _anchor.BN)(minBinId - this.lbPair.activeId),
|
|
15783
|
+
new (0, _anchor.BN)(maxBinId - this.lbPair.activeId),
|
|
15784
|
+
new (0, _anchor.BN)(this.lbPair.binStep),
|
|
15785
|
+
strategy.singleSidedX,
|
|
15786
|
+
new (0, _anchor.BN)(this.lbPair.activeId),
|
|
15787
|
+
getLiquidityStrategyParameterBuilder(strategy.strategyType)
|
|
15788
|
+
);
|
|
15789
|
+
const instructionsByPositions = [];
|
|
15790
|
+
let startBinId = minBinId;
|
|
15791
|
+
for (const position of positionKeypairs) {
|
|
15792
|
+
const endBinId = Math.min(
|
|
15793
|
+
startBinId + MAX_BINS_PER_POSITION.toNumber() - 1,
|
|
15794
|
+
maxBinId
|
|
15795
|
+
);
|
|
15796
|
+
const binCount2 = getBinCount(startBinId, endBinId);
|
|
15797
|
+
const positionWidth = Math.min(
|
|
15798
|
+
binCount2,
|
|
15799
|
+
DEFAULT_BIN_PER_POSITION.toNumber()
|
|
15800
|
+
);
|
|
15801
|
+
const initPositionIx = await this.program.methods.initializePosition(startBinId, positionWidth).accountsPartial({
|
|
15802
|
+
position: position.publicKey,
|
|
15803
|
+
lbPair: this.pubkey,
|
|
15804
|
+
owner,
|
|
15805
|
+
payer
|
|
15806
|
+
}).instruction();
|
|
15807
|
+
const chunkedAddLiquidityIx = await chunkDepositWithRebalanceEndpoint(
|
|
15808
|
+
this,
|
|
15809
|
+
strategy,
|
|
15810
|
+
slippagePercentage,
|
|
15811
|
+
maxActiveBinSlippage,
|
|
15812
|
+
position.publicKey,
|
|
15813
|
+
startBinId,
|
|
15814
|
+
endBinId,
|
|
15815
|
+
liquidityStrategyParameters,
|
|
15816
|
+
owner,
|
|
15817
|
+
payer,
|
|
15818
|
+
false
|
|
15819
|
+
);
|
|
15820
|
+
instructionsByPositions.push({
|
|
15821
|
+
positionKeypair: position,
|
|
15822
|
+
initializePositionIx: initPositionIx,
|
|
15823
|
+
addLiquidityIxs: chunkedAddLiquidityIx
|
|
15824
|
+
});
|
|
15825
|
+
startBinId = endBinId + 1;
|
|
15826
|
+
}
|
|
15827
|
+
return {
|
|
15828
|
+
instructionsByPositions
|
|
15829
|
+
};
|
|
15830
|
+
}
|
|
15831
|
+
/**
|
|
15832
|
+
* Adds liquidity to an existing position using a specified strategy, allowing for chunkable transactions.
|
|
15833
|
+
* If adding liquidity to bin out of position range, it will automatically expand. The limitation is 70 bins.
|
|
15834
|
+
*
|
|
15835
|
+
* @param {TInitializePositionAndAddLiquidityParamsByStrategy} params - The parameters required for adding liquidity.
|
|
15836
|
+
* @param {PublicKey} params.positionPubKey - The public key of the position to which liquidity is being added.
|
|
15837
|
+
* @param {BN} params.totalXAmount - The total amount of token X to be added as liquidity.
|
|
15838
|
+
* @param {BN} params.totalYAmount - The total amount of token Y to be added as liquidity.
|
|
15839
|
+
* @param {StrategyParameters} params.strategy - The strategy parameters for adding liquidity.
|
|
15840
|
+
* @param {PublicKey} params.user - The public key of the user adding liquidity.
|
|
15841
|
+
* @param {number} params.slippage - The slippage percentage allowed for the transaction.
|
|
15842
|
+
*
|
|
15843
|
+
* @returns {Promise<Transaction[]>} A promise that resolves to an array of transactions for adding liquidity.
|
|
15844
|
+
*/
|
|
15845
|
+
async addLiquidityByStrategyChunkable({
|
|
15846
|
+
positionPubKey,
|
|
15847
|
+
totalXAmount,
|
|
15848
|
+
totalYAmount,
|
|
15849
|
+
strategy,
|
|
15850
|
+
user,
|
|
15851
|
+
slippage
|
|
15852
|
+
}) {
|
|
15853
|
+
const maxActiveBinSlippage = getAndCapMaxActiveBinSlippage(
|
|
15854
|
+
slippage,
|
|
15855
|
+
this.lbPair.binStep,
|
|
15856
|
+
MAX_ACTIVE_BIN_SLIPPAGE
|
|
15857
|
+
);
|
|
15858
|
+
const { minBinId, maxBinId } = strategy;
|
|
15859
|
+
const liquidityStrategyParameters = buildLiquidityStrategyParameters(
|
|
15860
|
+
totalXAmount,
|
|
15861
|
+
totalYAmount,
|
|
15862
|
+
new (0, _anchor.BN)(minBinId - this.lbPair.activeId),
|
|
15863
|
+
new (0, _anchor.BN)(maxBinId - this.lbPair.activeId),
|
|
15864
|
+
new (0, _anchor.BN)(this.lbPair.binStep),
|
|
15865
|
+
strategy.singleSidedX,
|
|
15866
|
+
new (0, _anchor.BN)(this.lbPair.activeId),
|
|
15867
|
+
getLiquidityStrategyParameterBuilder(strategy.strategyType)
|
|
15868
|
+
);
|
|
15869
|
+
const chunkedAddLiquidityIx = await chunkDepositWithRebalanceEndpoint(
|
|
15870
|
+
this,
|
|
15871
|
+
strategy,
|
|
15872
|
+
slippage,
|
|
15873
|
+
maxActiveBinSlippage,
|
|
15874
|
+
positionPubKey,
|
|
15875
|
+
minBinId,
|
|
15876
|
+
maxBinId,
|
|
15877
|
+
liquidityStrategyParameters,
|
|
15878
|
+
user,
|
|
15879
|
+
user,
|
|
15880
|
+
true
|
|
15881
|
+
);
|
|
15882
|
+
const latestBlockhashInfo = await this.program.provider.connection.getLatestBlockhash();
|
|
15883
|
+
return chunkedAddLiquidityIx.map((ixs) => {
|
|
15884
|
+
return new (0, _web3js.Transaction)({
|
|
15885
|
+
...latestBlockhashInfo
|
|
15886
|
+
}).add(...ixs);
|
|
15887
|
+
});
|
|
15888
|
+
}
|
|
15490
15889
|
/**
|
|
15491
15890
|
* The function `initializePositionAndAddLiquidityByStrategy` function is used to initializes a position and adds liquidity
|
|
15492
15891
|
* @param {TInitializePositionAndAddLiquidityParamsByStrategy}
|
|
@@ -15847,7 +16246,7 @@ var DLMM = class {
|
|
|
15847
16246
|
return transactions;
|
|
15848
16247
|
}
|
|
15849
16248
|
/**
|
|
15850
|
-
* The `addLiquidityByStrategy` function is used to add liquidity to existing position
|
|
16249
|
+
* The `addLiquidityByStrategy` function is used to add liquidity to existing position
|
|
15851
16250
|
* @param {TInitializePositionAndAddLiquidityParamsByStrategy}
|
|
15852
16251
|
* - `positionPubKey`: The public key of the position account. (usually use `new Keypair()`)
|
|
15853
16252
|
* - `totalXAmount`: The total amount of token X to be added to the liquidity pool.
|
|
@@ -15855,7 +16254,8 @@ var DLMM = class {
|
|
|
15855
16254
|
* - `strategy`: The strategy parameters to be used for the liquidity pool (Can use `calculateStrategyParameter` to calculate).
|
|
15856
16255
|
* - `user`: The public key of the user account.
|
|
15857
16256
|
* - `slippage`: The slippage percentage to be used for the liquidity pool.
|
|
15858
|
-
* @returns {Promise<Transaction
|
|
16257
|
+
* @returns {Promise<Transaction>} The function `addLiquidityByWeight` returns a `Promise` that resolves to either a single
|
|
16258
|
+
* `Transaction` object
|
|
15859
16259
|
*/
|
|
15860
16260
|
async addLiquidityByStrategy({
|
|
15861
16261
|
positionPubKey,
|
|
@@ -15867,179 +16267,117 @@ var DLMM = class {
|
|
|
15867
16267
|
}) {
|
|
15868
16268
|
const { maxBinId, minBinId } = strategy;
|
|
15869
16269
|
const maxActiveBinSlippage = slippage ? Math.ceil(slippage / (this.lbPair.binStep / 100)) : MAX_ACTIVE_BIN_SLIPPAGE;
|
|
15870
|
-
const
|
|
15871
|
-
const
|
|
15872
|
-
|
|
15873
|
-
|
|
15874
|
-
|
|
15875
|
-
|
|
15876
|
-
|
|
15877
|
-
|
|
15878
|
-
|
|
15879
|
-
activeBin.yAmount,
|
|
15880
|
-
strategy.strategyType,
|
|
15881
|
-
this.tokenX.mint,
|
|
15882
|
-
this.tokenY.mint,
|
|
15883
|
-
this.clock
|
|
15884
|
-
);
|
|
15885
|
-
const chunkedBinRange = chunkBinRange(minBinId, maxBinId);
|
|
15886
|
-
const userTokenX = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
15887
|
-
this.lbPair.tokenXMint,
|
|
15888
|
-
user,
|
|
15889
|
-
true,
|
|
15890
|
-
this.tokenX.owner
|
|
15891
|
-
);
|
|
15892
|
-
const userTokenY = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
15893
|
-
this.lbPair.tokenYMint,
|
|
15894
|
-
user,
|
|
15895
|
-
true,
|
|
15896
|
-
this.tokenY.owner
|
|
16270
|
+
const preInstructions = [];
|
|
16271
|
+
const minBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(minBinId));
|
|
16272
|
+
const maxBinArrayIndex = binIdToBinArrayIndex(new (0, _anchor.BN)(maxBinId));
|
|
16273
|
+
const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
|
|
16274
|
+
const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
|
|
16275
|
+
const strategyParameters = toStrategyParameters(strategy);
|
|
16276
|
+
const binArrayIndexes = getBinArrayIndexesCoverage(
|
|
16277
|
+
new (0, _anchor.BN)(minBinId),
|
|
16278
|
+
new (0, _anchor.BN)(maxBinId)
|
|
15897
16279
|
);
|
|
15898
|
-
const
|
|
15899
|
-
|
|
15900
|
-
|
|
15901
|
-
|
|
15902
|
-
this.
|
|
15903
|
-
this.tokenX.owner
|
|
16280
|
+
const binArrayAccountsMeta = getBinArrayAccountMetasCoverage(
|
|
16281
|
+
new (0, _anchor.BN)(minBinId),
|
|
16282
|
+
new (0, _anchor.BN)(maxBinId),
|
|
16283
|
+
this.pubkey,
|
|
16284
|
+
this.program.programId
|
|
15904
16285
|
);
|
|
15905
|
-
const
|
|
15906
|
-
|
|
15907
|
-
|
|
15908
|
-
user,
|
|
15909
|
-
this.lbPair.tokenYMint,
|
|
15910
|
-
this.tokenY.owner
|
|
16286
|
+
const createBinArrayIxs = await this.createBinArraysIfNeeded(
|
|
16287
|
+
binArrayIndexes,
|
|
16288
|
+
user
|
|
15911
16289
|
);
|
|
15912
|
-
|
|
15913
|
-
const
|
|
15914
|
-
|
|
15915
|
-
|
|
15916
|
-
|
|
15917
|
-
|
|
15918
|
-
|
|
15919
|
-
|
|
15920
|
-
|
|
15921
|
-
|
|
15922
|
-
|
|
15923
|
-
|
|
15924
|
-
|
|
15925
|
-
|
|
15926
|
-
|
|
15927
|
-
|
|
15928
|
-
|
|
15929
|
-
|
|
15930
|
-
|
|
15931
|
-
|
|
15932
|
-
|
|
15933
|
-
|
|
15934
|
-
|
|
15935
|
-
new (0, _anchor.BN)(chunkedMinBinId),
|
|
15936
|
-
new (0, _anchor.BN)(chunkedMaxBinId),
|
|
15937
|
-
this.pubkey,
|
|
15938
|
-
this.program.programId
|
|
15939
|
-
);
|
|
15940
|
-
const createBinArrayIxs = await this.createBinArraysIfNeeded(
|
|
15941
|
-
binArrayIndexes,
|
|
15942
|
-
user
|
|
15943
|
-
);
|
|
15944
|
-
preInstructions.push(...createBinArrayIxs);
|
|
15945
|
-
preInstructions.push(createUserTokenXIx);
|
|
15946
|
-
preInstructions.push(createUserTokenYIx);
|
|
15947
|
-
if (this.tokenX.publicKey.equals(_spltoken.NATIVE_MINT) && !totalXAmount.isZero()) {
|
|
15948
|
-
const wrapSOLIx = wrapSOLInstruction(
|
|
15949
|
-
user,
|
|
15950
|
-
userTokenX,
|
|
15951
|
-
BigInt(totalXAmount.toString())
|
|
15952
|
-
);
|
|
15953
|
-
preInstructions.push(...wrapSOLIx);
|
|
15954
|
-
}
|
|
15955
|
-
if (this.tokenY.publicKey.equals(_spltoken.NATIVE_MINT) && !totalYAmount.isZero()) {
|
|
15956
|
-
const wrapSOLIx = wrapSOLInstruction(
|
|
15957
|
-
user,
|
|
15958
|
-
userTokenY,
|
|
15959
|
-
BigInt(totalYAmount.toString())
|
|
15960
|
-
);
|
|
15961
|
-
preInstructions.push(...wrapSOLIx);
|
|
15962
|
-
}
|
|
15963
|
-
const postInstructions = [];
|
|
15964
|
-
if ([
|
|
15965
|
-
this.tokenX.publicKey.toBase58(),
|
|
15966
|
-
this.tokenY.publicKey.toBase58()
|
|
15967
|
-
].includes(_spltoken.NATIVE_MINT.toBase58())) {
|
|
15968
|
-
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
15969
|
-
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
15970
|
-
}
|
|
15971
|
-
let chunkedTotalAmountX = new (0, _anchor.BN)(0);
|
|
15972
|
-
let chunkedTotalAmountY = new (0, _anchor.BN)(0);
|
|
15973
|
-
for (const { binId, amountX, amountY } of liquidityDistribution) {
|
|
15974
|
-
if (binId >= chunkedMinBinId && binId <= chunkedMaxBinId) {
|
|
15975
|
-
chunkedTotalAmountX = chunkedTotalAmountX.add(amountX);
|
|
15976
|
-
chunkedTotalAmountY = chunkedTotalAmountY.add(amountY);
|
|
15977
|
-
}
|
|
15978
|
-
}
|
|
15979
|
-
chunkedTotalAmountX = calculateTransferFeeIncludedAmount(
|
|
15980
|
-
chunkedTotalAmountX,
|
|
15981
|
-
this.tokenX.mint,
|
|
15982
|
-
this.clock.epoch.toNumber()
|
|
15983
|
-
).amount;
|
|
15984
|
-
chunkedTotalAmountY = calculateTransferFeeIncludedAmount(
|
|
15985
|
-
chunkedTotalAmountY,
|
|
15986
|
-
this.tokenY.mint,
|
|
15987
|
-
this.clock.epoch.toNumber()
|
|
15988
|
-
).amount;
|
|
15989
|
-
const liquidityParams = {
|
|
15990
|
-
amountX: chunkedTotalAmountX,
|
|
15991
|
-
amountY: chunkedTotalAmountY,
|
|
15992
|
-
activeId: this.lbPair.activeId,
|
|
15993
|
-
maxActiveBinSlippage,
|
|
15994
|
-
strategyParameters
|
|
15995
|
-
};
|
|
15996
|
-
const addLiquidityAccounts = {
|
|
15997
|
-
position: positionPubKey,
|
|
15998
|
-
lbPair: this.pubkey,
|
|
16290
|
+
preInstructions.push(...createBinArrayIxs);
|
|
16291
|
+
const [
|
|
16292
|
+
{ ataPubKey: userTokenX, ix: createPayerTokenXIx },
|
|
16293
|
+
{ ataPubKey: userTokenY, ix: createPayerTokenYIx }
|
|
16294
|
+
] = await Promise.all([
|
|
16295
|
+
getOrCreateATAInstruction(
|
|
16296
|
+
this.program.provider.connection,
|
|
16297
|
+
this.tokenX.publicKey,
|
|
16298
|
+
user,
|
|
16299
|
+
this.tokenX.owner
|
|
16300
|
+
),
|
|
16301
|
+
getOrCreateATAInstruction(
|
|
16302
|
+
this.program.provider.connection,
|
|
16303
|
+
this.tokenY.publicKey,
|
|
16304
|
+
user,
|
|
16305
|
+
this.tokenY.owner
|
|
16306
|
+
)
|
|
16307
|
+
]);
|
|
16308
|
+
createPayerTokenXIx && preInstructions.push(createPayerTokenXIx);
|
|
16309
|
+
createPayerTokenYIx && preInstructions.push(createPayerTokenYIx);
|
|
16310
|
+
if (this.tokenX.publicKey.equals(_spltoken.NATIVE_MINT) && !totalXAmount.isZero()) {
|
|
16311
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
16312
|
+
user,
|
|
15999
16313
|
userTokenX,
|
|
16314
|
+
BigInt(totalXAmount.toString())
|
|
16315
|
+
);
|
|
16316
|
+
preInstructions.push(...wrapSOLIx);
|
|
16317
|
+
}
|
|
16318
|
+
if (this.tokenY.publicKey.equals(_spltoken.NATIVE_MINT) && !totalYAmount.isZero()) {
|
|
16319
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
16320
|
+
user,
|
|
16000
16321
|
userTokenY,
|
|
16001
|
-
|
|
16002
|
-
reserveY: this.lbPair.reserveY,
|
|
16003
|
-
tokenXMint: this.lbPair.tokenXMint,
|
|
16004
|
-
tokenYMint: this.lbPair.tokenYMint,
|
|
16005
|
-
binArrayBitmapExtension,
|
|
16006
|
-
sender: user,
|
|
16007
|
-
tokenXProgram: this.tokenX.owner,
|
|
16008
|
-
tokenYProgram: this.tokenY.owner,
|
|
16009
|
-
memoProgram: MEMO_PROGRAM_ID
|
|
16010
|
-
};
|
|
16011
|
-
const programMethod = this.program.methods.addLiquidityByStrategy2(
|
|
16012
|
-
liquidityParams,
|
|
16013
|
-
{
|
|
16014
|
-
slices
|
|
16015
|
-
}
|
|
16322
|
+
BigInt(totalYAmount.toString())
|
|
16016
16323
|
);
|
|
16017
|
-
|
|
16018
|
-
const instructions = [
|
|
16019
|
-
...preInstructions,
|
|
16020
|
-
addLiquidityIx,
|
|
16021
|
-
...postInstructions
|
|
16022
|
-
];
|
|
16023
|
-
groupedInstructions.push(instructions);
|
|
16324
|
+
preInstructions.push(...wrapSOLIx);
|
|
16024
16325
|
}
|
|
16025
|
-
const
|
|
16026
|
-
|
|
16027
|
-
|
|
16028
|
-
|
|
16029
|
-
|
|
16030
|
-
|
|
16031
|
-
|
|
16032
|
-
|
|
16033
|
-
|
|
16326
|
+
const postInstructions = [];
|
|
16327
|
+
if ([
|
|
16328
|
+
this.tokenX.publicKey.toBase58(),
|
|
16329
|
+
this.tokenY.publicKey.toBase58()
|
|
16330
|
+
].includes(_spltoken.NATIVE_MINT.toBase58())) {
|
|
16331
|
+
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
16332
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
16333
|
+
}
|
|
16334
|
+
const liquidityParams = {
|
|
16335
|
+
amountX: totalXAmount,
|
|
16336
|
+
amountY: totalYAmount,
|
|
16337
|
+
activeId: this.lbPair.activeId,
|
|
16338
|
+
maxActiveBinSlippage,
|
|
16339
|
+
strategyParameters
|
|
16340
|
+
};
|
|
16341
|
+
const addLiquidityAccounts = {
|
|
16342
|
+
position: positionPubKey,
|
|
16343
|
+
lbPair: this.pubkey,
|
|
16344
|
+
userTokenX,
|
|
16345
|
+
userTokenY,
|
|
16346
|
+
reserveX: this.lbPair.reserveX,
|
|
16347
|
+
reserveY: this.lbPair.reserveY,
|
|
16348
|
+
tokenXMint: this.lbPair.tokenXMint,
|
|
16349
|
+
tokenYMint: this.lbPair.tokenYMint,
|
|
16350
|
+
binArrayBitmapExtension,
|
|
16351
|
+
sender: user,
|
|
16352
|
+
tokenXProgram: this.tokenX.owner,
|
|
16353
|
+
tokenYProgram: this.tokenY.owner,
|
|
16354
|
+
memoProgram: MEMO_PROGRAM_ID
|
|
16355
|
+
};
|
|
16356
|
+
const { slices, accounts: transferHookAccounts } = this.getPotentialToken2022IxDataAndAccounts(0 /* Liquidity */);
|
|
16357
|
+
const programMethod = this.program.methods.addLiquidityByStrategy2(
|
|
16358
|
+
liquidityParams,
|
|
16359
|
+
{
|
|
16360
|
+
slices
|
|
16361
|
+
}
|
|
16362
|
+
);
|
|
16363
|
+
const addLiquidityIx = await programMethod.accounts(addLiquidityAccounts).remainingAccounts(transferHookAccounts).remainingAccounts(binArrayAccountsMeta).instruction();
|
|
16364
|
+
const instructions = [
|
|
16365
|
+
...preInstructions,
|
|
16366
|
+
addLiquidityIx,
|
|
16367
|
+
...postInstructions
|
|
16368
|
+
];
|
|
16369
|
+
const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
|
|
16370
|
+
this.program.provider.connection,
|
|
16371
|
+
instructions,
|
|
16372
|
+
user
|
|
16034
16373
|
);
|
|
16374
|
+
instructions.unshift(setCUIx);
|
|
16035
16375
|
const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
|
|
16036
|
-
return
|
|
16037
|
-
|
|
16038
|
-
|
|
16039
|
-
|
|
16040
|
-
|
|
16041
|
-
}).add(...ixs);
|
|
16042
|
-
});
|
|
16376
|
+
return new (0, _web3js.Transaction)({
|
|
16377
|
+
blockhash,
|
|
16378
|
+
lastValidBlockHeight,
|
|
16379
|
+
feePayer: user
|
|
16380
|
+
}).add(...instructions);
|
|
16043
16381
|
}
|
|
16044
16382
|
/**
|
|
16045
16383
|
* @deprecated Use `addLiquidityByStrategy` instead which support both token and token2022.
|
|
@@ -18565,302 +18903,229 @@ var DLMM = class {
|
|
|
18565
18903
|
const { lbPair, shouldClaimFee, shouldClaimReward, owner, address } = rebalancePosition;
|
|
18566
18904
|
const { depositParams, withdrawParams } = simulationResult;
|
|
18567
18905
|
const activeId = new (0, _anchor.BN)(lbPair.activeId);
|
|
18568
|
-
|
|
18569
|
-
|
|
18570
|
-
|
|
18571
|
-
|
|
18572
|
-
const
|
|
18573
|
-
|
|
18574
|
-
|
|
18575
|
-
|
|
18576
|
-
|
|
18577
|
-
|
|
18578
|
-
|
|
18579
|
-
|
|
18580
|
-
|
|
18581
|
-
|
|
18582
|
-
|
|
18583
|
-
|
|
18584
|
-
|
|
18585
|
-
|
|
18586
|
-
|
|
18587
|
-
|
|
18588
|
-
|
|
18589
|
-
|
|
18590
|
-
|
|
18591
|
-
|
|
18592
|
-
|
|
18593
|
-
|
|
18594
|
-
continue;
|
|
18595
|
-
const newMin = Math.max(absMin, chunk.lowerBinId);
|
|
18596
|
-
const newMax = Math.min(absMax, chunk.upperBinId);
|
|
18597
|
-
res.push({
|
|
18598
|
-
...param,
|
|
18599
|
-
minDeltaId: newMin - activeId2.toNumber(),
|
|
18600
|
-
maxDeltaId: newMax - activeId2.toNumber()
|
|
18601
|
-
});
|
|
18602
|
-
}
|
|
18603
|
-
return res;
|
|
18604
|
-
}
|
|
18605
|
-
function splitWithdrawParamsForChunk(params, chunk) {
|
|
18606
|
-
const res = [];
|
|
18607
|
-
for (const param of params) {
|
|
18608
|
-
const absMin = param.minBinId !== null ? param.minBinId : activeId.toNumber();
|
|
18609
|
-
const absMax = param.maxBinId !== null ? param.maxBinId : activeId.toNumber();
|
|
18610
|
-
if (absMax < chunk.lowerBinId || absMin > chunk.upperBinId)
|
|
18611
|
-
continue;
|
|
18612
|
-
const newMin = Math.max(absMin, chunk.lowerBinId);
|
|
18613
|
-
const newMax = Math.min(absMax, chunk.upperBinId);
|
|
18614
|
-
res.push({
|
|
18615
|
-
...param,
|
|
18616
|
-
minBinId: newMin,
|
|
18617
|
-
maxBinId: newMax
|
|
18618
|
-
});
|
|
18619
|
-
}
|
|
18620
|
-
return res;
|
|
18621
|
-
}
|
|
18622
|
-
const allInstructions = [];
|
|
18623
|
-
for (const chunk of binChunks) {
|
|
18624
|
-
const chunkedDepositParams = splitDepositParamsForChunk(
|
|
18625
|
-
depositParams,
|
|
18626
|
-
chunk,
|
|
18627
|
-
activeId
|
|
18628
|
-
);
|
|
18629
|
-
const chunkedWithdrawParams = splitWithdrawParamsForChunk(
|
|
18630
|
-
withdrawParams,
|
|
18631
|
-
chunk
|
|
18632
|
-
);
|
|
18633
|
-
if (chunkedDepositParams.length === 0 && chunkedWithdrawParams.length === 0)
|
|
18634
|
-
continue;
|
|
18635
|
-
const { slices, accounts: transferHookAccounts } = this.getPotentialToken2022IxDataAndAccounts(0 /* Liquidity */);
|
|
18636
|
-
const preInstructions = [];
|
|
18637
|
-
const harvestRewardRemainingAccountMetas = [];
|
|
18638
|
-
if (shouldClaimReward) {
|
|
18639
|
-
for (const [idx, reward] of this.lbPair.rewardInfos.entries()) {
|
|
18640
|
-
if (!reward.mint.equals(_web3js.PublicKey.default)) {
|
|
18641
|
-
const rewardTokenInfo = this.rewards[idx];
|
|
18642
|
-
slices.push({
|
|
18643
|
-
accountsType: {
|
|
18644
|
-
transferHookMultiReward: {
|
|
18645
|
-
0: idx
|
|
18646
|
-
}
|
|
18647
|
-
},
|
|
18648
|
-
length: rewardTokenInfo.transferHookAccountMetas.length
|
|
18649
|
-
});
|
|
18650
|
-
transferHookAccounts.push(
|
|
18651
|
-
...rewardTokenInfo.transferHookAccountMetas
|
|
18652
|
-
);
|
|
18653
|
-
const userTokenRewardAddress = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
18654
|
-
reward.mint,
|
|
18906
|
+
const { slices, accounts: transferHookAccounts } = this.getPotentialToken2022IxDataAndAccounts(0 /* Liquidity */);
|
|
18907
|
+
const preInstructions = [];
|
|
18908
|
+
const harvestRewardRemainingAccountMetas = [];
|
|
18909
|
+
if (shouldClaimReward) {
|
|
18910
|
+
for (const [idx, reward] of this.lbPair.rewardInfos.entries()) {
|
|
18911
|
+
if (!reward.mint.equals(_web3js.PublicKey.default)) {
|
|
18912
|
+
const rewardTokenInfo = this.rewards[idx];
|
|
18913
|
+
slices.push({
|
|
18914
|
+
accountsType: {
|
|
18915
|
+
transferHookMultiReward: {
|
|
18916
|
+
0: idx
|
|
18917
|
+
}
|
|
18918
|
+
},
|
|
18919
|
+
length: rewardTokenInfo.transferHookAccountMetas.length
|
|
18920
|
+
});
|
|
18921
|
+
transferHookAccounts.push(
|
|
18922
|
+
...rewardTokenInfo.transferHookAccountMetas
|
|
18923
|
+
);
|
|
18924
|
+
const userTokenRewardAddress = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
18925
|
+
reward.mint,
|
|
18926
|
+
owner,
|
|
18927
|
+
true,
|
|
18928
|
+
rewardTokenInfo.owner
|
|
18929
|
+
);
|
|
18930
|
+
preInstructions.push(
|
|
18931
|
+
_spltoken.createAssociatedTokenAccountIdempotentInstruction.call(void 0,
|
|
18655
18932
|
owner,
|
|
18656
|
-
|
|
18933
|
+
userTokenRewardAddress,
|
|
18934
|
+
owner,
|
|
18935
|
+
reward.mint,
|
|
18657
18936
|
rewardTokenInfo.owner
|
|
18658
|
-
)
|
|
18659
|
-
|
|
18660
|
-
|
|
18661
|
-
|
|
18662
|
-
|
|
18663
|
-
|
|
18664
|
-
|
|
18665
|
-
|
|
18666
|
-
|
|
18667
|
-
|
|
18668
|
-
|
|
18669
|
-
|
|
18670
|
-
|
|
18671
|
-
|
|
18672
|
-
|
|
18673
|
-
|
|
18674
|
-
|
|
18675
|
-
|
|
18676
|
-
|
|
18677
|
-
|
|
18678
|
-
|
|
18679
|
-
|
|
18680
|
-
|
|
18681
|
-
|
|
18682
|
-
|
|
18683
|
-
|
|
18684
|
-
|
|
18685
|
-
|
|
18686
|
-
isWritable: false
|
|
18687
|
-
};
|
|
18688
|
-
harvestRewardRemainingAccountMetas.push(
|
|
18689
|
-
rewardVault,
|
|
18690
|
-
userTokenReward,
|
|
18691
|
-
rewardMint,
|
|
18692
|
-
rewardTokenProgram
|
|
18693
|
-
);
|
|
18694
|
-
}
|
|
18937
|
+
)
|
|
18938
|
+
);
|
|
18939
|
+
const rewardVault = {
|
|
18940
|
+
pubkey: reward.vault,
|
|
18941
|
+
isSigner: false,
|
|
18942
|
+
isWritable: true
|
|
18943
|
+
};
|
|
18944
|
+
const userTokenReward = {
|
|
18945
|
+
pubkey: userTokenRewardAddress,
|
|
18946
|
+
isSigner: false,
|
|
18947
|
+
isWritable: true
|
|
18948
|
+
};
|
|
18949
|
+
const rewardMint = {
|
|
18950
|
+
pubkey: reward.mint,
|
|
18951
|
+
isSigner: false,
|
|
18952
|
+
isWritable: false
|
|
18953
|
+
};
|
|
18954
|
+
const rewardTokenProgram = {
|
|
18955
|
+
pubkey: rewardTokenInfo.owner,
|
|
18956
|
+
isSigner: false,
|
|
18957
|
+
isWritable: false
|
|
18958
|
+
};
|
|
18959
|
+
harvestRewardRemainingAccountMetas.push(
|
|
18960
|
+
rewardVault,
|
|
18961
|
+
userTokenReward,
|
|
18962
|
+
rewardMint,
|
|
18963
|
+
rewardTokenProgram
|
|
18964
|
+
);
|
|
18695
18965
|
}
|
|
18696
18966
|
}
|
|
18697
|
-
|
|
18698
|
-
|
|
18699
|
-
|
|
18700
|
-
|
|
18701
|
-
|
|
18967
|
+
}
|
|
18968
|
+
const initBinArrayInstructions = [];
|
|
18969
|
+
const { binArrayBitmap, binArrayIndexes } = getRebalanceBinArrayIndexesAndBitmapCoverage(
|
|
18970
|
+
depositParams,
|
|
18971
|
+
withdrawParams,
|
|
18972
|
+
activeId.toNumber(),
|
|
18973
|
+
this.pubkey,
|
|
18974
|
+
this.program.programId
|
|
18975
|
+
);
|
|
18976
|
+
const binArrayPublicKeys = binArrayIndexes.map((index) => {
|
|
18977
|
+
const [binArrayPubkey] = deriveBinArray(
|
|
18702
18978
|
this.pubkey,
|
|
18979
|
+
index,
|
|
18703
18980
|
this.program.programId
|
|
18704
18981
|
);
|
|
18705
|
-
|
|
18706
|
-
|
|
18707
|
-
|
|
18708
|
-
|
|
18709
|
-
|
|
18710
|
-
|
|
18711
|
-
|
|
18712
|
-
|
|
18713
|
-
|
|
18714
|
-
|
|
18715
|
-
|
|
18716
|
-
|
|
18717
|
-
|
|
18718
|
-
|
|
18719
|
-
|
|
18720
|
-
|
|
18721
|
-
|
|
18722
|
-
const initBinArrayIx = await this.program.methods.initializeBinArray(binArrayIndex).accountsPartial({
|
|
18723
|
-
binArray: binArrayPubkey,
|
|
18724
|
-
funder: owner,
|
|
18725
|
-
lbPair: this.pubkey
|
|
18726
|
-
}).instruction();
|
|
18727
|
-
initBinArrayInstructions.push(initBinArrayIx);
|
|
18728
|
-
}
|
|
18729
|
-
}
|
|
18730
|
-
if (!binArrayBitmap.equals(_web3js.PublicKey.default)) {
|
|
18731
|
-
const bitmapAccount = await this.program.provider.connection.getAccountInfo(binArrayBitmap);
|
|
18732
|
-
if (!bitmapAccount) {
|
|
18733
|
-
const initBitmapExtensionIx = await this.program.methods.initializeBinArrayBitmapExtension().accountsPartial({
|
|
18734
|
-
binArrayBitmapExtension: binArrayBitmap,
|
|
18735
|
-
funder: owner,
|
|
18736
|
-
lbPair: this.pubkey
|
|
18737
|
-
}).preInstructions([
|
|
18738
|
-
_web3js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
18739
|
-
units: DEFAULT_INIT_BIN_ARRAY_CU
|
|
18740
|
-
})
|
|
18741
|
-
]).instruction();
|
|
18742
|
-
preInstructions.push(initBitmapExtensionIx);
|
|
18743
|
-
}
|
|
18744
|
-
}
|
|
18745
|
-
const [
|
|
18746
|
-
{ ataPubKey: userTokenX, ix: createUserTokenXIx },
|
|
18747
|
-
{ ataPubKey: userTokenY, ix: createUserTokenYIx }
|
|
18748
|
-
] = await Promise.all([
|
|
18749
|
-
getOrCreateATAInstruction(
|
|
18750
|
-
this.program.provider.connection,
|
|
18751
|
-
this.tokenX.publicKey,
|
|
18752
|
-
owner,
|
|
18753
|
-
this.tokenX.owner
|
|
18754
|
-
),
|
|
18755
|
-
getOrCreateATAInstruction(
|
|
18756
|
-
this.program.provider.connection,
|
|
18757
|
-
this.tokenY.publicKey,
|
|
18758
|
-
owner,
|
|
18759
|
-
this.tokenY.owner
|
|
18760
|
-
)
|
|
18761
|
-
]);
|
|
18762
|
-
createUserTokenXIx && preInstructions.push(createUserTokenXIx);
|
|
18763
|
-
createUserTokenYIx && preInstructions.push(createUserTokenYIx);
|
|
18764
|
-
slippage = capSlippagePercentage(slippage);
|
|
18765
|
-
const applySlippageMaxAmount = (amount, slippage2) => {
|
|
18766
|
-
return slippage2 == 100 ? U64_MAX : amount.muln(100 + slippage2).divn(100);
|
|
18767
|
-
};
|
|
18768
|
-
const applySlippageMinAmount = (amount, slippage2) => {
|
|
18769
|
-
return amount.muln(100 - slippage2).divn(100);
|
|
18770
|
-
};
|
|
18771
|
-
const maxDepositXAmount = applySlippageMaxAmount(
|
|
18772
|
-
simulationResult.actualAmountXDeposited,
|
|
18773
|
-
slippage
|
|
18774
|
-
);
|
|
18775
|
-
const maxDepositYAmount = applySlippageMaxAmount(
|
|
18776
|
-
simulationResult.actualAmountYDeposited,
|
|
18777
|
-
slippage
|
|
18778
|
-
);
|
|
18779
|
-
const minWithdrawXAmount = applySlippageMinAmount(
|
|
18780
|
-
simulationResult.actualAmountXWithdrawn,
|
|
18781
|
-
slippage
|
|
18782
|
-
);
|
|
18783
|
-
const minWithdrawYAmount = applySlippageMinAmount(
|
|
18784
|
-
simulationResult.actualAmountYWithdrawn,
|
|
18785
|
-
slippage
|
|
18786
|
-
);
|
|
18787
|
-
const postInstructions = [];
|
|
18788
|
-
if (this.tokenX.publicKey.equals(_spltoken.NATIVE_MINT) && simulationResult.actualAmountXDeposited.gtn(0)) {
|
|
18789
|
-
const wrapSOLIx = wrapSOLInstruction(
|
|
18790
|
-
owner,
|
|
18791
|
-
userTokenX,
|
|
18792
|
-
BigInt(simulationResult.actualAmountXDeposited.toString())
|
|
18793
|
-
);
|
|
18794
|
-
preInstructions.push(...wrapSOLIx);
|
|
18795
|
-
}
|
|
18796
|
-
if (this.tokenY.publicKey.equals(_spltoken.NATIVE_MINT) && simulationResult.actualAmountYDeposited.gtn(0)) {
|
|
18797
|
-
const wrapSOLIx = wrapSOLInstruction(
|
|
18798
|
-
owner,
|
|
18799
|
-
userTokenY,
|
|
18800
|
-
BigInt(simulationResult.actualAmountYDeposited.toString())
|
|
18801
|
-
);
|
|
18802
|
-
preInstructions.push(...wrapSOLIx);
|
|
18982
|
+
return binArrayPubkey;
|
|
18983
|
+
});
|
|
18984
|
+
const binArrayAccounts = await chunkedGetMultipleAccountInfos(
|
|
18985
|
+
this.program.provider.connection,
|
|
18986
|
+
binArrayPublicKeys
|
|
18987
|
+
);
|
|
18988
|
+
for (let i = 0; i < binArrayAccounts.length; i++) {
|
|
18989
|
+
const binArrayAccount = binArrayAccounts[i];
|
|
18990
|
+
if (!binArrayAccount) {
|
|
18991
|
+
const binArrayPubkey = binArrayPublicKeys[i];
|
|
18992
|
+
const binArrayIndex = binArrayIndexes[i];
|
|
18993
|
+
const initBinArrayIx = await this.program.methods.initializeBinArray(binArrayIndex).accountsPartial({
|
|
18994
|
+
binArray: binArrayPubkey,
|
|
18995
|
+
funder: owner,
|
|
18996
|
+
lbPair: this.pubkey
|
|
18997
|
+
}).instruction();
|
|
18998
|
+
initBinArrayInstructions.push(initBinArrayIx);
|
|
18803
18999
|
}
|
|
18804
|
-
|
|
18805
|
-
|
|
18806
|
-
|
|
19000
|
+
}
|
|
19001
|
+
if (!binArrayBitmap.equals(_web3js.PublicKey.default)) {
|
|
19002
|
+
const bitmapAccount = await this.program.provider.connection.getAccountInfo(binArrayBitmap);
|
|
19003
|
+
if (!bitmapAccount) {
|
|
19004
|
+
const initBitmapExtensionIx = await this.program.methods.initializeBinArrayBitmapExtension().accountsPartial({
|
|
19005
|
+
binArrayBitmapExtension: binArrayBitmap,
|
|
19006
|
+
funder: owner,
|
|
19007
|
+
lbPair: this.pubkey
|
|
19008
|
+
}).preInstructions([
|
|
19009
|
+
_web3js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
19010
|
+
units: DEFAULT_INIT_BIN_ARRAY_CU
|
|
19011
|
+
})
|
|
19012
|
+
]).instruction();
|
|
19013
|
+
preInstructions.push(initBitmapExtensionIx);
|
|
18807
19014
|
}
|
|
18808
|
-
|
|
18809
|
-
|
|
18810
|
-
|
|
18811
|
-
|
|
18812
|
-
|
|
18813
|
-
|
|
18814
|
-
|
|
18815
|
-
|
|
18816
|
-
|
|
18817
|
-
|
|
18818
|
-
|
|
18819
|
-
|
|
18820
|
-
|
|
18821
|
-
|
|
18822
|
-
|
|
18823
|
-
|
|
18824
|
-
|
|
18825
|
-
|
|
18826
|
-
|
|
18827
|
-
|
|
18828
|
-
|
|
19015
|
+
}
|
|
19016
|
+
const [
|
|
19017
|
+
{ ataPubKey: userTokenX, ix: createUserTokenXIx },
|
|
19018
|
+
{ ataPubKey: userTokenY, ix: createUserTokenYIx }
|
|
19019
|
+
] = await Promise.all([
|
|
19020
|
+
getOrCreateATAInstruction(
|
|
19021
|
+
this.program.provider.connection,
|
|
19022
|
+
this.tokenX.publicKey,
|
|
19023
|
+
owner,
|
|
19024
|
+
this.tokenX.owner
|
|
19025
|
+
),
|
|
19026
|
+
getOrCreateATAInstruction(
|
|
19027
|
+
this.program.provider.connection,
|
|
19028
|
+
this.tokenY.publicKey,
|
|
19029
|
+
owner,
|
|
19030
|
+
this.tokenY.owner
|
|
19031
|
+
)
|
|
19032
|
+
]);
|
|
19033
|
+
createUserTokenXIx && preInstructions.push(createUserTokenXIx);
|
|
19034
|
+
createUserTokenYIx && preInstructions.push(createUserTokenYIx);
|
|
19035
|
+
slippage = capSlippagePercentage(slippage);
|
|
19036
|
+
const maxDepositXAmount = getSlippageMaxAmount(
|
|
19037
|
+
simulationResult.actualAmountXDeposited,
|
|
19038
|
+
slippage
|
|
19039
|
+
);
|
|
19040
|
+
const maxDepositYAmount = getSlippageMaxAmount(
|
|
19041
|
+
simulationResult.actualAmountYDeposited,
|
|
19042
|
+
slippage
|
|
19043
|
+
);
|
|
19044
|
+
const minWithdrawXAmount = getSlippageMinAmount(
|
|
19045
|
+
simulationResult.actualAmountXWithdrawn,
|
|
19046
|
+
slippage
|
|
19047
|
+
);
|
|
19048
|
+
const minWithdrawYAmount = getSlippageMinAmount(
|
|
19049
|
+
simulationResult.actualAmountYWithdrawn,
|
|
19050
|
+
slippage
|
|
19051
|
+
);
|
|
19052
|
+
const postInstructions = [];
|
|
19053
|
+
if (this.tokenX.publicKey.equals(_spltoken.NATIVE_MINT) && simulationResult.actualAmountXDeposited.gtn(0)) {
|
|
19054
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
18829
19055
|
owner,
|
|
18830
19056
|
userTokenX,
|
|
19057
|
+
BigInt(simulationResult.actualAmountXDeposited.toString())
|
|
19058
|
+
);
|
|
19059
|
+
preInstructions.push(...wrapSOLIx);
|
|
19060
|
+
}
|
|
19061
|
+
if (this.tokenY.publicKey.equals(_spltoken.NATIVE_MINT) && simulationResult.actualAmountYDeposited.gtn(0)) {
|
|
19062
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
19063
|
+
owner,
|
|
18831
19064
|
userTokenY,
|
|
18832
|
-
|
|
18833
|
-
reserveY: this.lbPair.reserveY,
|
|
18834
|
-
tokenXMint: this.tokenX.publicKey,
|
|
18835
|
-
tokenYMint: this.tokenY.publicKey,
|
|
18836
|
-
tokenXProgram: this.tokenX.owner,
|
|
18837
|
-
tokenYProgram: this.tokenY.owner,
|
|
18838
|
-
memoProgram: MEMO_PROGRAM_ID,
|
|
18839
|
-
rentPayer: _nullishCoalesce(rentPayer, () => ( owner))
|
|
18840
|
-
}).remainingAccounts(transferHookAccounts).remainingAccounts(
|
|
18841
|
-
binArrayPublicKeys.map((pubkey) => {
|
|
18842
|
-
return {
|
|
18843
|
-
pubkey,
|
|
18844
|
-
isSigner: false,
|
|
18845
|
-
isWritable: true
|
|
18846
|
-
};
|
|
18847
|
-
})
|
|
18848
|
-
).instruction();
|
|
18849
|
-
const setCUIX = await getEstimatedComputeUnitIxWithBuffer(
|
|
18850
|
-
this.program.provider.connection,
|
|
18851
|
-
[instruction],
|
|
18852
|
-
owner
|
|
19065
|
+
BigInt(simulationResult.actualAmountYDeposited.toString())
|
|
18853
19066
|
);
|
|
18854
|
-
|
|
18855
|
-
setCUIX,
|
|
18856
|
-
...initBinArrayInstructions,
|
|
18857
|
-
...preInstructions,
|
|
18858
|
-
instruction,
|
|
18859
|
-
...postInstructions
|
|
18860
|
-
];
|
|
18861
|
-
allInstructions.push(rebalancePositionInstruction);
|
|
19067
|
+
preInstructions.push(...wrapSOLIx);
|
|
18862
19068
|
}
|
|
18863
|
-
|
|
19069
|
+
if (this.tokenX.publicKey.equals(_spltoken.NATIVE_MINT) || this.tokenY.publicKey.equals(_spltoken.NATIVE_MINT)) {
|
|
19070
|
+
const closeWrappedSOLIx = await unwrapSOLInstruction(owner);
|
|
19071
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
19072
|
+
}
|
|
19073
|
+
const instruction = await this.program.methods.rebalanceLiquidity(
|
|
19074
|
+
{
|
|
19075
|
+
adds: depositParams,
|
|
19076
|
+
removes: withdrawParams,
|
|
19077
|
+
activeId: activeId.toNumber(),
|
|
19078
|
+
shouldClaimFee,
|
|
19079
|
+
shouldClaimReward,
|
|
19080
|
+
maxActiveBinSlippage: maxActiveBinSlippage.toNumber(),
|
|
19081
|
+
maxDepositXAmount,
|
|
19082
|
+
maxDepositYAmount,
|
|
19083
|
+
minWithdrawXAmount,
|
|
19084
|
+
minWithdrawYAmount,
|
|
19085
|
+
padding: Array(32).fill(0)
|
|
19086
|
+
},
|
|
19087
|
+
{
|
|
19088
|
+
slices
|
|
19089
|
+
}
|
|
19090
|
+
).accountsPartial({
|
|
19091
|
+
lbPair: this.pubkey,
|
|
19092
|
+
binArrayBitmapExtension: binArrayBitmap,
|
|
19093
|
+
position: address,
|
|
19094
|
+
owner,
|
|
19095
|
+
userTokenX,
|
|
19096
|
+
userTokenY,
|
|
19097
|
+
reserveX: this.lbPair.reserveX,
|
|
19098
|
+
reserveY: this.lbPair.reserveY,
|
|
19099
|
+
tokenXMint: this.tokenX.publicKey,
|
|
19100
|
+
tokenYMint: this.tokenY.publicKey,
|
|
19101
|
+
tokenXProgram: this.tokenX.owner,
|
|
19102
|
+
tokenYProgram: this.tokenY.owner,
|
|
19103
|
+
memoProgram: MEMO_PROGRAM_ID,
|
|
19104
|
+
rentPayer: _nullishCoalesce(rentPayer, () => ( owner))
|
|
19105
|
+
}).remainingAccounts(transferHookAccounts).remainingAccounts(
|
|
19106
|
+
binArrayPublicKeys.map((pubkey) => {
|
|
19107
|
+
return {
|
|
19108
|
+
pubkey,
|
|
19109
|
+
isSigner: false,
|
|
19110
|
+
isWritable: true
|
|
19111
|
+
};
|
|
19112
|
+
})
|
|
19113
|
+
).instruction();
|
|
19114
|
+
const setCUIX = await getEstimatedComputeUnitIxWithBuffer(
|
|
19115
|
+
this.program.provider.connection,
|
|
19116
|
+
[instruction],
|
|
19117
|
+
owner
|
|
19118
|
+
);
|
|
19119
|
+
const rebalancePositionInstruction = [
|
|
19120
|
+
setCUIX,
|
|
19121
|
+
...preInstructions,
|
|
19122
|
+
instruction,
|
|
19123
|
+
...postInstructions
|
|
19124
|
+
];
|
|
19125
|
+
return {
|
|
19126
|
+
initBinArrayInstructions,
|
|
19127
|
+
rebalancePositionInstruction
|
|
19128
|
+
};
|
|
18864
19129
|
}
|
|
18865
19130
|
/**
|
|
18866
19131
|
* Create an extended empty position.
|
|
@@ -19636,5 +19901,14 @@ var src_default = DLMM;
|
|
|
19636
19901
|
|
|
19637
19902
|
|
|
19638
19903
|
|
|
19639
|
-
|
|
19904
|
+
|
|
19905
|
+
|
|
19906
|
+
|
|
19907
|
+
|
|
19908
|
+
|
|
19909
|
+
|
|
19910
|
+
|
|
19911
|
+
|
|
19912
|
+
|
|
19913
|
+
exports.ADMIN = ADMIN; exports.ActionType = ActionType; exports.ActivationType = ActivationType; exports.BASIS_POINT_MAX = BASIS_POINT_MAX; exports.BIN_ARRAY_BITMAP_FEE = BIN_ARRAY_BITMAP_FEE; exports.BIN_ARRAY_BITMAP_FEE_BN = BIN_ARRAY_BITMAP_FEE_BN; exports.BIN_ARRAY_BITMAP_SIZE = BIN_ARRAY_BITMAP_SIZE; exports.BIN_ARRAY_FEE = BIN_ARRAY_FEE; exports.BIN_ARRAY_FEE_BN = BIN_ARRAY_FEE_BN; exports.BinLiquidity = BinLiquidity; exports.BitmapType = BitmapType; exports.ClockLayout = ClockLayout; exports.DEFAULT_BIN_PER_POSITION = DEFAULT_BIN_PER_POSITION; exports.DLMMError = DLMMError; exports.DlmmSdkError = DlmmSdkError; exports.EXTENSION_BINARRAY_BITMAP_SIZE = EXTENSION_BINARRAY_BITMAP_SIZE; exports.FEE_PRECISION = FEE_PRECISION; exports.IDL = dlmm_default; exports.ILM_BASE = ILM_BASE; exports.LBCLMM_PROGRAM_IDS = LBCLMM_PROGRAM_IDS; exports.MAX_ACTIVE_BIN_SLIPPAGE = MAX_ACTIVE_BIN_SLIPPAGE; exports.MAX_BINS_PER_POSITION = MAX_BINS_PER_POSITION; exports.MAX_BIN_ARRAY_SIZE = MAX_BIN_ARRAY_SIZE; exports.MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX = MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX; exports.MAX_CLAIM_ALL_ALLOWED = MAX_CLAIM_ALL_ALLOWED; exports.MAX_EXTRA_BIN_ARRAYS = MAX_EXTRA_BIN_ARRAYS; exports.MAX_FEE_RATE = MAX_FEE_RATE; exports.MAX_RESIZE_LENGTH = MAX_RESIZE_LENGTH; exports.MEMO_PROGRAM_ID = MEMO_PROGRAM_ID; exports.Network = Network; exports.POOL_FEE = POOL_FEE; exports.POOL_FEE_BN = POOL_FEE_BN; exports.POSITION_BIN_DATA_SIZE = POSITION_BIN_DATA_SIZE; exports.POSITION_FEE = POSITION_FEE; exports.POSITION_FEE_BN = POSITION_FEE_BN; exports.POSITION_MAX_LENGTH = POSITION_MAX_LENGTH; exports.POSITION_MIN_SIZE = POSITION_MIN_SIZE; exports.PRECISION = PRECISION; exports.PairStatus = PairStatus; exports.PairType = PairType; exports.PositionVersion = PositionVersion; exports.RebalancePosition = RebalancePosition; exports.ResizeSide = ResizeSide; exports.SCALE = SCALE; exports.SCALE_OFFSET = SCALE_OFFSET; exports.SIMULATION_USER = SIMULATION_USER; exports.Strategy = Strategy; exports.StrategyType = StrategyType; exports.TOKEN_ACCOUNT_FEE = TOKEN_ACCOUNT_FEE; exports.TOKEN_ACCOUNT_FEE_BN = TOKEN_ACCOUNT_FEE_BN; exports.U64_MAX = U64_MAX; exports.autoFillXByStrategy = autoFillXByStrategy; exports.autoFillXByWeight = autoFillXByWeight; exports.autoFillYByStrategy = autoFillYByStrategy; exports.autoFillYByWeight = autoFillYByWeight; exports.binIdToBinArrayIndex = binIdToBinArrayIndex; exports.buildBitFlagAndNegateStrategyParameters = buildBitFlagAndNegateStrategyParameters; exports.buildLiquidityStrategyParameters = buildLiquidityStrategyParameters; exports.calculateBidAskDistribution = calculateBidAskDistribution; exports.calculateNormalDistribution = calculateNormalDistribution; exports.calculateSpotDistribution = calculateSpotDistribution; exports.capSlippagePercentage = capSlippagePercentage; exports.chunkDepositWithRebalanceEndpoint = chunkDepositWithRebalanceEndpoint; exports.chunkedFetchMultipleBinArrayBitmapExtensionAccount = chunkedFetchMultipleBinArrayBitmapExtensionAccount; exports.chunkedFetchMultiplePoolAccount = chunkedFetchMultiplePoolAccount; exports.chunkedGetMultipleAccountInfos = chunkedGetMultipleAccountInfos; exports.chunks = chunks; exports.computeFee = computeFee; exports.computeFeeFromAmount = computeFeeFromAmount; exports.computeProtocolFee = computeProtocolFee; exports.createProgram = createProgram; exports.decodeAccount = decodeAccount; exports.default = src_default; exports.deriveBinArray = deriveBinArray; exports.deriveBinArrayBitmapExtension = deriveBinArrayBitmapExtension; exports.deriveCustomizablePermissionlessLbPair = deriveCustomizablePermissionlessLbPair; exports.deriveEventAuthority = deriveEventAuthority; exports.deriveLbPair = deriveLbPair; exports.deriveLbPair2 = deriveLbPair2; exports.deriveLbPairWithPresetParamWithIndexKey = deriveLbPairWithPresetParamWithIndexKey; exports.deriveOracle = deriveOracle; exports.derivePermissionLbPair = derivePermissionLbPair; exports.derivePlaceHolderAccountMeta = derivePlaceHolderAccountMeta; exports.derivePosition = derivePosition; exports.derivePresetParameter = derivePresetParameter; exports.derivePresetParameter2 = derivePresetParameter2; exports.derivePresetParameterWithIndex = derivePresetParameterWithIndex; exports.deriveReserve = deriveReserve; exports.deriveRewardVault = deriveRewardVault; exports.deriveTokenBadge = deriveTokenBadge; exports.enumerateBins = enumerateBins; exports.findNextBinArrayIndexWithLiquidity = findNextBinArrayIndexWithLiquidity; exports.findNextBinArrayWithLiquidity = findNextBinArrayWithLiquidity; exports.fromWeightDistributionToAmount = fromWeightDistributionToAmount; exports.fromWeightDistributionToAmountOneSide = fromWeightDistributionToAmountOneSide; exports.getAccountDiscriminator = getAccountDiscriminator; exports.getAmountInBinsAskSide = getAmountInBinsAskSide; exports.getAmountInBinsBidSide = getAmountInBinsBidSide; exports.getAndCapMaxActiveBinSlippage = getAndCapMaxActiveBinSlippage; exports.getAutoFillAmountByRebalancedPosition = getAutoFillAmountByRebalancedPosition; exports.getBaseFee = getBaseFee; exports.getBinArrayLowerUpperBinId = getBinArrayLowerUpperBinId; exports.getBinArraysRequiredByPositionRange = getBinArraysRequiredByPositionRange; exports.getBinCount = getBinCount; exports.getBinFromBinArray = getBinFromBinArray; exports.getBinIdIndexInBinArray = getBinIdIndexInBinArray; exports.getEstimatedComputeUnitIxWithBuffer = getEstimatedComputeUnitIxWithBuffer; exports.getEstimatedComputeUnitUsageWithBuffer = getEstimatedComputeUnitUsageWithBuffer; exports.getLiquidityStrategyParameterBuilder = getLiquidityStrategyParameterBuilder; exports.getOrCreateATAInstruction = getOrCreateATAInstruction; exports.getOutAmount = getOutAmount; exports.getPositionCountByBinCount = getPositionCountByBinCount; exports.getPriceOfBinByBinId = getPriceOfBinByBinId; exports.getRebalanceBinArrayIndexesAndBitmapCoverage = getRebalanceBinArrayIndexesAndBitmapCoverage; exports.getSlippageMaxAmount = getSlippageMaxAmount; exports.getSlippageMinAmount = getSlippageMinAmount; exports.getTokenBalance = getTokenBalance; exports.getTokenDecimals = getTokenDecimals; exports.getTokenProgramId = getTokenProgramId; exports.getTokensMintFromPoolAddress = getTokensMintFromPoolAddress; exports.getTotalFee = getTotalFee; exports.getVariableFee = getVariableFee; exports.isBinIdWithinBinArray = isBinIdWithinBinArray; exports.isOverflowDefaultBinArrayBitmap = isOverflowDefaultBinArrayBitmap; exports.parseLogs = parseLogs; exports.range = range; exports.resetUninvolvedLiquidityParams = resetUninvolvedLiquidityParams; exports.suggestBalancedXParametersFromY = suggestBalancedXParametersFromY; exports.suggestBalancedYParametersFromX = suggestBalancedYParametersFromX; exports.swapExactInQuoteAtBin = swapExactInQuoteAtBin; exports.swapExactOutQuoteAtBin = swapExactOutQuoteAtBin; exports.toAmountAskSide = toAmountAskSide; exports.toAmountBidSide = toAmountBidSide; exports.toAmountBothSide = toAmountBothSide; exports.toAmountIntoBins = toAmountIntoBins; exports.toAmountsBothSideByStrategy = toAmountsBothSideByStrategy; exports.toStrategyParameters = toStrategyParameters; exports.toWeightDistribution = toWeightDistribution; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.updateBinArray = updateBinArray; exports.wrapSOLInstruction = wrapSOLInstruction;
|
|
19640
19914
|
//# sourceMappingURL=index.js.map
|