@meteora-ag/dlmm 1.4.0 → 1.4.1
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/README.md +147 -120
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,35 +52,35 @@ const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
|
|
|
52
52
|
|
|
53
53
|
```ts
|
|
54
54
|
const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
const minBinId = activeBin.binId - TOTAL_RANGE_INTERVAL;
|
|
56
|
+
const maxBinId = activeBin.binId + TOTAL_RANGE_INTERVAL;
|
|
57
|
+
|
|
58
|
+
const totalXAmount = new BN(100 * 10 ** baseMint.decimals);
|
|
59
|
+
const totalYAmount = autoFillYByStrategy(
|
|
60
|
+
activeBin.binId,
|
|
61
|
+
dlmmPool.lbPair.binStep,
|
|
62
|
+
totalXAmount,
|
|
63
|
+
activeBin.xAmount,
|
|
64
|
+
activeBin.yAmount,
|
|
65
|
+
minBinId,
|
|
66
|
+
maxBinId,
|
|
67
|
+
StrategyType.Spot // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
68
|
+
);
|
|
69
|
+
const newBalancePosition = new Keypair();
|
|
57
70
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
// Create Position
|
|
72
|
+
const createPositionTx =
|
|
73
|
+
await dlmmPool.initializePositionAndAddLiquidityByStrategy({
|
|
74
|
+
positionPubKey: newBalancePosition.publicKey,
|
|
75
|
+
user: user.publicKey,
|
|
62
76
|
totalXAmount,
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
// Create Position
|
|
72
|
-
const createPositionTx =
|
|
73
|
-
await dlmmPool.initializePositionAndAddLiquidityByStrategy({
|
|
74
|
-
positionPubKey: newBalancePosition.publicKey,
|
|
75
|
-
user: user.publicKey,
|
|
76
|
-
totalXAmount,
|
|
77
|
-
totalYAmount,
|
|
78
|
-
strategy: {
|
|
79
|
-
maxBinId,
|
|
80
|
-
minBinId,
|
|
81
|
-
strategyType: StrategyType.Spot, // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
82
|
-
},
|
|
83
|
-
});
|
|
77
|
+
totalYAmount,
|
|
78
|
+
strategy: {
|
|
79
|
+
maxBinId,
|
|
80
|
+
minBinId,
|
|
81
|
+
strategyType: StrategyType.Spot, // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
84
|
|
|
85
85
|
try {
|
|
86
86
|
const createBalancePositionTxHash = await sendAndConfirmTransaction(
|
|
@@ -92,30 +92,31 @@ try {
|
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
- Create Imbalance Position
|
|
95
|
+
|
|
95
96
|
```ts
|
|
96
97
|
const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
98
|
+
const minBinId = activeBin.binId - TOTAL_RANGE_INTERVAL;
|
|
99
|
+
const maxBinId = activeBin.binId + TOTAL_RANGE_INTERVAL;
|
|
100
|
+
|
|
101
|
+
const totalXAmount = new BN(100 * 10 ** baseMint.decimals);
|
|
102
|
+
const totalYAmount = new BN(0.5 * 10 ** 9); // SOL
|
|
103
|
+
const newImbalancePosition = new Keypair();
|
|
104
|
+
|
|
105
|
+
// Create Position
|
|
106
|
+
const createPositionTx =
|
|
107
|
+
await dlmmPool.initializePositionAndAddLiquidityByStrategy({
|
|
108
|
+
positionPubKey: newImbalancePosition.publicKey,
|
|
109
|
+
user: user.publicKey,
|
|
110
|
+
totalXAmount,
|
|
111
|
+
totalYAmount,
|
|
112
|
+
strategy: {
|
|
113
|
+
maxBinId,
|
|
114
|
+
minBinId,
|
|
115
|
+
strategyType: StrategyType.Spot, // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
try {
|
|
119
120
|
const createBalancePositionTxHash = await sendAndConfirmTransaction(
|
|
120
121
|
connection,
|
|
121
122
|
createPositionTx,
|
|
@@ -128,34 +129,34 @@ const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
|
|
|
128
129
|
|
|
129
130
|
```ts
|
|
130
131
|
const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const totalXAmount = new BN(100 * 10 ** baseMint.decimals);
|
|
135
|
-
const totalYAmount = new BN(0);
|
|
136
|
-
const newOneSidePosition = new Keypair();
|
|
137
|
-
|
|
138
|
-
// Create Position
|
|
139
|
-
const createPositionTx =
|
|
140
|
-
await dlmmPool.initializePositionAndAddLiquidityByStrategy({
|
|
141
|
-
positionPubKey: newOneSidePosition.publicKey,
|
|
142
|
-
user: user.publicKey,
|
|
143
|
-
totalXAmount,
|
|
144
|
-
totalYAmount,
|
|
145
|
-
strategy: {
|
|
146
|
-
maxBinId,
|
|
147
|
-
minBinId,
|
|
148
|
-
strategyType: StrategyType.Spot, // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
149
|
-
},
|
|
150
|
-
});
|
|
132
|
+
const minBinId = activeBin.binId;
|
|
133
|
+
const maxBinId = activeBin.binId + TOTAL_RANGE_INTERVAL * 2;
|
|
151
134
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
135
|
+
const totalXAmount = new BN(100 * 10 ** baseMint.decimals);
|
|
136
|
+
const totalYAmount = new BN(0);
|
|
137
|
+
const newOneSidePosition = new Keypair();
|
|
138
|
+
|
|
139
|
+
// Create Position
|
|
140
|
+
const createPositionTx =
|
|
141
|
+
await dlmmPool.initializePositionAndAddLiquidityByStrategy({
|
|
142
|
+
positionPubKey: newOneSidePosition.publicKey,
|
|
143
|
+
user: user.publicKey,
|
|
144
|
+
totalXAmount,
|
|
145
|
+
totalYAmount,
|
|
146
|
+
strategy: {
|
|
147
|
+
maxBinId,
|
|
148
|
+
minBinId,
|
|
149
|
+
strategyType: StrategyType.Spot, // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
const createOneSidePositionTxHash = await sendAndConfirmTransaction(
|
|
155
|
+
connection,
|
|
156
|
+
createPositionTx,
|
|
157
|
+
[user, newOneSidePosition]
|
|
158
|
+
);
|
|
159
|
+
} catch (error) {}
|
|
159
160
|
```
|
|
160
161
|
|
|
161
162
|
- Get list of positions
|
|
@@ -171,33 +172,33 @@ const binData = userPositions[0].positionData.positionBinData;
|
|
|
171
172
|
|
|
172
173
|
```ts
|
|
173
174
|
const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
|
|
174
|
-
|
|
175
|
-
|
|
175
|
+
const minBinId = activeBin.binId - TOTAL_RANGE_INTERVAL;
|
|
176
|
+
const maxBinId = activeBin.binId + TOTAL_RANGE_INTERVAL;
|
|
177
|
+
|
|
178
|
+
const totalXAmount = new BN(100 * 10 ** baseMint.decimals);
|
|
179
|
+
const totalYAmount = autoFillYByStrategy(
|
|
180
|
+
activeBin.binId,
|
|
181
|
+
dlmmPool.lbPair.binStep,
|
|
182
|
+
totalXAmount,
|
|
183
|
+
activeBin.xAmount,
|
|
184
|
+
activeBin.yAmount,
|
|
185
|
+
minBinId,
|
|
186
|
+
maxBinId,
|
|
187
|
+
StrategyType.Spot // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
188
|
+
);
|
|
176
189
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
minBinId,
|
|
190
|
+
// Add Liquidity to existing position
|
|
191
|
+
const addLiquidityTx = await dlmmPool.addLiquidityByStrategy({
|
|
192
|
+
positionPubKey: newBalancePosition.publicKey,
|
|
193
|
+
user: user.publicKey,
|
|
194
|
+
totalXAmount,
|
|
195
|
+
totalYAmount,
|
|
196
|
+
strategy: {
|
|
185
197
|
maxBinId,
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const addLiquidityTx = await dlmmPool.addLiquidityByStrategy({
|
|
191
|
-
positionPubKey: newBalancePosition.publicKey,
|
|
192
|
-
user: user.publicKey,
|
|
193
|
-
totalXAmount,
|
|
194
|
-
totalYAmount,
|
|
195
|
-
strategy: {
|
|
196
|
-
maxBinId,
|
|
197
|
-
minBinId,
|
|
198
|
-
strategyType: StrategyType.Spot, // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
199
|
-
},
|
|
200
|
-
});
|
|
198
|
+
minBinId,
|
|
199
|
+
strategyType: StrategyType.Spot, // can be StrategyType.Spot, StrategyType.BidAsk, StrategyType.Curve
|
|
200
|
+
},
|
|
201
|
+
});
|
|
201
202
|
|
|
202
203
|
try {
|
|
203
204
|
const addLiquidityTxHash = await sendAndConfirmTransaction(
|
|
@@ -242,6 +243,27 @@ try {
|
|
|
242
243
|
} catch (error) {}
|
|
243
244
|
```
|
|
244
245
|
|
|
246
|
+
- Claim Fee
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
async function claimFee(dlmmPool: DLMM) {
|
|
250
|
+
const claimFeeTxs = await dlmmPool.claimAllSwapFee({
|
|
251
|
+
owner: user.publicKey,
|
|
252
|
+
positions: userPositions,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
try {
|
|
256
|
+
for (const claimFeeTx of claimFeeTxs) {
|
|
257
|
+
const claimFeeTxHash = await sendAndConfirmTransaction(
|
|
258
|
+
connection,
|
|
259
|
+
claimFeeTx,
|
|
260
|
+
[user]
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
} catch (error) {}
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
245
267
|
- Close Position
|
|
246
268
|
|
|
247
269
|
```ts
|
|
@@ -251,12 +273,12 @@ const closePositionTx = await dlmmPool.closePosition({
|
|
|
251
273
|
});
|
|
252
274
|
|
|
253
275
|
try {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
276
|
+
const closePositionTxHash = await sendAndConfirmTransaction(
|
|
277
|
+
connection,
|
|
278
|
+
closePositionTx,
|
|
279
|
+
[user],
|
|
280
|
+
{ skipPreflight: false, preflightCommitment: "singleGossip" }
|
|
281
|
+
);
|
|
260
282
|
} catch (error) {}
|
|
261
283
|
```
|
|
262
284
|
|
|
@@ -264,22 +286,27 @@ try {
|
|
|
264
286
|
|
|
265
287
|
```ts
|
|
266
288
|
const swapAmount = new BN(0.1 * 10 ** 9);
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
289
|
+
// Swap quote
|
|
290
|
+
const swapYtoX = true;
|
|
291
|
+
const binArrays = await dlmmPool.getBinArrayForSwap(swapYtoX);
|
|
292
|
+
|
|
293
|
+
const swapQuote = await dlmmPool.swapQuote(
|
|
294
|
+
swapAmount,
|
|
295
|
+
swapYtoX,
|
|
296
|
+
new BN(1),
|
|
297
|
+
binArrays
|
|
298
|
+
);
|
|
272
299
|
|
|
273
300
|
// Swap
|
|
274
301
|
const swapTx = await dlmmPool.swap({
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
302
|
+
inToken: dlmmPool.tokenX.publicKey,
|
|
303
|
+
binArraysPubkey: swapQuote.binArraysPubkey,
|
|
304
|
+
inAmount: swapAmount,
|
|
305
|
+
lbPair: dlmmPool.pubkey,
|
|
306
|
+
user: user.publicKey,
|
|
307
|
+
minOutAmount: swapQuote.minOutAmount,
|
|
308
|
+
outToken: dlmmPool.tokenY.publicKey,
|
|
309
|
+
});
|
|
283
310
|
|
|
284
311
|
try {
|
|
285
312
|
const swapTxHash = await sendAndConfirmTransaction(connection, swapTx, [
|