@hawksightco/hawk-sdk 0.0.70 → 1.0.0
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/addresses.d.ts +18 -0
- package/dist/src/addresses.d.ts.map +1 -0
- package/dist/src/addresses.js +54 -0
- package/dist/src/anchor.d.ts +16 -0
- package/dist/src/anchor.d.ts.map +1 -0
- package/dist/src/anchor.js +58 -0
- package/dist/src/classes/CreateTxMetadata.d.ts +1 -0
- package/dist/src/classes/CreateTxMetadata.d.ts.map +1 -1
- package/dist/src/classes/General.d.ts +1 -0
- package/dist/src/classes/General.d.ts.map +1 -1
- package/dist/src/classes/GeneralUtility.d.ts +10 -0
- package/dist/src/classes/GeneralUtility.d.ts.map +1 -1
- package/dist/src/classes/GeneralUtility.js +18 -0
- package/dist/src/classes/HawkAPI.d.ts.map +1 -1
- package/dist/src/classes/Transaction.d.ts +1 -0
- package/dist/src/classes/Transaction.d.ts.map +1 -1
- package/dist/src/classes/Transactions.d.ts +108 -0
- package/dist/src/classes/Transactions.d.ts.map +1 -0
- package/dist/src/classes/Transactions.js +742 -0
- package/dist/src/classes/TxGenerator.d.ts +1 -45
- package/dist/src/classes/TxGenerator.d.ts.map +1 -1
- package/dist/src/classes/TxGenerator.js +211 -115
- package/dist/src/classes/TxGeneratorAutomations.d.ts +1 -0
- package/dist/src/classes/TxGeneratorAutomations.d.ts.map +1 -1
- package/dist/src/classes/TxGeneratorAutomations.js +7 -0
- package/dist/src/errors.d.ts +3 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +6 -0
- package/dist/src/functions.d.ts +203 -1
- package/dist/src/functions.d.ts.map +1 -1
- package/dist/src/functions.js +411 -1
- package/dist/src/hawksight.d.ts +17 -0
- package/dist/src/hawksight.d.ts.map +1 -0
- package/dist/src/hawksight.js +128 -0
- package/dist/src/hsToMeteora.d.ts +18 -0
- package/dist/src/hsToMeteora.d.ts.map +1 -0
- package/dist/src/hsToMeteora.js +1142 -0
- package/dist/src/idl/iyf-extension-idl.d.ts +7483 -0
- package/dist/src/idl/iyf-extension-idl.d.ts.map +1 -0
- package/dist/src/idl/iyf-extension-idl.js +7484 -0
- package/dist/src/idl/iyf-main-idl.d.ts +6840 -0
- package/dist/src/idl/iyf-main-idl.d.ts.map +1 -0
- package/dist/src/idl/iyf-main-idl.js +6841 -0
- package/dist/src/meteora.d.ts +304 -0
- package/dist/src/meteora.d.ts.map +1 -0
- package/dist/src/meteora.js +615 -0
- package/dist/src/types.d.ts +138 -18
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +11 -1
- package/package.json +8 -3
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.InitializePositionAndAddLiquidityByStrategyBuilder = exports.RemoveLiquidityBuilder = exports.AddLiquidityByStrategyBuilder = exports.ClaimAllRewardsByPositionBuilder = exports.MeteoraDLMM = void 0;
|
|
36
|
+
const dlmm_1 = __importStar(require("@meteora-ag/dlmm"));
|
|
37
|
+
const errors_1 = require("./errors");
|
|
38
|
+
const addresses_1 = require("./addresses");
|
|
39
|
+
const functions_1 = require("./functions");
|
|
40
|
+
const hawksight_1 = require("./hawksight");
|
|
41
|
+
const anchor_1 = require("./anchor");
|
|
42
|
+
class MeteoraDLMM {
|
|
43
|
+
constructor(dlmm) {
|
|
44
|
+
this.dlmm = dlmm;
|
|
45
|
+
this.pubkey = this.dlmm.pubkey;
|
|
46
|
+
}
|
|
47
|
+
static create(connection, poolAddress) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
const dlmm = yield dlmm_1.default.create(connection, poolAddress);
|
|
50
|
+
return new MeteoraDLMM(dlmm);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Meteora DLMM Program
|
|
55
|
+
*
|
|
56
|
+
* @param connection
|
|
57
|
+
*/
|
|
58
|
+
static program(connection) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const dlmm = yield dlmm_1.default.create(connection, addresses_1.SOME_METEORA_DLMM_POOL);
|
|
61
|
+
return dlmm.program;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
getXYAmountDistribution(distribution, activeBin, lowerRange, upperRange) {
|
|
65
|
+
let xYAmountDistribution;
|
|
66
|
+
const bins = [];
|
|
67
|
+
if (lowerRange >= upperRange) {
|
|
68
|
+
throw new errors_1.AppError(`Lower range (${lowerRange}) cannot be greater than or less than upper range ((${upperRange})).`);
|
|
69
|
+
}
|
|
70
|
+
for (let i = lowerRange; i <= upperRange; i++) {
|
|
71
|
+
bins.push(i);
|
|
72
|
+
}
|
|
73
|
+
if (distribution === "SPOT") {
|
|
74
|
+
xYAmountDistribution = (0, dlmm_1.calculateSpotDistribution)(activeBin.binId, bins);
|
|
75
|
+
}
|
|
76
|
+
else if (distribution === "CURVE") {
|
|
77
|
+
xYAmountDistribution = (0, dlmm_1.calculateNormalDistribution)(activeBin.binId, bins);
|
|
78
|
+
}
|
|
79
|
+
else if (distribution === "BID-ASK") {
|
|
80
|
+
xYAmountDistribution = (0, dlmm_1.calculateBidAskDistribution)(activeBin.binId, bins);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
throw new errors_1.AppError(`Invalid strategy ${distribution}. Can only be SPOT, CURVE, or BID-ASK.`);
|
|
84
|
+
}
|
|
85
|
+
return xYAmountDistribution;
|
|
86
|
+
}
|
|
87
|
+
getActiveBin() {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
return yield this.dlmm.getActiveBin();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
getPositionsByUserAndLbPair(userPubKey) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
return yield this.dlmm.getPositionsByUserAndLbPair(userPubKey);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Initializes a position and adds liquidity to the specified strategy, with various steps including
|
|
99
|
+
* handling wrapped SOL tokens, depositing tokens, adding liquidity, and handling dust withdrawals.
|
|
100
|
+
*
|
|
101
|
+
* @param {web3.PublicKey} userWallet - The public key of the user's wallet executing the transaction.
|
|
102
|
+
* @param {web3.PublicKey} payer - The public key of the transaction payer.
|
|
103
|
+
* @param {Object} params - The parameters for initializing the position and adding liquidity.
|
|
104
|
+
* @param {web3.PublicKey} params.positionPubKey - The public key of the position to be initialized.
|
|
105
|
+
* @param {BN} params.totalXAmount - The total amount of X tokens to be used for liquidity.
|
|
106
|
+
* @param {BN} params.totalYAmount - The total amount of Y tokens to be used for liquidity.
|
|
107
|
+
* @param {Object} params.strategy - The strategy details for adding liquidity.
|
|
108
|
+
* @param {number} params.strategy.maxBinId - The maximum bin ID for the strategy.
|
|
109
|
+
* @param {number} params.strategy.minBinId - The minimum bin ID for the strategy.
|
|
110
|
+
* @param {StrategyType} params.strategy.strategyType - The type of strategy to be applied.
|
|
111
|
+
* @param {web3.PublicKey} params.user - The public key of the user executing the transaction.
|
|
112
|
+
* @param {number} [params.slippage] - Optional slippage tolerance for the transaction.
|
|
113
|
+
*
|
|
114
|
+
* @returns {Promise<web3.TransactionInstruction[]>} - A promise that resolves to an array of transaction instructions.
|
|
115
|
+
*
|
|
116
|
+
* @throws {Error} - Throws an error if input token(s) do not exist on the blockchain.
|
|
117
|
+
*/
|
|
118
|
+
initializePositionAndAddLiquidityByStrategy(userWallet, payer, params, meteoraToHawksight) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
if (!!!params.skipInputTokenCheck) {
|
|
121
|
+
// Throw error if input token(s) do not exist on blockchain
|
|
122
|
+
yield (0, functions_1.inputTokenExists)(anchor_1.Anchor.instance().connection, userWallet, [
|
|
123
|
+
this.dlmm.tokenX.publicKey,
|
|
124
|
+
this.dlmm.tokenY.publicKey,
|
|
125
|
+
]);
|
|
126
|
+
}
|
|
127
|
+
// Generate InitializePosition and AddLiquidityByStrategy instruction from Meteora API
|
|
128
|
+
const txn = yield this.dlmm.initializePositionAndAddLiquidityByStrategy(params);
|
|
129
|
+
// Filter out non-meteora instructions
|
|
130
|
+
const ixs = (0, functions_1.getIxs)(txn).filter((ix) => ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM));
|
|
131
|
+
// Find mint from meteora instructions
|
|
132
|
+
const mints = (0, functions_1.getMintsFromInstruction)({
|
|
133
|
+
instructions: ixs,
|
|
134
|
+
find: {
|
|
135
|
+
AddLiquidityByStrategy: {
|
|
136
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
137
|
+
mintIndices: [7, 8],
|
|
138
|
+
},
|
|
139
|
+
AddLiquidityByStrategyOneSide: {
|
|
140
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
141
|
+
mintIndices: [5],
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
return new InitializePositionAndAddLiquidityByStrategyBuilder(
|
|
146
|
+
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
147
|
+
(0, functions_1.wrapSolIfMintIsWsol)(userWallet, payer, [
|
|
148
|
+
{ mint: this.dlmm.tokenX.publicKey, amount: params.totalXAmount },
|
|
149
|
+
{ mint: this.dlmm.tokenY.publicKey, amount: params.totalYAmount },
|
|
150
|
+
]),
|
|
151
|
+
// Step 2: Deposit X and Y token to UserPDA
|
|
152
|
+
yield (0, hawksight_1.depositMultipleToken)({
|
|
153
|
+
payer: userWallet,
|
|
154
|
+
deposit: [
|
|
155
|
+
{ mint: this.dlmm.tokenX.publicKey, amount: params.totalXAmount },
|
|
156
|
+
{ mint: this.dlmm.tokenY.publicKey, amount: params.totalYAmount },
|
|
157
|
+
],
|
|
158
|
+
}),
|
|
159
|
+
// Step 3: Initialize position and add liquidity by strategy
|
|
160
|
+
yield meteoraToHawksight({
|
|
161
|
+
ixs,
|
|
162
|
+
userPda: params.user,
|
|
163
|
+
authority: userWallet,
|
|
164
|
+
}),
|
|
165
|
+
// Step 4: Withdraw dust to user wallet
|
|
166
|
+
yield (0, hawksight_1.withdrawMultipleToken)({
|
|
167
|
+
payer: userWallet,
|
|
168
|
+
withdraw: mints.map((mint) => {
|
|
169
|
+
return { mint };
|
|
170
|
+
}),
|
|
171
|
+
}),
|
|
172
|
+
// Step 5: Close wSOL account (if there's any)
|
|
173
|
+
(0, functions_1.unwrapSolIfMintIsWsol)(userWallet, mints));
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Adds liquidity to an existing position according to a specified strategy.
|
|
178
|
+
*
|
|
179
|
+
* This method involves multiple steps including checking for token existence, handling wrapped SOL (wSOL), depositing tokens,
|
|
180
|
+
* adding liquidity, and handling dust withdrawals. It ensures that all necessary preparations and operations are performed
|
|
181
|
+
* to add liquidity based on the provided strategy.
|
|
182
|
+
*
|
|
183
|
+
* @param {web3.PublicKey} userWallet - The public key of the user's wallet executing the transaction.
|
|
184
|
+
* @param {web3.PublicKey} payer - The public key of the transaction payer.
|
|
185
|
+
* @param {Object} params - The parameters for adding liquidity to the position.
|
|
186
|
+
* @param {web3.PublicKey} params.positionPubKey - The public key of the position to add liquidity to.
|
|
187
|
+
* @param {BN} params.totalXAmount - The total amount of X tokens to be used for liquidity.
|
|
188
|
+
* @param {BN} params.totalYAmount - The total amount of Y tokens to be used for liquidity.
|
|
189
|
+
* @param {Object} params.strategy - The strategy details for adding liquidity.
|
|
190
|
+
* @param {number} params.strategy.maxBinId - The maximum bin ID for the strategy.
|
|
191
|
+
* @param {number} params.strategy.minBinId - The minimum bin ID for the strategy.
|
|
192
|
+
* @param {StrategyType} params.strategy.strategyType - The type of strategy to be applied.
|
|
193
|
+
* @param {web3.PublicKey} params.user - The public key of the user executing the transaction.
|
|
194
|
+
*
|
|
195
|
+
* @returns {Promise<web3.TransactionInstruction[]>} - A promise that resolves to an array of transaction instructions.
|
|
196
|
+
*
|
|
197
|
+
* @throws {Error} - Throws an error if input token(s) do not exist on the blockchain.
|
|
198
|
+
*/
|
|
199
|
+
addLiquidityByStrategy(userWallet, payer, params, meteoraToHawksight) {
|
|
200
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
201
|
+
if (!!!params.skipInputTokenCheck) {
|
|
202
|
+
// Throw error if input token(s) do not exist on blockchain
|
|
203
|
+
yield (0, functions_1.inputTokenExists)(anchor_1.Anchor.instance().connection, userWallet, [
|
|
204
|
+
this.dlmm.tokenX.publicKey,
|
|
205
|
+
this.dlmm.tokenY.publicKey,
|
|
206
|
+
]);
|
|
207
|
+
}
|
|
208
|
+
// AddLiquidityByStrategy instruction from Meteora API
|
|
209
|
+
const txn = yield this.dlmm.addLiquidityByStrategy(params);
|
|
210
|
+
// Filter out non-meteora instructions
|
|
211
|
+
const ixs = (0, functions_1.getIxs)(txn).filter((ix) => ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM));
|
|
212
|
+
// Find mint from meteora instructions
|
|
213
|
+
const mints = (0, functions_1.getMintsFromInstruction)({
|
|
214
|
+
instructions: ixs,
|
|
215
|
+
find: {
|
|
216
|
+
AddLiquidityByStrategy: {
|
|
217
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
218
|
+
mintIndices: [7, 8],
|
|
219
|
+
},
|
|
220
|
+
AddLiquidityByStrategyOneSide: {
|
|
221
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
222
|
+
mintIndices: [5],
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
return new AddLiquidityByStrategyBuilder(
|
|
227
|
+
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
228
|
+
(0, functions_1.wrapSolIfMintIsWsol)(userWallet, payer, [
|
|
229
|
+
{ mint: this.dlmm.tokenX.publicKey, amount: params.totalXAmount },
|
|
230
|
+
{ mint: this.dlmm.tokenY.publicKey, amount: params.totalYAmount },
|
|
231
|
+
]),
|
|
232
|
+
// Step 2: Deposit X and Y token to UserPDA
|
|
233
|
+
yield (0, hawksight_1.depositMultipleToken)({
|
|
234
|
+
payer: userWallet,
|
|
235
|
+
deposit: [
|
|
236
|
+
{ mint: this.dlmm.tokenX.publicKey, amount: params.totalXAmount },
|
|
237
|
+
{ mint: this.dlmm.tokenY.publicKey, amount: params.totalYAmount },
|
|
238
|
+
],
|
|
239
|
+
}),
|
|
240
|
+
// Step 3: Add liquidity by strategy
|
|
241
|
+
yield meteoraToHawksight({
|
|
242
|
+
ixs,
|
|
243
|
+
userPda: params.user,
|
|
244
|
+
authority: userWallet,
|
|
245
|
+
}),
|
|
246
|
+
// Step 4: Withdraw dust to user wallet
|
|
247
|
+
yield (0, hawksight_1.withdrawMultipleToken)({
|
|
248
|
+
payer: userWallet,
|
|
249
|
+
withdraw: mints.map((mint) => {
|
|
250
|
+
return { mint };
|
|
251
|
+
}),
|
|
252
|
+
}),
|
|
253
|
+
// Step 5: Close wSOL account (if there's any)
|
|
254
|
+
(0, functions_1.unwrapSolIfMintIsWsol)(userWallet, mints));
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Removes liquidity from an existing position.
|
|
259
|
+
*
|
|
260
|
+
* This method communicates with the DLMM (Dynamic Liquidity Market Maker) to remove liquidity
|
|
261
|
+
* from a specified trading position. It filters the transaction instructions to include only those
|
|
262
|
+
* relevant to the Meteora DLMM program.
|
|
263
|
+
*
|
|
264
|
+
* @param {web3.PublicKey} userWallet - The public key of the user's wallet executing the transaction.
|
|
265
|
+
* @param {web3.PublicKey} payer - The public key of the transaction payer.
|
|
266
|
+
* @param {Object} params - The parameters for removing liquidity from the position.
|
|
267
|
+
* @param {web3.PublicKey} params.user - The public key of the user executing the transaction.
|
|
268
|
+
* @param {web3.PublicKey} params.position - The public key of the position from which liquidity is being removed.
|
|
269
|
+
* @param {number[]} params.binIds - An array of bin IDs indicating which bins to remove liquidity from.
|
|
270
|
+
* @param {BN} params.bps - The basis points (in BN) indicating the proportion of liquidity to remove.
|
|
271
|
+
* @param {boolean} [params.shouldClaimAndClose] - Optional flag to indicate whether the position should be claimed and closed after removing liquidity.
|
|
272
|
+
*
|
|
273
|
+
* @returns {Promise<web3.TransactionInstruction[]>} - A promise that resolves to an array of transaction instructions relevant to the Meteora DLMM program.
|
|
274
|
+
*/
|
|
275
|
+
removeLiquidity(userWallet, payer, params, meteoraToHawksight) {
|
|
276
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
277
|
+
// Claim fees and/or rewards, remove liquidity, and possibly close position from Meteora API (if set)
|
|
278
|
+
const txn = yield this.dlmm.removeLiquidity(params);
|
|
279
|
+
// Filter out non-meteora instructions
|
|
280
|
+
const ixs = (0, functions_1.getIxs)(txn).filter((ix) => ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM));
|
|
281
|
+
// Find mint from meteora instructions
|
|
282
|
+
const mints = (0, functions_1.getMintsFromInstruction)({
|
|
283
|
+
instructions: ixs,
|
|
284
|
+
find: {
|
|
285
|
+
RemoveLiquidity: {
|
|
286
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
287
|
+
mintIndices: [7, 8],
|
|
288
|
+
},
|
|
289
|
+
RemoveLiquidityByRange: {
|
|
290
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
291
|
+
mintIndices: [7, 8],
|
|
292
|
+
},
|
|
293
|
+
ClaimFee: {
|
|
294
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
295
|
+
mintIndices: [9, 10],
|
|
296
|
+
},
|
|
297
|
+
ClaimReward: {
|
|
298
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
299
|
+
mintIndices: [6],
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
return new RemoveLiquidityBuilder(
|
|
304
|
+
// Step 1: Init ATA prior to withdrawal
|
|
305
|
+
(0, functions_1.createAtaIdempotentIxs)({
|
|
306
|
+
accounts: mints.map((mint) => {
|
|
307
|
+
return { owner: params.user, payer, mint };
|
|
308
|
+
}),
|
|
309
|
+
}),
|
|
310
|
+
// Step 2: Claim fees and/or rewards, remove liquidity, and possibly close position (if set)
|
|
311
|
+
yield meteoraToHawksight({
|
|
312
|
+
ixs,
|
|
313
|
+
userPda: params.user,
|
|
314
|
+
authority: userWallet,
|
|
315
|
+
}),
|
|
316
|
+
// Step 3: Withdraw tokens to user wallet
|
|
317
|
+
yield (0, hawksight_1.withdrawMultipleToken)({
|
|
318
|
+
payer: userWallet,
|
|
319
|
+
withdraw: mints.map((mint) => {
|
|
320
|
+
return { mint };
|
|
321
|
+
}),
|
|
322
|
+
}),
|
|
323
|
+
// Step 4: Close wSOL account (if there's any)
|
|
324
|
+
(0, functions_1.unwrapSolIfMintIsWsol)(userWallet, mints));
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Claims all rewards for a specified position.
|
|
329
|
+
*
|
|
330
|
+
* This method communicates with the DLMM (Dynamic Liquidity Market Maker) to claim all rewards
|
|
331
|
+
* for a given position. It filters the transaction instructions to include only those relevant
|
|
332
|
+
* to the Meteora DLMM program. The method returns a `ClaimAllRewardsByPositionBuilder` object
|
|
333
|
+
* which provides a structured way to build the necessary transaction instructions for the reward
|
|
334
|
+
* claiming process.
|
|
335
|
+
*
|
|
336
|
+
* @param {web3.PublicKey} userWallet - The public key of the user's wallet executing the transaction.
|
|
337
|
+
* @param {web3.PublicKey} payer - The public key of the transaction payer.
|
|
338
|
+
* @param {Object} params - The parameters for claiming rewards.
|
|
339
|
+
* @param {web3.PublicKey} params.owner - The public key of the owner of the position.
|
|
340
|
+
* @param {LbPosition} params.position - The position object representing the liquidity position.
|
|
341
|
+
* @param {Function} meteoraToHawksight - A function to convert Meteora instructions to Hawksight instructions.
|
|
342
|
+
*
|
|
343
|
+
* @returns {Promise<ClaimAllRewardsByPositionBuilder>} - A promise that resolves to a `ClaimAllRewardsByPositionBuilder` object.
|
|
344
|
+
*/
|
|
345
|
+
claimAllRewardsByPosition(userWallet, payer, params, meteoraToHawksight) {
|
|
346
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
347
|
+
// Claim all fees and rewards instruction from Meteora API
|
|
348
|
+
const txn = yield this.dlmm.claimAllRewardsByPosition(params);
|
|
349
|
+
// Filter out non-meteora instructions
|
|
350
|
+
const ixs = (0, functions_1.getIxs)(txn).filter((ix) => ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM));
|
|
351
|
+
// Find mint from meteora instructions
|
|
352
|
+
const mints = (0, functions_1.getMintsFromInstruction)({
|
|
353
|
+
instructions: ixs,
|
|
354
|
+
find: {
|
|
355
|
+
ClaimFee: {
|
|
356
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
357
|
+
mintIndices: [9, 10],
|
|
358
|
+
},
|
|
359
|
+
ClaimReward: {
|
|
360
|
+
programId: addresses_1.METEORA_DLMM_PROGRAM.toBase58(),
|
|
361
|
+
mintIndices: [6],
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
});
|
|
365
|
+
return new ClaimAllRewardsByPositionBuilder(
|
|
366
|
+
// Step 1: Init ATA prior to withdrawal
|
|
367
|
+
(0, functions_1.createAtaIdempotentIxs)({
|
|
368
|
+
accounts: mints.map((mint) => {
|
|
369
|
+
return { owner: params.owner, payer, mint };
|
|
370
|
+
}),
|
|
371
|
+
}),
|
|
372
|
+
// Step 2: Claim fees and/or rewards, remove liquidity, and possibly close position (if set)
|
|
373
|
+
yield meteoraToHawksight({
|
|
374
|
+
ixs,
|
|
375
|
+
userPda: params.owner,
|
|
376
|
+
authority: userWallet,
|
|
377
|
+
}),
|
|
378
|
+
// Step 3: Withdraw tokens to user wallet
|
|
379
|
+
yield (0, hawksight_1.withdrawMultipleToken)({
|
|
380
|
+
payer: userWallet,
|
|
381
|
+
withdraw: mints.map((mint) => {
|
|
382
|
+
return { mint };
|
|
383
|
+
}),
|
|
384
|
+
}),
|
|
385
|
+
// Step 4: Close wSOL account (if there's any)
|
|
386
|
+
(0, functions_1.unwrapSolIfMintIsWsol)(userWallet, mints));
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Closes a specified liquidity position.
|
|
391
|
+
*
|
|
392
|
+
* This method communicates with the DLMM (Dynamic Liquidity Market Maker) to close a given liquidity
|
|
393
|
+
* position. It filters the transaction instructions to include only those relevant to the Meteora DLMM program.
|
|
394
|
+
*
|
|
395
|
+
* @param {Object} params - The parameters for closing the position.
|
|
396
|
+
* @param {web3.PublicKey} params.owner - The public key of the owner of the position.
|
|
397
|
+
* @param {LbPosition} params.position - The position object representing the liquidity position.
|
|
398
|
+
*
|
|
399
|
+
* @returns {Promise<web3.TransactionInstruction[]>} - A promise that resolves to an array of transaction instructions relevant to the Meteora DLMM program.
|
|
400
|
+
*/
|
|
401
|
+
closePosition(userWallet, params, meteoraToHawksight) {
|
|
402
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
403
|
+
// Close position from Meteora API
|
|
404
|
+
const txn = yield this.dlmm.closePosition(params);
|
|
405
|
+
// Filter out non-meteora instructions
|
|
406
|
+
const ixs = (0, functions_1.getIxs)(txn).filter((ix) => ix.programId.equals(addresses_1.METEORA_DLMM_PROGRAM));
|
|
407
|
+
return [
|
|
408
|
+
// Step 1: Close meteora position
|
|
409
|
+
...(yield meteoraToHawksight({
|
|
410
|
+
ixs,
|
|
411
|
+
userPda: params.owner,
|
|
412
|
+
authority: userWallet,
|
|
413
|
+
})),
|
|
414
|
+
];
|
|
415
|
+
return ixs;
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
exports.MeteoraDLMM = MeteoraDLMM;
|
|
420
|
+
/**
|
|
421
|
+
* A builder class for constructing transaction instructions to claim all rewards by position.
|
|
422
|
+
*
|
|
423
|
+
* This class provides a structured way to build the necessary transaction instructions for claiming
|
|
424
|
+
* rewards, removing liquidity, and managing wrapped SOL (wSOL) accounts.
|
|
425
|
+
*/
|
|
426
|
+
class ClaimAllRewardsByPositionBuilder {
|
|
427
|
+
/**
|
|
428
|
+
* Constructs an instance of the ClaimAllRewardsByPositionBuilder class.
|
|
429
|
+
*
|
|
430
|
+
* @param {web3.TransactionInstruction[]} createAtaIxs - Instructions to create associated token accounts (ATAs) prior to withdrawal.
|
|
431
|
+
* @param {web3.TransactionInstruction[]} mainIxs - Main instructions for claiming fees and/or rewards.
|
|
432
|
+
* @param {web3.TransactionInstruction} withdrawMultipleTokenIx - Instruction to withdraw multiple tokens to the user wallet.
|
|
433
|
+
* @param {web3.TransactionInstruction[]} unwrapWsolIxs - Instructions to close wSOL accounts, if any.
|
|
434
|
+
*/
|
|
435
|
+
constructor(createAtaIxs, mainIxs, withdrawMultipleTokenIx, unwrapWsolIxs) {
|
|
436
|
+
this.createAtaIxs = createAtaIxs;
|
|
437
|
+
this.mainIxs = mainIxs;
|
|
438
|
+
this.withdrawMultipleTokenIx = withdrawMultipleTokenIx;
|
|
439
|
+
this.unwrapWsolIxs = unwrapWsolIxs;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Builds the default sequence of transaction instructions for claiming all rewards by position.
|
|
443
|
+
*
|
|
444
|
+
* This method returns an array of transaction instructions that includes:
|
|
445
|
+
* - Initializing ATAs prior to withdrawal
|
|
446
|
+
* - Claiming fees and/or rewards
|
|
447
|
+
* - Withdrawing tokens to the user wallet
|
|
448
|
+
* - Closing wSOL accounts, if any
|
|
449
|
+
*
|
|
450
|
+
* @returns {web3.TransactionInstruction[]} - An array of transaction instructions in the required order.
|
|
451
|
+
*/
|
|
452
|
+
default() {
|
|
453
|
+
return [
|
|
454
|
+
// Step 1: Init ATA prior to withdrawal
|
|
455
|
+
...this.createAtaIxs,
|
|
456
|
+
// Step 2: Claim fees and/or rewards, remove liquidity, and possibly close position (if set)
|
|
457
|
+
...this.mainIxs,
|
|
458
|
+
// Step 3: Withdraw tokens to user wallet
|
|
459
|
+
this.withdrawMultipleTokenIx,
|
|
460
|
+
// Step 4: Close wSOL account (if there's any)
|
|
461
|
+
...this.unwrapWsolIxs,
|
|
462
|
+
];
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
exports.ClaimAllRewardsByPositionBuilder = ClaimAllRewardsByPositionBuilder;
|
|
466
|
+
/**
|
|
467
|
+
* A builder class for constructing transaction instructions to add liquidity according to a specified strategy.
|
|
468
|
+
*
|
|
469
|
+
* This class provides a structured way to build the necessary transaction instructions for adding liquidity to a position
|
|
470
|
+
* based on a strategy, handling operations such as wrapping SOL, depositing tokens, adding liquidity, refunding dust, and
|
|
471
|
+
* managing wrapped SOL (wSOL) accounts.
|
|
472
|
+
*/
|
|
473
|
+
class AddLiquidityByStrategyBuilder {
|
|
474
|
+
/**
|
|
475
|
+
* Constructs an instance of the AddLiquidityByStrategyBuilder class.
|
|
476
|
+
*
|
|
477
|
+
* @param {web3.TransactionInstruction[]} wrapSolIxs - Instructions to wrap SOL to wSOL if needed.
|
|
478
|
+
* @param {web3.TransactionInstruction} depositToPdaIxs - Instruction to deposit X and Y tokens to the UserPDA.
|
|
479
|
+
* @param {web3.TransactionInstruction[]} mainIxs - Main instructions for adding liquidity by strategy.
|
|
480
|
+
* @param {web3.TransactionInstruction} refundDustIxs - Instruction to refund dust tokens to the user wallet.
|
|
481
|
+
* @param {web3.TransactionInstruction[]} unwrapWsolIxs - Instructions to unwrap wSOL to SOL if any.
|
|
482
|
+
*/
|
|
483
|
+
constructor(wrapSolIxs, depositToPdaIxs, mainIxs, refundDustIxs, unwrapWsolIxs) {
|
|
484
|
+
this.wrapSolIxs = wrapSolIxs;
|
|
485
|
+
this.depositToPdaIxs = depositToPdaIxs;
|
|
486
|
+
this.mainIxs = mainIxs;
|
|
487
|
+
this.refundDustIxs = refundDustIxs;
|
|
488
|
+
this.unwrapWsolIxs = unwrapWsolIxs;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Builds the default sequence of transaction instructions for adding liquidity by strategy.
|
|
492
|
+
*
|
|
493
|
+
* This method returns an array of transaction instructions that includes:
|
|
494
|
+
* - Initializing wSOL token accounts if needed
|
|
495
|
+
* - Depositing X and Y tokens to the UserPDA
|
|
496
|
+
* - Adding liquidity according to the strategy
|
|
497
|
+
* - Refunding dust tokens to the user wallet
|
|
498
|
+
* - Closing wSOL accounts if any
|
|
499
|
+
*
|
|
500
|
+
* @returns {web3.TransactionInstruction[]} - An array of transaction instructions in the required order.
|
|
501
|
+
*/
|
|
502
|
+
default() {
|
|
503
|
+
return [
|
|
504
|
+
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
505
|
+
...this.wrapSolIxs,
|
|
506
|
+
// Step 2: Deposit X and Y token to UserPDA
|
|
507
|
+
this.depositToPdaIxs,
|
|
508
|
+
// Step 3: Add liquidity by strategy
|
|
509
|
+
...this.mainIxs,
|
|
510
|
+
// Step 4: Refund dust to user wallet
|
|
511
|
+
this.refundDustIxs,
|
|
512
|
+
// Step 5: Close wSOL account (if there's any)
|
|
513
|
+
...this.unwrapWsolIxs,
|
|
514
|
+
];
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
exports.AddLiquidityByStrategyBuilder = AddLiquidityByStrategyBuilder;
|
|
518
|
+
/**
|
|
519
|
+
* A builder class for constructing transaction instructions to remove liquidity from a position.
|
|
520
|
+
*
|
|
521
|
+
* This class provides a structured way to build the necessary transaction instructions for removing liquidity
|
|
522
|
+
* from a position, handling operations such as creating associated token accounts (ATAs), claiming rewards,
|
|
523
|
+
* withdrawing tokens, and managing wrapped SOL (wSOL) accounts.
|
|
524
|
+
*/
|
|
525
|
+
class RemoveLiquidityBuilder {
|
|
526
|
+
/**
|
|
527
|
+
* Constructs an instance of the RemoveLiquidityBuilder class.
|
|
528
|
+
*
|
|
529
|
+
* @param {web3.TransactionInstruction[]} createAtaIxs - Instructions to create associated token accounts (ATAs) prior to withdrawal.
|
|
530
|
+
* @param {web3.TransactionInstruction[]} mainIxs - Main instructions for claiming fees/rewards, removing liquidity, and possibly closing the position.
|
|
531
|
+
* @param {web3.TransactionInstruction} withdrawMultipleTokenIx - Instruction to withdraw multiple tokens to the user wallet.
|
|
532
|
+
* @param {web3.TransactionInstruction[]} unwrapWsolIxs - Instructions to close wSOL accounts, if any.
|
|
533
|
+
*/
|
|
534
|
+
constructor(createAtaIxs, mainIxs, withdrawMultipleTokenIx, unwrapWsolIxs) {
|
|
535
|
+
this.createAtaIxs = createAtaIxs;
|
|
536
|
+
this.mainIxs = mainIxs;
|
|
537
|
+
this.withdrawMultipleTokenIx = withdrawMultipleTokenIx;
|
|
538
|
+
this.unwrapWsolIxs = unwrapWsolIxs;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Builds the default sequence of transaction instructions for removing liquidity from a position.
|
|
542
|
+
*
|
|
543
|
+
* This method returns an array of transaction instructions that includes:
|
|
544
|
+
* - Initializing associated token accounts (ATAs) prior to withdrawal
|
|
545
|
+
* - Claiming fees and/or rewards, removing liquidity, and possibly closing the position
|
|
546
|
+
* - Withdrawing tokens to the user wallet
|
|
547
|
+
* - Closing wSOL accounts, if any
|
|
548
|
+
*
|
|
549
|
+
* @returns {web3.TransactionInstruction[]} - An array of transaction instructions in the required order.
|
|
550
|
+
*/
|
|
551
|
+
default() {
|
|
552
|
+
return [
|
|
553
|
+
// Step 1: Init ATA prior to withdrawal
|
|
554
|
+
...this.createAtaIxs,
|
|
555
|
+
// Step 2: Claim fees and/or rewards, remove liquidity, and possibly close position (if set)
|
|
556
|
+
...this.mainIxs,
|
|
557
|
+
// Step 3: Withdraw tokens to user wallet
|
|
558
|
+
this.withdrawMultipleTokenIx,
|
|
559
|
+
// Step 4: Close wSOL account (if there's any)
|
|
560
|
+
...this.unwrapWsolIxs,
|
|
561
|
+
];
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
exports.RemoveLiquidityBuilder = RemoveLiquidityBuilder;
|
|
565
|
+
/**
|
|
566
|
+
* A builder class for constructing transaction instructions to remove liquidity from a position.
|
|
567
|
+
*
|
|
568
|
+
* This class provides a structured way to build the necessary transaction instructions for removing liquidity
|
|
569
|
+
* from a position, handling operations such as creating associated token accounts (ATAs), claiming rewards,
|
|
570
|
+
* withdrawing tokens, and managing wrapped SOL (wSOL) accounts.
|
|
571
|
+
*/
|
|
572
|
+
class InitializePositionAndAddLiquidityByStrategyBuilder {
|
|
573
|
+
/**
|
|
574
|
+
* Constructs an instance of the InitializePositionAndAddLiquidityByStrategyBuilder class.
|
|
575
|
+
*
|
|
576
|
+
* @param {web3.TransactionInstruction[]} wrapSolIxs - Instructions to wrap SOL to wSOL if needed.
|
|
577
|
+
* @param {web3.TransactionInstruction} depositToPdaIxs - Instruction to deposit X and Y tokens to the UserPDA.
|
|
578
|
+
* @param {web3.TransactionInstruction[]} mainIxs - Main instructions for adding liquidity by strategy.
|
|
579
|
+
* @param {web3.TransactionInstruction} refundDustIxs - Instruction to refund dust tokens to the user wallet.
|
|
580
|
+
* @param {web3.TransactionInstruction[]} unwrapWsolIxs - Instructions to unwrap wSOL to SOL if any.
|
|
581
|
+
*/
|
|
582
|
+
constructor(wrapSolIxs, depositToPdaIxs, mainIxs, refundDustIxs, unwrapWsolIxs) {
|
|
583
|
+
this.wrapSolIxs = wrapSolIxs;
|
|
584
|
+
this.depositToPdaIxs = depositToPdaIxs;
|
|
585
|
+
this.mainIxs = mainIxs;
|
|
586
|
+
this.refundDustIxs = refundDustIxs;
|
|
587
|
+
this.unwrapWsolIxs = unwrapWsolIxs;
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Builds the default sequence of transaction instructions for removing liquidity from a position.
|
|
591
|
+
*
|
|
592
|
+
* This method returns an array of transaction instructions that includes:
|
|
593
|
+
* - Initializing associated token accounts (ATAs) prior to withdrawal
|
|
594
|
+
* - Claiming fees and/or rewards, removing liquidity, and possibly closing the position
|
|
595
|
+
* - Withdrawing tokens to the user wallet
|
|
596
|
+
* - Closing wSOL accounts, if any
|
|
597
|
+
*
|
|
598
|
+
* @returns {web3.TransactionInstruction[]} - An array of transaction instructions in the required order.
|
|
599
|
+
*/
|
|
600
|
+
default() {
|
|
601
|
+
return [
|
|
602
|
+
// Step 1: Init wSOL token account for user wallet (if X or Y token is wSOL)
|
|
603
|
+
...this.wrapSolIxs,
|
|
604
|
+
// Step 2: Deposit X and Y token to UserPDA
|
|
605
|
+
this.depositToPdaIxs,
|
|
606
|
+
// Step 3: Add liquidity by strategy
|
|
607
|
+
...this.mainIxs,
|
|
608
|
+
// Step 4: Refund dust to user wallet
|
|
609
|
+
this.refundDustIxs,
|
|
610
|
+
// Step 5: Close wSOL account (if there's any)
|
|
611
|
+
...this.unwrapWsolIxs,
|
|
612
|
+
];
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
exports.InitializePositionAndAddLiquidityByStrategyBuilder = InitializePositionAndAddLiquidityByStrategyBuilder;
|