@hawksightco/hawk-sdk 1.3.169 → 1.3.171
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 +1 -0
- package/dist/src/addresses.d.ts.map +1 -1
- package/dist/src/addresses.js +2 -1
- package/dist/src/classes/Transactions.d.ts +72 -1
- package/dist/src/classes/Transactions.d.ts.map +1 -1
- package/dist/src/classes/Transactions.js +560 -0
- package/dist/src/classes/TxGenerator.d.ts +72 -1
- package/dist/src/classes/TxGenerator.d.ts.map +1 -1
- package/dist/src/classes/TxGenerator.js +332 -0
- package/dist/src/functions.d.ts +18 -1
- package/dist/src/functions.d.ts.map +1 -1
- package/dist/src/functions.js +148 -0
- package/dist/src/idl/jupiter-idl.d.ts +36 -0
- package/dist/src/idl/jupiter-idl.d.ts.map +1 -1
- package/dist/src/idl/jupiter-idl.js +36 -0
- package/dist/src/ixGenerator/IyfMainIxGenerator.d.ts +6 -0
- package/dist/src/ixGenerator/IyfMainIxGenerator.d.ts.map +1 -1
- package/dist/src/ixGenerator/IyfMainIxGenerator.js +22 -0
- package/dist/src/ixGenerator/MeteoraDlmmIxGenerator.d.ts +167 -1
- package/dist/src/ixGenerator/MeteoraDlmmIxGenerator.d.ts.map +1 -1
- package/dist/src/ixGenerator/MeteoraDlmmIxGenerator.js +517 -0
- package/dist/src/meteora/index.d.ts +2 -0
- package/dist/src/meteora/index.d.ts.map +1 -0
- package/dist/src/meteora/index.js +17 -0
- package/dist/src/meteora/liquidityStrategy.d.ts +268 -0
- package/dist/src/meteora/liquidityStrategy.d.ts.map +1 -0
- package/dist/src/meteora/liquidityStrategy.js +1069 -0
- package/dist/src/meteora.d.ts +1 -0
- package/dist/src/meteora.d.ts.map +1 -1
- package/dist/src/meteora.js +6 -2
- package/dist/src/types.d.ts +139 -0
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +16 -1
- package/package.json +7 -3
- package/test/artifacts/temp/.gitignore +2 -0
- package/test/artifacts/temp/accounts/.gitignore +2 -0
- package/test/visualization/output/.gitignore +2 -0
|
@@ -100,6 +100,53 @@ class MeteoraDlmmIxGenerator {
|
|
|
100
100
|
this.ix = ix;
|
|
101
101
|
this.pda = pda;
|
|
102
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Initialize a Meteora DLMM position via IYF Main's meteora dynamic CPI.
|
|
105
|
+
*
|
|
106
|
+
* This is the raw Meteora initializePosition instruction invoked through IYF Main.
|
|
107
|
+
*
|
|
108
|
+
* @param connection - Solana connection
|
|
109
|
+
* @param params - Initialize position parameters (Meteora-aligned: lowerBinId, width)
|
|
110
|
+
* @returns TransactionInstruction for initializing the position
|
|
111
|
+
*/
|
|
112
|
+
initializePosition(_a) {
|
|
113
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, userWallet, lbPair, position, lowerBinId, width }) {
|
|
114
|
+
// Initialize anchor
|
|
115
|
+
anchor_1.Anchor.initialize(connection);
|
|
116
|
+
// Generate user PDA
|
|
117
|
+
const userPda = (0, functions_1.generateUserPda)(userWallet);
|
|
118
|
+
// Build instruction data: sighash + lowerBinId (i32) + width (i32)
|
|
119
|
+
const lowerBinIdBuffer = Buffer.alloc(4);
|
|
120
|
+
lowerBinIdBuffer.writeInt32LE(lowerBinId, 0);
|
|
121
|
+
const widthBuffer = Buffer.alloc(4);
|
|
122
|
+
widthBuffer.writeInt32LE(width, 0);
|
|
123
|
+
// Build raw Meteora instruction (accounts only - no hawksight wrapper accounts)
|
|
124
|
+
// meteoraDynamicCpi will prepend userPda and authority
|
|
125
|
+
const meteoraIx = new web3.TransactionInstruction({
|
|
126
|
+
data: Buffer.concat([
|
|
127
|
+
Buffer.from(util.sighash("initialize_position")),
|
|
128
|
+
lowerBinIdBuffer,
|
|
129
|
+
widthBuffer,
|
|
130
|
+
]),
|
|
131
|
+
keys: [
|
|
132
|
+
{ pubkey: userWallet, isSigner: false, isWritable: true }, // payer (Mutable, Signer - signed by IYF Main)
|
|
133
|
+
{ pubkey: position, isSigner: true, isWritable: true }, // position (Mutable, Signer)
|
|
134
|
+
{ pubkey: lbPair, isSigner: false, isWritable: false }, // lbPair
|
|
135
|
+
{ pubkey: userPda, isSigner: false, isWritable: false }, // owner (Signer - signed by IYF Main)
|
|
136
|
+
{ pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false }, // systemProgram
|
|
137
|
+
{ pubkey: web3.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // rent
|
|
138
|
+
{ pubkey: METEORA_EVENT_AUTHORITY, isSigner: false, isWritable: false }, // eventAuthority (PDA)
|
|
139
|
+
{ pubkey: METEORA_DLMM_PROGRAM, isSigner: false, isWritable: false }, // program
|
|
140
|
+
],
|
|
141
|
+
programId: METEORA_DLMM_PROGRAM,
|
|
142
|
+
});
|
|
143
|
+
return yield this.ix.iyfMain.meteoraDynamicCpi({
|
|
144
|
+
connection,
|
|
145
|
+
userWallet,
|
|
146
|
+
meteoraIx,
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
103
150
|
initializePositionAutomation(connection_1, _a) {
|
|
104
151
|
return __awaiter(this, arguments, void 0, function* (connection, { userWallet, lbPair, position, lowerBinId, upperBinId }) {
|
|
105
152
|
// Initialize anchor
|
|
@@ -994,6 +1041,45 @@ class MeteoraDlmmIxGenerator {
|
|
|
994
1041
|
});
|
|
995
1042
|
});
|
|
996
1043
|
}
|
|
1044
|
+
/**
|
|
1045
|
+
* Initialize bin arrays for Meteora DLMM pool, invoked through meteora dlmm program
|
|
1046
|
+
* which means initialization is paid by the user.
|
|
1047
|
+
*
|
|
1048
|
+
* @param connection - Solana connection
|
|
1049
|
+
* @param userWallet - User wallet public key
|
|
1050
|
+
* @param lbPair - LB pair public key
|
|
1051
|
+
* @param lowerBinId - Lower bin ID
|
|
1052
|
+
* @param upperBinId - Upper bin ID
|
|
1053
|
+
* @returns
|
|
1054
|
+
*/
|
|
1055
|
+
meteoraDlmmInitializeBinArrayDefault(_a) {
|
|
1056
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, userWallet, lbPair, lowerBinId, upperBinId, }) {
|
|
1057
|
+
// Initialize anchor
|
|
1058
|
+
anchor_1.Anchor.initialize(connection);
|
|
1059
|
+
const funder = userWallet;
|
|
1060
|
+
const systemProgram = web3.SystemProgram.programId;
|
|
1061
|
+
const tickIndices = meteora_1.MeteoraFunctions.findMeteoraTickArrayIndices(lowerBinId, upperBinId);
|
|
1062
|
+
const upperTickIndex = tickIndices[tickIndices.length - 1].add(new bn_js_1.default(1));
|
|
1063
|
+
const binArrays = meteora_1.MeteoraFunctions.findMeteoraTickArrays(lbPair, lowerBinId, upperBinId);
|
|
1064
|
+
const ixs = binArrays.map((binArray, i) => {
|
|
1065
|
+
const index = tickIndices[i] !== undefined ? tickIndices[i] : upperTickIndex;
|
|
1066
|
+
return new web3.TransactionInstruction({
|
|
1067
|
+
data: Buffer.concat([
|
|
1068
|
+
Buffer.from(util.sighash("initialize_bin_array")),
|
|
1069
|
+
new bn_js_1.default(index).toTwos(64).toArrayLike(Buffer, "le", 8),
|
|
1070
|
+
]),
|
|
1071
|
+
keys: [
|
|
1072
|
+
{ pubkey: lbPair, isSigner: false, isWritable: false },
|
|
1073
|
+
{ pubkey: binArray, isSigner: false, isWritable: true },
|
|
1074
|
+
{ pubkey: funder, isSigner: true, isWritable: true },
|
|
1075
|
+
{ pubkey: systemProgram, isSigner: false, isWritable: true },
|
|
1076
|
+
],
|
|
1077
|
+
programId: METEORA_DLMM_PROGRAM,
|
|
1078
|
+
});
|
|
1079
|
+
});
|
|
1080
|
+
return ixs;
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
997
1083
|
syncWithMarketPrice(_a) {
|
|
998
1084
|
return __awaiter(this, arguments, void 0, function* ({ connection, lbPair, marketPrice, owner, skipCanSyncCheck = false, }) {
|
|
999
1085
|
var _b;
|
|
@@ -1017,5 +1103,436 @@ class MeteoraDlmmIxGenerator {
|
|
|
1017
1103
|
return { canSync: true, instruction };
|
|
1018
1104
|
});
|
|
1019
1105
|
}
|
|
1106
|
+
/**
|
|
1107
|
+
* Increase the length of a Meteora DLMM position via IYF Main's meteora dynamic CPI.
|
|
1108
|
+
*
|
|
1109
|
+
* This instruction can only extend for up to 10kb in size, thus this instruction needs to be called
|
|
1110
|
+
* multiple times.
|
|
1111
|
+
*
|
|
1112
|
+
* This is the raw Meteora initializePosition instruction invoked through IYF Main.
|
|
1113
|
+
*
|
|
1114
|
+
* @param connection - Solana connection
|
|
1115
|
+
* @param params - Increase position length parameters
|
|
1116
|
+
* @returns TransactionInstruction for initializing the position
|
|
1117
|
+
*/
|
|
1118
|
+
increasePositionLength(_a) {
|
|
1119
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, userWallet, lbPair, position, lengthToAdd, side }) {
|
|
1120
|
+
// Initialize anchor
|
|
1121
|
+
anchor_1.Anchor.initialize(connection);
|
|
1122
|
+
// Generate user PDA
|
|
1123
|
+
const userPda = (0, functions_1.generateUserPda)(userWallet);
|
|
1124
|
+
// Build instruction data: sighash + lengthToAdd (u16) + side (u8)
|
|
1125
|
+
const lengthToAddBuffer = Buffer.alloc(2);
|
|
1126
|
+
lengthToAddBuffer.writeUInt16LE(lengthToAdd, 0);
|
|
1127
|
+
const sideBuffer = Buffer.alloc(1);
|
|
1128
|
+
sideBuffer.writeUInt8(side, 0);
|
|
1129
|
+
// Build raw Meteora instruction (accounts only - no hawksight wrapper accounts)
|
|
1130
|
+
// meteoraDynamicCpi will prepend userPda and authority
|
|
1131
|
+
const meteoraIx = new web3.TransactionInstruction({
|
|
1132
|
+
data: Buffer.concat([
|
|
1133
|
+
Buffer.from(util.sighash("increase_position_length")),
|
|
1134
|
+
lengthToAddBuffer,
|
|
1135
|
+
sideBuffer,
|
|
1136
|
+
]),
|
|
1137
|
+
keys: [
|
|
1138
|
+
{ pubkey: userWallet, isSigner: false, isWritable: true }, // funder (Mutable, Signer - signed by IYF Main)
|
|
1139
|
+
{ pubkey: lbPair, isSigner: false, isWritable: false }, // lbPair
|
|
1140
|
+
{ pubkey: position, isSigner: true, isWritable: true }, // position (Mutable, Signer)
|
|
1141
|
+
{ pubkey: userPda, isSigner: false, isWritable: false }, // owner (Signer - signed by IYF Main)
|
|
1142
|
+
{ pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false }, // systemProgram (required for realloc)
|
|
1143
|
+
{ pubkey: METEORA_EVENT_AUTHORITY, isSigner: false, isWritable: false }, // eventAuthority (PDA)
|
|
1144
|
+
{ pubkey: METEORA_DLMM_PROGRAM, isSigner: false, isWritable: false }, // Meteora DLMM program
|
|
1145
|
+
],
|
|
1146
|
+
programId: METEORA_DLMM_PROGRAM,
|
|
1147
|
+
});
|
|
1148
|
+
return yield this.ix.iyfMain.meteoraDynamicCpi({
|
|
1149
|
+
connection,
|
|
1150
|
+
userWallet,
|
|
1151
|
+
meteoraIx,
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Create a Meteora DLMM rebalance_liquidity instruction via IYF Main's meteora dynamic CPI.
|
|
1157
|
+
*
|
|
1158
|
+
* The rebalance_liquidity instruction allows for complex liquidity management including:
|
|
1159
|
+
* - Removing liquidity from specific bin ranges
|
|
1160
|
+
* - Adding liquidity to new bin ranges
|
|
1161
|
+
* - Claiming fees and rewards
|
|
1162
|
+
* - All in a single atomic transaction
|
|
1163
|
+
*
|
|
1164
|
+
* @param params - RebalanceLiquidityIxParams containing all necessary accounts and parameters
|
|
1165
|
+
* @returns TransactionInstruction for the rebalance_liquidity operation
|
|
1166
|
+
*/
|
|
1167
|
+
rebalanceLiquidity(_a) {
|
|
1168
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, userWallet, position, lbPair, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram, activeId, pdaTokenType, maxActiveBinSlippage, shouldClaimFee, shouldClaimReward, minWithdrawXAmount, maxDepositXAmount, minWithdrawYAmount, maxDepositYAmount, removeLiquidityParams, addLiquidityParams, }) {
|
|
1169
|
+
// Initialize Anchor
|
|
1170
|
+
anchor_1.Anchor.initialize(connection);
|
|
1171
|
+
// Generate user PDA
|
|
1172
|
+
const userPda = (0, functions_1.generateUserPda)(userWallet);
|
|
1173
|
+
// Generate user token accounts based on pdaTokenType
|
|
1174
|
+
let userTokenX;
|
|
1175
|
+
let userTokenY;
|
|
1176
|
+
if (pdaTokenType === types_1.TokenType.LTA) {
|
|
1177
|
+
userTokenX = (0, functions_1.generateLimitToken)(userPda, tokenXMint);
|
|
1178
|
+
userTokenY = (0, functions_1.generateLimitToken)(userPda, tokenYMint);
|
|
1179
|
+
}
|
|
1180
|
+
else if (pdaTokenType === types_1.TokenType.STA) {
|
|
1181
|
+
userTokenX = (0, functions_1.generateUserPdaStorageAccount)(userPda, tokenXMint);
|
|
1182
|
+
userTokenY = (0, functions_1.generateUserPdaStorageAccount)(userPda, tokenYMint);
|
|
1183
|
+
}
|
|
1184
|
+
else {
|
|
1185
|
+
// Default: ATA
|
|
1186
|
+
userTokenX = (0, functions_1.generateAta)(userPda, tokenXMint, tokenXProgram);
|
|
1187
|
+
userTokenY = (0, functions_1.generateAta)(userPda, tokenYMint, tokenYProgram);
|
|
1188
|
+
}
|
|
1189
|
+
// Derive bin array bitmap extension (use program ID as placeholder if not needed)
|
|
1190
|
+
const binArrayBitmapExtension = METEORA_DLMM_PROGRAM;
|
|
1191
|
+
// Derive reserves
|
|
1192
|
+
const reserveX = this.pda.meteora.deriveReserve(tokenXMint, lbPair);
|
|
1193
|
+
const reserveY = this.pda.meteora.deriveReserve(tokenYMint, lbPair);
|
|
1194
|
+
// Derive bin arrays based on activeId, minDeltaId, and maxDeltaId
|
|
1195
|
+
const lowerBinId = activeId + addLiquidityParams[0].minDeltaId;
|
|
1196
|
+
const upperBinId = activeId + addLiquidityParams[0].maxDeltaId;
|
|
1197
|
+
const binArrays = this.pda.meteora.deriveBinArrays(lbPair, lowerBinId, upperBinId);
|
|
1198
|
+
// Serialize RebalanceLiquidityParams
|
|
1199
|
+
const rebalanceParamsData = this.serializeRebalanceLiquidityParams({
|
|
1200
|
+
activeId,
|
|
1201
|
+
maxActiveBinSlippage,
|
|
1202
|
+
shouldClaimFee,
|
|
1203
|
+
shouldClaimReward,
|
|
1204
|
+
minWithdrawXAmount,
|
|
1205
|
+
maxDepositXAmount,
|
|
1206
|
+
minWithdrawYAmount,
|
|
1207
|
+
maxDepositYAmount,
|
|
1208
|
+
removeLiquidityParams,
|
|
1209
|
+
addLiquidityParams,
|
|
1210
|
+
});
|
|
1211
|
+
// Serialize RemainingAccountsInfo (default empty for now - no transfer hooks)
|
|
1212
|
+
const remainingAccountsInfo = this.serializeRemainingAccountsInfo({ slices: [] });
|
|
1213
|
+
// Build instruction data: sighash + RebalanceLiquidityParams + RemainingAccountsInfo
|
|
1214
|
+
const instructionData = Buffer.concat([
|
|
1215
|
+
Buffer.from(util.sighash("rebalance_liquidity")),
|
|
1216
|
+
rebalanceParamsData,
|
|
1217
|
+
remainingAccountsInfo,
|
|
1218
|
+
]);
|
|
1219
|
+
// Build raw Meteora instruction
|
|
1220
|
+
const meteoraIx = new web3.TransactionInstruction({
|
|
1221
|
+
data: instructionData,
|
|
1222
|
+
keys: [
|
|
1223
|
+
{ pubkey: position, isSigner: false, isWritable: true }, // position (Mutable)
|
|
1224
|
+
{ pubkey: lbPair, isSigner: false, isWritable: true }, // lbPair (Mutable)
|
|
1225
|
+
{ pubkey: binArrayBitmapExtension, isSigner: false, isWritable: false }, // binArrayBitmapExtension
|
|
1226
|
+
{ pubkey: userTokenX, isSigner: false, isWritable: true }, // userTokenX (Mutable)
|
|
1227
|
+
{ pubkey: userTokenY, isSigner: false, isWritable: true }, // userTokenY (Mutable)
|
|
1228
|
+
{ pubkey: reserveX, isSigner: false, isWritable: true }, // reserveX (Mutable)
|
|
1229
|
+
{ pubkey: reserveY, isSigner: false, isWritable: true }, // reserveY (Mutable)
|
|
1230
|
+
{ pubkey: tokenXMint, isSigner: false, isWritable: false }, // tokenXMint
|
|
1231
|
+
{ pubkey: tokenYMint, isSigner: false, isWritable: false }, // tokenYMint
|
|
1232
|
+
{ pubkey: userPda, isSigner: false, isWritable: false }, // sender (Signer - signed by IYF Main)
|
|
1233
|
+
{ pubkey: userWallet, isSigner: false, isWritable: true }, // rentReceiver (Mutable)
|
|
1234
|
+
{ pubkey: tokenXProgram, isSigner: false, isWritable: false }, // tokenXProgram
|
|
1235
|
+
{ pubkey: tokenYProgram, isSigner: false, isWritable: false }, // tokenYProgram
|
|
1236
|
+
{ pubkey: addresses_1.MEMO_PROGRAM, isSigner: false, isWritable: false }, // memoProgram
|
|
1237
|
+
{ pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false }, // systemProgram
|
|
1238
|
+
{ pubkey: METEORA_EVENT_AUTHORITY, isSigner: false, isWritable: false }, // eventAuthority
|
|
1239
|
+
{ pubkey: METEORA_DLMM_PROGRAM, isSigner: false, isWritable: false }, // program
|
|
1240
|
+
// Bin arrays as remaining accounts
|
|
1241
|
+
...binArrays.map(pubkey => ({ pubkey, isSigner: false, isWritable: true })),
|
|
1242
|
+
{ pubkey: METEORA_DLMM_PROGRAM, isSigner: false, isWritable: false }, // program
|
|
1243
|
+
],
|
|
1244
|
+
programId: METEORA_DLMM_PROGRAM,
|
|
1245
|
+
});
|
|
1246
|
+
return yield this.ix.iyfMain.meteoraDynamicCpi({
|
|
1247
|
+
connection,
|
|
1248
|
+
userWallet,
|
|
1249
|
+
meteoraIx,
|
|
1250
|
+
});
|
|
1251
|
+
});
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* Serialize RebalanceLiquidityParams to match Rust struct layout
|
|
1255
|
+
*/
|
|
1256
|
+
serializeRebalanceLiquidityParams(params) {
|
|
1257
|
+
const buffers = [];
|
|
1258
|
+
// activeId (i32)
|
|
1259
|
+
const activeIdBuf = Buffer.alloc(4);
|
|
1260
|
+
activeIdBuf.writeInt32LE(params.activeId, 0);
|
|
1261
|
+
buffers.push(activeIdBuf);
|
|
1262
|
+
// maxActiveBinSlippage (u16)
|
|
1263
|
+
const slippageBuf = Buffer.alloc(2);
|
|
1264
|
+
slippageBuf.writeUInt16LE(params.maxActiveBinSlippage, 0);
|
|
1265
|
+
buffers.push(slippageBuf);
|
|
1266
|
+
// shouldClaimFee (bool as u8)
|
|
1267
|
+
buffers.push(Buffer.from([params.shouldClaimFee ? 1 : 0]));
|
|
1268
|
+
// shouldClaimReward (bool as u8)
|
|
1269
|
+
buffers.push(Buffer.from([params.shouldClaimReward ? 1 : 0]));
|
|
1270
|
+
// minWithdrawXAmount (u64)
|
|
1271
|
+
buffers.push(params.minWithdrawXAmount.toArrayLike(Buffer, 'le', 8));
|
|
1272
|
+
// maxDepositXAmount (u64)
|
|
1273
|
+
buffers.push(params.maxDepositXAmount.toArrayLike(Buffer, 'le', 8));
|
|
1274
|
+
// minWithdrawYAmount (u64)
|
|
1275
|
+
buffers.push(params.minWithdrawYAmount.toArrayLike(Buffer, 'le', 8));
|
|
1276
|
+
// maxDepositYAmount (u64)
|
|
1277
|
+
buffers.push(params.maxDepositYAmount.toArrayLike(Buffer, 'le', 8));
|
|
1278
|
+
// padding [u8; 32] - first byte is shrinkMode (3 = no shrink both sides)
|
|
1279
|
+
const paddingBuf = Buffer.alloc(32, 0);
|
|
1280
|
+
paddingBuf[0] = 3; // shrinkMode: 3 = no shrink both
|
|
1281
|
+
buffers.push(paddingBuf);
|
|
1282
|
+
// removeLiquidityParams Vec (length as u32, then each RemoveLiquidityParams)
|
|
1283
|
+
const removeLenBuf = Buffer.alloc(4);
|
|
1284
|
+
removeLenBuf.writeUInt32LE(params.removeLiquidityParams.length, 0);
|
|
1285
|
+
buffers.push(removeLenBuf);
|
|
1286
|
+
for (const removeParam of params.removeLiquidityParams) {
|
|
1287
|
+
// minBinId: Option<i32>
|
|
1288
|
+
if (removeParam.minBinId !== undefined) {
|
|
1289
|
+
buffers.push(Buffer.from([1])); // Some
|
|
1290
|
+
const minBinIdBuf = Buffer.alloc(4);
|
|
1291
|
+
minBinIdBuf.writeInt32LE(removeParam.minBinId, 0);
|
|
1292
|
+
buffers.push(minBinIdBuf);
|
|
1293
|
+
}
|
|
1294
|
+
else {
|
|
1295
|
+
buffers.push(Buffer.from([0])); // None
|
|
1296
|
+
}
|
|
1297
|
+
// maxBinId: Option<i32>
|
|
1298
|
+
if (removeParam.maxBinId !== undefined) {
|
|
1299
|
+
buffers.push(Buffer.from([1])); // Some
|
|
1300
|
+
const maxBinIdBuf = Buffer.alloc(4);
|
|
1301
|
+
maxBinIdBuf.writeInt32LE(removeParam.maxBinId, 0);
|
|
1302
|
+
buffers.push(maxBinIdBuf);
|
|
1303
|
+
}
|
|
1304
|
+
else {
|
|
1305
|
+
buffers.push(Buffer.from([0])); // None
|
|
1306
|
+
}
|
|
1307
|
+
// bps (u16)
|
|
1308
|
+
const bpsBuf = Buffer.alloc(2);
|
|
1309
|
+
bpsBuf.writeUInt16LE(removeParam.bps, 0);
|
|
1310
|
+
buffers.push(bpsBuf);
|
|
1311
|
+
// padding [u8; 16]
|
|
1312
|
+
buffers.push(Buffer.alloc(16, 0));
|
|
1313
|
+
}
|
|
1314
|
+
// addLiquidityParams Vec (length as u32, then each AddLiquidityParams)
|
|
1315
|
+
const addLenBuf = Buffer.alloc(4);
|
|
1316
|
+
addLenBuf.writeUInt32LE(params.addLiquidityParams.length, 0);
|
|
1317
|
+
buffers.push(addLenBuf);
|
|
1318
|
+
for (const addParam of params.addLiquidityParams) {
|
|
1319
|
+
// minDeltaId (i32)
|
|
1320
|
+
const minDeltaIdBuf = Buffer.alloc(4);
|
|
1321
|
+
minDeltaIdBuf.writeInt32LE(addParam.minDeltaId, 0);
|
|
1322
|
+
buffers.push(minDeltaIdBuf);
|
|
1323
|
+
// maxDeltaId (i32)
|
|
1324
|
+
const maxDeltaIdBuf = Buffer.alloc(4);
|
|
1325
|
+
maxDeltaIdBuf.writeInt32LE(addParam.maxDeltaId, 0);
|
|
1326
|
+
buffers.push(maxDeltaIdBuf);
|
|
1327
|
+
// x0 (u64)
|
|
1328
|
+
buffers.push(addParam.x0.toArrayLike(Buffer, 'le', 8));
|
|
1329
|
+
// y0 (u64)
|
|
1330
|
+
buffers.push(addParam.y0.toArrayLike(Buffer, 'le', 8));
|
|
1331
|
+
// deltaX (u64)
|
|
1332
|
+
buffers.push(addParam.deltaX.toArrayLike(Buffer, 'le', 8));
|
|
1333
|
+
// deltaY (u64)
|
|
1334
|
+
buffers.push(addParam.deltaY.toArrayLike(Buffer, 'le', 8));
|
|
1335
|
+
// bitFlag (u8)
|
|
1336
|
+
buffers.push(Buffer.from([addParam.bitFlag]));
|
|
1337
|
+
// favorXInActiveId (bool as u8)
|
|
1338
|
+
buffers.push(Buffer.from([addParam.favorXInActiveId ? 1 : 0]));
|
|
1339
|
+
// padding [u8; 16]
|
|
1340
|
+
buffers.push(Buffer.alloc(16, 0));
|
|
1341
|
+
}
|
|
1342
|
+
return Buffer.concat(buffers);
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Serialize RemainingAccountsInfo to match Rust struct layout
|
|
1346
|
+
*/
|
|
1347
|
+
serializeRemainingAccountsInfo(info) {
|
|
1348
|
+
const buffers = [];
|
|
1349
|
+
// Vec length (u32)
|
|
1350
|
+
const lenBuf = Buffer.alloc(4);
|
|
1351
|
+
lenBuf.writeUInt32LE(info.slices.length, 0);
|
|
1352
|
+
buffers.push(lenBuf);
|
|
1353
|
+
for (const slice of info.slices) {
|
|
1354
|
+
// AccountsType enum (varies in size)
|
|
1355
|
+
if (slice.accountsType === 3 && slice.multiRewardIndex !== undefined) {
|
|
1356
|
+
// TransferHookMultiReward(u8)
|
|
1357
|
+
buffers.push(Buffer.from([3, slice.multiRewardIndex]));
|
|
1358
|
+
}
|
|
1359
|
+
else {
|
|
1360
|
+
buffers.push(Buffer.from([slice.accountsType]));
|
|
1361
|
+
}
|
|
1362
|
+
// length (u8)
|
|
1363
|
+
buffers.push(Buffer.from([slice.length]));
|
|
1364
|
+
}
|
|
1365
|
+
return Buffer.concat(buffers);
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Remove liquidity from a bin range in a Meteora DLMM position via IYF Main's meteora dynamic CPI.
|
|
1369
|
+
*
|
|
1370
|
+
* This instruction withdraws liquidity from the specified bin range.
|
|
1371
|
+
* The bin arrays for the range are automatically derived and included as remaining accounts.
|
|
1372
|
+
*
|
|
1373
|
+
* @param params - RemoveLiquidityByRange2 parameters
|
|
1374
|
+
* @returns TransactionInstruction for the remove_liquidity_by_range2 operation
|
|
1375
|
+
*/
|
|
1376
|
+
removeLiquidityByRange2(_a) {
|
|
1377
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, userWallet, lbPair, position, tokenXMint, tokenYMint, tokenXProgram, tokenYProgram, fromBinId, toBinId, bpsToRemove, pdaTokenType, remainingAccountsInfo: remainingAccountsInfoParam, }) {
|
|
1378
|
+
var _b, _c;
|
|
1379
|
+
// Initialize Anchor
|
|
1380
|
+
anchor_1.Anchor.initialize(connection);
|
|
1381
|
+
// Generate user PDA
|
|
1382
|
+
const userPda = (0, functions_1.generateUserPda)(userWallet);
|
|
1383
|
+
// Generate user token accounts based on pdaTokenType
|
|
1384
|
+
let userTokenX;
|
|
1385
|
+
let userTokenY;
|
|
1386
|
+
if (pdaTokenType === types_1.TokenType.LTA) {
|
|
1387
|
+
userTokenX = (0, functions_1.generateLimitToken)(userPda, tokenXMint);
|
|
1388
|
+
userTokenY = (0, functions_1.generateLimitToken)(userPda, tokenYMint);
|
|
1389
|
+
}
|
|
1390
|
+
else if (pdaTokenType === types_1.TokenType.STA) {
|
|
1391
|
+
userTokenX = (0, functions_1.generateUserPdaStorageAccount)(userPda, tokenXMint);
|
|
1392
|
+
userTokenY = (0, functions_1.generateUserPdaStorageAccount)(userPda, tokenYMint);
|
|
1393
|
+
}
|
|
1394
|
+
else {
|
|
1395
|
+
// Default: ATA
|
|
1396
|
+
userTokenX = (0, functions_1.generateAta)(userPda, tokenXMint, tokenXProgram);
|
|
1397
|
+
userTokenY = (0, functions_1.generateAta)(userPda, tokenYMint, tokenYProgram);
|
|
1398
|
+
}
|
|
1399
|
+
// Derive bin array bitmap extension (use program ID as placeholder if not needed)
|
|
1400
|
+
const binArrayBitmapExtension = METEORA_DLMM_PROGRAM;
|
|
1401
|
+
// Derive reserves
|
|
1402
|
+
const reserveX = this.pda.meteora.deriveReserve(tokenXMint, lbPair);
|
|
1403
|
+
const reserveY = this.pda.meteora.deriveReserve(tokenYMint, lbPair);
|
|
1404
|
+
// Derive bin arrays for the range
|
|
1405
|
+
const binArrays = this.pda.meteora.deriveBinArrays(lbPair, fromBinId, toBinId);
|
|
1406
|
+
// Build instruction data: sighash + fromBinId(i32) + toBinId(i32) + bpsToRemove(u16) + remainingAccountsInfo
|
|
1407
|
+
const fromBinIdBuffer = Buffer.alloc(4);
|
|
1408
|
+
fromBinIdBuffer.writeInt32LE(fromBinId, 0);
|
|
1409
|
+
const toBinIdBuffer = Buffer.alloc(4);
|
|
1410
|
+
toBinIdBuffer.writeInt32LE(toBinId, 0);
|
|
1411
|
+
const bpsToRemoveBuffer = Buffer.alloc(2);
|
|
1412
|
+
bpsToRemoveBuffer.writeUInt16LE(bpsToRemove, 0);
|
|
1413
|
+
// Serialize RemainingAccountsInfo
|
|
1414
|
+
// Use provided remainingAccountsInfo or default to empty slices (no transfer hooks)
|
|
1415
|
+
// Note: Bin arrays are passed as regular remaining accounts, NOT in slices
|
|
1416
|
+
const remainingAccountsInfoSerialized = this.serializeRemainingAccountsInfo({
|
|
1417
|
+
slices: (_b = remainingAccountsInfoParam === null || remainingAccountsInfoParam === void 0 ? void 0 : remainingAccountsInfoParam.slices) !== null && _b !== void 0 ? _b : [],
|
|
1418
|
+
});
|
|
1419
|
+
const instructionData = Buffer.concat([
|
|
1420
|
+
Buffer.from(util.sighash("remove_liquidity_by_range2", undefined, true)),
|
|
1421
|
+
fromBinIdBuffer,
|
|
1422
|
+
toBinIdBuffer,
|
|
1423
|
+
bpsToRemoveBuffer,
|
|
1424
|
+
remainingAccountsInfoSerialized,
|
|
1425
|
+
]);
|
|
1426
|
+
// Build raw Meteora instruction
|
|
1427
|
+
// Account order from IDL: position, lbPair, binArrayBitmapExtension, userTokenX, userTokenY,
|
|
1428
|
+
// reserveX, reserveY, tokenXMint, tokenYMint, sender, tokenXProgram, tokenYProgram,
|
|
1429
|
+
// memoProgram, eventAuthority, program + bin arrays as remaining accounts + transfer hook accounts
|
|
1430
|
+
const meteoraIx = new web3.TransactionInstruction({
|
|
1431
|
+
data: instructionData,
|
|
1432
|
+
keys: [
|
|
1433
|
+
{ pubkey: position, isSigner: false, isWritable: true }, // position (Mutable)
|
|
1434
|
+
{ pubkey: lbPair, isSigner: false, isWritable: true }, // lbPair (Mutable)
|
|
1435
|
+
{ pubkey: binArrayBitmapExtension, isSigner: false, isWritable: true }, // binArrayBitmapExtension (optional, writable)
|
|
1436
|
+
{ pubkey: userTokenX, isSigner: false, isWritable: true }, // userTokenX (Mutable)
|
|
1437
|
+
{ pubkey: userTokenY, isSigner: false, isWritable: true }, // userTokenY (Mutable)
|
|
1438
|
+
{ pubkey: reserveX, isSigner: false, isWritable: true }, // reserveX (Mutable)
|
|
1439
|
+
{ pubkey: reserveY, isSigner: false, isWritable: true }, // reserveY (Mutable)
|
|
1440
|
+
{ pubkey: tokenXMint, isSigner: false, isWritable: false }, // tokenXMint
|
|
1441
|
+
{ pubkey: tokenYMint, isSigner: false, isWritable: false }, // tokenYMint
|
|
1442
|
+
{ pubkey: userPda, isSigner: false, isWritable: false }, // sender (Signer - signed by IYF Main)
|
|
1443
|
+
{ pubkey: tokenXProgram, isSigner: false, isWritable: false }, // tokenXProgram
|
|
1444
|
+
{ pubkey: tokenYProgram, isSigner: false, isWritable: false }, // tokenYProgram
|
|
1445
|
+
{ pubkey: addresses_1.MEMO_PROGRAM, isSigner: false, isWritable: false }, // memoProgram
|
|
1446
|
+
{ pubkey: METEORA_EVENT_AUTHORITY, isSigner: false, isWritable: false }, // eventAuthority
|
|
1447
|
+
{ pubkey: METEORA_DLMM_PROGRAM, isSigner: false, isWritable: false }, // program
|
|
1448
|
+
// Bin arrays as remaining accounts
|
|
1449
|
+
...binArrays.map(pubkey => ({ pubkey, isSigner: false, isWritable: true })),
|
|
1450
|
+
// Transfer hook accounts (if any) - order must match slices order
|
|
1451
|
+
...((_c = remainingAccountsInfoParam === null || remainingAccountsInfoParam === void 0 ? void 0 : remainingAccountsInfoParam.accounts) !== null && _c !== void 0 ? _c : []),
|
|
1452
|
+
],
|
|
1453
|
+
programId: METEORA_DLMM_PROGRAM,
|
|
1454
|
+
});
|
|
1455
|
+
return yield this.ix.iyfMain.meteoraDynamicCpi({
|
|
1456
|
+
connection,
|
|
1457
|
+
userWallet,
|
|
1458
|
+
meteoraIx,
|
|
1459
|
+
});
|
|
1460
|
+
});
|
|
1461
|
+
}
|
|
1462
|
+
/**
|
|
1463
|
+
* Close an empty Meteora DLMM position via IYF Main's meteora dynamic CPI.
|
|
1464
|
+
*
|
|
1465
|
+
* This instruction closes a position and returns the rent to the user.
|
|
1466
|
+
* The position must be empty (no liquidity) for this to succeed.
|
|
1467
|
+
*
|
|
1468
|
+
* @param params - ClosePosition2 parameters
|
|
1469
|
+
* @returns TransactionInstruction for the close_position2 operation
|
|
1470
|
+
*/
|
|
1471
|
+
closePosition2(_a) {
|
|
1472
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, userWallet, position, }) {
|
|
1473
|
+
// Initialize Anchor
|
|
1474
|
+
anchor_1.Anchor.initialize(connection);
|
|
1475
|
+
// Generate user PDA
|
|
1476
|
+
const userPda = (0, functions_1.generateUserPda)(userWallet);
|
|
1477
|
+
// Build instruction data: sighash only (no args)
|
|
1478
|
+
const instructionData = Buffer.from(util.sighash("close_position2", undefined, true));
|
|
1479
|
+
// Build raw Meteora instruction
|
|
1480
|
+
// Account order from IDL: position, sender, rentReceiver, eventAuthority, program
|
|
1481
|
+
const meteoraIx = new web3.TransactionInstruction({
|
|
1482
|
+
data: instructionData,
|
|
1483
|
+
keys: [
|
|
1484
|
+
{ pubkey: position, isSigner: false, isWritable: true }, // position (Mutable)
|
|
1485
|
+
{ pubkey: userPda, isSigner: false, isWritable: false }, // sender (Signer - signed by IYF Main)
|
|
1486
|
+
{ pubkey: userWallet, isSigner: false, isWritable: true }, // rentReceiver (Mutable)
|
|
1487
|
+
{ pubkey: METEORA_EVENT_AUTHORITY, isSigner: false, isWritable: false }, // eventAuthority
|
|
1488
|
+
{ pubkey: METEORA_DLMM_PROGRAM, isSigner: false, isWritable: false }, // program
|
|
1489
|
+
],
|
|
1490
|
+
programId: METEORA_DLMM_PROGRAM,
|
|
1491
|
+
});
|
|
1492
|
+
return yield this.ix.iyfMain.meteoraDynamicCpi({
|
|
1493
|
+
connection,
|
|
1494
|
+
userWallet,
|
|
1495
|
+
meteoraIx,
|
|
1496
|
+
});
|
|
1497
|
+
});
|
|
1498
|
+
}
|
|
1499
|
+
/**
|
|
1500
|
+
* Close a Meteora DLMM position only if it is empty via IYF Main's meteora dynamic CPI.
|
|
1501
|
+
*
|
|
1502
|
+
* This instruction is similar to closePosition2 but will fail silently if the position
|
|
1503
|
+
* still has liquidity. This is useful for safely attempting to close a position without
|
|
1504
|
+
* knowing its current state.
|
|
1505
|
+
*
|
|
1506
|
+
* @param params - ClosePositionIfEmpty parameters
|
|
1507
|
+
* @returns TransactionInstruction for the close_position_if_empty operation
|
|
1508
|
+
*/
|
|
1509
|
+
closePositionIfEmpty(_a) {
|
|
1510
|
+
return __awaiter(this, arguments, void 0, function* ({ connection, userWallet, position, }) {
|
|
1511
|
+
// Initialize Anchor
|
|
1512
|
+
anchor_1.Anchor.initialize(connection);
|
|
1513
|
+
// Generate user PDA
|
|
1514
|
+
const userPda = (0, functions_1.generateUserPda)(userWallet);
|
|
1515
|
+
// Build instruction data: sighash only (no args)
|
|
1516
|
+
const instructionData = Buffer.from(util.sighash("close_position_if_empty"));
|
|
1517
|
+
// Build raw Meteora instruction
|
|
1518
|
+
// Account order from IDL: position, sender, rentReceiver, eventAuthority, program
|
|
1519
|
+
const meteoraIx = new web3.TransactionInstruction({
|
|
1520
|
+
data: instructionData,
|
|
1521
|
+
keys: [
|
|
1522
|
+
{ pubkey: position, isSigner: false, isWritable: true }, // position (Mutable)
|
|
1523
|
+
{ pubkey: userPda, isSigner: false, isWritable: false }, // sender (Signer - signed by IYF Main)
|
|
1524
|
+
{ pubkey: userWallet, isSigner: false, isWritable: true }, // rentReceiver (Mutable)
|
|
1525
|
+
{ pubkey: METEORA_EVENT_AUTHORITY, isSigner: false, isWritable: false }, // eventAuthority
|
|
1526
|
+
{ pubkey: METEORA_DLMM_PROGRAM, isSigner: false, isWritable: false }, // program
|
|
1527
|
+
],
|
|
1528
|
+
programId: METEORA_DLMM_PROGRAM,
|
|
1529
|
+
});
|
|
1530
|
+
return yield this.ix.iyfMain.meteoraDynamicCpi({
|
|
1531
|
+
connection,
|
|
1532
|
+
userWallet,
|
|
1533
|
+
meteoraIx,
|
|
1534
|
+
});
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1020
1537
|
}
|
|
1021
1538
|
exports.MeteoraDlmmIxGenerator = MeteoraDlmmIxGenerator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/meteora/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./liquidityStrategy"), exports);
|