@hawksightco/hawk-sdk 1.3.235 → 1.3.237
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/src/classes/HawkAPI.d.ts +2 -2
- package/dist/src/classes/HawkAPI.d.ts.map +1 -1
- package/dist/src/classes/Transactions.d.ts +8 -1
- package/dist/src/classes/Transactions.d.ts.map +1 -1
- package/dist/src/classes/Transactions.js +649 -93
- package/dist/src/classes/TxGeneratorAutomations.d.ts +7 -5
- package/dist/src/classes/TxGeneratorAutomations.d.ts.map +1 -1
- package/dist/src/classes/TxGeneratorAutomations.js +120 -34
- package/dist/src/idl/jupiter-idl.d.ts +130 -0
- package/dist/src/idl/jupiter-idl.d.ts.map +1 -1
- package/dist/src/idl/jupiter-idl.js +130 -0
- package/dist/src/ixGenerator/MeteoraDlmmIxGenerator.d.ts +46 -17
- package/dist/src/ixGenerator/MeteoraDlmmIxGenerator.d.ts.map +1 -1
- package/dist/src/ixGenerator/MeteoraDlmmIxGenerator.js +550 -186
- package/package.json +1 -1
|
@@ -46,6 +46,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
46
46
|
};
|
|
47
47
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
48
|
exports.txgen = exports.Transactions = void 0;
|
|
49
|
+
exports.getHybridChunkOffsets = getHybridChunkOffsets;
|
|
50
|
+
exports.batchHybridChunkInstructionGroups = batchHybridChunkInstructionGroups;
|
|
49
51
|
const errors_1 = require("../errors");
|
|
50
52
|
const dlmm_1 = require("@meteora-ag/dlmm");
|
|
51
53
|
const web3 = __importStar(require("@solana/web3.js"));
|
|
@@ -70,6 +72,53 @@ const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
|
70
72
|
const RaydiumIxGenerator_1 = require("../ixGenerator/RaydiumIxGenerator");
|
|
71
73
|
const RaydiumSDK_1 = require("./RaydiumSDK");
|
|
72
74
|
const types_3 = require("../types");
|
|
75
|
+
function getHybridChunkOffsets(positionLowerBinId, chunkLowerBinId, chunkUpperBinId) {
|
|
76
|
+
return {
|
|
77
|
+
chunkLowerOffset: chunkLowerBinId - positionLowerBinId,
|
|
78
|
+
chunkUpperOffset: chunkUpperBinId - positionLowerBinId,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function getInstructionDedupKey(ix) {
|
|
82
|
+
return [
|
|
83
|
+
ix.programId.toString(),
|
|
84
|
+
ix.data.toString('base64'),
|
|
85
|
+
...ix.keys.map(meta => [
|
|
86
|
+
meta.pubkey.toString(),
|
|
87
|
+
meta.isSigner ? '1' : '0',
|
|
88
|
+
meta.isWritable ? '1' : '0',
|
|
89
|
+
].join(':')),
|
|
90
|
+
].join('|');
|
|
91
|
+
}
|
|
92
|
+
function dedupeInitBinArrayInstructions(ixs) {
|
|
93
|
+
const seenInitKeys = new Set();
|
|
94
|
+
const deduped = [];
|
|
95
|
+
for (const ix of ixs) {
|
|
96
|
+
if (ix.label !== 'init_bin_array') {
|
|
97
|
+
deduped.push(ix);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const dedupKey = getInstructionDedupKey(ix.instruction);
|
|
101
|
+
if (seenInitKeys.has(dedupKey)) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
seenInitKeys.add(dedupKey);
|
|
105
|
+
deduped.push(ix);
|
|
106
|
+
}
|
|
107
|
+
return deduped;
|
|
108
|
+
}
|
|
109
|
+
function batchHybridChunkInstructionGroups(chunkInstructionGroups, chunksPerTransaction = 2) {
|
|
110
|
+
if (!Number.isInteger(chunksPerTransaction) || chunksPerTransaction <= 0) {
|
|
111
|
+
throw new Error('chunksPerTransaction must be a positive integer');
|
|
112
|
+
}
|
|
113
|
+
const batchedGroups = [];
|
|
114
|
+
for (let i = 0; i < chunkInstructionGroups.length; i += chunksPerTransaction) {
|
|
115
|
+
const mergedGroup = chunkInstructionGroups
|
|
116
|
+
.slice(i, i + chunksPerTransaction)
|
|
117
|
+
.flat();
|
|
118
|
+
batchedGroups.push(dedupeInitBinArrayInstructions(mergedGroup));
|
|
119
|
+
}
|
|
120
|
+
return batchedGroups;
|
|
121
|
+
}
|
|
73
122
|
class Transactions {
|
|
74
123
|
/**
|
|
75
124
|
* Prohibit creating instance other than getInstance
|
|
@@ -4437,16 +4486,21 @@ class Transactions {
|
|
|
4437
4486
|
}
|
|
4438
4487
|
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
|
|
4439
4488
|
const chunk = chunks[chunkIndex];
|
|
4440
|
-
const
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4489
|
+
const chunkAddLiquidityParams = chunk.layers.map(layer => {
|
|
4490
|
+
const onChainParams = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(layer.params.x0, layer.params.y0, layer.params.deltaX, layer.params.deltaY);
|
|
4491
|
+
return {
|
|
4492
|
+
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
4493
|
+
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
4494
|
+
x0: onChainParams.x0,
|
|
4495
|
+
y0: onChainParams.y0,
|
|
4496
|
+
deltaX: onChainParams.deltaX,
|
|
4497
|
+
deltaY: onChainParams.deltaY,
|
|
4498
|
+
bitFlag: onChainParams.bitFlag,
|
|
4499
|
+
favorXInActiveId,
|
|
4500
|
+
};
|
|
4501
|
+
});
|
|
4502
|
+
const chunkMaxX = chunk.layers.reduce((s, layer) => s.add(layer.maxAmountX), new bn_js_1.default(0));
|
|
4503
|
+
const chunkMaxY = chunk.layers.reduce((s, layer) => s.add(layer.maxAmountY), new bn_js_1.default(0));
|
|
4450
4504
|
const instructions = [];
|
|
4451
4505
|
if (!depositIncluded) {
|
|
4452
4506
|
depositIncluded = true;
|
|
@@ -4482,18 +4536,7 @@ class Transactions {
|
|
|
4482
4536
|
minWithdrawYAmount: new bn_js_1.default(0),
|
|
4483
4537
|
maxDepositYAmount: chunkMaxY,
|
|
4484
4538
|
removeLiquidityParams: [],
|
|
4485
|
-
addLiquidityParams:
|
|
4486
|
-
{
|
|
4487
|
-
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
4488
|
-
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
4489
|
-
x0: onChainParams.x0,
|
|
4490
|
-
y0: onChainParams.y0,
|
|
4491
|
-
deltaX: onChainParams.deltaX,
|
|
4492
|
-
deltaY: onChainParams.deltaY,
|
|
4493
|
-
bitFlag: onChainParams.bitFlag,
|
|
4494
|
-
favorXInActiveId,
|
|
4495
|
-
},
|
|
4496
|
-
],
|
|
4539
|
+
addLiquidityParams: chunkAddLiquidityParams,
|
|
4497
4540
|
});
|
|
4498
4541
|
instructions.push({
|
|
4499
4542
|
instruction: depositIx,
|
|
@@ -4823,7 +4866,7 @@ class Transactions {
|
|
|
4823
4866
|
};
|
|
4824
4867
|
return {
|
|
4825
4868
|
instruction: ix,
|
|
4826
|
-
label: 'Add liquidity by strategy (meteora deposit)'
|
|
4869
|
+
label: 'Add liquidity by strategy (meteora deposit)',
|
|
4827
4870
|
};
|
|
4828
4871
|
}),
|
|
4829
4872
|
];
|
|
@@ -6626,34 +6669,21 @@ class Transactions {
|
|
|
6626
6669
|
rebuildTxGroups.push(initExtendIxs);
|
|
6627
6670
|
const depositChunks = (0, liquidityStrategy_1.chunkHybridDepositParameters)(strategyResults, new bn_js_1.default(minDeltaId), new bn_js_1.default(maxDeltaId), new bn_js_1.default(activeId), binStep, MAX_BINS_PER_DEPOSIT_CHUNK, favorXInActiveId);
|
|
6628
6671
|
for (const chunk of depositChunks) {
|
|
6629
|
-
|
|
6630
|
-
|
|
6631
|
-
|
|
6632
|
-
y0: acc.y0.add(l.params.y0),
|
|
6633
|
-
deltaX: acc.deltaX.add(l.params.deltaX),
|
|
6634
|
-
deltaY: acc.deltaY.add(l.params.deltaY),
|
|
6635
|
-
}), {
|
|
6636
|
-
x0: new bn_js_1.default(0),
|
|
6637
|
-
y0: new bn_js_1.default(0),
|
|
6638
|
-
deltaX: new bn_js_1.default(0),
|
|
6639
|
-
deltaY: new bn_js_1.default(0),
|
|
6640
|
-
});
|
|
6641
|
-
const chunkMergedP = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(chunkMergedLayer.x0, chunkMergedLayer.y0, chunkMergedLayer.deltaX, chunkMergedLayer.deltaY);
|
|
6642
|
-
const chunkAddLiquidityParams = [
|
|
6643
|
-
{
|
|
6672
|
+
const chunkAddLiquidityParams = chunk.layers.map(layer => {
|
|
6673
|
+
const onChainParams = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(layer.params.x0, layer.params.y0, layer.params.deltaX, layer.params.deltaY);
|
|
6674
|
+
return {
|
|
6644
6675
|
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
6645
6676
|
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
6646
|
-
x0:
|
|
6647
|
-
y0:
|
|
6648
|
-
deltaX:
|
|
6649
|
-
deltaY:
|
|
6650
|
-
bitFlag:
|
|
6677
|
+
x0: onChainParams.x0,
|
|
6678
|
+
y0: onChainParams.y0,
|
|
6679
|
+
deltaX: onChainParams.deltaX,
|
|
6680
|
+
deltaY: onChainParams.deltaY,
|
|
6681
|
+
bitFlag: onChainParams.bitFlag,
|
|
6651
6682
|
favorXInActiveId,
|
|
6652
|
-
}
|
|
6653
|
-
|
|
6654
|
-
const
|
|
6655
|
-
const
|
|
6656
|
-
const chunkTotalMaxY = chunkMergedBinsAmounts.reduce((s, b) => s.add(b.amountY), new bn_js_1.default(0));
|
|
6683
|
+
};
|
|
6684
|
+
});
|
|
6685
|
+
const chunkTotalMaxX = chunk.layers.reduce((s, layer) => s.add(layer.maxAmountX), new bn_js_1.default(0));
|
|
6686
|
+
const chunkTotalMaxY = chunk.layers.reduce((s, layer) => s.add(layer.maxAmountY), new bn_js_1.default(0));
|
|
6657
6687
|
const binArrayInitIxs = yield this._initBinArraysForRange(connection, lbPair, dlmmPool, chunk.lowerBinId, chunk.upperBinId);
|
|
6658
6688
|
rebuildTxGroups.push([
|
|
6659
6689
|
...binArrayInitIxs.map(ix => ({
|
|
@@ -6699,11 +6729,13 @@ class Transactions {
|
|
|
6699
6729
|
checkRange: params.checkRange,
|
|
6700
6730
|
pdaTokenType: types_1.TokenType.ATA,
|
|
6701
6731
|
});
|
|
6702
|
-
rebuildTxGroups.push([
|
|
6732
|
+
rebuildTxGroups.push([
|
|
6733
|
+
{
|
|
6703
6734
|
label: 'sweep_dust',
|
|
6704
6735
|
class: CLASS,
|
|
6705
6736
|
instruction: sweepIx,
|
|
6706
|
-
}
|
|
6737
|
+
},
|
|
6738
|
+
]);
|
|
6707
6739
|
// =========================================================================
|
|
6708
6740
|
// Assemble final TX list
|
|
6709
6741
|
// =========================================================================
|
|
@@ -6727,6 +6759,309 @@ class Transactions {
|
|
|
6727
6759
|
}
|
|
6728
6760
|
});
|
|
6729
6761
|
}
|
|
6762
|
+
meteoraRebalanceHybridLiquidityV2Automation(_a) {
|
|
6763
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
6764
|
+
var _b, _c;
|
|
6765
|
+
try {
|
|
6766
|
+
const MAX_INIT_WIDTH = 70;
|
|
6767
|
+
const MAX_EXTEND_LENGTH = 91;
|
|
6768
|
+
const MAX_BINS_PER_WITHDRAW_CHUNK = 175;
|
|
6769
|
+
const CHUNKS_PER_TRANSACTION = 3;
|
|
6770
|
+
const CLASS = 'meteoraRebalanceHybridLiquidityV2Automation';
|
|
6771
|
+
// --- Validate layers ---
|
|
6772
|
+
const layers = params.layers;
|
|
6773
|
+
if (layers.length === 0 || layers.length > 3) {
|
|
6774
|
+
throw new Error('layers must have 1-3 entries');
|
|
6775
|
+
}
|
|
6776
|
+
const sumX = layers.reduce((s, l) => s + l.x.percentage, 0);
|
|
6777
|
+
const sumY = layers.reduce((s, l) => s + l.y.percentage, 0);
|
|
6778
|
+
if (sumX !== 100 || sumY !== 100) {
|
|
6779
|
+
throw new Error(`Layer percentages must sum to 100 per side (got X=${sumX}, Y=${sumY})`);
|
|
6780
|
+
}
|
|
6781
|
+
const { lbPair, lbPairState, dlmmPool, lowerBinId: currentLowerBinId, upperBinId: currentUpperBinId, estimatedTotalX, estimatedTotalY, activeId, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram, withdrawFn, baseRemainingAccountsInfo, } = yield this.resolvePositionContext({
|
|
6782
|
+
connection,
|
|
6783
|
+
userWallet: params.userWallet,
|
|
6784
|
+
position: params.currentPosition,
|
|
6785
|
+
compound: params.compound,
|
|
6786
|
+
targetActiveBin: params.targetActiveBin,
|
|
6787
|
+
});
|
|
6788
|
+
if (activeId < params.checkRange.lowerRange ||
|
|
6789
|
+
activeId > params.checkRange.upperRange) {
|
|
6790
|
+
throw new Error(`Active bin ${activeId} outside checkRange [${params.checkRange.lowerRange}, ${params.checkRange.upperRange}]`);
|
|
6791
|
+
}
|
|
6792
|
+
const newLowerBinId = activeId + params.relativeBinRange.lowerRange;
|
|
6793
|
+
const newUpperBinId = activeId + params.relativeBinRange.upperRange;
|
|
6794
|
+
const newWidth = newUpperBinId - newLowerBinId + 1;
|
|
6795
|
+
const currentWidth = currentUpperBinId - currentLowerBinId + 1;
|
|
6796
|
+
const maxBinsPerDepositChunk = newWidth === 69 ? 69 : 50;
|
|
6797
|
+
const useLayeredDepositsForSmallPosition = newWidth <= 69 && layers.length > 1;
|
|
6798
|
+
// =========================================================================
|
|
6799
|
+
// PHASE 1: TEARDOWN (withdraw from old position + close)
|
|
6800
|
+
// =========================================================================
|
|
6801
|
+
const teardownTxGroups = [];
|
|
6802
|
+
if (currentWidth <= MAX_BINS_PER_WITHDRAW_CHUNK) {
|
|
6803
|
+
teardownTxGroups.push([
|
|
6804
|
+
...(yield withdrawFn({
|
|
6805
|
+
position: params.currentPosition,
|
|
6806
|
+
minBinId: currentLowerBinId,
|
|
6807
|
+
maxBinId: currentUpperBinId,
|
|
6808
|
+
compound: params.compound,
|
|
6809
|
+
origin: CLASS,
|
|
6810
|
+
})),
|
|
6811
|
+
{
|
|
6812
|
+
label: 'close_position',
|
|
6813
|
+
class: CLASS,
|
|
6814
|
+
instruction: yield this.ix.meteoraDlmm.closePosition2Automation({
|
|
6815
|
+
connection,
|
|
6816
|
+
userWallet: params.userWallet,
|
|
6817
|
+
position: params.currentPosition,
|
|
6818
|
+
lbPair,
|
|
6819
|
+
}),
|
|
6820
|
+
},
|
|
6821
|
+
]);
|
|
6822
|
+
}
|
|
6823
|
+
else {
|
|
6824
|
+
const withdrawChunks = (0, liquidityStrategy_1.chunkBinRange)(currentLowerBinId, currentUpperBinId, MAX_BINS_PER_WITHDRAW_CHUNK);
|
|
6825
|
+
for (let i = 0; i < withdrawChunks.length; i++) {
|
|
6826
|
+
const chunk = withdrawChunks[i];
|
|
6827
|
+
const isLast = i === withdrawChunks.length - 1;
|
|
6828
|
+
const chunkIxs = [
|
|
6829
|
+
...(yield withdrawFn({
|
|
6830
|
+
position: params.currentPosition,
|
|
6831
|
+
minBinId: chunk.lowerBinId,
|
|
6832
|
+
maxBinId: chunk.upperBinId,
|
|
6833
|
+
compound: params.compound,
|
|
6834
|
+
origin: CLASS,
|
|
6835
|
+
})),
|
|
6836
|
+
];
|
|
6837
|
+
if (isLast) {
|
|
6838
|
+
chunkIxs.push({
|
|
6839
|
+
label: 'close_position',
|
|
6840
|
+
class: CLASS,
|
|
6841
|
+
instruction: yield this.ix.meteoraDlmm.closePosition2Automation({
|
|
6842
|
+
connection,
|
|
6843
|
+
userWallet: params.userWallet,
|
|
6844
|
+
position: params.currentPosition,
|
|
6845
|
+
lbPair,
|
|
6846
|
+
}),
|
|
6847
|
+
});
|
|
6848
|
+
}
|
|
6849
|
+
teardownTxGroups.push(chunkIxs);
|
|
6850
|
+
}
|
|
6851
|
+
}
|
|
6852
|
+
// =========================================================================
|
|
6853
|
+
// PHASE 2: REBUILD (init + extend + deposit with NEW add_liquidity_2 instruction)
|
|
6854
|
+
// =========================================================================
|
|
6855
|
+
const effectiveAmountX = (_b = params.amountXOverride) !== null && _b !== void 0 ? _b : estimatedTotalX;
|
|
6856
|
+
const effectiveAmountY = (_c = params.amountYOverride) !== null && _c !== void 0 ? _c : estimatedTotalY;
|
|
6857
|
+
const favorXInActiveId = effectiveAmountY.isZero() && !effectiveAmountX.isZero();
|
|
6858
|
+
const minDeltaId = newLowerBinId - activeId;
|
|
6859
|
+
const maxDeltaId = newUpperBinId - activeId;
|
|
6860
|
+
const binStep = new bn_js_1.default(lbPairState.binStep);
|
|
6861
|
+
const contractLayers = layers.map(l => ({
|
|
6862
|
+
strategyTypeX: (0, liquidityStrategy_1.distributionToStrategyType)(l.x.distribution),
|
|
6863
|
+
strategyTypeY: (0, liquidityStrategy_1.distributionToStrategyType)(l.y.distribution),
|
|
6864
|
+
percentageX: l.x.percentage,
|
|
6865
|
+
percentageY: l.y.percentage,
|
|
6866
|
+
}));
|
|
6867
|
+
// Build init + extend instructions
|
|
6868
|
+
const initExtendIxs = [];
|
|
6869
|
+
const initWidth = Math.min(MAX_INIT_WIDTH, newWidth);
|
|
6870
|
+
initExtendIxs.push({
|
|
6871
|
+
label: 'initialize_position_relative',
|
|
6872
|
+
class: CLASS,
|
|
6873
|
+
instruction: yield this.ix.meteoraDlmm.initializePositionRelativeAutomation(connection, {
|
|
6874
|
+
userWallet: params.userWallet,
|
|
6875
|
+
lbPair,
|
|
6876
|
+
position: params.newPosition,
|
|
6877
|
+
relativeLowerBinId: params.relativeBinRange.lowerRange,
|
|
6878
|
+
width: initWidth,
|
|
6879
|
+
}),
|
|
6880
|
+
});
|
|
6881
|
+
let positionBinsAllocated = initWidth;
|
|
6882
|
+
while (positionBinsAllocated < newWidth) {
|
|
6883
|
+
const toAdd = Math.min(MAX_EXTEND_LENGTH, newWidth - positionBinsAllocated);
|
|
6884
|
+
initExtendIxs.push({
|
|
6885
|
+
label: 'increase_position_length',
|
|
6886
|
+
class: CLASS,
|
|
6887
|
+
instruction: yield this.ix.meteoraDlmm.increasePositionLengthAutomation({
|
|
6888
|
+
connection,
|
|
6889
|
+
userWallet: params.userWallet,
|
|
6890
|
+
lbPair,
|
|
6891
|
+
position: params.newPosition,
|
|
6892
|
+
lengthToAdd: toAdd,
|
|
6893
|
+
side: types_1.MeteoraPositionSide.Upper,
|
|
6894
|
+
}),
|
|
6895
|
+
});
|
|
6896
|
+
positionBinsAllocated += toAdd;
|
|
6897
|
+
}
|
|
6898
|
+
const rebuildTxGroups = [];
|
|
6899
|
+
if (newWidth <= maxBinsPerDepositChunk) {
|
|
6900
|
+
const binArrayInitIxs = yield this._initBinArraysForRange(connection, lbPair, dlmmPool, newLowerBinId, newUpperBinId);
|
|
6901
|
+
const binArrayInitIxsLabeled = binArrayInitIxs.map(ix => ({
|
|
6902
|
+
label: 'init_bin_array',
|
|
6903
|
+
class: CLASS,
|
|
6904
|
+
instruction: ix,
|
|
6905
|
+
}));
|
|
6906
|
+
if (useLayeredDepositsForSmallPosition) {
|
|
6907
|
+
const strategyResults = (0, liquidityStrategy_1.distributionToHybridStrategy)({
|
|
6908
|
+
hybridDistribution: layers,
|
|
6909
|
+
amountX: effectiveAmountX,
|
|
6910
|
+
amountY: effectiveAmountY,
|
|
6911
|
+
minDeltaId: new bn_js_1.default(minDeltaId),
|
|
6912
|
+
maxDeltaId: new bn_js_1.default(maxDeltaId),
|
|
6913
|
+
binStep,
|
|
6914
|
+
activeId: new bn_js_1.default(activeId),
|
|
6915
|
+
});
|
|
6916
|
+
rebuildTxGroups.push(initExtendIxs);
|
|
6917
|
+
const layeredDepositIxs = [
|
|
6918
|
+
...binArrayInitIxsLabeled,
|
|
6919
|
+
];
|
|
6920
|
+
for (let i = 0; i < strategyResults.length; i++) {
|
|
6921
|
+
const layerStrategy = strategyResults[i];
|
|
6922
|
+
if (layerStrategy.maxAmountX.isZero() && layerStrategy.maxAmountY.isZero()) {
|
|
6923
|
+
continue;
|
|
6924
|
+
}
|
|
6925
|
+
const singleLayer = {
|
|
6926
|
+
strategyTypeX: contractLayers[i].strategyTypeX,
|
|
6927
|
+
strategyTypeY: contractLayers[i].strategyTypeY,
|
|
6928
|
+
percentageX: layers[i].x.percentage > 0 ? 100 : 0,
|
|
6929
|
+
percentageY: layers[i].y.percentage > 0 ? 100 : 0,
|
|
6930
|
+
};
|
|
6931
|
+
const depositIx = yield this.ix.meteoraDlmm.addLiquidity2Automation({
|
|
6932
|
+
connection,
|
|
6933
|
+
userWallet: params.userWallet,
|
|
6934
|
+
lbPair,
|
|
6935
|
+
position: params.newPosition,
|
|
6936
|
+
tokenXMint,
|
|
6937
|
+
tokenYMint,
|
|
6938
|
+
tokenXProgram,
|
|
6939
|
+
tokenYProgram,
|
|
6940
|
+
layers: [singleLayer],
|
|
6941
|
+
checkRange: params.checkRange,
|
|
6942
|
+
userTokenXAmount: layerStrategy.maxAmountX,
|
|
6943
|
+
userTokenYAmount: layerStrategy.maxAmountY,
|
|
6944
|
+
remainingAccountsInfo: baseRemainingAccountsInfo,
|
|
6945
|
+
pdaTokenType: types_1.TokenType.ATA,
|
|
6946
|
+
lowerBinId: newLowerBinId,
|
|
6947
|
+
upperBinId: newUpperBinId,
|
|
6948
|
+
});
|
|
6949
|
+
layeredDepositIxs.push({ label: 'deposit_v2', class: CLASS, instruction: depositIx });
|
|
6950
|
+
}
|
|
6951
|
+
if (layeredDepositIxs.length > 0) {
|
|
6952
|
+
rebuildTxGroups.push(layeredDepositIxs);
|
|
6953
|
+
}
|
|
6954
|
+
}
|
|
6955
|
+
else {
|
|
6956
|
+
const depositIx = yield this.ix.meteoraDlmm.addLiquidity2Automation({
|
|
6957
|
+
connection,
|
|
6958
|
+
userWallet: params.userWallet,
|
|
6959
|
+
lbPair,
|
|
6960
|
+
position: params.newPosition,
|
|
6961
|
+
tokenXMint,
|
|
6962
|
+
tokenYMint,
|
|
6963
|
+
tokenXProgram,
|
|
6964
|
+
tokenYProgram,
|
|
6965
|
+
layers: contractLayers,
|
|
6966
|
+
checkRange: params.checkRange,
|
|
6967
|
+
userTokenXAmount: effectiveAmountX,
|
|
6968
|
+
userTokenYAmount: effectiveAmountY,
|
|
6969
|
+
remainingAccountsInfo: baseRemainingAccountsInfo,
|
|
6970
|
+
pdaTokenType: types_1.TokenType.ATA,
|
|
6971
|
+
lowerBinId: newLowerBinId,
|
|
6972
|
+
upperBinId: newUpperBinId,
|
|
6973
|
+
});
|
|
6974
|
+
if (newWidth <= MAX_INIT_WIDTH) {
|
|
6975
|
+
rebuildTxGroups.push([
|
|
6976
|
+
...initExtendIxs,
|
|
6977
|
+
...binArrayInitIxsLabeled,
|
|
6978
|
+
{ label: 'deposit_v2', class: CLASS, instruction: depositIx },
|
|
6979
|
+
]);
|
|
6980
|
+
}
|
|
6981
|
+
else {
|
|
6982
|
+
rebuildTxGroups.push(initExtendIxs);
|
|
6983
|
+
rebuildTxGroups.push([
|
|
6984
|
+
...binArrayInitIxsLabeled,
|
|
6985
|
+
{ label: 'deposit_v2', class: CLASS, instruction: depositIx },
|
|
6986
|
+
]);
|
|
6987
|
+
}
|
|
6988
|
+
}
|
|
6989
|
+
}
|
|
6990
|
+
else {
|
|
6991
|
+
rebuildTxGroups.push(initExtendIxs);
|
|
6992
|
+
const strategyResults = (0, liquidityStrategy_1.distributionToHybridStrategy)({
|
|
6993
|
+
hybridDistribution: layers,
|
|
6994
|
+
amountX: effectiveAmountX,
|
|
6995
|
+
amountY: effectiveAmountY,
|
|
6996
|
+
minDeltaId: new bn_js_1.default(minDeltaId),
|
|
6997
|
+
maxDeltaId: new bn_js_1.default(maxDeltaId),
|
|
6998
|
+
binStep,
|
|
6999
|
+
activeId: new bn_js_1.default(activeId),
|
|
7000
|
+
});
|
|
7001
|
+
const depositChunks = (0, liquidityStrategy_1.chunkHybridDepositParameters)(strategyResults, new bn_js_1.default(minDeltaId), new bn_js_1.default(maxDeltaId), new bn_js_1.default(activeId), binStep, maxBinsPerDepositChunk, favorXInActiveId);
|
|
7002
|
+
const chunkInstructionGroups = [];
|
|
7003
|
+
for (const chunk of depositChunks) {
|
|
7004
|
+
const { chunkLowerOffset, chunkUpperOffset } = getHybridChunkOffsets(newLowerBinId, chunk.lowerBinId, chunk.upperBinId);
|
|
7005
|
+
const chunkAmountX = chunk.layers.reduce((sum, layer) => sum.add(layer.maxAmountX), new bn_js_1.default(0));
|
|
7006
|
+
const chunkAmountY = chunk.layers.reduce((sum, layer) => sum.add(layer.maxAmountY), new bn_js_1.default(0));
|
|
7007
|
+
if (chunkAmountX.isZero() && chunkAmountY.isZero()) {
|
|
7008
|
+
continue;
|
|
7009
|
+
}
|
|
7010
|
+
const binArrayInitIxs = yield this._initBinArraysForRange(connection, lbPair, dlmmPool, chunk.lowerBinId, chunk.upperBinId);
|
|
7011
|
+
const depositIx = yield this.ix.meteoraDlmm.addLiquidity2Automation({
|
|
7012
|
+
connection,
|
|
7013
|
+
userWallet: params.userWallet,
|
|
7014
|
+
lbPair,
|
|
7015
|
+
position: params.newPosition,
|
|
7016
|
+
tokenXMint,
|
|
7017
|
+
tokenYMint,
|
|
7018
|
+
tokenXProgram,
|
|
7019
|
+
tokenYProgram,
|
|
7020
|
+
layers: contractLayers,
|
|
7021
|
+
checkRange: params.checkRange,
|
|
7022
|
+
userTokenXAmount: chunkAmountX,
|
|
7023
|
+
userTokenYAmount: chunkAmountY,
|
|
7024
|
+
chunkLowerOffset,
|
|
7025
|
+
chunkUpperOffset,
|
|
7026
|
+
remainingAccountsInfo: baseRemainingAccountsInfo,
|
|
7027
|
+
pdaTokenType: types_1.TokenType.ATA,
|
|
7028
|
+
lowerBinId: chunk.lowerBinId,
|
|
7029
|
+
upperBinId: chunk.upperBinId,
|
|
7030
|
+
});
|
|
7031
|
+
chunkInstructionGroups.push([
|
|
7032
|
+
...binArrayInitIxs.map(ix => ({
|
|
7033
|
+
label: 'init_bin_array',
|
|
7034
|
+
class: CLASS,
|
|
7035
|
+
instruction: ix,
|
|
7036
|
+
})),
|
|
7037
|
+
{ label: 'deposit_v2', class: CLASS, instruction: depositIx },
|
|
7038
|
+
]);
|
|
7039
|
+
}
|
|
7040
|
+
rebuildTxGroups.push(...batchHybridChunkInstructionGroups(chunkInstructionGroups, CHUNKS_PER_TRANSACTION));
|
|
7041
|
+
}
|
|
7042
|
+
// =========================================================================
|
|
7043
|
+
// Assemble final TX list
|
|
7044
|
+
// =========================================================================
|
|
7045
|
+
const allTxGroups = [...teardownTxGroups, ...rebuildTxGroups];
|
|
7046
|
+
const transactions = [];
|
|
7047
|
+
for (const txIxs of allTxGroups) {
|
|
7048
|
+
transactions.push(yield (0, functions_1.createTransactionMeta2)({
|
|
7049
|
+
payer: params.userWallet,
|
|
7050
|
+
addressLookupTableAddresses: addresses_1.GLOBAL_ALT,
|
|
7051
|
+
mainInstructions: txIxs,
|
|
7052
|
+
}));
|
|
7053
|
+
}
|
|
7054
|
+
return transactions;
|
|
7055
|
+
}
|
|
7056
|
+
catch (error) {
|
|
7057
|
+
console.error('=== REBALANCE HYBRID LIQUIDITY V2 AUTOMATION ERROR ===');
|
|
7058
|
+
console.error('Error:', error instanceof Error ? error.message : String(error));
|
|
7059
|
+
console.error('Stack:', error instanceof Error ? error.stack : 'No stack trace');
|
|
7060
|
+
console.error('=== END ERROR LOG ===');
|
|
7061
|
+
throw error;
|
|
7062
|
+
}
|
|
7063
|
+
});
|
|
7064
|
+
}
|
|
6730
7065
|
/**
|
|
6731
7066
|
* Hybrid liquidity redeposit for automation: withdraw all liquidity from the same
|
|
6732
7067
|
* position and re-deposit with independent X/Y distributions per layer.
|
|
@@ -6756,9 +7091,13 @@ class Transactions {
|
|
|
6756
7091
|
userWallet: params.userWallet,
|
|
6757
7092
|
position: params.position,
|
|
6758
7093
|
compound: params.compound,
|
|
6759
|
-
targetActiveBin:
|
|
7094
|
+
targetActiveBin: undefined,
|
|
6760
7095
|
skipClaim: true,
|
|
6761
7096
|
});
|
|
7097
|
+
if (params.targetActiveBin !== undefined &&
|
|
7098
|
+
params.targetActiveBin !== activeId) {
|
|
7099
|
+
throw new Error(`targetActiveBin ${params.targetActiveBin} does not match live active bin ${activeId} for V2 redeposit`);
|
|
7100
|
+
}
|
|
6762
7101
|
const positionWidth = upperBinId - lowerBinId + 1;
|
|
6763
7102
|
const binStep = new bn_js_1.default(lbPairState.binStep);
|
|
6764
7103
|
const effectiveAmountX = (_b = params.amountXOverride) !== null && _b !== void 0 ? _b : estimatedTotalX;
|
|
@@ -6858,34 +7197,21 @@ class Transactions {
|
|
|
6858
7197
|
// Chunked deposit — one TX per chunk, all layers per chunk
|
|
6859
7198
|
const depositChunks = (0, liquidityStrategy_1.chunkHybridDepositParameters)(strategyResults, new bn_js_1.default(minDeltaId), new bn_js_1.default(maxDeltaId), new bn_js_1.default(activeId), binStep, MAX_BINS_PER_DEPOSIT_CHUNK, favorXInActiveId);
|
|
6860
7199
|
for (const chunk of depositChunks) {
|
|
6861
|
-
|
|
6862
|
-
|
|
6863
|
-
|
|
6864
|
-
y0: acc.y0.add(l.params.y0),
|
|
6865
|
-
deltaX: acc.deltaX.add(l.params.deltaX),
|
|
6866
|
-
deltaY: acc.deltaY.add(l.params.deltaY),
|
|
6867
|
-
}), {
|
|
6868
|
-
x0: new bn_js_1.default(0),
|
|
6869
|
-
y0: new bn_js_1.default(0),
|
|
6870
|
-
deltaX: new bn_js_1.default(0),
|
|
6871
|
-
deltaY: new bn_js_1.default(0),
|
|
6872
|
-
});
|
|
6873
|
-
const chunkMergedP = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(chunkMergedLayer.x0, chunkMergedLayer.y0, chunkMergedLayer.deltaX, chunkMergedLayer.deltaY);
|
|
6874
|
-
const chunkAddLiquidityParams = [
|
|
6875
|
-
{
|
|
7200
|
+
const chunkAddLiquidityParams = chunk.layers.map(layer => {
|
|
7201
|
+
const onChainParams = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(layer.params.x0, layer.params.y0, layer.params.deltaX, layer.params.deltaY);
|
|
7202
|
+
return {
|
|
6876
7203
|
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
6877
7204
|
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
6878
|
-
x0:
|
|
6879
|
-
y0:
|
|
6880
|
-
deltaX:
|
|
6881
|
-
deltaY:
|
|
6882
|
-
bitFlag:
|
|
7205
|
+
x0: onChainParams.x0,
|
|
7206
|
+
y0: onChainParams.y0,
|
|
7207
|
+
deltaX: onChainParams.deltaX,
|
|
7208
|
+
deltaY: onChainParams.deltaY,
|
|
7209
|
+
bitFlag: onChainParams.bitFlag,
|
|
6883
7210
|
favorXInActiveId,
|
|
6884
|
-
}
|
|
6885
|
-
|
|
6886
|
-
const
|
|
6887
|
-
const
|
|
6888
|
-
const chunkTotalMaxY = chunkMergedBinsAmounts.reduce((s, b) => s.add(b.amountY), new bn_js_1.default(0));
|
|
7211
|
+
};
|
|
7212
|
+
});
|
|
7213
|
+
const chunkTotalMaxX = chunk.layers.reduce((s, layer) => s.add(layer.maxAmountX), new bn_js_1.default(0));
|
|
7214
|
+
const chunkTotalMaxY = chunk.layers.reduce((s, layer) => s.add(layer.maxAmountY), new bn_js_1.default(0));
|
|
6889
7215
|
depositTxGroups.push([
|
|
6890
7216
|
yield depositFn({
|
|
6891
7217
|
position: params.position,
|
|
@@ -6924,11 +7250,13 @@ class Transactions {
|
|
|
6924
7250
|
optionalTokenYAmount: undefined, // sweep — use full ATA balance
|
|
6925
7251
|
pdaTokenType: types_1.TokenType.ATA,
|
|
6926
7252
|
});
|
|
6927
|
-
depositTxGroups.push([
|
|
7253
|
+
depositTxGroups.push([
|
|
7254
|
+
{
|
|
6928
7255
|
label: 'sweep_dust',
|
|
6929
7256
|
class: CLASS,
|
|
6930
7257
|
instruction: sweepIx,
|
|
6931
|
-
}
|
|
7258
|
+
},
|
|
7259
|
+
]);
|
|
6932
7260
|
// =========================================================================
|
|
6933
7261
|
// Assemble final TX list
|
|
6934
7262
|
// =========================================================================
|
|
@@ -6952,6 +7280,215 @@ class Transactions {
|
|
|
6952
7280
|
}
|
|
6953
7281
|
});
|
|
6954
7282
|
}
|
|
7283
|
+
meteoraRedepositHybridLiquidityV2Automation(_a) {
|
|
7284
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
7285
|
+
var _b, _c;
|
|
7286
|
+
try {
|
|
7287
|
+
const MAX_BINS_PER_WITHDRAW_CHUNK = 150;
|
|
7288
|
+
const CHUNKS_PER_TRANSACTION = 3;
|
|
7289
|
+
const CLASS = 'meteoraRedepositHybridLiquidityV2Automation';
|
|
7290
|
+
// --- Validate layers ---
|
|
7291
|
+
const layers = params.layers;
|
|
7292
|
+
if (layers.length === 0 || layers.length > 3) {
|
|
7293
|
+
throw new Error('layers must have 1-3 entries');
|
|
7294
|
+
}
|
|
7295
|
+
const sumX = layers.reduce((s, l) => s + l.x.percentage, 0);
|
|
7296
|
+
const sumY = layers.reduce((s, l) => s + l.y.percentage, 0);
|
|
7297
|
+
if (sumX !== 100 || sumY !== 100) {
|
|
7298
|
+
throw new Error(`Layer percentages must sum to 100 per side (got X=${sumX}, Y=${sumY})`);
|
|
7299
|
+
}
|
|
7300
|
+
const { lbPair, lbPairState, dlmmPool, lowerBinId, upperBinId, estimatedTotalX, estimatedTotalY, activeId, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram, withdrawFn, baseRemainingAccountsInfo, } = yield this.resolvePositionContext({
|
|
7301
|
+
connection,
|
|
7302
|
+
userWallet: params.userWallet,
|
|
7303
|
+
position: params.position,
|
|
7304
|
+
compound: params.compound,
|
|
7305
|
+
targetActiveBin: params.targetActiveBin,
|
|
7306
|
+
skipClaim: true,
|
|
7307
|
+
});
|
|
7308
|
+
const positionWidth = upperBinId - lowerBinId + 1;
|
|
7309
|
+
const maxBinsPerDepositChunk = positionWidth === 69 ? 69 : 50;
|
|
7310
|
+
const useLayeredDepositsForSmallPosition = positionWidth <= 69 && layers.length > 1;
|
|
7311
|
+
const effectiveAmountX = (_b = params.amountXOverride) !== null && _b !== void 0 ? _b : estimatedTotalX;
|
|
7312
|
+
const effectiveAmountY = (_c = params.amountYOverride) !== null && _c !== void 0 ? _c : estimatedTotalY;
|
|
7313
|
+
const favorXInActiveId = effectiveAmountY.isZero() && !effectiveAmountX.isZero();
|
|
7314
|
+
const minDeltaId = lowerBinId - activeId;
|
|
7315
|
+
const maxDeltaId = upperBinId - activeId;
|
|
7316
|
+
const binStep = new bn_js_1.default(lbPairState.binStep);
|
|
7317
|
+
const contractLayers = layers.map(l => ({
|
|
7318
|
+
strategyTypeX: (0, liquidityStrategy_1.distributionToStrategyType)(l.x.distribution),
|
|
7319
|
+
strategyTypeY: (0, liquidityStrategy_1.distributionToStrategyType)(l.y.distribution),
|
|
7320
|
+
percentageX: l.x.percentage,
|
|
7321
|
+
percentageY: l.y.percentage,
|
|
7322
|
+
}));
|
|
7323
|
+
// =========================================================================
|
|
7324
|
+
// PHASE 1: WITHDRAW (no close — same position)
|
|
7325
|
+
// =========================================================================
|
|
7326
|
+
const withdrawTxGroups = [];
|
|
7327
|
+
if (positionWidth <= MAX_BINS_PER_WITHDRAW_CHUNK) {
|
|
7328
|
+
withdrawTxGroups.push([
|
|
7329
|
+
...(yield withdrawFn({
|
|
7330
|
+
position: params.position,
|
|
7331
|
+
minBinId: lowerBinId,
|
|
7332
|
+
maxBinId: upperBinId,
|
|
7333
|
+
compound: params.compound,
|
|
7334
|
+
origin: CLASS,
|
|
7335
|
+
})),
|
|
7336
|
+
]);
|
|
7337
|
+
}
|
|
7338
|
+
else {
|
|
7339
|
+
const withdrawChunks = (0, liquidityStrategy_1.chunkBinRange)(lowerBinId, upperBinId, MAX_BINS_PER_WITHDRAW_CHUNK);
|
|
7340
|
+
for (const chunk of withdrawChunks) {
|
|
7341
|
+
withdrawTxGroups.push([
|
|
7342
|
+
...(yield withdrawFn({
|
|
7343
|
+
position: params.position,
|
|
7344
|
+
minBinId: chunk.lowerBinId,
|
|
7345
|
+
maxBinId: chunk.upperBinId,
|
|
7346
|
+
compound: params.compound,
|
|
7347
|
+
origin: CLASS,
|
|
7348
|
+
})),
|
|
7349
|
+
]);
|
|
7350
|
+
}
|
|
7351
|
+
}
|
|
7352
|
+
// =========================================================================
|
|
7353
|
+
// PHASE 2: DEPOSIT (hybrid layers via new add_liquidity_2 instruction)
|
|
7354
|
+
// =========================================================================
|
|
7355
|
+
const depositTxGroups = [];
|
|
7356
|
+
if (positionWidth <= maxBinsPerDepositChunk) {
|
|
7357
|
+
if (useLayeredDepositsForSmallPosition) {
|
|
7358
|
+
const strategyResults = (0, liquidityStrategy_1.distributionToHybridStrategy)({
|
|
7359
|
+
hybridDistribution: layers,
|
|
7360
|
+
amountX: effectiveAmountX,
|
|
7361
|
+
amountY: effectiveAmountY,
|
|
7362
|
+
minDeltaId: new bn_js_1.default(minDeltaId),
|
|
7363
|
+
maxDeltaId: new bn_js_1.default(maxDeltaId),
|
|
7364
|
+
binStep,
|
|
7365
|
+
activeId: new bn_js_1.default(activeId),
|
|
7366
|
+
});
|
|
7367
|
+
const layeredDepositIxs = [];
|
|
7368
|
+
for (let i = 0; i < strategyResults.length; i++) {
|
|
7369
|
+
const layerStrategy = strategyResults[i];
|
|
7370
|
+
if (layerStrategy.maxAmountX.isZero() && layerStrategy.maxAmountY.isZero()) {
|
|
7371
|
+
continue;
|
|
7372
|
+
}
|
|
7373
|
+
const singleLayer = {
|
|
7374
|
+
strategyTypeX: contractLayers[i].strategyTypeX,
|
|
7375
|
+
strategyTypeY: contractLayers[i].strategyTypeY,
|
|
7376
|
+
percentageX: layers[i].x.percentage > 0 ? 100 : 0,
|
|
7377
|
+
percentageY: layers[i].y.percentage > 0 ? 100 : 0,
|
|
7378
|
+
};
|
|
7379
|
+
const depositIx = yield this.ix.meteoraDlmm.addLiquidity2Automation({
|
|
7380
|
+
connection,
|
|
7381
|
+
userWallet: params.userWallet,
|
|
7382
|
+
lbPair,
|
|
7383
|
+
position: params.position,
|
|
7384
|
+
tokenXMint,
|
|
7385
|
+
tokenYMint,
|
|
7386
|
+
tokenXProgram,
|
|
7387
|
+
tokenYProgram,
|
|
7388
|
+
layers: [singleLayer],
|
|
7389
|
+
userTokenXAmount: layerStrategy.maxAmountX,
|
|
7390
|
+
userTokenYAmount: layerStrategy.maxAmountY,
|
|
7391
|
+
remainingAccountsInfo: baseRemainingAccountsInfo,
|
|
7392
|
+
pdaTokenType: types_1.TokenType.ATA,
|
|
7393
|
+
lowerBinId,
|
|
7394
|
+
upperBinId,
|
|
7395
|
+
});
|
|
7396
|
+
layeredDepositIxs.push({ label: 'deposit_v2', class: CLASS, instruction: depositIx });
|
|
7397
|
+
}
|
|
7398
|
+
if (layeredDepositIxs.length > 0) {
|
|
7399
|
+
depositTxGroups.push(layeredDepositIxs);
|
|
7400
|
+
}
|
|
7401
|
+
}
|
|
7402
|
+
else {
|
|
7403
|
+
const depositIx = yield this.ix.meteoraDlmm.addLiquidity2Automation({
|
|
7404
|
+
connection,
|
|
7405
|
+
userWallet: params.userWallet,
|
|
7406
|
+
lbPair,
|
|
7407
|
+
position: params.position,
|
|
7408
|
+
tokenXMint,
|
|
7409
|
+
tokenYMint,
|
|
7410
|
+
tokenXProgram,
|
|
7411
|
+
tokenYProgram,
|
|
7412
|
+
layers: contractLayers,
|
|
7413
|
+
userTokenXAmount: effectiveAmountX,
|
|
7414
|
+
userTokenYAmount: effectiveAmountY,
|
|
7415
|
+
remainingAccountsInfo: baseRemainingAccountsInfo,
|
|
7416
|
+
pdaTokenType: types_1.TokenType.ATA,
|
|
7417
|
+
lowerBinId,
|
|
7418
|
+
upperBinId,
|
|
7419
|
+
});
|
|
7420
|
+
depositTxGroups.push([
|
|
7421
|
+
{ label: 'deposit_v2', class: CLASS, instruction: depositIx },
|
|
7422
|
+
]);
|
|
7423
|
+
}
|
|
7424
|
+
}
|
|
7425
|
+
else {
|
|
7426
|
+
const strategyResults = (0, liquidityStrategy_1.distributionToHybridStrategy)({
|
|
7427
|
+
hybridDistribution: layers,
|
|
7428
|
+
amountX: effectiveAmountX,
|
|
7429
|
+
amountY: effectiveAmountY,
|
|
7430
|
+
minDeltaId: new bn_js_1.default(minDeltaId),
|
|
7431
|
+
maxDeltaId: new bn_js_1.default(maxDeltaId),
|
|
7432
|
+
binStep,
|
|
7433
|
+
activeId: new bn_js_1.default(activeId),
|
|
7434
|
+
});
|
|
7435
|
+
const depositChunks = (0, liquidityStrategy_1.chunkHybridDepositParameters)(strategyResults, new bn_js_1.default(minDeltaId), new bn_js_1.default(maxDeltaId), new bn_js_1.default(activeId), binStep, maxBinsPerDepositChunk, favorXInActiveId);
|
|
7436
|
+
const chunkInstructionGroups = [];
|
|
7437
|
+
for (const chunk of depositChunks) {
|
|
7438
|
+
const { chunkLowerOffset, chunkUpperOffset } = getHybridChunkOffsets(lowerBinId, chunk.lowerBinId, chunk.upperBinId);
|
|
7439
|
+
const chunkAmountX = chunk.layers.reduce((sum, layer) => sum.add(layer.maxAmountX), new bn_js_1.default(0));
|
|
7440
|
+
const chunkAmountY = chunk.layers.reduce((sum, layer) => sum.add(layer.maxAmountY), new bn_js_1.default(0));
|
|
7441
|
+
if (chunkAmountX.isZero() && chunkAmountY.isZero()) {
|
|
7442
|
+
continue;
|
|
7443
|
+
}
|
|
7444
|
+
const depositIx = yield this.ix.meteoraDlmm.addLiquidity2Automation({
|
|
7445
|
+
connection,
|
|
7446
|
+
userWallet: params.userWallet,
|
|
7447
|
+
lbPair,
|
|
7448
|
+
position: params.position,
|
|
7449
|
+
tokenXMint,
|
|
7450
|
+
tokenYMint,
|
|
7451
|
+
tokenXProgram,
|
|
7452
|
+
tokenYProgram,
|
|
7453
|
+
layers: contractLayers,
|
|
7454
|
+
userTokenXAmount: chunkAmountX,
|
|
7455
|
+
userTokenYAmount: chunkAmountY,
|
|
7456
|
+
chunkLowerOffset,
|
|
7457
|
+
chunkUpperOffset,
|
|
7458
|
+
remainingAccountsInfo: baseRemainingAccountsInfo,
|
|
7459
|
+
pdaTokenType: types_1.TokenType.ATA,
|
|
7460
|
+
lowerBinId: chunk.lowerBinId,
|
|
7461
|
+
upperBinId: chunk.upperBinId,
|
|
7462
|
+
});
|
|
7463
|
+
chunkInstructionGroups.push([
|
|
7464
|
+
{ label: 'deposit_v2', class: CLASS, instruction: depositIx },
|
|
7465
|
+
]);
|
|
7466
|
+
}
|
|
7467
|
+
depositTxGroups.push(...batchHybridChunkInstructionGroups(chunkInstructionGroups, CHUNKS_PER_TRANSACTION));
|
|
7468
|
+
}
|
|
7469
|
+
// =========================================================================
|
|
7470
|
+
// Assemble final TX list
|
|
7471
|
+
// =========================================================================
|
|
7472
|
+
const allTxGroups = [...withdrawTxGroups, ...depositTxGroups];
|
|
7473
|
+
const transactions = [];
|
|
7474
|
+
for (const txIxs of allTxGroups) {
|
|
7475
|
+
transactions.push(yield (0, functions_1.createTransactionMeta2)({
|
|
7476
|
+
payer: params.userWallet,
|
|
7477
|
+
addressLookupTableAddresses: addresses_1.GLOBAL_ALT,
|
|
7478
|
+
mainInstructions: txIxs,
|
|
7479
|
+
}));
|
|
7480
|
+
}
|
|
7481
|
+
return transactions;
|
|
7482
|
+
}
|
|
7483
|
+
catch (error) {
|
|
7484
|
+
console.error('=== REDEPOSIT HYBRID LIQUIDITY V2 AUTOMATION ERROR ===');
|
|
7485
|
+
console.error('Error:', error instanceof Error ? error.message : String(error));
|
|
7486
|
+
console.error('Stack:', error instanceof Error ? error.stack : 'No stack trace');
|
|
7487
|
+
console.error('=== END ERROR LOG ===');
|
|
7488
|
+
throw error;
|
|
7489
|
+
}
|
|
7490
|
+
});
|
|
7491
|
+
}
|
|
6955
7492
|
/**
|
|
6956
7493
|
* Compute the bin range to use for a dust sweep deposit.
|
|
6957
7494
|
*
|
|
@@ -7019,7 +7556,10 @@ class Transactions {
|
|
|
7019
7556
|
if (userPosition === undefined) {
|
|
7020
7557
|
throw new Error(`Unexpected error: Position (${position}) doesn't exist on-chain`);
|
|
7021
7558
|
}
|
|
7022
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
7559
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
7560
|
+
tokenXMint,
|
|
7561
|
+
tokenYMint,
|
|
7562
|
+
]);
|
|
7023
7563
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
7024
7564
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
7025
7565
|
const baseRemainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
@@ -7052,8 +7592,14 @@ class Transactions {
|
|
|
7052
7592
|
const combinedAccounts = [];
|
|
7053
7593
|
const xSlice = baseRemainingAccountsInfo.slices.find(s => s.accountsType === types_1.RemainingAccountsType.TransferHookX);
|
|
7054
7594
|
const ySlice = baseRemainingAccountsInfo.slices.find(s => s.accountsType === types_1.RemainingAccountsType.TransferHookY);
|
|
7055
|
-
combinedSlices.push({
|
|
7056
|
-
|
|
7595
|
+
combinedSlices.push({
|
|
7596
|
+
accountsType: types_1.RemainingAccountsType.TransferHookX,
|
|
7597
|
+
length: (_b = xSlice === null || xSlice === void 0 ? void 0 : xSlice.length) !== null && _b !== void 0 ? _b : 0,
|
|
7598
|
+
});
|
|
7599
|
+
combinedSlices.push({
|
|
7600
|
+
accountsType: types_1.RemainingAccountsType.TransferHookY,
|
|
7601
|
+
length: (_c = ySlice === null || ySlice === void 0 ? void 0 : ySlice.length) !== null && _c !== void 0 ? _c : 0,
|
|
7602
|
+
});
|
|
7057
7603
|
combinedAccounts.push(...baseRemainingAccountsInfo.accounts);
|
|
7058
7604
|
for (const reward of rewardInfos) {
|
|
7059
7605
|
const rewardTransferHookInfo = yield (0, functions_1.fetchRemainingAccountsInfoForReward)(connection, reward.rewardMint, reward.rewardTokenProgram);
|
|
@@ -7081,12 +7627,16 @@ class Transactions {
|
|
|
7081
7627
|
const feeY = positionData.feeY;
|
|
7082
7628
|
const HAWK_FEE_BPS = 800;
|
|
7083
7629
|
const FEE_BPS_DENOM = 10000;
|
|
7084
|
-
const netFeeX = feeX
|
|
7085
|
-
|
|
7086
|
-
|
|
7630
|
+
const netFeeX = feeX
|
|
7631
|
+
.mul(new bn_js_1.default(FEE_BPS_DENOM - HAWK_FEE_BPS))
|
|
7632
|
+
.div(new bn_js_1.default(FEE_BPS_DENOM));
|
|
7633
|
+
const netFeeY = feeY
|
|
7634
|
+
.mul(new bn_js_1.default(FEE_BPS_DENOM - HAWK_FEE_BPS))
|
|
7635
|
+
.div(new bn_js_1.default(FEE_BPS_DENOM));
|
|
7636
|
+
const estimatedTotalX = compound && !skipClaim
|
|
7087
7637
|
? new bn_js_1.default(positionData.totalXAmount).add(netFeeX)
|
|
7088
7638
|
: new bn_js_1.default(positionData.totalXAmount);
|
|
7089
|
-
const estimatedTotalY =
|
|
7639
|
+
const estimatedTotalY = compound && !skipClaim
|
|
7090
7640
|
? new bn_js_1.default(positionData.totalYAmount).add(netFeeY)
|
|
7091
7641
|
: new bn_js_1.default(positionData.totalYAmount);
|
|
7092
7642
|
const _rebalanceLiquidityAutomation = (_a) => __awaiter(this, [_a], void 0, function* ({ position, maxDepositXAmount, maxDepositYAmount, pdaTokenType, shrinkMode, shouldClaimFee, shouldClaimReward, removeLiquidityParams, addLiquidityParams, maxActiveBinSlippage, }) {
|
|
@@ -7095,7 +7645,10 @@ class Transactions {
|
|
|
7095
7645
|
userWallet: userWallet,
|
|
7096
7646
|
position,
|
|
7097
7647
|
lbPair,
|
|
7098
|
-
tokenXMint,
|
|
7648
|
+
tokenXMint,
|
|
7649
|
+
tokenYMint,
|
|
7650
|
+
tokenXProgram,
|
|
7651
|
+
tokenYProgram,
|
|
7099
7652
|
activeId,
|
|
7100
7653
|
pdaTokenType,
|
|
7101
7654
|
maxActiveBinSlippage,
|
|
@@ -7127,7 +7680,9 @@ class Transactions {
|
|
|
7127
7680
|
});
|
|
7128
7681
|
});
|
|
7129
7682
|
const withdrawFn = (_a) => __awaiter(this, [_a], void 0, function* ({ position, minBinId, maxBinId, compound, origin, }) {
|
|
7130
|
-
const pdaTokenTypeForClaimables = compound
|
|
7683
|
+
const pdaTokenTypeForClaimables = compound
|
|
7684
|
+
? types_1.TokenType.ATA
|
|
7685
|
+
: types_1.TokenType.STA;
|
|
7131
7686
|
const ixs = [];
|
|
7132
7687
|
if (!skipClaim) {
|
|
7133
7688
|
// 1. Claim fees with hawk-side cut (must happen before withdraw+shrink invalidates position)
|
|
@@ -7179,7 +7734,7 @@ class Transactions {
|
|
|
7179
7734
|
maxBinId,
|
|
7180
7735
|
shouldClaimReward: false,
|
|
7181
7736
|
shouldClaimFee: false,
|
|
7182
|
-
shrinkMode: types_3.ShrinkMode.
|
|
7737
|
+
shrinkMode: types_3.ShrinkMode.NoShrinkBoth,
|
|
7183
7738
|
pdaTokenType: types_1.TokenType.ATA,
|
|
7184
7739
|
}),
|
|
7185
7740
|
});
|
|
@@ -7261,17 +7816,18 @@ class Transactions {
|
|
|
7261
7816
|
const ownerFeeReward = (0, functions_1.generateAta)(addresses_1.SITE_FEE_OWNER, pool.rewardMint);
|
|
7262
7817
|
// Mints that need ATAs on userPdaLamport (for withdrawal)
|
|
7263
7818
|
const withdrawMints = [
|
|
7264
|
-
...new Set([
|
|
7265
|
-
pool.tokenMint.toString(),
|
|
7266
|
-
pool.rewardMint.toString(),
|
|
7267
|
-
]),
|
|
7819
|
+
...new Set([pool.tokenMint.toString(), pool.rewardMint.toString()]),
|
|
7268
7820
|
].map(v => new web3.PublicKey(v));
|
|
7269
7821
|
// 1. Init ATAs for output token + reward on user PDA lamport
|
|
7270
7822
|
const initAtaIxs = withdrawMints.map(mint => {
|
|
7271
7823
|
return (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(userWallet, (0, functions_1.generateAta)(userPdaLamport, mint), userPdaLamport, mint);
|
|
7272
7824
|
});
|
|
7273
7825
|
// Init ATAs for reward-related tokens on userPda (for harvest)
|
|
7274
|
-
const initHarvestAtaIxs = [
|
|
7826
|
+
const initHarvestAtaIxs = [
|
|
7827
|
+
pool.lpMint,
|
|
7828
|
+
pool.rewardMint,
|
|
7829
|
+
addresses_1.SABER_IOU_MINT,
|
|
7830
|
+
].map(mint => {
|
|
7275
7831
|
return (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(userWallet, (0, functions_1.generateAta)(userPda, mint), userPda, mint);
|
|
7276
7832
|
});
|
|
7277
7833
|
// 2. Harvest rewards from Quarry
|