@hawksightco/hawk-sdk 1.3.224 → 1.3.226
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/Transactions.d.ts +10 -1
- package/dist/src/classes/Transactions.d.ts.map +1 -1
- package/dist/src/classes/Transactions.js +771 -230
- package/dist/src/classes/TxGenerator.d.ts +11 -9
- package/dist/src/classes/TxGenerator.d.ts.map +1 -1
- package/dist/src/classes/TxGenerator.js +152 -72
- package/dist/src/types.d.ts +34 -8
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +16 -17
- package/package.json +1 -1
|
@@ -484,7 +484,8 @@ class Transactions {
|
|
|
484
484
|
return mainInstructions;
|
|
485
485
|
}
|
|
486
486
|
catch (error) {
|
|
487
|
-
if (error instanceof Error &&
|
|
487
|
+
if (error instanceof Error &&
|
|
488
|
+
error.message.includes('No liquidity to remove')) {
|
|
488
489
|
console.warn('No liquidity to remove, closing position instead.');
|
|
489
490
|
const { userPositions } = yield dlmmPool.getPositionsByUserAndLbPair(userPda);
|
|
490
491
|
const index = userPositions.findIndex(v => v.publicKey.equals(params.position));
|
|
@@ -634,8 +635,13 @@ class Transactions {
|
|
|
634
635
|
return [result];
|
|
635
636
|
}
|
|
636
637
|
// ≥70 bins: multi-tx chunked withdraw
|
|
637
|
-
const resolved = yield this._resolveWithdrawPoolData({
|
|
638
|
-
|
|
638
|
+
const resolved = yield this._resolveWithdrawPoolData({
|
|
639
|
+
connection,
|
|
640
|
+
lbPair,
|
|
641
|
+
});
|
|
642
|
+
const amountBps = params.shouldClaimAndClose
|
|
643
|
+
? new bn_js_1.default(10000)
|
|
644
|
+
: params.amountBps;
|
|
639
645
|
// Chunk into ≤149-bin ranges
|
|
640
646
|
const CHUNK_SIZE = 149;
|
|
641
647
|
const chunks = [];
|
|
@@ -663,7 +669,7 @@ class Transactions {
|
|
|
663
669
|
* Call once, then pass the result to _buildWithdrawInstructions for each chunk.
|
|
664
670
|
*/
|
|
665
671
|
_resolveWithdrawPoolData(_a) {
|
|
666
|
-
return __awaiter(this, arguments, void 0, function* ({ connection, lbPair }) {
|
|
672
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, lbPair, }) {
|
|
667
673
|
// Create DLMM pool first — this also initializes MeteoraDLMM._program
|
|
668
674
|
// so we don't need a separate MeteoraDLMM.program() call (which fetches a dummy pool).
|
|
669
675
|
const dlmmPool = yield meteora_1.MeteoraDLMM.create(connection, lbPair, this.ix);
|
|
@@ -671,12 +677,22 @@ class Transactions {
|
|
|
671
677
|
const lbPairState = dlmmPool.dlmm.lbPair;
|
|
672
678
|
const tokenXMint = lbPairState.tokenXMint;
|
|
673
679
|
const tokenYMint = lbPairState.tokenYMint;
|
|
674
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
680
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
681
|
+
tokenXMint,
|
|
682
|
+
tokenYMint,
|
|
683
|
+
]);
|
|
675
684
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
676
685
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
677
686
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
678
687
|
const lbPairInfo = yield program.account.lbPair.fetch(lbPair);
|
|
679
|
-
return {
|
|
688
|
+
return {
|
|
689
|
+
tokenXMint,
|
|
690
|
+
tokenYMint,
|
|
691
|
+
tokenXProgram,
|
|
692
|
+
tokenYProgram,
|
|
693
|
+
remainingAccountsInfo,
|
|
694
|
+
lbPairInfo,
|
|
695
|
+
};
|
|
680
696
|
});
|
|
681
697
|
}
|
|
682
698
|
/**
|
|
@@ -689,7 +705,8 @@ class Transactions {
|
|
|
689
705
|
const mainInstructions = [];
|
|
690
706
|
let userTokenX, userTokenY;
|
|
691
707
|
const userPda = (0, functions_1.generateUserPda)(params.userWallet);
|
|
692
|
-
if (params.pdaTokenType === types_1.TokenType.ATA ||
|
|
708
|
+
if (params.pdaTokenType === types_1.TokenType.ATA ||
|
|
709
|
+
params.pdaTokenType === undefined) {
|
|
693
710
|
userTokenX = (0, functions_1.generateAta)(userPda, params.tokenXMint, params.tokenXProgram);
|
|
694
711
|
userTokenY = (0, functions_1.generateAta)(userPda, params.tokenYMint, params.tokenYProgram);
|
|
695
712
|
mainInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(params.userWallet, userTokenX, userPda, params.tokenXMint, params.tokenXProgram));
|
|
@@ -773,6 +790,12 @@ class Transactions {
|
|
|
773
790
|
hasAmount: false,
|
|
774
791
|
});
|
|
775
792
|
mainInstructions.push(withdrawFromPdaIx);
|
|
793
|
+
// 6. Unwrap wSOL to native SOL (if either mint is wSOL)
|
|
794
|
+
const unwrapIxs = (0, functions_1.unwrapSolIfMintIsWsol)(params.userWallet, [
|
|
795
|
+
params.tokenXMint,
|
|
796
|
+
params.tokenYMint,
|
|
797
|
+
]);
|
|
798
|
+
mainInstructions.push(...unwrapIxs);
|
|
776
799
|
return mainInstructions;
|
|
777
800
|
});
|
|
778
801
|
}
|
|
@@ -820,6 +843,7 @@ class Transactions {
|
|
|
820
843
|
*/
|
|
821
844
|
meteoraClaimLargePosition(_a) {
|
|
822
845
|
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
846
|
+
var _b;
|
|
823
847
|
// Determine lbPair and bin range
|
|
824
848
|
let lbPair;
|
|
825
849
|
let lowerBinId;
|
|
@@ -842,11 +866,18 @@ class Transactions {
|
|
|
842
866
|
const totalBins = upperBinId - lowerBinId + 1;
|
|
843
867
|
// ≤149 bins: single-tx path
|
|
844
868
|
if (totalBins <= 149) {
|
|
845
|
-
const result = yield this.meteoraClaim({
|
|
869
|
+
const result = yield this.meteoraClaim({
|
|
870
|
+
connection,
|
|
871
|
+
params,
|
|
872
|
+
fetch: undefined,
|
|
873
|
+
});
|
|
846
874
|
return [result];
|
|
847
875
|
}
|
|
848
876
|
// ≥150 bins: multi-tx chunked claim
|
|
849
|
-
const resolved = yield this._resolveWithdrawPoolData({
|
|
877
|
+
const resolved = yield this._resolveWithdrawPoolData({
|
|
878
|
+
connection,
|
|
879
|
+
lbPair,
|
|
880
|
+
});
|
|
850
881
|
// Chunk into ≤149-bin ranges
|
|
851
882
|
const CHUNK_SIZE = 149;
|
|
852
883
|
const chunks = [];
|
|
@@ -890,6 +921,31 @@ class Transactions {
|
|
|
890
921
|
});
|
|
891
922
|
instructions.push(claimRewardIx);
|
|
892
923
|
}
|
|
924
|
+
// Withdraw tokens from PDA back to user wallet
|
|
925
|
+
const withdrawMints = [
|
|
926
|
+
{ mint: resolved.tokenXMint, amount: new bn_js_1.default(0) },
|
|
927
|
+
{ mint: resolved.tokenYMint, amount: new bn_js_1.default(0) },
|
|
928
|
+
];
|
|
929
|
+
for (let rewardIndex = 0; rewardIndex < 2; rewardIndex++) {
|
|
930
|
+
const rewardInfo = resolved.lbPairInfo.rewardInfos[rewardIndex];
|
|
931
|
+
if (rewardInfo && !rewardInfo.mint.equals(web3.PublicKey.default)) {
|
|
932
|
+
withdrawMints.push({ mint: rewardInfo.mint, amount: new bn_js_1.default(0) });
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
const withdrawFromPdaIx = yield this.ix.iyfMain.withdrawMultipleTokenWithToken2022({
|
|
936
|
+
connection,
|
|
937
|
+
payer: params.userWallet,
|
|
938
|
+
withdraw: withdrawMints,
|
|
939
|
+
pdaTokenType: (_b = params.pdaTokenType) !== null && _b !== void 0 ? _b : types_1.TokenType.ATA,
|
|
940
|
+
hasAmount: false,
|
|
941
|
+
});
|
|
942
|
+
instructions.push(withdrawFromPdaIx);
|
|
943
|
+
// Unwrap wSOL to native SOL (if either mint is wSOL)
|
|
944
|
+
const unwrapIxs = (0, functions_1.unwrapSolIfMintIsWsol)(params.userWallet, [
|
|
945
|
+
resolved.tokenXMint,
|
|
946
|
+
resolved.tokenYMint,
|
|
947
|
+
]);
|
|
948
|
+
instructions.push(...unwrapIxs);
|
|
893
949
|
transactions.push(yield (0, functions_1.createTransactionMeta)({
|
|
894
950
|
payer: params.userWallet,
|
|
895
951
|
description: `Claim from large Meteora DLMM Position (chunk ${i + 1}/${chunks.length})`,
|
|
@@ -1091,7 +1147,7 @@ class Transactions {
|
|
|
1091
1147
|
// Re-deposit fees (TODO: How to re-deposit reward tokens that is not X or Y token?)
|
|
1092
1148
|
// Get slippage amount that returns a 10 bin slippage (meteora defaults to 3 bins if not provided)
|
|
1093
1149
|
const binStep = yield dlmmPool.dlmm.lbPair.binStep;
|
|
1094
|
-
|
|
1150
|
+
const slippageAmount = Math.round(10 * (binStep / 100) * 100) / 100; // 2 decimal places round
|
|
1095
1151
|
const addLiquidityBuilder = yield dlmmPool.addLiquidityByStrategy(connection, params.userWallet, addresses_1.HS_AUTHORITY, {
|
|
1096
1152
|
positionPubKey: params.position,
|
|
1097
1153
|
user: userPda,
|
|
@@ -1167,7 +1223,7 @@ class Transactions {
|
|
|
1167
1223
|
shouldClaimAndClose: false, // Do NOT close position
|
|
1168
1224
|
}, hsToMeteora_1.meteoraToHawksightAutomationIxs);
|
|
1169
1225
|
// Configure fee destination based on useAta
|
|
1170
|
-
if (
|
|
1226
|
+
if (params.useAta) {
|
|
1171
1227
|
removeLiquidityBuilder.replaceClaimFeeTokenToATA();
|
|
1172
1228
|
removeLiquidityBuilder.replaceClaimRewardToATA();
|
|
1173
1229
|
}
|
|
@@ -1178,7 +1234,7 @@ class Transactions {
|
|
|
1178
1234
|
// Step 2: Add liquidity back to the SAME position with specified distribution
|
|
1179
1235
|
// Get slippage amount (10 bin slippage like compound)
|
|
1180
1236
|
const binStep = dlmmPool.dlmm.lbPair.binStep;
|
|
1181
|
-
|
|
1237
|
+
const slippageAmount = Math.round(10 * (binStep / 100) * 100) / 100;
|
|
1182
1238
|
const addLiquidityBuilder = yield dlmmPool.addLiquidityByStrategy(connection, params.userWallet, addresses_1.HS_AUTHORITY, {
|
|
1183
1239
|
positionPubKey: params.position,
|
|
1184
1240
|
user: userPda,
|
|
@@ -1242,7 +1298,7 @@ class Transactions {
|
|
|
1242
1298
|
bps: new bn_js_1.default(10000),
|
|
1243
1299
|
shouldClaimAndClose: false,
|
|
1244
1300
|
}, hsToMeteora_1.meteoraToHawksightAutomationIxs);
|
|
1245
|
-
if (
|
|
1301
|
+
if (params.useAta) {
|
|
1246
1302
|
removeLiquidityBuilder.replaceClaimFeeTokenToATA();
|
|
1247
1303
|
removeLiquidityBuilder.replaceClaimRewardToATA();
|
|
1248
1304
|
}
|
|
@@ -1255,7 +1311,10 @@ class Transactions {
|
|
|
1255
1311
|
const activeBinId = lbPairState.activeId;
|
|
1256
1312
|
const tokenXMint = lbPairState.tokenXMint;
|
|
1257
1313
|
const tokenYMint = lbPairState.tokenYMint;
|
|
1258
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
1314
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
1315
|
+
tokenXMint,
|
|
1316
|
+
tokenYMint,
|
|
1317
|
+
]);
|
|
1259
1318
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
1260
1319
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
1261
1320
|
const relativeLower = position.lowerBinId - activeBinId;
|
|
@@ -1275,10 +1334,12 @@ class Transactions {
|
|
|
1275
1334
|
upperRange: relativeUpper,
|
|
1276
1335
|
},
|
|
1277
1336
|
strategyType: types_3.StrategyTypeMap[params.distribution],
|
|
1278
|
-
checkRange: params.checkRange
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1337
|
+
checkRange: params.checkRange
|
|
1338
|
+
? {
|
|
1339
|
+
lowerRange: params.checkRange.lowerRange,
|
|
1340
|
+
upperRange: params.checkRange.upperRange,
|
|
1341
|
+
}
|
|
1342
|
+
: undefined,
|
|
1282
1343
|
});
|
|
1283
1344
|
const mainInstructions = [
|
|
1284
1345
|
...removeLiquidityBuilder.createAtaIxs,
|
|
@@ -1364,13 +1425,20 @@ class Transactions {
|
|
|
1364
1425
|
const binCount = upperBinId - lowerBinId + 1;
|
|
1365
1426
|
// For ≤149 bins, delegate to existing single-TX claimAutomationIx
|
|
1366
1427
|
if (binCount <= 149) {
|
|
1367
|
-
const singleResult = yield this.claimAutomationIx({
|
|
1428
|
+
const singleResult = yield this.claimAutomationIx({
|
|
1429
|
+
connection,
|
|
1430
|
+
params,
|
|
1431
|
+
fetch: undefined,
|
|
1432
|
+
});
|
|
1368
1433
|
return [singleResult];
|
|
1369
1434
|
}
|
|
1370
1435
|
// ≥150 bins: chunked multi-TX approach
|
|
1371
1436
|
// Resolve pool data (tokens, programs, remaining accounts)
|
|
1372
|
-
const resolved = yield this._resolveWithdrawPoolData({
|
|
1373
|
-
|
|
1437
|
+
const resolved = yield this._resolveWithdrawPoolData({
|
|
1438
|
+
connection,
|
|
1439
|
+
lbPair,
|
|
1440
|
+
});
|
|
1441
|
+
const { tokenXMint, tokenYMint, tokenXProgram, tokenYProgram, remainingAccountsInfo, lbPairInfo, } = resolved;
|
|
1374
1442
|
// Get reward infos from lbPair state
|
|
1375
1443
|
const rewardInfos = lbPairInfo.rewardInfos;
|
|
1376
1444
|
// Chunk [lowerBinId, upperBinId] into ≤149-bin pieces
|
|
@@ -1479,7 +1547,11 @@ class Transactions {
|
|
|
1479
1547
|
const binCount = upperBinId - lowerBinId + 1;
|
|
1480
1548
|
// For ≤70 bins, delegate to existing single-TX compoundAutomationIx
|
|
1481
1549
|
if (binCount <= 70) {
|
|
1482
|
-
const singleResult = yield this.compoundAutomationIx({
|
|
1550
|
+
const singleResult = yield this.compoundAutomationIx({
|
|
1551
|
+
connection,
|
|
1552
|
+
params,
|
|
1553
|
+
fetch: undefined,
|
|
1554
|
+
});
|
|
1483
1555
|
return [singleResult];
|
|
1484
1556
|
}
|
|
1485
1557
|
// ≥150 bins: chunked multi-TX approach
|
|
@@ -1499,11 +1571,14 @@ class Transactions {
|
|
|
1499
1571
|
const tokenXMint = lbPairState.tokenXMint;
|
|
1500
1572
|
const tokenYMint = lbPairState.tokenYMint;
|
|
1501
1573
|
const rewardInfos = lbPairState.rewardInfos;
|
|
1502
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
1574
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
1575
|
+
tokenXMint,
|
|
1576
|
+
tokenYMint,
|
|
1577
|
+
]);
|
|
1503
1578
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
1504
1579
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
1505
1580
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
1506
|
-
const lbPairInfo = yield
|
|
1581
|
+
const lbPairInfo = yield program.account.lbPair.fetch(lbPair);
|
|
1507
1582
|
// =========================================================================
|
|
1508
1583
|
// Phase 1: Chunked claim (to ATA for re-deposit)
|
|
1509
1584
|
// =========================================================================
|
|
@@ -1573,9 +1648,18 @@ class Transactions {
|
|
|
1573
1648
|
// Phase 2: Chunked re-deposit back into the same position
|
|
1574
1649
|
// =========================================================================
|
|
1575
1650
|
const distributionToStrategyType = {
|
|
1576
|
-
|
|
1577
|
-
'
|
|
1578
|
-
'
|
|
1651
|
+
SPOT: liquidityStrategy_1.StrategyType.SPOT,
|
|
1652
|
+
'SPOT-IMBALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
1653
|
+
'SPOT-ONE-SIDE': liquidityStrategy_1.StrategyType.SPOT,
|
|
1654
|
+
'SPOT-BALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
1655
|
+
CURVE: liquidityStrategy_1.StrategyType.CURVE,
|
|
1656
|
+
'CURVE-IMBALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
1657
|
+
'CURVE-ONE-SIDE': liquidityStrategy_1.StrategyType.CURVE,
|
|
1658
|
+
'CURVE-BALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
1659
|
+
'BID-ASK': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
1660
|
+
'BID-ASK-IMBALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
1661
|
+
'BID-ASK-ONE-SIDE': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
1662
|
+
'BID-ASK-BALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
1579
1663
|
};
|
|
1580
1664
|
const strategyType = (_c = distributionToStrategyType[(_b = params.distribution) !== null && _b !== void 0 ? _b : 'SPOT-IMBALANCED']) !== null && _c !== void 0 ? _c : liquidityStrategy_1.StrategyType.SPOT;
|
|
1581
1665
|
const { binId: activeId } = yield dlmmPool.dlmm.getActiveBin();
|
|
@@ -1646,7 +1730,7 @@ class Transactions {
|
|
|
1646
1730
|
bps: new bn_js_1.default(10000),
|
|
1647
1731
|
shouldClaimAndClose: true,
|
|
1648
1732
|
}, hsToMeteora_1.meteoraToHawksightAutomationIxs);
|
|
1649
|
-
if (
|
|
1733
|
+
if (params.useAta) {
|
|
1650
1734
|
removeLiquidityBuilder.replaceClaimFeeTokenToATA();
|
|
1651
1735
|
removeLiquidityBuilder.replaceClaimRewardToATA();
|
|
1652
1736
|
}
|
|
@@ -1724,7 +1808,7 @@ class Transactions {
|
|
|
1724
1808
|
bps: new bn_js_1.default(10000),
|
|
1725
1809
|
shouldClaimAndClose: true,
|
|
1726
1810
|
}, hsToMeteora_1.meteoraToHawksightAutomationIxs);
|
|
1727
|
-
if (
|
|
1811
|
+
if (params.useAta) {
|
|
1728
1812
|
removeLiquidityBuilder.replaceClaimFeeTokenToATA();
|
|
1729
1813
|
removeLiquidityBuilder.replaceClaimRewardToATA();
|
|
1730
1814
|
}
|
|
@@ -1793,7 +1877,13 @@ class Transactions {
|
|
|
1793
1877
|
// --- Validate and normalize layers ---
|
|
1794
1878
|
let layers;
|
|
1795
1879
|
if (params.distribution && !params.layers) {
|
|
1796
|
-
layers = [
|
|
1880
|
+
layers = [
|
|
1881
|
+
{
|
|
1882
|
+
distribution: params.distribution,
|
|
1883
|
+
percentageX: 100,
|
|
1884
|
+
percentageY: 100,
|
|
1885
|
+
},
|
|
1886
|
+
];
|
|
1797
1887
|
}
|
|
1798
1888
|
else if (params.layers && !params.distribution) {
|
|
1799
1889
|
layers = params.layers;
|
|
@@ -1827,11 +1917,16 @@ class Transactions {
|
|
|
1827
1917
|
const tokenXMint = lbPairState.tokenXMint;
|
|
1828
1918
|
const tokenYMint = lbPairState.tokenYMint;
|
|
1829
1919
|
const rewardInfos = lbPairState.rewardInfos;
|
|
1830
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
1920
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
1921
|
+
tokenXMint,
|
|
1922
|
+
tokenYMint,
|
|
1923
|
+
]);
|
|
1831
1924
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
1832
1925
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
1833
1926
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
1834
|
-
const pdaTokenTypeForClaimables = params.useAta
|
|
1927
|
+
const pdaTokenTypeForClaimables = params.useAta
|
|
1928
|
+
? types_1.TokenType.ATA
|
|
1929
|
+
: types_1.TokenType.STA;
|
|
1835
1930
|
// Estimate total X/Y from position data (liquidity + fees)
|
|
1836
1931
|
const estimatedTotalX = new bn_js_1.default(userPosition.positionData.totalXAmount).add(userPosition.positionData.feeX);
|
|
1837
1932
|
const estimatedTotalY = new bn_js_1.default(userPosition.positionData.totalYAmount).add(userPosition.positionData.feeY);
|
|
@@ -1842,7 +1937,10 @@ class Transactions {
|
|
|
1842
1937
|
userWallet: params.userWallet,
|
|
1843
1938
|
lbPair,
|
|
1844
1939
|
position: params.currentPosition,
|
|
1845
|
-
tokenXMint,
|
|
1940
|
+
tokenXMint,
|
|
1941
|
+
tokenYMint,
|
|
1942
|
+
tokenXProgram,
|
|
1943
|
+
tokenYProgram,
|
|
1846
1944
|
fromBinId: currentLowerBinId,
|
|
1847
1945
|
toBinId: currentUpperBinId,
|
|
1848
1946
|
bpsToRemove: 10000,
|
|
@@ -1855,9 +1953,12 @@ class Transactions {
|
|
|
1855
1953
|
userWallet: params.userWallet,
|
|
1856
1954
|
lbPair,
|
|
1857
1955
|
position: params.currentPosition,
|
|
1858
|
-
tokenMintX: tokenXMint,
|
|
1859
|
-
|
|
1860
|
-
|
|
1956
|
+
tokenMintX: tokenXMint,
|
|
1957
|
+
tokenMintY: tokenYMint,
|
|
1958
|
+
tokenProgramX: tokenXProgram,
|
|
1959
|
+
tokenProgramY: tokenYProgram,
|
|
1960
|
+
lowerBinId: currentLowerBinId,
|
|
1961
|
+
upperBinId: currentUpperBinId,
|
|
1861
1962
|
pdaTokenType: pdaTokenTypeForClaimables,
|
|
1862
1963
|
remainingAccountsInfo,
|
|
1863
1964
|
});
|
|
@@ -1878,7 +1979,8 @@ class Transactions {
|
|
|
1878
1979
|
rewardMint: rewardInfo.mint,
|
|
1879
1980
|
rewardVault: rewardInfo.vault,
|
|
1880
1981
|
tokenProgram: rewardTokenProgram,
|
|
1881
|
-
lowerBinId: currentLowerBinId,
|
|
1982
|
+
lowerBinId: currentLowerBinId,
|
|
1983
|
+
upperBinId: currentUpperBinId,
|
|
1882
1984
|
pdaTokenType: types_1.TokenType.STA,
|
|
1883
1985
|
remainingAccountsInfo: rewardRemainingAccountsInfo,
|
|
1884
1986
|
});
|
|
@@ -1913,7 +2015,9 @@ class Transactions {
|
|
|
1913
2015
|
optionalTokenXAmount = undefined; // sweep remaining
|
|
1914
2016
|
}
|
|
1915
2017
|
else {
|
|
1916
|
-
optionalTokenXAmount = estimatedTotalX
|
|
2018
|
+
optionalTokenXAmount = estimatedTotalX
|
|
2019
|
+
.mul(new bn_js_1.default(layer.percentageX))
|
|
2020
|
+
.div(new bn_js_1.default(100));
|
|
1917
2021
|
}
|
|
1918
2022
|
if (layer.percentageY === 0) {
|
|
1919
2023
|
optionalTokenYAmount = new bn_js_1.default(0);
|
|
@@ -1922,7 +2026,9 @@ class Transactions {
|
|
|
1922
2026
|
optionalTokenYAmount = undefined; // sweep remaining
|
|
1923
2027
|
}
|
|
1924
2028
|
else {
|
|
1925
|
-
optionalTokenYAmount = estimatedTotalY
|
|
2029
|
+
optionalTokenYAmount = estimatedTotalY
|
|
2030
|
+
.mul(new bn_js_1.default(layer.percentageY))
|
|
2031
|
+
.div(new bn_js_1.default(100));
|
|
1926
2032
|
}
|
|
1927
2033
|
return { optionalTokenXAmount, optionalTokenYAmount };
|
|
1928
2034
|
};
|
|
@@ -1937,7 +2043,10 @@ class Transactions {
|
|
|
1937
2043
|
relativeLowerBinId: params.relativeBinRange.lowerRange,
|
|
1938
2044
|
relativeUpperBinId: params.relativeBinRange.upperRange,
|
|
1939
2045
|
strategyType: firstStrategyType,
|
|
1940
|
-
checkRange: {
|
|
2046
|
+
checkRange: {
|
|
2047
|
+
minBinId: params.checkRange.lowerRange,
|
|
2048
|
+
maxBinId: params.checkRange.upperRange,
|
|
2049
|
+
},
|
|
1941
2050
|
targetActiveBin: (_b = params.targetActiveBin) !== null && _b !== void 0 ? _b : undefined,
|
|
1942
2051
|
userTokenXAmount: firstAmounts.optionalTokenXAmount,
|
|
1943
2052
|
userTokenYAmount: firstAmounts.optionalTokenYAmount,
|
|
@@ -1958,7 +2067,10 @@ class Transactions {
|
|
|
1958
2067
|
userWallet: params.userWallet,
|
|
1959
2068
|
lbPair,
|
|
1960
2069
|
position: params.newPosition,
|
|
1961
|
-
tokenXMint,
|
|
2070
|
+
tokenXMint,
|
|
2071
|
+
tokenYMint,
|
|
2072
|
+
tokenXProgram,
|
|
2073
|
+
tokenYProgram,
|
|
1962
2074
|
activeId: effectiveActiveId,
|
|
1963
2075
|
maxActiveBinSlippage: 0,
|
|
1964
2076
|
strategyParametersMinBinId: absLower,
|
|
@@ -2014,7 +2126,10 @@ class Transactions {
|
|
|
2014
2126
|
const tokenYMint = lbPairState.tokenYMint;
|
|
2015
2127
|
const rewardInfos = lbPairState.rewardInfos;
|
|
2016
2128
|
// Get token programs for each mint
|
|
2017
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2129
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2130
|
+
tokenXMint,
|
|
2131
|
+
tokenYMint,
|
|
2132
|
+
]);
|
|
2018
2133
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
2019
2134
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
2020
2135
|
// Get position width
|
|
@@ -2026,7 +2141,9 @@ class Transactions {
|
|
|
2026
2141
|
// Fetch remainingAccountsInfo for Token2022 transfer hooks (if any)
|
|
2027
2142
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
2028
2143
|
// Whether to compound during rebalance, or nope.
|
|
2029
|
-
const pdaTokenTypeForClaimables = params.useAta
|
|
2144
|
+
const pdaTokenTypeForClaimables = params.useAta
|
|
2145
|
+
? types_1.TokenType.ATA
|
|
2146
|
+
: types_1.TokenType.STA;
|
|
2030
2147
|
const transactions = [];
|
|
2031
2148
|
// =========================================================================
|
|
2032
2149
|
// Group 1 consists of remove liquidity, claim fees/rewards, close position,
|
|
@@ -2262,7 +2379,10 @@ class Transactions {
|
|
|
2262
2379
|
const currentUpperBinId = userPosition.positionData.upperBinId;
|
|
2263
2380
|
const tokenXMint = lbPairState.tokenXMint;
|
|
2264
2381
|
const tokenYMint = lbPairState.tokenYMint;
|
|
2265
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2382
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2383
|
+
tokenXMint,
|
|
2384
|
+
tokenYMint,
|
|
2385
|
+
]);
|
|
2266
2386
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
2267
2387
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
2268
2388
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
@@ -2292,9 +2412,18 @@ class Transactions {
|
|
|
2292
2412
|
totalYAmount: acc.totalYAmount.add(new bn_js_1.default(bin.positionYAmount)),
|
|
2293
2413
|
}), { totalXAmount: new bn_js_1.default(0), totalYAmount: new bn_js_1.default(0) });
|
|
2294
2414
|
const distributionToStrategyType = {
|
|
2295
|
-
|
|
2296
|
-
'
|
|
2297
|
-
'
|
|
2415
|
+
SPOT: liquidityStrategy_1.StrategyType.SPOT,
|
|
2416
|
+
'SPOT-IMBALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
2417
|
+
'SPOT-ONE-SIDE': liquidityStrategy_1.StrategyType.SPOT,
|
|
2418
|
+
'SPOT-BALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
2419
|
+
CURVE: liquidityStrategy_1.StrategyType.CURVE,
|
|
2420
|
+
'CURVE-IMBALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
2421
|
+
'CURVE-ONE-SIDE': liquidityStrategy_1.StrategyType.CURVE,
|
|
2422
|
+
'CURVE-BALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
2423
|
+
'BID-ASK': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2424
|
+
'BID-ASK-IMBALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2425
|
+
'BID-ASK-ONE-SIDE': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2426
|
+
'BID-ASK-BALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2298
2427
|
};
|
|
2299
2428
|
const strategyType = (_b = distributionToStrategyType[params.distribution]) !== null && _b !== void 0 ? _b : liquidityStrategy_1.StrategyType.SPOT;
|
|
2300
2429
|
// Deposit using chunked rebalanceLiquidityAutomation (handles >70 bins)
|
|
@@ -2387,16 +2516,30 @@ class Transactions {
|
|
|
2387
2516
|
const tokenXMint = lbPairState.tokenXMint;
|
|
2388
2517
|
const tokenYMint = lbPairState.tokenYMint;
|
|
2389
2518
|
const rewardInfos = lbPairState.rewardInfos;
|
|
2390
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2519
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2520
|
+
tokenXMint,
|
|
2521
|
+
tokenYMint,
|
|
2522
|
+
]);
|
|
2391
2523
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
2392
2524
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
2393
2525
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
2394
|
-
const pdaTokenTypeForClaimables = params.useAta
|
|
2526
|
+
const pdaTokenTypeForClaimables = params.useAta
|
|
2527
|
+
? types_1.TokenType.ATA
|
|
2528
|
+
: types_1.TokenType.STA;
|
|
2395
2529
|
const { binId: activeId } = yield dlmmPool.dlmm.getActiveBin();
|
|
2396
2530
|
const distributionToStrategyType = {
|
|
2397
|
-
|
|
2398
|
-
'
|
|
2399
|
-
'
|
|
2531
|
+
SPOT: liquidityStrategy_1.StrategyType.SPOT,
|
|
2532
|
+
'SPOT-IMBALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
2533
|
+
'SPOT-ONE-SIDE': liquidityStrategy_1.StrategyType.SPOT,
|
|
2534
|
+
'SPOT-BALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
2535
|
+
CURVE: liquidityStrategy_1.StrategyType.CURVE,
|
|
2536
|
+
'CURVE-IMBALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
2537
|
+
'CURVE-ONE-SIDE': liquidityStrategy_1.StrategyType.CURVE,
|
|
2538
|
+
'CURVE-BALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
2539
|
+
'BID-ASK': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2540
|
+
'BID-ASK-IMBALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2541
|
+
'BID-ASK-ONE-SIDE': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2542
|
+
'BID-ASK-BALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2400
2543
|
};
|
|
2401
2544
|
const strategyType = (_b = distributionToStrategyType[params.distribution]) !== null && _b !== void 0 ? _b : liquidityStrategy_1.StrategyType.SPOT;
|
|
2402
2545
|
// =========================================================================
|
|
@@ -2408,7 +2551,10 @@ class Transactions {
|
|
|
2408
2551
|
userWallet: params.userWallet,
|
|
2409
2552
|
lbPair,
|
|
2410
2553
|
position: params.currentPosition,
|
|
2411
|
-
tokenXMint,
|
|
2554
|
+
tokenXMint,
|
|
2555
|
+
tokenYMint,
|
|
2556
|
+
tokenXProgram,
|
|
2557
|
+
tokenYProgram,
|
|
2412
2558
|
fromBinId: currentLowerBinId,
|
|
2413
2559
|
toBinId: currentUpperBinId,
|
|
2414
2560
|
bpsToRemove: 10000,
|
|
@@ -2420,9 +2566,12 @@ class Transactions {
|
|
|
2420
2566
|
userWallet: params.userWallet,
|
|
2421
2567
|
lbPair,
|
|
2422
2568
|
position: params.currentPosition,
|
|
2423
|
-
tokenMintX: tokenXMint,
|
|
2424
|
-
|
|
2425
|
-
|
|
2569
|
+
tokenMintX: tokenXMint,
|
|
2570
|
+
tokenMintY: tokenYMint,
|
|
2571
|
+
tokenProgramX: tokenXProgram,
|
|
2572
|
+
tokenProgramY: tokenYProgram,
|
|
2573
|
+
lowerBinId: currentLowerBinId,
|
|
2574
|
+
upperBinId: currentUpperBinId,
|
|
2426
2575
|
pdaTokenType: pdaTokenTypeForClaimables,
|
|
2427
2576
|
remainingAccountsInfo,
|
|
2428
2577
|
});
|
|
@@ -2442,7 +2591,8 @@ class Transactions {
|
|
|
2442
2591
|
rewardMint: rewardInfo.mint,
|
|
2443
2592
|
rewardVault: rewardInfo.vault,
|
|
2444
2593
|
tokenProgram: rewardTokenProgram,
|
|
2445
|
-
lowerBinId: currentLowerBinId,
|
|
2594
|
+
lowerBinId: currentLowerBinId,
|
|
2595
|
+
upperBinId: currentUpperBinId,
|
|
2446
2596
|
pdaTokenType: types_1.TokenType.STA,
|
|
2447
2597
|
remainingAccountsInfo: rewardRemainingAccountsInfo,
|
|
2448
2598
|
});
|
|
@@ -2503,7 +2653,10 @@ class Transactions {
|
|
|
2503
2653
|
userWallet: params.userWallet,
|
|
2504
2654
|
lbPair,
|
|
2505
2655
|
position: params.newPosition,
|
|
2506
|
-
tokenXMint,
|
|
2656
|
+
tokenXMint,
|
|
2657
|
+
tokenYMint,
|
|
2658
|
+
tokenXProgram,
|
|
2659
|
+
tokenYProgram,
|
|
2507
2660
|
lowerBinId: newLowerBinId,
|
|
2508
2661
|
upperBinId: newUpperBinId,
|
|
2509
2662
|
strategyType,
|
|
@@ -2574,13 +2727,21 @@ class Transactions {
|
|
|
2574
2727
|
const currentBinCount = currentUpperBinId - currentLowerBinId + 1;
|
|
2575
2728
|
// For ≤70 bins, delegate to existing rebalanceSmallPositionAutomation
|
|
2576
2729
|
if (currentBinCount <= 70 && params.width <= 70) {
|
|
2577
|
-
return this.rebalanceSmallPositionAutomation({
|
|
2730
|
+
return this.rebalanceSmallPositionAutomation({
|
|
2731
|
+
connection,
|
|
2732
|
+
params,
|
|
2733
|
+
fetch: undefined,
|
|
2734
|
+
});
|
|
2578
2735
|
}
|
|
2579
2736
|
// For ≤149 target bins, use phased approach (separate withdraw/deposit TXs).
|
|
2580
2737
|
// Only the target width is constrained (deposit must fit in one 149-bin chunk).
|
|
2581
2738
|
// The withdraw TX handles any current position size via full-range removeLiquidity.
|
|
2582
2739
|
if (params.width <= 149) {
|
|
2583
|
-
return this.rebalancePhasedPositionAutomation({
|
|
2740
|
+
return this.rebalancePhasedPositionAutomation({
|
|
2741
|
+
connection,
|
|
2742
|
+
params,
|
|
2743
|
+
fetch: undefined,
|
|
2744
|
+
});
|
|
2584
2745
|
}
|
|
2585
2746
|
// ≥150 bins: multi-TX chunked approach
|
|
2586
2747
|
const dlmmPool = yield meteora_1.MeteoraDLMM.create(connection, lbPair, this.ix);
|
|
@@ -2600,21 +2761,35 @@ class Transactions {
|
|
|
2600
2761
|
const tokenXMint = lbPairState.tokenXMint;
|
|
2601
2762
|
const tokenYMint = lbPairState.tokenYMint;
|
|
2602
2763
|
const rewardInfos = lbPairState.rewardInfos;
|
|
2603
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2764
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2765
|
+
tokenXMint,
|
|
2766
|
+
tokenYMint,
|
|
2767
|
+
]);
|
|
2604
2768
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
2605
2769
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
2606
2770
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
2607
|
-
const lbPairInfo = yield
|
|
2771
|
+
const lbPairInfo = yield program.account.lbPair.fetch(lbPair);
|
|
2608
2772
|
// Whether to compound during rebalance, or nope.
|
|
2609
|
-
const pdaTokenTypeForClaimables = params.useAta
|
|
2773
|
+
const pdaTokenTypeForClaimables = params.useAta
|
|
2774
|
+
? types_1.TokenType.ATA
|
|
2775
|
+
: types_1.TokenType.STA;
|
|
2610
2776
|
// =========================================================================
|
|
2611
2777
|
// Merged approach: withdraw + init/extend + deposit per TX (~74-bins each)
|
|
2612
2778
|
// =========================================================================
|
|
2613
2779
|
const { binId: activeId } = yield dlmmPool.dlmm.getActiveBin();
|
|
2614
2780
|
const distributionToStrategyType = {
|
|
2615
|
-
|
|
2616
|
-
'
|
|
2617
|
-
'
|
|
2781
|
+
SPOT: liquidityStrategy_1.StrategyType.SPOT,
|
|
2782
|
+
'SPOT-IMBALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
2783
|
+
'SPOT-ONE-SIDE': liquidityStrategy_1.StrategyType.SPOT,
|
|
2784
|
+
'SPOT-BALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
2785
|
+
CURVE: liquidityStrategy_1.StrategyType.CURVE,
|
|
2786
|
+
'CURVE-IMBALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
2787
|
+
'CURVE-ONE-SIDE': liquidityStrategy_1.StrategyType.CURVE,
|
|
2788
|
+
'CURVE-BALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
2789
|
+
'BID-ASK': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2790
|
+
'BID-ASK-IMBALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2791
|
+
'BID-ASK-ONE-SIDE': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2792
|
+
'BID-ASK-BALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
2618
2793
|
};
|
|
2619
2794
|
const strategyType = (_b = distributionToStrategyType[params.distribution]) !== null && _b !== void 0 ? _b : liquidityStrategy_1.StrategyType.SPOT;
|
|
2620
2795
|
const binStep = new bn_js_1.default(dlmmPool.dlmm.lbPair.binStep);
|
|
@@ -2641,7 +2816,7 @@ class Transactions {
|
|
|
2641
2816
|
activeId,
|
|
2642
2817
|
totalXAmount,
|
|
2643
2818
|
totalYAmount,
|
|
2644
|
-
chunkSize: 73
|
|
2819
|
+
chunkSize: 73,
|
|
2645
2820
|
});
|
|
2646
2821
|
// Wrap each instruction array with createTransactionMeta
|
|
2647
2822
|
const transactions = [];
|
|
@@ -2698,7 +2873,10 @@ class Transactions {
|
|
|
2698
2873
|
const lbPairState = dlmmPool.dlmm.lbPair;
|
|
2699
2874
|
const tokenXMint = lbPairState.tokenXMint;
|
|
2700
2875
|
const tokenYMint = lbPairState.tokenYMint;
|
|
2701
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2876
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
2877
|
+
tokenXMint,
|
|
2878
|
+
tokenYMint,
|
|
2879
|
+
]);
|
|
2702
2880
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
2703
2881
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
2704
2882
|
// Fetch transfer hook accounts for token X and Y (Token2022 support)
|
|
@@ -2711,8 +2889,14 @@ class Transactions {
|
|
|
2711
2889
|
// Always add TransferHookX and TransferHookY slices (even if length=0)
|
|
2712
2890
|
const xSlice = baseRemainingAccountsInfo.slices.find(s => s.accountsType === types_1.RemainingAccountsType.TransferHookX);
|
|
2713
2891
|
const ySlice = baseRemainingAccountsInfo.slices.find(s => s.accountsType === types_1.RemainingAccountsType.TransferHookY);
|
|
2714
|
-
combinedSlices.push({
|
|
2715
|
-
|
|
2892
|
+
combinedSlices.push({
|
|
2893
|
+
accountsType: types_1.RemainingAccountsType.TransferHookX,
|
|
2894
|
+
length: (_b = xSlice === null || xSlice === void 0 ? void 0 : xSlice.length) !== null && _b !== void 0 ? _b : 0,
|
|
2895
|
+
});
|
|
2896
|
+
combinedSlices.push({
|
|
2897
|
+
accountsType: types_1.RemainingAccountsType.TransferHookY,
|
|
2898
|
+
length: (_c = ySlice === null || ySlice === void 0 ? void 0 : ySlice.length) !== null && _c !== void 0 ? _c : 0,
|
|
2899
|
+
});
|
|
2716
2900
|
// Add transfer hook accounts from base (for X and Y)
|
|
2717
2901
|
combinedAccounts.push(...baseRemainingAccountsInfo.accounts);
|
|
2718
2902
|
const remainingAccountsInfo = {
|
|
@@ -2720,12 +2904,16 @@ class Transactions {
|
|
|
2720
2904
|
accounts: combinedAccounts,
|
|
2721
2905
|
};
|
|
2722
2906
|
return {
|
|
2723
|
-
instructions: [
|
|
2907
|
+
instructions: [
|
|
2908
|
+
yield this.ix.meteoraDlmm.rebalanceLiquidityAutomation({
|
|
2724
2909
|
connection,
|
|
2725
2910
|
userWallet: params.userWallet,
|
|
2726
2911
|
position: params.position,
|
|
2727
2912
|
lbPair: params.lbPair,
|
|
2728
|
-
tokenXMint,
|
|
2913
|
+
tokenXMint,
|
|
2914
|
+
tokenYMint,
|
|
2915
|
+
tokenXProgram,
|
|
2916
|
+
tokenYProgram,
|
|
2729
2917
|
activeId: params.activeId,
|
|
2730
2918
|
pdaTokenType: types_1.TokenType.ATA,
|
|
2731
2919
|
maxActiveBinSlippage: 0,
|
|
@@ -2739,7 +2927,8 @@ class Transactions {
|
|
|
2739
2927
|
addLiquidityParams: params.addLiquidityParams,
|
|
2740
2928
|
shrinkMode: types_3.ShrinkMode.NoShrinkBoth,
|
|
2741
2929
|
remainingAccountsInfo,
|
|
2742
|
-
})
|
|
2930
|
+
}),
|
|
2931
|
+
],
|
|
2743
2932
|
labels: ['rebalance_liquidity'],
|
|
2744
2933
|
};
|
|
2745
2934
|
});
|
|
@@ -2906,7 +3095,7 @@ class Transactions {
|
|
|
2906
3095
|
// Add liquidity via rebalanceLiquidityAutomation
|
|
2907
3096
|
// Use u64::MAX for maxDeposit amounts so they don't cap the deposit.
|
|
2908
3097
|
// The on-chain program uses ATA balance for actual amounts; these are just safety ceilings.
|
|
2909
|
-
const U64_MAX = new bn_js_1.default(
|
|
3098
|
+
const U64_MAX = new bn_js_1.default('18446744073709551615');
|
|
2910
3099
|
const depositIx = yield this.ix.meteoraDlmm.rebalanceLiquidityAutomation({
|
|
2911
3100
|
connection,
|
|
2912
3101
|
userWallet,
|
|
@@ -2926,7 +3115,8 @@ class Transactions {
|
|
|
2926
3115
|
minWithdrawYAmount: new bn_js_1.default(0),
|
|
2927
3116
|
maxDepositYAmount: U64_MAX,
|
|
2928
3117
|
removeLiquidityParams: [],
|
|
2929
|
-
addLiquidityParams: [
|
|
3118
|
+
addLiquidityParams: [
|
|
3119
|
+
{
|
|
2930
3120
|
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
2931
3121
|
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
2932
3122
|
x0: onChainParams.x0,
|
|
@@ -2935,7 +3125,8 @@ class Transactions {
|
|
|
2935
3125
|
deltaY: onChainParams.deltaY,
|
|
2936
3126
|
bitFlag: onChainParams.bitFlag,
|
|
2937
3127
|
favorXInActiveId,
|
|
2938
|
-
}
|
|
3128
|
+
},
|
|
3129
|
+
],
|
|
2939
3130
|
});
|
|
2940
3131
|
instructions.push(depositIx);
|
|
2941
3132
|
txInstructions.push(instructions);
|
|
@@ -3036,7 +3227,7 @@ class Transactions {
|
|
|
3036
3227
|
// === Withdraw from old position ===
|
|
3037
3228
|
if (i < oldChunks.length) {
|
|
3038
3229
|
const chunk = oldChunks[i];
|
|
3039
|
-
const isLastOldChunk =
|
|
3230
|
+
const isLastOldChunk = i === oldChunks.length - 1;
|
|
3040
3231
|
const removeLiquidityIx = yield this.ix.meteoraDlmm.removeLiquidityByRange2Automation({
|
|
3041
3232
|
connection,
|
|
3042
3233
|
userWallet,
|
|
@@ -3125,7 +3316,8 @@ class Transactions {
|
|
|
3125
3316
|
minWithdrawYAmount: new bn_js_1.default(0),
|
|
3126
3317
|
maxDepositYAmount: chunk.maxAmountY,
|
|
3127
3318
|
removeLiquidityParams: [],
|
|
3128
|
-
addLiquidityParams: [
|
|
3319
|
+
addLiquidityParams: [
|
|
3320
|
+
{
|
|
3129
3321
|
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
3130
3322
|
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
3131
3323
|
x0: onChainParams.x0,
|
|
@@ -3134,7 +3326,8 @@ class Transactions {
|
|
|
3134
3326
|
deltaY: onChainParams.deltaY,
|
|
3135
3327
|
bitFlag: onChainParams.bitFlag,
|
|
3136
3328
|
favorXInActiveId,
|
|
3137
|
-
}
|
|
3329
|
+
},
|
|
3330
|
+
],
|
|
3138
3331
|
});
|
|
3139
3332
|
instructions.push(depositIx);
|
|
3140
3333
|
}
|
|
@@ -3180,7 +3373,7 @@ class Transactions {
|
|
|
3180
3373
|
}
|
|
3181
3374
|
relativeOpenAutomationIx(_a) {
|
|
3182
3375
|
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
3183
|
-
const { userWallet, pool, position, relativeBinRange, checkRange, distribution, targetActiveBin, fuelAccount, pdaTokenType, userTokenXAmount, userTokenYAmount } = params;
|
|
3376
|
+
const { userWallet, pool, position, relativeBinRange, checkRange, distribution, targetActiveBin, fuelAccount, pdaTokenType, userTokenXAmount, userTokenYAmount, } = params;
|
|
3184
3377
|
const openParams = {
|
|
3185
3378
|
userWallet,
|
|
3186
3379
|
lbPair: pool,
|
|
@@ -3277,8 +3470,7 @@ class Transactions {
|
|
|
3277
3470
|
catch (error) {
|
|
3278
3471
|
throw new Error(`Failed to generate bin IDs for position (lowerBinId: ${position.lowerBinId}, upperBinId: ${position.upperBinId}): ${error}`);
|
|
3279
3472
|
}
|
|
3280
|
-
const totalLiquidity = position.liquidityShares
|
|
3281
|
-
.reduce((acc, bn) => acc.add(bn), new bn_js_1.default(0));
|
|
3473
|
+
const totalLiquidity = position.liquidityShares.reduce((acc, bn) => acc.add(bn), new bn_js_1.default(0));
|
|
3282
3474
|
let mainInstructions;
|
|
3283
3475
|
if (totalLiquidity.gt(new bn_js_1.default(0))) {
|
|
3284
3476
|
let removeLiquidityBuilder;
|
|
@@ -3298,9 +3490,7 @@ class Transactions {
|
|
|
3298
3490
|
throw new Error(`Failed to generate remove liquidity from position tx ${params.position}: ${error}`);
|
|
3299
3491
|
}
|
|
3300
3492
|
try {
|
|
3301
|
-
mainInstructions = [
|
|
3302
|
-
...removeLiquidityBuilder.mainIxs,
|
|
3303
|
-
];
|
|
3493
|
+
mainInstructions = [...removeLiquidityBuilder.mainIxs];
|
|
3304
3494
|
}
|
|
3305
3495
|
catch (error) {
|
|
3306
3496
|
throw new Error(`Failed to build main instructions: ${error}`);
|
|
@@ -3311,7 +3501,10 @@ class Transactions {
|
|
|
3311
3501
|
const tokenXMint = lbPairState.tokenXMint;
|
|
3312
3502
|
const tokenYMint = lbPairState.tokenYMint;
|
|
3313
3503
|
const rewardInfos = lbPairState.rewardInfos;
|
|
3314
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
3504
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
3505
|
+
tokenXMint,
|
|
3506
|
+
tokenYMint,
|
|
3507
|
+
]);
|
|
3315
3508
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
3316
3509
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
3317
3510
|
const remainingAccountsInfo = yield (0, functions_1.fetchRemainingAccountsInfo)(connection, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram);
|
|
@@ -3401,7 +3594,7 @@ class Transactions {
|
|
|
3401
3594
|
const iyfProgram = addresses_1.IYF_MAIN;
|
|
3402
3595
|
const hawksightAuthority = addresses_1.HS_AUTHORITY;
|
|
3403
3596
|
const { params: _params, data: chunkData } = this.removeLiquidityByRange2Info(ixs);
|
|
3404
|
-
|
|
3597
|
+
const { position: _position, lbPair, binArrayBitmapExtension, reserveX, reserveY, tokenXMint, tokenYMint, sender, tokenXProgram, tokenYProgram, memoProgram, eventAuthority, program: meteoraDlmmProgram, binArrays: _binArrays, } = _params;
|
|
3405
3598
|
// Reconstruct param data with full position range (not just first chunk)
|
|
3406
3599
|
const fromBinIdBuf = Buffer.alloc(4);
|
|
3407
3600
|
fromBinIdBuf.writeInt32LE(position.lowerBinId, 0);
|
|
@@ -3410,7 +3603,12 @@ class Transactions {
|
|
|
3410
3603
|
const bpsBuf = Buffer.alloc(2);
|
|
3411
3604
|
bpsBuf.writeUInt16LE(10000, 0);
|
|
3412
3605
|
const remainingAccountsInfoBytes = chunkData.length > 10 ? chunkData.slice(10) : Buffer.alloc(0);
|
|
3413
|
-
const data = Buffer.concat([
|
|
3606
|
+
const data = Buffer.concat([
|
|
3607
|
+
fromBinIdBuf,
|
|
3608
|
+
toBinIdBuf,
|
|
3609
|
+
bpsBuf,
|
|
3610
|
+
remainingAccountsInfoBytes,
|
|
3611
|
+
]);
|
|
3414
3612
|
// Derive bin arrays for the full position range
|
|
3415
3613
|
const binArrays = this.ix.pda.meteora.deriveBinArrays(lbPair.pubkey, position.lowerBinId, position.upperBinId);
|
|
3416
3614
|
let ix;
|
|
@@ -3501,7 +3699,7 @@ class Transactions {
|
|
|
3501
3699
|
}
|
|
3502
3700
|
limitCloseAutomationIx2(_a) {
|
|
3503
3701
|
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
3504
|
-
return errors_1.UniversalErrorHandler.catchErrorsAsync(
|
|
3702
|
+
return errors_1.UniversalErrorHandler.catchErrorsAsync('limitCloseAutomationIx2', () => __awaiter(this, void 0, void 0, function* () {
|
|
3505
3703
|
const program = yield errors_1.AppError.handleErrorAsync(() => __awaiter(this, void 0, void 0, function* () { return yield meteora_1.MeteoraDLMM.program(connection); }));
|
|
3506
3704
|
const position = yield errors_1.AppError.handleErrorAsync(() => __awaiter(this, void 0, void 0, function* () { return yield program.account.positionV2.fetch(params.position); }));
|
|
3507
3705
|
const dlmmPool = yield errors_1.AppError.handleErrorAsync(() => __awaiter(this, void 0, void 0, function* () { return yield meteora_1.MeteoraDLMM.create(connection, position.lbPair, this.ix); }));
|
|
@@ -3524,7 +3722,7 @@ class Transactions {
|
|
|
3524
3722
|
const iyfProgram = addresses_1.IYF_MAIN;
|
|
3525
3723
|
const hawksightAuthority = addresses_1.HS_AUTHORITY;
|
|
3526
3724
|
const { params: _params, data: chunkData } = this.removeLiquidityByRange2Info(ixs);
|
|
3527
|
-
|
|
3725
|
+
const { position: _position, lbPair, binArrayBitmapExtension, reserveX, reserveY, tokenXMint, tokenYMint, sender, tokenXProgram, tokenYProgram, memoProgram, eventAuthority, program: meteoraDlmmProgram, binArrays: _binArrays, } = _params;
|
|
3528
3726
|
// Reconstruct param data with full position range (not just first chunk)
|
|
3529
3727
|
const fromBinIdBuf = Buffer.alloc(4);
|
|
3530
3728
|
fromBinIdBuf.writeInt32LE(position.lowerBinId, 0);
|
|
@@ -3533,7 +3731,12 @@ class Transactions {
|
|
|
3533
3731
|
const bpsBuf = Buffer.alloc(2);
|
|
3534
3732
|
bpsBuf.writeUInt16LE(10000, 0);
|
|
3535
3733
|
const remainingAccountsInfoBytes = chunkData.length > 10 ? chunkData.slice(10) : Buffer.alloc(0);
|
|
3536
|
-
const data = Buffer.concat([
|
|
3734
|
+
const data = Buffer.concat([
|
|
3735
|
+
fromBinIdBuf,
|
|
3736
|
+
toBinIdBuf,
|
|
3737
|
+
bpsBuf,
|
|
3738
|
+
remainingAccountsInfoBytes,
|
|
3739
|
+
]);
|
|
3537
3740
|
// Derive bin arrays for the full position range
|
|
3538
3741
|
const binArrays = this.ix.pda.meteora.deriveBinArrays(lbPair.pubkey, position.lowerBinId, position.upperBinId);
|
|
3539
3742
|
let ix;
|
|
@@ -3650,7 +3853,7 @@ class Transactions {
|
|
|
3650
3853
|
};
|
|
3651
3854
|
}
|
|
3652
3855
|
}
|
|
3653
|
-
throw new Error(
|
|
3856
|
+
throw new Error('Unexpected error: Cannot find "RemoveLiquidityByRange" or "RemoveLiquidityByRange2" instruction from instructions');
|
|
3654
3857
|
}
|
|
3655
3858
|
removeLiquidityByRange2Info(ixs) {
|
|
3656
3859
|
for (const ix of ixs) {
|
|
@@ -3681,7 +3884,7 @@ class Transactions {
|
|
|
3681
3884
|
};
|
|
3682
3885
|
}
|
|
3683
3886
|
}
|
|
3684
|
-
throw new Error(
|
|
3887
|
+
throw new Error('Unexpected error: Cannot find "RemoveLiquidityByRange" or "RemoveLiquidityByRange2" instruction from instructions');
|
|
3685
3888
|
}
|
|
3686
3889
|
/**
|
|
3687
3890
|
* Creates a large position with more than 69 bins.
|
|
@@ -3760,7 +3963,10 @@ class Transactions {
|
|
|
3760
3963
|
}
|
|
3761
3964
|
meteoraInitializeLargePosition(_a) {
|
|
3762
3965
|
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
3763
|
-
const txInstructions = yield this._meteoraInitializeLargePosition({
|
|
3966
|
+
const txInstructions = yield this._meteoraInitializeLargePosition({
|
|
3967
|
+
connection,
|
|
3968
|
+
params,
|
|
3969
|
+
});
|
|
3764
3970
|
const transactions = [];
|
|
3765
3971
|
for (const instructions of txInstructions) {
|
|
3766
3972
|
transactions.push(yield (0, functions_1.createTransactionMeta2)({
|
|
@@ -3846,7 +4052,7 @@ class Transactions {
|
|
|
3846
4052
|
})).map(ix => {
|
|
3847
4053
|
return {
|
|
3848
4054
|
instruction: ix,
|
|
3849
|
-
label: `Initialize bin arrays for bins ${chunk.lowerBinId} to ${chunk.upperBinId}
|
|
4055
|
+
label: `Initialize bin arrays for bins ${chunk.lowerBinId} to ${chunk.upperBinId}`,
|
|
3850
4056
|
};
|
|
3851
4057
|
});
|
|
3852
4058
|
instructions.push(...initBinArraysIx);
|
|
@@ -3871,7 +4077,8 @@ class Transactions {
|
|
|
3871
4077
|
minWithdrawYAmount: new bn_js_1.default(0),
|
|
3872
4078
|
maxDepositYAmount: chunk.maxAmountY,
|
|
3873
4079
|
removeLiquidityParams: [], // No removal
|
|
3874
|
-
addLiquidityParams: [
|
|
4080
|
+
addLiquidityParams: [
|
|
4081
|
+
{
|
|
3875
4082
|
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
3876
4083
|
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
3877
4084
|
x0: onChainParams.x0,
|
|
@@ -3880,11 +4087,12 @@ class Transactions {
|
|
|
3880
4087
|
deltaY: onChainParams.deltaY,
|
|
3881
4088
|
bitFlag: onChainParams.bitFlag,
|
|
3882
4089
|
favorXInActiveId,
|
|
3883
|
-
}
|
|
4090
|
+
},
|
|
4091
|
+
],
|
|
3884
4092
|
});
|
|
3885
4093
|
instructions.push({
|
|
3886
4094
|
instruction: depositIx,
|
|
3887
|
-
label: `Deposit liquidity to bins ${chunk.lowerBinId} to ${chunk.upperBinId} via rebalanceLiquidity
|
|
4095
|
+
label: `Deposit liquidity to bins ${chunk.lowerBinId} to ${chunk.upperBinId} via rebalanceLiquidity`,
|
|
3888
4096
|
});
|
|
3889
4097
|
txInstructions.push(instructions);
|
|
3890
4098
|
}
|
|
@@ -3946,17 +4154,31 @@ class Transactions {
|
|
|
3946
4154
|
const lbPairState = dlmmPool.dlmm.lbPair;
|
|
3947
4155
|
const tokenXMint = lbPairState.tokenXMint;
|
|
3948
4156
|
const tokenYMint = lbPairState.tokenYMint;
|
|
3949
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
4157
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
4158
|
+
tokenXMint,
|
|
4159
|
+
tokenYMint,
|
|
4160
|
+
]);
|
|
3950
4161
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
3951
4162
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
3952
4163
|
// Convert Distribution to liquidityStrategy.StrategyType
|
|
3953
4164
|
const distributionToStrategyType = {
|
|
3954
|
-
|
|
3955
|
-
'
|
|
3956
|
-
'
|
|
4165
|
+
SPOT: liquidityStrategy_1.StrategyType.SPOT,
|
|
4166
|
+
'SPOT-IMBALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
4167
|
+
'SPOT-ONE-SIDE': liquidityStrategy_1.StrategyType.SPOT,
|
|
4168
|
+
'SPOT-BALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
4169
|
+
CURVE: liquidityStrategy_1.StrategyType.CURVE,
|
|
4170
|
+
'CURVE-IMBALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
4171
|
+
'CURVE-ONE-SIDE': liquidityStrategy_1.StrategyType.CURVE,
|
|
4172
|
+
'CURVE-BALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
4173
|
+
'BID-ASK': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
4174
|
+
'BID-ASK-IMBALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
4175
|
+
'BID-ASK-ONE-SIDE': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
4176
|
+
'BID-ASK-BALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
3957
4177
|
};
|
|
3958
4178
|
const strategyType = (_b = distributionToStrategyType[params.distribution]) !== null && _b !== void 0 ? _b : liquidityStrategy_1.StrategyType.SPOT;
|
|
3959
|
-
const txInstructions = yield this._meteoraDepositToLargePosition({
|
|
4179
|
+
const txInstructions = yield this._meteoraDepositToLargePosition({
|
|
4180
|
+
connection,
|
|
4181
|
+
params: {
|
|
3960
4182
|
userWallet: params.userWallet,
|
|
3961
4183
|
lbPair,
|
|
3962
4184
|
position: params.position,
|
|
@@ -3970,7 +4192,8 @@ class Transactions {
|
|
|
3970
4192
|
totalYAmount: params.totalYAmount,
|
|
3971
4193
|
strategyType,
|
|
3972
4194
|
pdaTokenType: params.pdaTokenType,
|
|
3973
|
-
}
|
|
4195
|
+
},
|
|
4196
|
+
});
|
|
3974
4197
|
const transactions = [];
|
|
3975
4198
|
for (let i = 0; i < txInstructions.length; i++) {
|
|
3976
4199
|
transactions.push(yield (0, functions_1.createTransactionMeta2)({
|
|
@@ -4018,15 +4241,24 @@ class Transactions {
|
|
|
4018
4241
|
payer: params.userWallet,
|
|
4019
4242
|
addressLookupTableAddresses: addresses_1.GLOBAL_ALT,
|
|
4020
4243
|
mainInstructions: [...mainInstructions],
|
|
4021
|
-
})
|
|
4244
|
+
}),
|
|
4022
4245
|
];
|
|
4023
4246
|
}
|
|
4024
4247
|
// ≥150 bins: multi-tx path
|
|
4025
4248
|
// Convert Distribution string to liquidityStrategy.StrategyType number for chunked deposit
|
|
4026
4249
|
const distributionToStrategyType = {
|
|
4027
|
-
|
|
4028
|
-
'
|
|
4029
|
-
'
|
|
4250
|
+
SPOT: liquidityStrategy_1.StrategyType.SPOT,
|
|
4251
|
+
'SPOT-IMBALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
4252
|
+
'SPOT-ONE-SIDE': liquidityStrategy_1.StrategyType.SPOT,
|
|
4253
|
+
'SPOT-BALANCED': liquidityStrategy_1.StrategyType.SPOT,
|
|
4254
|
+
CURVE: liquidityStrategy_1.StrategyType.CURVE,
|
|
4255
|
+
'CURVE-IMBALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
4256
|
+
'CURVE-ONE-SIDE': liquidityStrategy_1.StrategyType.CURVE,
|
|
4257
|
+
'CURVE-BALANCED': liquidityStrategy_1.StrategyType.CURVE,
|
|
4258
|
+
'BID-ASK': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
4259
|
+
'BID-ASK-IMBALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
4260
|
+
'BID-ASK-ONE-SIDE': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
4261
|
+
'BID-ASK-BALANCED': liquidityStrategy_1.StrategyType.BID_ASK,
|
|
4030
4262
|
};
|
|
4031
4263
|
const strategyType = (_b = distributionToStrategyType[params.distribution]) !== null && _b !== void 0 ? _b : liquidityStrategy_1.StrategyType.SPOT;
|
|
4032
4264
|
// Step 1: Initialize position
|
|
@@ -4045,7 +4277,10 @@ class Transactions {
|
|
|
4045
4277
|
const lbPairState = dlmmPool.dlmm.lbPair;
|
|
4046
4278
|
const tokenXMint = lbPairState.tokenXMint;
|
|
4047
4279
|
const tokenYMint = lbPairState.tokenYMint;
|
|
4048
|
-
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
4280
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
4281
|
+
tokenXMint,
|
|
4282
|
+
tokenYMint,
|
|
4283
|
+
]);
|
|
4049
4284
|
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
4050
4285
|
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
4051
4286
|
// Step 3: Build deposit instructions
|
|
@@ -4072,15 +4307,17 @@ class Transactions {
|
|
|
4072
4307
|
const initIdempotent = (mint, label) => {
|
|
4073
4308
|
return {
|
|
4074
4309
|
instruction: (0, functions_1.createAtaIdempotentIxs)({
|
|
4075
|
-
accounts: [
|
|
4310
|
+
accounts: [
|
|
4311
|
+
{
|
|
4076
4312
|
payer: params.userWallet,
|
|
4077
4313
|
mint,
|
|
4078
4314
|
owner: params.userWallet,
|
|
4079
4315
|
programId: tokenProgramMap[mint.toString()],
|
|
4080
|
-
}
|
|
4316
|
+
},
|
|
4317
|
+
],
|
|
4081
4318
|
})[0],
|
|
4082
4319
|
label: `Idempotently initialize zero-amount ATA for ${label} mint`,
|
|
4083
|
-
class:
|
|
4320
|
+
class: 'initZeroAmountAtaLabeled',
|
|
4084
4321
|
};
|
|
4085
4322
|
};
|
|
4086
4323
|
const initAtaIxs = [];
|
|
@@ -4158,6 +4395,248 @@ class Transactions {
|
|
|
4158
4395
|
return transactions;
|
|
4159
4396
|
});
|
|
4160
4397
|
}
|
|
4398
|
+
_meteoraDepositHybridToLargePosition(_a) {
|
|
4399
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
4400
|
+
const MAX_BINS_PER_TX = 149;
|
|
4401
|
+
const { userWallet, lbPair, position, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram, lowerBinId, upperBinId, totalXAmount, totalYAmount, layers, pdaTokenType, } = params;
|
|
4402
|
+
const favorXInActiveId = totalYAmount.isZero() && !totalXAmount.isZero();
|
|
4403
|
+
const dlmmPool = yield meteora_1.MeteoraDLMM.create(connection, lbPair, this.ix);
|
|
4404
|
+
const { binId: activeId } = yield dlmmPool.dlmm.getActiveBin();
|
|
4405
|
+
const binStep = new bn_js_1.default(dlmmPool.dlmm.lbPair.binStep);
|
|
4406
|
+
const minDeltaId = new bn_js_1.default(lowerBinId - activeId);
|
|
4407
|
+
const maxDeltaId = new bn_js_1.default(upperBinId - activeId);
|
|
4408
|
+
const strategyResults = (0, liquidityStrategy_1.distributionToHybridStrategy)({
|
|
4409
|
+
hybridDistribution: layers,
|
|
4410
|
+
amountX: totalXAmount,
|
|
4411
|
+
amountY: totalYAmount,
|
|
4412
|
+
minDeltaId,
|
|
4413
|
+
maxDeltaId,
|
|
4414
|
+
binStep,
|
|
4415
|
+
activeId: new bn_js_1.default(activeId),
|
|
4416
|
+
});
|
|
4417
|
+
const chunks = (0, liquidityStrategy_1.chunkHybridDepositParameters)(strategyResults, minDeltaId, maxDeltaId, new bn_js_1.default(activeId), binStep, MAX_BINS_PER_TX, favorXInActiveId);
|
|
4418
|
+
const txInstructions = [];
|
|
4419
|
+
const depositInstructions = [];
|
|
4420
|
+
const deposits = [];
|
|
4421
|
+
deposits.push({ mint: tokenXMint, amount: totalXAmount });
|
|
4422
|
+
deposits.push({ mint: tokenYMint, amount: totalYAmount });
|
|
4423
|
+
const wrapSolIx = (0, functions_1.wrapSolIfMintIsWsolLabeled)(userWallet, userWallet, deposits);
|
|
4424
|
+
depositInstructions.push(...wrapSolIx);
|
|
4425
|
+
if (deposits.length > 0) {
|
|
4426
|
+
depositInstructions.push(yield this.ix.iyfMain.depositMultipleTokenWithToken2022WithLabel({
|
|
4427
|
+
connection,
|
|
4428
|
+
payer: userWallet,
|
|
4429
|
+
deposit: deposits,
|
|
4430
|
+
pdaTokenType,
|
|
4431
|
+
}));
|
|
4432
|
+
}
|
|
4433
|
+
let depositIncluded = false;
|
|
4434
|
+
if (wrapSolIx.length > 0) {
|
|
4435
|
+
depositIncluded = true;
|
|
4436
|
+
txInstructions.push(depositInstructions);
|
|
4437
|
+
}
|
|
4438
|
+
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
|
|
4439
|
+
const chunk = chunks[chunkIndex];
|
|
4440
|
+
const mergedLayer = chunk.layers.reduce((acc, l) => ({
|
|
4441
|
+
x0: acc.x0.add(l.params.x0),
|
|
4442
|
+
y0: acc.y0.add(l.params.y0),
|
|
4443
|
+
deltaX: acc.deltaX.add(l.params.deltaX),
|
|
4444
|
+
deltaY: acc.deltaY.add(l.params.deltaY),
|
|
4445
|
+
}), { x0: new bn_js_1.default(0), y0: new bn_js_1.default(0), deltaX: new bn_js_1.default(0), deltaY: new bn_js_1.default(0) });
|
|
4446
|
+
const onChainParams = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(mergedLayer.x0, mergedLayer.y0, mergedLayer.deltaX, mergedLayer.deltaY);
|
|
4447
|
+
const mergedBinsAmounts = (0, liquidityStrategy_1.toAmountIntoBins)(new bn_js_1.default(activeId), chunk.minDeltaId, chunk.maxDeltaId, mergedLayer.deltaX, mergedLayer.deltaY, mergedLayer.x0, mergedLayer.y0, binStep, favorXInActiveId);
|
|
4448
|
+
const chunkMaxX = mergedBinsAmounts.reduce((s, b) => s.add(b.amountX), new bn_js_1.default(0));
|
|
4449
|
+
const chunkMaxY = mergedBinsAmounts.reduce((s, b) => s.add(b.amountY), new bn_js_1.default(0));
|
|
4450
|
+
const instructions = [];
|
|
4451
|
+
if (!depositIncluded) {
|
|
4452
|
+
depositIncluded = true;
|
|
4453
|
+
instructions.push(...depositInstructions);
|
|
4454
|
+
}
|
|
4455
|
+
const initBinArraysIx = (yield this.ix.meteoraDlmm.meteoraDlmmInitializeBinArrayDefault({
|
|
4456
|
+
connection,
|
|
4457
|
+
userWallet,
|
|
4458
|
+
lbPair,
|
|
4459
|
+
lowerBinId: chunk.lowerBinId,
|
|
4460
|
+
upperBinId: chunk.upperBinId,
|
|
4461
|
+
})).map(ix => ({
|
|
4462
|
+
instruction: ix,
|
|
4463
|
+
label: `Initialize bin arrays for bins ${chunk.lowerBinId} to ${chunk.upperBinId}`,
|
|
4464
|
+
}));
|
|
4465
|
+
instructions.push(...initBinArraysIx);
|
|
4466
|
+
const depositIx = yield this.ix.meteoraDlmm.rebalanceLiquidity({
|
|
4467
|
+
connection,
|
|
4468
|
+
userWallet,
|
|
4469
|
+
position,
|
|
4470
|
+
lbPair,
|
|
4471
|
+
tokenXMint,
|
|
4472
|
+
tokenYMint,
|
|
4473
|
+
tokenXProgram,
|
|
4474
|
+
tokenYProgram,
|
|
4475
|
+
activeId,
|
|
4476
|
+
pdaTokenType,
|
|
4477
|
+
maxActiveBinSlippage: 0,
|
|
4478
|
+
shouldClaimFee: false,
|
|
4479
|
+
shouldClaimReward: false,
|
|
4480
|
+
minWithdrawXAmount: new bn_js_1.default(0),
|
|
4481
|
+
maxDepositXAmount: chunkMaxX,
|
|
4482
|
+
minWithdrawYAmount: new bn_js_1.default(0),
|
|
4483
|
+
maxDepositYAmount: chunkMaxY,
|
|
4484
|
+
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
|
+
],
|
|
4497
|
+
});
|
|
4498
|
+
instructions.push({
|
|
4499
|
+
instruction: depositIx,
|
|
4500
|
+
label: `Hybrid deposit to bins ${chunk.lowerBinId} to ${chunk.upperBinId} via rebalanceLiquidity`,
|
|
4501
|
+
});
|
|
4502
|
+
txInstructions.push(instructions);
|
|
4503
|
+
}
|
|
4504
|
+
return txInstructions;
|
|
4505
|
+
});
|
|
4506
|
+
}
|
|
4507
|
+
meteoraDepositHybridToLargePosition(_a) {
|
|
4508
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
4509
|
+
const txInstructions = yield this._meteoraDepositHybridToLargePosition({
|
|
4510
|
+
connection,
|
|
4511
|
+
params,
|
|
4512
|
+
});
|
|
4513
|
+
const transactions = [];
|
|
4514
|
+
for (const ixGroup of txInstructions) {
|
|
4515
|
+
transactions.push(yield (0, functions_1.createTransactionMeta2)({
|
|
4516
|
+
payer: params.userWallet,
|
|
4517
|
+
addressLookupTableAddresses: addresses_1.GLOBAL_ALT,
|
|
4518
|
+
mainInstructions: ixGroup,
|
|
4519
|
+
}));
|
|
4520
|
+
}
|
|
4521
|
+
return transactions;
|
|
4522
|
+
});
|
|
4523
|
+
}
|
|
4524
|
+
meteoraCreatePositionAndDepositHybridToLargePosition(_a) {
|
|
4525
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, params, }) {
|
|
4526
|
+
const lowerBinId = params.binRange.lowerRange;
|
|
4527
|
+
const upperBinId = params.binRange.upperRange;
|
|
4528
|
+
const initTxs = yield this._meteoraInitializeLargePosition({
|
|
4529
|
+
connection,
|
|
4530
|
+
params: {
|
|
4531
|
+
userWallet: params.userWallet,
|
|
4532
|
+
lbPair: params.pool,
|
|
4533
|
+
position: params.position,
|
|
4534
|
+
lowerBinId,
|
|
4535
|
+
upperBinId,
|
|
4536
|
+
},
|
|
4537
|
+
});
|
|
4538
|
+
const dlmmPool = yield meteora_1.MeteoraDLMM.create(connection, params.pool, this.ix);
|
|
4539
|
+
const lbPairState = dlmmPool.dlmm.lbPair;
|
|
4540
|
+
const tokenXMint = lbPairState.tokenXMint;
|
|
4541
|
+
const tokenYMint = lbPairState.tokenYMint;
|
|
4542
|
+
const tokenProgramMap = yield (0, functions_1.getTokenProgramMapForMints)(connection, [
|
|
4543
|
+
tokenXMint,
|
|
4544
|
+
tokenYMint,
|
|
4545
|
+
]);
|
|
4546
|
+
const tokenXProgram = tokenProgramMap[tokenXMint.toString()];
|
|
4547
|
+
const tokenYProgram = tokenProgramMap[tokenYMint.toString()];
|
|
4548
|
+
const depositTxs = yield this._meteoraDepositHybridToLargePosition({
|
|
4549
|
+
connection,
|
|
4550
|
+
params: {
|
|
4551
|
+
userWallet: params.userWallet,
|
|
4552
|
+
lbPair: params.pool,
|
|
4553
|
+
position: params.position,
|
|
4554
|
+
tokenXMint,
|
|
4555
|
+
tokenYMint,
|
|
4556
|
+
tokenXProgram,
|
|
4557
|
+
tokenYProgram,
|
|
4558
|
+
lowerBinId,
|
|
4559
|
+
upperBinId,
|
|
4560
|
+
totalXAmount: params.totalXAmount,
|
|
4561
|
+
totalYAmount: params.totalYAmount,
|
|
4562
|
+
layers: params.layers,
|
|
4563
|
+
pdaTokenType: params.pdaTokenType,
|
|
4564
|
+
},
|
|
4565
|
+
});
|
|
4566
|
+
const initIdempotent = (mint, label) => ({
|
|
4567
|
+
instruction: (0, functions_1.createAtaIdempotentIxs)({
|
|
4568
|
+
accounts: [
|
|
4569
|
+
{
|
|
4570
|
+
payer: params.userWallet,
|
|
4571
|
+
mint,
|
|
4572
|
+
owner: params.userWallet,
|
|
4573
|
+
programId: tokenProgramMap[mint.toString()],
|
|
4574
|
+
},
|
|
4575
|
+
],
|
|
4576
|
+
})[0],
|
|
4577
|
+
label: `Idempotently initialize zero-amount ATA for ${label} mint`,
|
|
4578
|
+
class: 'initZeroAmountAtaLabeled',
|
|
4579
|
+
});
|
|
4580
|
+
const initAtaIxs = [];
|
|
4581
|
+
if (params.totalXAmount.isZero()) {
|
|
4582
|
+
initAtaIxs.push(initIdempotent(tokenXMint, 'X'));
|
|
4583
|
+
}
|
|
4584
|
+
else if (params.totalYAmount.isZero()) {
|
|
4585
|
+
initAtaIxs.push(initIdempotent(tokenYMint, 'Y'));
|
|
4586
|
+
}
|
|
4587
|
+
const wrapSolIxs = [];
|
|
4588
|
+
const depositToPdaIxs = [];
|
|
4589
|
+
// Moving wrap SOL instruction from depositTxs to initTxs
|
|
4590
|
+
for (let i = 0; i < depositTxs.length; i++) {
|
|
4591
|
+
for (let j = depositTxs[i].length - 1; j >= 0; j--) {
|
|
4592
|
+
const labelledIx = depositTxs[i][j];
|
|
4593
|
+
const classification = labelledIx.class;
|
|
4594
|
+
if (classification === 'wrapSolToWsolLabeled') {
|
|
4595
|
+
wrapSolIxs.push(labelledIx);
|
|
4596
|
+
depositTxs[i].splice(j, 1);
|
|
4597
|
+
}
|
|
4598
|
+
if (classification === 'depositMultipleTokenWithToken2022WithLabel') {
|
|
4599
|
+
depositToPdaIxs.push(labelledIx);
|
|
4600
|
+
depositTxs[i].splice(j, 1);
|
|
4601
|
+
}
|
|
4602
|
+
if (depositTxs[i].length === 0) {
|
|
4603
|
+
depositTxs.splice(i, 1);
|
|
4604
|
+
break;
|
|
4605
|
+
}
|
|
4606
|
+
}
|
|
4607
|
+
}
|
|
4608
|
+
// Add wrap SOL instructions to the end of the last init transaction (correct order)
|
|
4609
|
+
initTxs[initTxs.length - 1].push(...initAtaIxs, ...wrapSolIxs.reverse(), ...depositToPdaIxs.reverse());
|
|
4610
|
+
// Assemble — merge init's last TX into deposit's first TX if init has >1 TX
|
|
4611
|
+
const assembled = [];
|
|
4612
|
+
if (initTxs.length > 1) {
|
|
4613
|
+
for (let i = 0; i < initTxs.length - 1; i++) {
|
|
4614
|
+
assembled.push(initTxs[i]);
|
|
4615
|
+
}
|
|
4616
|
+
const lastInitTx = initTxs[initTxs.length - 1];
|
|
4617
|
+
const firstDepositTx = depositTxs[0];
|
|
4618
|
+
assembled.push([...lastInitTx, ...firstDepositTx]);
|
|
4619
|
+
for (let i = 1; i < depositTxs.length; i++) {
|
|
4620
|
+
assembled.push(depositTxs[i]);
|
|
4621
|
+
}
|
|
4622
|
+
}
|
|
4623
|
+
else {
|
|
4624
|
+
assembled.push(initTxs[0]);
|
|
4625
|
+
for (const tx of depositTxs) {
|
|
4626
|
+
assembled.push(tx);
|
|
4627
|
+
}
|
|
4628
|
+
}
|
|
4629
|
+
const transactions = [];
|
|
4630
|
+
for (const ixGroup of assembled) {
|
|
4631
|
+
transactions.push(yield (0, functions_1.createTransactionMeta2)({
|
|
4632
|
+
payer: params.userWallet,
|
|
4633
|
+
addressLookupTableAddresses: addresses_1.GLOBAL_ALT,
|
|
4634
|
+
mainInstructions: ixGroup,
|
|
4635
|
+
}));
|
|
4636
|
+
}
|
|
4637
|
+
return transactions;
|
|
4638
|
+
});
|
|
4639
|
+
}
|
|
4161
4640
|
/**
|
|
4162
4641
|
* Deposit liquidity to a Meteora DLMM position (up to 128 bins) with optional position initialization.
|
|
4163
4642
|
*
|
|
@@ -4192,7 +4671,7 @@ class Transactions {
|
|
|
4192
4671
|
if (initializePosition) {
|
|
4193
4672
|
if (totalBins <= MAX_INIT_WIDTH) {
|
|
4194
4673
|
// Simple case: <= 70 bins - use initializePositionAndAddLiquidityByStrategy
|
|
4195
|
-
const initPositionAndAddLiquidityIxsObj =
|
|
4674
|
+
const initPositionAndAddLiquidityIxsObj = yield dlmmPool.initializePositionAndAddLiquidityByStrategy(connection, userWallet, userWallet, {
|
|
4196
4675
|
positionPubKey: position,
|
|
4197
4676
|
totalXAmount,
|
|
4198
4677
|
totalYAmount,
|
|
@@ -4207,7 +4686,7 @@ class Transactions {
|
|
|
4207
4686
|
opt: {
|
|
4208
4687
|
pdaTokenType,
|
|
4209
4688
|
},
|
|
4210
|
-
}, hsToMeteora_1.meteoraToHawksight)
|
|
4689
|
+
}, hsToMeteora_1.meteoraToHawksight);
|
|
4211
4690
|
const initPositionAndAddLiquidityIxs = [
|
|
4212
4691
|
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
4213
4692
|
...initPositionAndAddLiquidityIxsObj.wrapSolIxs.map(ix => {
|
|
@@ -4282,7 +4761,7 @@ class Transactions {
|
|
|
4282
4761
|
});
|
|
4283
4762
|
instructions.push({
|
|
4284
4763
|
instruction: initializePositionIx,
|
|
4285
|
-
label: `Initialize position for bins ${initWidth}
|
|
4764
|
+
label: `Initialize position for bins ${initWidth}`,
|
|
4286
4765
|
});
|
|
4287
4766
|
// Step 2b: Extend position to cover all bins
|
|
4288
4767
|
let currentUpperBinId = lowerBinId + initWidth - 1;
|
|
@@ -4299,12 +4778,12 @@ class Transactions {
|
|
|
4299
4778
|
});
|
|
4300
4779
|
instructions.push({
|
|
4301
4780
|
instruction: extendIx,
|
|
4302
|
-
label: `Extend position by ${binsToAdd} bins to cover up to bin ${currentUpperBinId + binsToAdd}
|
|
4781
|
+
label: `Extend position by ${binsToAdd} bins to cover up to bin ${currentUpperBinId + binsToAdd}`,
|
|
4303
4782
|
});
|
|
4304
4783
|
currentUpperBinId += binsToAdd;
|
|
4305
4784
|
}
|
|
4306
4785
|
// Step 2c: Add liquidity to the full position
|
|
4307
|
-
const addLiquidityIxsObj =
|
|
4786
|
+
const addLiquidityIxsObj = yield dlmmPool.addLiquidityByStrategy(connection, userWallet, userWallet, {
|
|
4308
4787
|
positionPubKey: position,
|
|
4309
4788
|
totalXAmount,
|
|
4310
4789
|
totalYAmount,
|
|
@@ -4317,7 +4796,7 @@ class Transactions {
|
|
|
4317
4796
|
slippage,
|
|
4318
4797
|
skipInputTokenCheck,
|
|
4319
4798
|
pdaTokenType,
|
|
4320
|
-
}, hsToMeteora_1.meteoraToHawksight)
|
|
4799
|
+
}, hsToMeteora_1.meteoraToHawksight);
|
|
4321
4800
|
const addLiquidityIxs = [
|
|
4322
4801
|
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
4323
4802
|
...addLiquidityIxsObj.wrapSolIxs.map(ix => {
|
|
@@ -4336,14 +4815,15 @@ class Transactions {
|
|
|
4336
4815
|
const initializeBinArraySighash = (0, functions_1.sighash)('initialize_bin_array');
|
|
4337
4816
|
const ixSighash = ix.data.subarray(0, 8);
|
|
4338
4817
|
const isInitBinArray = Buffer.from(ixSighash).equals(Buffer.from(initializeBinArraySighash));
|
|
4339
|
-
if (!ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM) ||
|
|
4818
|
+
if (!ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM) ||
|
|
4819
|
+
(ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM) && isInitBinArray))
|
|
4340
4820
|
return {
|
|
4341
4821
|
instruction: ix,
|
|
4342
4822
|
label: '',
|
|
4343
4823
|
};
|
|
4344
4824
|
return {
|
|
4345
4825
|
instruction: ix,
|
|
4346
|
-
label:
|
|
4826
|
+
label: 'Add liquidity by strategy (meteora deposit)'
|
|
4347
4827
|
};
|
|
4348
4828
|
}),
|
|
4349
4829
|
];
|
|
@@ -4361,7 +4841,7 @@ class Transactions {
|
|
|
4361
4841
|
}
|
|
4362
4842
|
else {
|
|
4363
4843
|
// Just add liquidity to existing position
|
|
4364
|
-
const addLiquidityIxsObj =
|
|
4844
|
+
const addLiquidityIxsObj = yield dlmmPool.addLiquidityByStrategy(connection, userWallet, userWallet, {
|
|
4365
4845
|
positionPubKey: position,
|
|
4366
4846
|
totalXAmount,
|
|
4367
4847
|
totalYAmount,
|
|
@@ -4374,7 +4854,7 @@ class Transactions {
|
|
|
4374
4854
|
slippage,
|
|
4375
4855
|
skipInputTokenCheck,
|
|
4376
4856
|
pdaTokenType,
|
|
4377
|
-
}, hsToMeteora_1.meteoraToHawksight)
|
|
4857
|
+
}, hsToMeteora_1.meteoraToHawksight);
|
|
4378
4858
|
const addLiquidityIxs = [
|
|
4379
4859
|
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
4380
4860
|
...addLiquidityIxsObj.wrapSolIxs.map(ix => {
|
|
@@ -4954,7 +5434,7 @@ class Transactions {
|
|
|
4954
5434
|
const ownerFeeB = (0, functions_1.generateAta)(addresses_1.SITE_FEE_OWNER, mintB);
|
|
4955
5435
|
const [oracle] = web3.PublicKey.findProgramAddressSync([anchor.utils.bytes.utf8.encode('oracle'), whirlpool.toBuffer()], addresses_1.ORCA_WHIRLPOOL_PROGRAM);
|
|
4956
5436
|
// TODO: Derive based on amountToSwap calculation
|
|
4957
|
-
|
|
5437
|
+
const aToB = params.aToB;
|
|
4958
5438
|
const tickArrays = whirlpools_sdk_1.SwapUtils.getTickArrayPublicKeys(whirlpoolData.tickCurrentIndex, whirlpoolData.tickSpacing, aToB, addresses_1.ORCA_WHIRLPOOL_PROGRAM, whirlpool);
|
|
4959
5439
|
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
4960
5440
|
// const wrapSolIx = wrapSolIfMintIsWsol(params.userWallet, params.userWallet, [
|
|
@@ -5335,7 +5815,7 @@ class Transactions {
|
|
|
5335
5815
|
associatedTokenProgram: addresses_1.ASSOCIATED_TOKEN_PROGRAM,
|
|
5336
5816
|
})
|
|
5337
5817
|
.instruction();
|
|
5338
|
-
|
|
5818
|
+
const orcaCloseIx = yield anchor_1.Anchor.instance()
|
|
5339
5819
|
.iyfMain.methods.iyfExtensionExecute(extensionIx.data)
|
|
5340
5820
|
.accounts({
|
|
5341
5821
|
farm,
|
|
@@ -5507,11 +5987,11 @@ class Transactions {
|
|
|
5507
5987
|
// Get the Raydium SDK instance
|
|
5508
5988
|
const sdk = (0, RaydiumSDK_1.raydium)();
|
|
5509
5989
|
// Get the pool keys (for lookup table account)
|
|
5510
|
-
(0, functions_1.benchmark)({ name:
|
|
5990
|
+
(0, functions_1.benchmark)({ name: 'raydiumOpenPosition', msg: 'sdk.clmm.getClmmPoolKeys' });
|
|
5511
5991
|
const poolKeys = yield sdk.clmm.getClmmPoolKeys(params.poolState.toString());
|
|
5512
5992
|
(0, functions_1.benchmark)({
|
|
5513
|
-
name:
|
|
5514
|
-
msg:
|
|
5993
|
+
name: 'raydiumOpenPosition',
|
|
5994
|
+
msg: 'sdk.clmm.getClmmPoolKeys',
|
|
5515
5995
|
end: true,
|
|
5516
5996
|
});
|
|
5517
5997
|
const addressLookupTableAddresses = [];
|
|
@@ -5587,7 +6067,7 @@ class Transactions {
|
|
|
5587
6067
|
];
|
|
5588
6068
|
return yield (0, functions_1.createTransactionMeta)({
|
|
5589
6069
|
payer: params.userWallet,
|
|
5590
|
-
description:
|
|
6070
|
+
description: 'Open Raydium Position',
|
|
5591
6071
|
addressLookupTableAddresses: [
|
|
5592
6072
|
...addresses_1.GLOBAL_ALT,
|
|
5593
6073
|
...addressLookupTableAddresses,
|
|
@@ -5615,13 +6095,13 @@ class Transactions {
|
|
|
5615
6095
|
const sdk = (0, RaydiumSDK_1.raydium)();
|
|
5616
6096
|
// Get the pool keys (for lookup table account)
|
|
5617
6097
|
(0, functions_1.benchmark)({
|
|
5618
|
-
name:
|
|
5619
|
-
msg:
|
|
6098
|
+
name: 'raydiumWithdrawAndClosePosition',
|
|
6099
|
+
msg: 'sdk.clmm.getClmmPoolKeys',
|
|
5620
6100
|
});
|
|
5621
6101
|
const poolKeys = yield sdk.clmm.getClmmPoolKeys(fetch.positionInfo.poolId.toString());
|
|
5622
6102
|
(0, functions_1.benchmark)({
|
|
5623
|
-
name:
|
|
5624
|
-
msg:
|
|
6103
|
+
name: 'raydiumWithdrawAndClosePosition',
|
|
6104
|
+
msg: 'sdk.clmm.getClmmPoolKeys',
|
|
5625
6105
|
end: true,
|
|
5626
6106
|
});
|
|
5627
6107
|
const addressLookupTableAddresses = [];
|
|
@@ -5683,7 +6163,7 @@ class Transactions {
|
|
|
5683
6163
|
];
|
|
5684
6164
|
return (0, functions_1.createTransactionMeta)({
|
|
5685
6165
|
payer: params.userWallet,
|
|
5686
|
-
description:
|
|
6166
|
+
description: 'Close Raydium Position',
|
|
5687
6167
|
addressLookupTableAddresses: [
|
|
5688
6168
|
...addresses_1.GLOBAL_ALT,
|
|
5689
6169
|
...addressLookupTableAddresses,
|
|
@@ -5702,7 +6182,7 @@ class Transactions {
|
|
|
5702
6182
|
];
|
|
5703
6183
|
return (0, functions_1.createTransactionMeta)({
|
|
5704
6184
|
payer: params.userWallet,
|
|
5705
|
-
description:
|
|
6185
|
+
description: 'Withdraw and close Raydium Position',
|
|
5706
6186
|
addressLookupTableAddresses: [
|
|
5707
6187
|
...addresses_1.GLOBAL_ALT,
|
|
5708
6188
|
...addressLookupTableAddresses,
|
|
@@ -5735,7 +6215,7 @@ class Transactions {
|
|
|
5735
6215
|
];
|
|
5736
6216
|
return (0, functions_1.createTransactionMeta)({
|
|
5737
6217
|
payer: params.userWallet,
|
|
5738
|
-
description:
|
|
6218
|
+
description: 'Close Raydium Position',
|
|
5739
6219
|
addressLookupTableAddresses: addresses_1.GLOBAL_ALT,
|
|
5740
6220
|
mainInstructions,
|
|
5741
6221
|
});
|
|
@@ -5761,13 +6241,13 @@ class Transactions {
|
|
|
5761
6241
|
const sdk = (0, RaydiumSDK_1.raydium)();
|
|
5762
6242
|
// Get the pool keys (for lookup table account)
|
|
5763
6243
|
(0, functions_1.benchmark)({
|
|
5764
|
-
name:
|
|
5765
|
-
msg:
|
|
6244
|
+
name: 'raydiumIncreaseLiquidity',
|
|
6245
|
+
msg: 'sdk.clmm.getClmmPoolKeys',
|
|
5766
6246
|
});
|
|
5767
6247
|
const poolKeys = yield sdk.clmm.getClmmPoolKeys(fetch.positionInfo.poolId.toString());
|
|
5768
6248
|
(0, functions_1.benchmark)({
|
|
5769
|
-
name:
|
|
5770
|
-
msg:
|
|
6249
|
+
name: 'raydiumIncreaseLiquidity',
|
|
6250
|
+
msg: 'sdk.clmm.getClmmPoolKeys',
|
|
5771
6251
|
end: true,
|
|
5772
6252
|
});
|
|
5773
6253
|
const addressLookupTableAddresses = [];
|
|
@@ -5821,7 +6301,7 @@ class Transactions {
|
|
|
5821
6301
|
];
|
|
5822
6302
|
return (0, functions_1.createTransactionMeta)({
|
|
5823
6303
|
payer: params.userWallet,
|
|
5824
|
-
description:
|
|
6304
|
+
description: 'Raydium increase liquidity',
|
|
5825
6305
|
addressLookupTableAddresses: [
|
|
5826
6306
|
...addresses_1.GLOBAL_ALT,
|
|
5827
6307
|
...addressLookupTableAddresses,
|
|
@@ -5848,13 +6328,13 @@ class Transactions {
|
|
|
5848
6328
|
const sdk = (0, RaydiumSDK_1.raydium)();
|
|
5849
6329
|
// Get the pool keys (for lookup table account)
|
|
5850
6330
|
(0, functions_1.benchmark)({
|
|
5851
|
-
name:
|
|
5852
|
-
msg:
|
|
6331
|
+
name: 'raydiumDecreaseLiquidity',
|
|
6332
|
+
msg: 'sdk.clmm.getClmmPoolKeys',
|
|
5853
6333
|
});
|
|
5854
6334
|
const poolKeys = yield sdk.clmm.getClmmPoolKeys(fetch.positionInfo.poolId.toString());
|
|
5855
6335
|
(0, functions_1.benchmark)({
|
|
5856
|
-
name:
|
|
5857
|
-
msg:
|
|
6336
|
+
name: 'raydiumDecreaseLiquidity',
|
|
6337
|
+
msg: 'sdk.clmm.getClmmPoolKeys',
|
|
5858
6338
|
end: true,
|
|
5859
6339
|
});
|
|
5860
6340
|
const addressLookupTableAddresses = [];
|
|
@@ -5907,7 +6387,7 @@ class Transactions {
|
|
|
5907
6387
|
];
|
|
5908
6388
|
return (0, functions_1.createTransactionMeta)({
|
|
5909
6389
|
payer: params.userWallet,
|
|
5910
|
-
description:
|
|
6390
|
+
description: 'Raydium decrease liquidity',
|
|
5911
6391
|
addressLookupTableAddresses: [
|
|
5912
6392
|
...addresses_1.GLOBAL_ALT,
|
|
5913
6393
|
...addressLookupTableAddresses,
|
|
@@ -5951,7 +6431,8 @@ class Transactions {
|
|
|
5951
6431
|
targetActiveBin: params.targetActiveBin,
|
|
5952
6432
|
});
|
|
5953
6433
|
// checkRange validation
|
|
5954
|
-
if (activeId < params.checkRange.lowerRange ||
|
|
6434
|
+
if (activeId < params.checkRange.lowerRange ||
|
|
6435
|
+
activeId > params.checkRange.upperRange) {
|
|
5955
6436
|
throw new Error(`Active bin ${activeId} outside checkRange [${params.checkRange.lowerRange}, ${params.checkRange.upperRange}]`);
|
|
5956
6437
|
}
|
|
5957
6438
|
const newLowerBinId = activeId + params.relativeBinRange.lowerRange;
|
|
@@ -5966,13 +6447,13 @@ class Transactions {
|
|
|
5966
6447
|
if (currentWidth <= MAX_BINS_PER_WITHDRAW_CHUNK) {
|
|
5967
6448
|
// Small/medium: single withdraw TX
|
|
5968
6449
|
teardownTxGroups.push([
|
|
5969
|
-
...yield withdrawFn({
|
|
6450
|
+
...(yield withdrawFn({
|
|
5970
6451
|
position: params.currentPosition,
|
|
5971
6452
|
minBinId: currentLowerBinId,
|
|
5972
6453
|
maxBinId: currentUpperBinId,
|
|
5973
6454
|
compound: params.compound,
|
|
5974
6455
|
origin: CLASS,
|
|
5975
|
-
}),
|
|
6456
|
+
})),
|
|
5976
6457
|
{
|
|
5977
6458
|
label: 'close_position',
|
|
5978
6459
|
class: CLASS,
|
|
@@ -5992,13 +6473,13 @@ class Transactions {
|
|
|
5992
6473
|
const chunk = withdrawChunks[i];
|
|
5993
6474
|
const isLast = i === withdrawChunks.length - 1;
|
|
5994
6475
|
const chunkIxs = [
|
|
5995
|
-
...yield withdrawFn({
|
|
6476
|
+
...(yield withdrawFn({
|
|
5996
6477
|
position: params.currentPosition,
|
|
5997
6478
|
minBinId: chunk.lowerBinId,
|
|
5998
6479
|
maxBinId: chunk.upperBinId,
|
|
5999
6480
|
compound: params.compound,
|
|
6000
6481
|
origin: CLASS,
|
|
6001
|
-
}),
|
|
6482
|
+
})),
|
|
6002
6483
|
];
|
|
6003
6484
|
if (isLast) {
|
|
6004
6485
|
chunkIxs.push({
|
|
@@ -6080,20 +6561,34 @@ class Transactions {
|
|
|
6080
6561
|
y0: acc.y0.add(l.y0),
|
|
6081
6562
|
deltaX: acc.deltaX.add(l.deltaX),
|
|
6082
6563
|
deltaY: acc.deltaY.add(l.deltaY),
|
|
6083
|
-
}), {
|
|
6564
|
+
}), {
|
|
6565
|
+
x0: new bn_js_1.default(0),
|
|
6566
|
+
y0: new bn_js_1.default(0),
|
|
6567
|
+
deltaX: new bn_js_1.default(0),
|
|
6568
|
+
deltaY: new bn_js_1.default(0),
|
|
6569
|
+
});
|
|
6084
6570
|
const mergedP = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(mergedLayer.x0, mergedLayer.y0, mergedLayer.deltaX, mergedLayer.deltaY);
|
|
6085
|
-
const allAddLiquidityParams = [
|
|
6571
|
+
const allAddLiquidityParams = [
|
|
6572
|
+
{
|
|
6086
6573
|
minDeltaId,
|
|
6087
6574
|
maxDeltaId,
|
|
6088
|
-
x0: mergedP.x0,
|
|
6575
|
+
x0: mergedP.x0,
|
|
6576
|
+
y0: mergedP.y0,
|
|
6577
|
+
deltaX: mergedP.deltaX,
|
|
6578
|
+
deltaY: mergedP.deltaY,
|
|
6089
6579
|
bitFlag: mergedP.bitFlag,
|
|
6090
6580
|
favorXInActiveId,
|
|
6091
|
-
}
|
|
6581
|
+
},
|
|
6582
|
+
];
|
|
6092
6583
|
const mergedBinsAmounts = (0, liquidityStrategy_1.toAmountIntoBins)(new bn_js_1.default(activeId), new bn_js_1.default(minDeltaId), new bn_js_1.default(maxDeltaId), mergedLayer.deltaX, mergedLayer.deltaY, mergedLayer.x0, mergedLayer.y0, binStep, favorXInActiveId);
|
|
6093
6584
|
const totalMaxX = mergedBinsAmounts.reduce((s, b) => s.add(b.amountX), new bn_js_1.default(0));
|
|
6094
6585
|
const totalMaxY = mergedBinsAmounts.reduce((s, b) => s.add(b.amountY), new bn_js_1.default(0));
|
|
6095
6586
|
const binArrayInitIxs = yield this._initBinArraysForRange(connection, lbPair, dlmmPool, newLowerBinId, newUpperBinId);
|
|
6096
|
-
const binArrayInitIxsLabeled = binArrayInitIxs.map(ix => ({
|
|
6587
|
+
const binArrayInitIxsLabeled = binArrayInitIxs.map(ix => ({
|
|
6588
|
+
label: 'init_bin_array',
|
|
6589
|
+
class: CLASS,
|
|
6590
|
+
instruction: ix,
|
|
6591
|
+
}));
|
|
6097
6592
|
const depositIx = yield depositFn({
|
|
6098
6593
|
position: params.newPosition,
|
|
6099
6594
|
shouldClaimFee: false,
|
|
@@ -6106,7 +6601,11 @@ class Transactions {
|
|
|
6106
6601
|
});
|
|
6107
6602
|
if (newWidth <= MAX_INIT_WIDTH) {
|
|
6108
6603
|
// ≤70 bins: no extension needed, everything fits in one TX
|
|
6109
|
-
rebuildTxGroups.push([
|
|
6604
|
+
rebuildTxGroups.push([
|
|
6605
|
+
...initExtendIxs,
|
|
6606
|
+
...binArrayInitIxsLabeled,
|
|
6607
|
+
depositIx,
|
|
6608
|
+
]);
|
|
6110
6609
|
}
|
|
6111
6610
|
else {
|
|
6112
6611
|
// 71-149 bins: extension needed, split init+extend from deposit to stay within TX size limit
|
|
@@ -6125,21 +6624,35 @@ class Transactions {
|
|
|
6125
6624
|
y0: acc.y0.add(l.params.y0),
|
|
6126
6625
|
deltaX: acc.deltaX.add(l.params.deltaX),
|
|
6127
6626
|
deltaY: acc.deltaY.add(l.params.deltaY),
|
|
6128
|
-
}), {
|
|
6627
|
+
}), {
|
|
6628
|
+
x0: new bn_js_1.default(0),
|
|
6629
|
+
y0: new bn_js_1.default(0),
|
|
6630
|
+
deltaX: new bn_js_1.default(0),
|
|
6631
|
+
deltaY: new bn_js_1.default(0),
|
|
6632
|
+
});
|
|
6129
6633
|
const chunkMergedP = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(chunkMergedLayer.x0, chunkMergedLayer.y0, chunkMergedLayer.deltaX, chunkMergedLayer.deltaY);
|
|
6130
|
-
const chunkAddLiquidityParams = [
|
|
6634
|
+
const chunkAddLiquidityParams = [
|
|
6635
|
+
{
|
|
6131
6636
|
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
6132
6637
|
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
6133
|
-
x0: chunkMergedP.x0,
|
|
6638
|
+
x0: chunkMergedP.x0,
|
|
6639
|
+
y0: chunkMergedP.y0,
|
|
6640
|
+
deltaX: chunkMergedP.deltaX,
|
|
6641
|
+
deltaY: chunkMergedP.deltaY,
|
|
6134
6642
|
bitFlag: chunkMergedP.bitFlag,
|
|
6135
6643
|
favorXInActiveId,
|
|
6136
|
-
}
|
|
6644
|
+
},
|
|
6645
|
+
];
|
|
6137
6646
|
const chunkMergedBinsAmounts = (0, liquidityStrategy_1.toAmountIntoBins)(new bn_js_1.default(activeId), chunk.minDeltaId, chunk.maxDeltaId, chunkMergedLayer.deltaX, chunkMergedLayer.deltaY, chunkMergedLayer.x0, chunkMergedLayer.y0, binStep, favorXInActiveId);
|
|
6138
6647
|
const chunkTotalMaxX = chunkMergedBinsAmounts.reduce((s, b) => s.add(b.amountX), new bn_js_1.default(0));
|
|
6139
6648
|
const chunkTotalMaxY = chunkMergedBinsAmounts.reduce((s, b) => s.add(b.amountY), new bn_js_1.default(0));
|
|
6140
6649
|
const binArrayInitIxs = yield this._initBinArraysForRange(connection, lbPair, dlmmPool, chunk.lowerBinId, chunk.upperBinId);
|
|
6141
6650
|
rebuildTxGroups.push([
|
|
6142
|
-
...binArrayInitIxs.map(ix => ({
|
|
6651
|
+
...binArrayInitIxs.map(ix => ({
|
|
6652
|
+
label: 'init_bin_array',
|
|
6653
|
+
class: CLASS,
|
|
6654
|
+
instruction: ix,
|
|
6655
|
+
})),
|
|
6143
6656
|
yield depositFn({
|
|
6144
6657
|
position: params.newPosition,
|
|
6145
6658
|
shouldClaimFee: false,
|
|
@@ -6220,26 +6733,26 @@ class Transactions {
|
|
|
6220
6733
|
const withdrawTxGroups = [];
|
|
6221
6734
|
if (positionWidth <= MAX_BINS_PER_WITHDRAW_CHUNK) {
|
|
6222
6735
|
withdrawTxGroups.push([
|
|
6223
|
-
...yield withdrawFn({
|
|
6736
|
+
...(yield withdrawFn({
|
|
6224
6737
|
position: params.position,
|
|
6225
6738
|
minBinId: lowerBinId,
|
|
6226
6739
|
maxBinId: upperBinId,
|
|
6227
6740
|
compound: params.compound,
|
|
6228
6741
|
origin: CLASS,
|
|
6229
|
-
}),
|
|
6742
|
+
})),
|
|
6230
6743
|
]);
|
|
6231
6744
|
}
|
|
6232
6745
|
else {
|
|
6233
6746
|
const withdrawChunks = (0, liquidityStrategy_1.chunkBinRange)(lowerBinId, upperBinId, MAX_BINS_PER_WITHDRAW_CHUNK);
|
|
6234
6747
|
for (const chunk of withdrawChunks) {
|
|
6235
6748
|
withdrawTxGroups.push([
|
|
6236
|
-
...yield withdrawFn({
|
|
6749
|
+
...(yield withdrawFn({
|
|
6237
6750
|
position: params.position,
|
|
6238
6751
|
minBinId: chunk.lowerBinId,
|
|
6239
6752
|
maxBinId: chunk.upperBinId,
|
|
6240
6753
|
compound: params.compound,
|
|
6241
6754
|
origin: CLASS,
|
|
6242
|
-
}),
|
|
6755
|
+
})),
|
|
6243
6756
|
]);
|
|
6244
6757
|
}
|
|
6245
6758
|
}
|
|
@@ -6267,15 +6780,25 @@ class Transactions {
|
|
|
6267
6780
|
y0: acc.y0.add(l.y0),
|
|
6268
6781
|
deltaX: acc.deltaX.add(l.deltaX),
|
|
6269
6782
|
deltaY: acc.deltaY.add(l.deltaY),
|
|
6270
|
-
}), {
|
|
6783
|
+
}), {
|
|
6784
|
+
x0: new bn_js_1.default(0),
|
|
6785
|
+
y0: new bn_js_1.default(0),
|
|
6786
|
+
deltaX: new bn_js_1.default(0),
|
|
6787
|
+
deltaY: new bn_js_1.default(0),
|
|
6788
|
+
});
|
|
6271
6789
|
const mergedP = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(mergedLayer.x0, mergedLayer.y0, mergedLayer.deltaX, mergedLayer.deltaY);
|
|
6272
|
-
const allAddLiquidityParams = [
|
|
6790
|
+
const allAddLiquidityParams = [
|
|
6791
|
+
{
|
|
6273
6792
|
minDeltaId,
|
|
6274
6793
|
maxDeltaId,
|
|
6275
|
-
x0: mergedP.x0,
|
|
6794
|
+
x0: mergedP.x0,
|
|
6795
|
+
y0: mergedP.y0,
|
|
6796
|
+
deltaX: mergedP.deltaX,
|
|
6797
|
+
deltaY: mergedP.deltaY,
|
|
6276
6798
|
bitFlag: mergedP.bitFlag,
|
|
6277
6799
|
favorXInActiveId,
|
|
6278
|
-
}
|
|
6800
|
+
},
|
|
6801
|
+
];
|
|
6279
6802
|
const mergedBinsAmounts = (0, liquidityStrategy_1.toAmountIntoBins)(new bn_js_1.default(activeId), new bn_js_1.default(minDeltaId), new bn_js_1.default(maxDeltaId), mergedLayer.deltaX, mergedLayer.deltaY, mergedLayer.x0, mergedLayer.y0, binStep, favorXInActiveId);
|
|
6280
6803
|
const totalMaxX = mergedBinsAmounts.reduce((s, b) => s.add(b.amountX), new bn_js_1.default(0));
|
|
6281
6804
|
const totalMaxY = mergedBinsAmounts.reduce((s, b) => s.add(b.amountY), new bn_js_1.default(0));
|
|
@@ -6302,15 +6825,25 @@ class Transactions {
|
|
|
6302
6825
|
y0: acc.y0.add(l.params.y0),
|
|
6303
6826
|
deltaX: acc.deltaX.add(l.params.deltaX),
|
|
6304
6827
|
deltaY: acc.deltaY.add(l.params.deltaY),
|
|
6305
|
-
}), {
|
|
6828
|
+
}), {
|
|
6829
|
+
x0: new bn_js_1.default(0),
|
|
6830
|
+
y0: new bn_js_1.default(0),
|
|
6831
|
+
deltaX: new bn_js_1.default(0),
|
|
6832
|
+
deltaY: new bn_js_1.default(0),
|
|
6833
|
+
});
|
|
6306
6834
|
const chunkMergedP = (0, liquidityStrategy_1.buildBitFlagAndNegateStrategyParameters)(chunkMergedLayer.x0, chunkMergedLayer.y0, chunkMergedLayer.deltaX, chunkMergedLayer.deltaY);
|
|
6307
|
-
const chunkAddLiquidityParams = [
|
|
6835
|
+
const chunkAddLiquidityParams = [
|
|
6836
|
+
{
|
|
6308
6837
|
minDeltaId: chunk.minDeltaId.toNumber(),
|
|
6309
6838
|
maxDeltaId: chunk.maxDeltaId.toNumber(),
|
|
6310
|
-
x0: chunkMergedP.x0,
|
|
6839
|
+
x0: chunkMergedP.x0,
|
|
6840
|
+
y0: chunkMergedP.y0,
|
|
6841
|
+
deltaX: chunkMergedP.deltaX,
|
|
6842
|
+
deltaY: chunkMergedP.deltaY,
|
|
6311
6843
|
bitFlag: chunkMergedP.bitFlag,
|
|
6312
6844
|
favorXInActiveId,
|
|
6313
|
-
}
|
|
6845
|
+
},
|
|
6846
|
+
];
|
|
6314
6847
|
const chunkMergedBinsAmounts = (0, liquidityStrategy_1.toAmountIntoBins)(new bn_js_1.default(activeId), chunk.minDeltaId, chunk.maxDeltaId, chunkMergedLayer.deltaX, chunkMergedLayer.deltaY, chunkMergedLayer.x0, chunkMergedLayer.y0, binStep, favorXInActiveId);
|
|
6315
6848
|
const chunkTotalMaxX = chunkMergedBinsAmounts.reduce((s, b) => s.add(b.amountX), new bn_js_1.default(0));
|
|
6316
6849
|
const chunkTotalMaxY = chunkMergedBinsAmounts.reduce((s, b) => s.add(b.amountY), new bn_js_1.default(0));
|
|
@@ -6429,6 +6962,7 @@ class Transactions {
|
|
|
6429
6962
|
combinedAccounts.push(...baseRemainingAccountsInfo.accounts);
|
|
6430
6963
|
for (const reward of rewardInfos) {
|
|
6431
6964
|
const rewardTransferHookInfo = yield (0, functions_1.fetchRemainingAccountsInfoForReward)(connection, reward.rewardMint, reward.rewardTokenProgram);
|
|
6965
|
+
reward.rewardRemainingAccountsInfo = rewardTransferHookInfo;
|
|
6432
6966
|
const transferHookLength = rewardTransferHookInfo.slices.length > 0
|
|
6433
6967
|
? rewardTransferHookInfo.slices[0].length
|
|
6434
6968
|
: 0;
|
|
@@ -6444,14 +6978,21 @@ class Transactions {
|
|
|
6444
6978
|
accounts: combinedAccounts,
|
|
6445
6979
|
};
|
|
6446
6980
|
// --- Estimate total amounts ---
|
|
6981
|
+
// When compound=true, fees are claimed via claimFee2Automation which takes an 8% hawk-side
|
|
6982
|
+
// cut (HARVEST_FEE_NUMERATOR=800, FEE_DENOMINATOR=10000). The net fee after cut is what's
|
|
6983
|
+
// available for re-deposit.
|
|
6447
6984
|
const positionData = userPosition.positionData;
|
|
6448
6985
|
const feeX = positionData.feeX;
|
|
6449
6986
|
const feeY = positionData.feeY;
|
|
6987
|
+
const HAWK_FEE_BPS = 800;
|
|
6988
|
+
const FEE_BPS_DENOM = 10000;
|
|
6989
|
+
const netFeeX = feeX.mul(new bn_js_1.default(FEE_BPS_DENOM - HAWK_FEE_BPS)).div(new bn_js_1.default(FEE_BPS_DENOM));
|
|
6990
|
+
const netFeeY = feeY.mul(new bn_js_1.default(FEE_BPS_DENOM - HAWK_FEE_BPS)).div(new bn_js_1.default(FEE_BPS_DENOM));
|
|
6450
6991
|
const estimatedTotalX = compound
|
|
6451
|
-
? new bn_js_1.default(positionData.totalXAmount).add(
|
|
6992
|
+
? new bn_js_1.default(positionData.totalXAmount).add(netFeeX)
|
|
6452
6993
|
: new bn_js_1.default(positionData.totalXAmount);
|
|
6453
6994
|
const estimatedTotalY = compound
|
|
6454
|
-
? new bn_js_1.default(positionData.totalYAmount).add(
|
|
6995
|
+
? new bn_js_1.default(positionData.totalYAmount).add(netFeeY)
|
|
6455
6996
|
: new bn_js_1.default(positionData.totalYAmount);
|
|
6456
6997
|
const _rebalanceLiquidityAutomation = (_a) => __awaiter(this, [_a], void 0, function* ({ position, maxDepositXAmount, maxDepositYAmount, pdaTokenType, shrinkMode, shouldClaimFee, shouldClaimReward, removeLiquidityParams, addLiquidityParams, maxActiveBinSlippage, }) {
|
|
6457
6998
|
return yield this.ix.meteoraDlmm.rebalanceLiquidityAutomation({
|
|
@@ -6491,62 +7032,61 @@ class Transactions {
|
|
|
6491
7032
|
});
|
|
6492
7033
|
});
|
|
6493
7034
|
const withdrawFn = (_a) => __awaiter(this, [_a], void 0, function* ({ position, minBinId, maxBinId, compound, origin, }) {
|
|
6494
|
-
|
|
6495
|
-
|
|
6496
|
-
|
|
6497
|
-
|
|
6498
|
-
|
|
6499
|
-
|
|
6500
|
-
|
|
6501
|
-
|
|
6502
|
-
|
|
6503
|
-
|
|
6504
|
-
|
|
6505
|
-
|
|
6506
|
-
|
|
6507
|
-
|
|
6508
|
-
|
|
6509
|
-
|
|
6510
|
-
|
|
6511
|
-
|
|
6512
|
-
|
|
6513
|
-
|
|
6514
|
-
|
|
6515
|
-
|
|
6516
|
-
|
|
6517
|
-
|
|
6518
|
-
|
|
6519
|
-
|
|
6520
|
-
|
|
6521
|
-
|
|
6522
|
-
shrinkMode: types_3.ShrinkMode.NoShrinkBoth,
|
|
6523
|
-
shouldClaimFee: false,
|
|
6524
|
-
// Note:
|
|
6525
|
-
// User Reward Token is not affected by this parameter. It's always STA regardless.
|
|
6526
|
-
// pdaTokenType only affects token deposits (which essentially are fees as well)
|
|
6527
|
-
pdaTokenType: types_1.TokenType.ATA,
|
|
6528
|
-
})
|
|
6529
|
-
},
|
|
6530
|
-
// Claim fees and shrink position
|
|
6531
|
-
{
|
|
6532
|
-
label: 'claim_fees_using_rbl',
|
|
6533
|
-
class: 'withdrawFn',
|
|
6534
|
-
instruction: yield _withdrawFn({
|
|
7035
|
+
const pdaTokenTypeForClaimables = compound ? types_1.TokenType.ATA : types_1.TokenType.STA;
|
|
7036
|
+
const ixs = [];
|
|
7037
|
+
// 1. Claim fees with hawk-side cut (must happen before withdraw+shrink invalidates position)
|
|
7038
|
+
ixs.push({
|
|
7039
|
+
label: 'claim_fee',
|
|
7040
|
+
class: origin,
|
|
7041
|
+
instruction: yield this.ix.meteoraDlmm.claimFee2Automation(connection, {
|
|
7042
|
+
userWallet,
|
|
7043
|
+
lbPair,
|
|
7044
|
+
position,
|
|
7045
|
+
tokenMintX: tokenXMint,
|
|
7046
|
+
tokenMintY: tokenYMint,
|
|
7047
|
+
tokenProgramX: tokenXProgram,
|
|
7048
|
+
tokenProgramY: tokenYProgram,
|
|
7049
|
+
lowerBinId: minBinId,
|
|
7050
|
+
upperBinId: maxBinId,
|
|
7051
|
+
pdaTokenType: pdaTokenTypeForClaimables,
|
|
7052
|
+
remainingAccountsInfo: baseRemainingAccountsInfo,
|
|
7053
|
+
}),
|
|
7054
|
+
});
|
|
7055
|
+
// 2. Claim rewards with hawk-side cut (must happen before withdraw+shrink invalidates position)
|
|
7056
|
+
for (const reward of rewardInfos) {
|
|
7057
|
+
ixs.push({
|
|
7058
|
+
label: 'claim_reward',
|
|
7059
|
+
class: origin,
|
|
7060
|
+
instruction: yield this.ix.meteoraDlmm.claimReward2Automation(connection, {
|
|
7061
|
+
userWallet,
|
|
7062
|
+
lbPair,
|
|
6535
7063
|
position,
|
|
6536
|
-
|
|
6537
|
-
|
|
6538
|
-
|
|
6539
|
-
|
|
6540
|
-
|
|
6541
|
-
|
|
6542
|
-
shouldClaimFee: true,
|
|
6543
|
-
// Note (continuation):
|
|
6544
|
-
// That is why we split it to 2 ix, because we want fees to be transfered to STA if
|
|
6545
|
-
// compound is off
|
|
7064
|
+
rewardIndex: reward.rewardIndex,
|
|
7065
|
+
rewardMint: reward.rewardMint,
|
|
7066
|
+
rewardVault: reward.rewardVault,
|
|
7067
|
+
tokenProgram: reward.rewardTokenProgram,
|
|
7068
|
+
lowerBinId: minBinId,
|
|
7069
|
+
upperBinId: maxBinId,
|
|
6546
7070
|
pdaTokenType: types_1.TokenType.STA,
|
|
6547
|
-
|
|
6548
|
-
|
|
6549
|
-
|
|
7071
|
+
remainingAccountsInfo: reward.rewardRemainingAccountsInfo,
|
|
7072
|
+
}),
|
|
7073
|
+
});
|
|
7074
|
+
}
|
|
7075
|
+
// 3. Withdraw liquidity + shrink (position may be invalidated after this)
|
|
7076
|
+
ixs.push({
|
|
7077
|
+
label: 'withdraw_using_rbl',
|
|
7078
|
+
class: origin,
|
|
7079
|
+
instruction: yield _withdrawFn({
|
|
7080
|
+
position,
|
|
7081
|
+
minBinId,
|
|
7082
|
+
maxBinId,
|
|
7083
|
+
shouldClaimReward: false,
|
|
7084
|
+
shouldClaimFee: false,
|
|
7085
|
+
shrinkMode: types_3.ShrinkMode.Default,
|
|
7086
|
+
pdaTokenType: types_1.TokenType.ATA,
|
|
7087
|
+
}),
|
|
7088
|
+
});
|
|
7089
|
+
return ixs;
|
|
6550
7090
|
});
|
|
6551
7091
|
const depositFn = (_a) => __awaiter(this, [_a], void 0, function* ({ position, maxDepositXAmount, maxDepositYAmount, pdaTokenType, addLiquidityParams, origin, }) {
|
|
6552
7092
|
return {
|
|
@@ -6563,7 +7103,7 @@ class Transactions {
|
|
|
6563
7103
|
removeLiquidityParams: [],
|
|
6564
7104
|
addLiquidityParams,
|
|
6565
7105
|
maxActiveBinSlippage: 0,
|
|
6566
|
-
})
|
|
7106
|
+
}),
|
|
6567
7107
|
};
|
|
6568
7108
|
});
|
|
6569
7109
|
return {
|
|
@@ -6580,6 +7120,7 @@ class Transactions {
|
|
|
6580
7120
|
tokenYMint,
|
|
6581
7121
|
tokenXProgram,
|
|
6582
7122
|
tokenYProgram,
|
|
7123
|
+
baseRemainingAccountsInfo,
|
|
6583
7124
|
remainingAccountsInfo,
|
|
6584
7125
|
rewardInfos,
|
|
6585
7126
|
estimatedTotalX,
|