@meteora-ag/dlmm 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/README.md +230 -0
- package/dist/index.d.ts +5122 -0
- package/dist/index.js +8133 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +8133 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# DLMM SDK
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img align="center" src="https://vaults.mercurial.finance/icons/logo.svg" width="180" height="180" />
|
|
5
|
+
</p>
|
|
6
|
+
<br>
|
|
7
|
+
|
|
8
|
+
## Getting started
|
|
9
|
+
|
|
10
|
+
NPM: https://www.npmjs.com/package/@meteora-ag/dlmm-sdk-public
|
|
11
|
+
|
|
12
|
+
SDK: https://github.com/MeteoraAg/dlmm-sdk
|
|
13
|
+
|
|
14
|
+
<!-- Docs: https://docs.mercurial.finance/mercurial-dynamic-yield-infra/ -->
|
|
15
|
+
|
|
16
|
+
Discord: https://discord.com/channels/841152225564950528/864859354335412224
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
1. Install deps
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
npm i @meteora-ag/dlmm-sdk-public @coral-xyz/anchor @solana/web3.js
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
2. Initialize DLMM instance
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
const USDC_USDT_POOL = new PublicKey('ARwi1S4DaiTG5DX7S4M4ZsrXqpMD1MrTmbu9ue2tpmEq') // You can get your desired pool address from the API https://dlmm-api.meteora.ag/pair/all
|
|
30
|
+
const dlmmPool = await DLMM.create(connection, USDC_USDT_POOL, {
|
|
31
|
+
cluster: "devnet",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// If you need to create multiple, can consider using `createMultiple`
|
|
35
|
+
const dlmmPool = await DLMM.create(connection, [USDC_USDT_POOL, ...], {
|
|
36
|
+
cluster: "devnet",
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
3. To interact with the AmmImpl
|
|
42
|
+
|
|
43
|
+
- Get Active Bin
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const activeBin = await dlmmPool.getActiveBin();
|
|
47
|
+
const activeBinPriceLamport = activeBin.price;
|
|
48
|
+
const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
|
|
49
|
+
Number(activeBin.price)
|
|
50
|
+
);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
- Create Position
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
|
|
57
|
+
const bins = [activeBin.binId]; // Make sure bins is less than 70, as currently only support up to 70 bins for 1 position
|
|
58
|
+
for (
|
|
59
|
+
let i = activeBin.binId;
|
|
60
|
+
i < activeBin.binId + TOTAL_RANGE_INTERVAL / 2;
|
|
61
|
+
i++
|
|
62
|
+
) {
|
|
63
|
+
const rightNextBinId = i + 1;
|
|
64
|
+
const leftPrevBinId = activeBin.binId - (rightNextBinId - activeBin.binId);
|
|
65
|
+
bins.push(rightNextBinId);
|
|
66
|
+
bins.unshift(leftPrevBinId);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
|
|
70
|
+
Number(activeBin.price)
|
|
71
|
+
);
|
|
72
|
+
const totalXAmount = new BN(100);
|
|
73
|
+
const totalYAmount = totalXAmount.mul(new BN(Number(activeBinPricePerToken)));
|
|
74
|
+
|
|
75
|
+
// Get spot distribution (You can calculate with other strategy `calculateSpotDistribution`, `calculateNormalDistribution`)
|
|
76
|
+
const spotXYAmountDistribution = calculateSpotDistribution(
|
|
77
|
+
activeBin.binId,
|
|
78
|
+
bins
|
|
79
|
+
);
|
|
80
|
+
const newPosition = new Keypair();
|
|
81
|
+
const createPositionTx =
|
|
82
|
+
await dlmmPool.initializePositionAndAddLiquidityByWeight({
|
|
83
|
+
positionPubKey: newPosition.publicKey,
|
|
84
|
+
lbPairPubKey: dlmmPool.pubkey,
|
|
85
|
+
user: user.publicKey,
|
|
86
|
+
totalXAmount,
|
|
87
|
+
totalYAmount,
|
|
88
|
+
xYAmountDistribution: spotXYAmountDistribution,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
for (let tx of Array.isArray(createPositionTx)
|
|
93
|
+
? createPositionTx
|
|
94
|
+
: [createPositionTx]) {
|
|
95
|
+
const createPositionTxHash = await sendAndConfirmTransaction(
|
|
96
|
+
connection,
|
|
97
|
+
tx,
|
|
98
|
+
[user, newPosition]
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- Get list of positions
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const { userPositions } = await dlmmPool.getPositionsByUserAndLbPair(
|
|
108
|
+
user.publicKey
|
|
109
|
+
);
|
|
110
|
+
const binData = userPositions[0].positionData.positionBinData;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
- Add liquidity to existing position
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const addLiquidityTx = await dlmmPool.addLiquidityByWeight({
|
|
117
|
+
positionPubKey: userPositions[0].publicKey,
|
|
118
|
+
lbPairPubKey: dlmmPool.pubkey,
|
|
119
|
+
user: user.publicKey,
|
|
120
|
+
totalXAmount,
|
|
121
|
+
totalYAmount,
|
|
122
|
+
xYAmountDistribution: spotXYAmountDistribution,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
for (let tx of Array.isArray(addLiquidityTx)
|
|
127
|
+
? addLiquidityTx
|
|
128
|
+
: [addLiquidityTx]) {
|
|
129
|
+
const addLiquidityTxHash = await sendAndConfirmTransaction(connection, tx, [
|
|
130
|
+
user,
|
|
131
|
+
newPosition,
|
|
132
|
+
]);
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
- Remove Liquidity
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const binIdsToRemove = userPositions[0].positionData.positionBinData.map(
|
|
141
|
+
(bin) => bin.binId
|
|
142
|
+
);
|
|
143
|
+
const removeLiquidityTx = await dlmmPool.removeLiquidity({
|
|
144
|
+
position: userPositions[0].publicKey,
|
|
145
|
+
user: user.publicKey,
|
|
146
|
+
binIds: binIdsToRemove,
|
|
147
|
+
liquiditiesBpsToRemove: new Array(binIdsToRemove.length).fill(
|
|
148
|
+
new BN(100 * 100)
|
|
149
|
+
), // 100% (range from 0 to 100)
|
|
150
|
+
shouldClaimAndClose: true, // should claim swap fee and close position together
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
for (let tx of Array.isArray(removeLiquidityTx)
|
|
155
|
+
? removeLiquidityTx
|
|
156
|
+
: [removeLiquidityTx]) {
|
|
157
|
+
const removeLiquidityTxHash = await sendAndConfirmTransaction(
|
|
158
|
+
connection,
|
|
159
|
+
tx,
|
|
160
|
+
[user, newPosition]
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
- Swap
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
const swapAmount = new BN(100);
|
|
170
|
+
// Swap quote
|
|
171
|
+
const swapQuote = await dlmmPool.swapQuote(swapAmount, true, new BN(10));
|
|
172
|
+
|
|
173
|
+
// Swap
|
|
174
|
+
const swapTx = await dlmmPool.swap({
|
|
175
|
+
inToken: dlmmPool.tokenX.publicKey,
|
|
176
|
+
binArraysPubkey: swapQuote.binArraysPubkey,
|
|
177
|
+
inAmount: swapAmount,
|
|
178
|
+
lbPair: dlmmPool.pubkey,
|
|
179
|
+
user: user.publicKey,
|
|
180
|
+
minOutAmount: swapQuote.minOutAmount,
|
|
181
|
+
outToken: dlmmPool.tokenY.publicKey,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
const swapTxHash = await sendAndConfirmTransaction(connection, swapTx, [
|
|
186
|
+
user,
|
|
187
|
+
]);
|
|
188
|
+
} catch (error) {}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Static functions
|
|
192
|
+
|
|
193
|
+
| Function | Description | Return |
|
|
194
|
+
| ----------------------------- | ---------------------------------------------------------------------------------- | ------------------------------------ |
|
|
195
|
+
| `create` | Given the DLMM address, create an instance to access the state and functions | `Promise<DLMM>` |
|
|
196
|
+
| `createMultiple` | Given a list of DLMM addresses, create instances to access the state and functions | `Promise<Array<DLMM>>` |
|
|
197
|
+
| `getAllLbPairPositionsByUser` | Given a list of DLMM addresses, create instances to access the state and functions | `Promise<Map<string, PositionInfo>>` |
|
|
198
|
+
|
|
199
|
+
## DLMM instance functions
|
|
200
|
+
|
|
201
|
+
| Function | Description | Return |
|
|
202
|
+
| ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
|
|
203
|
+
| `refetchStates` | Update onchain state of DLMM instance. It's recommend to call this before interact with the program (Deposit/ Withdraw/ Swap) | `Promise<void>` |
|
|
204
|
+
| `getBinArrays` | Retrieves List of Bin Arrays | `Promise<BinArrayAccount[]>` |
|
|
205
|
+
| `getFeeInfo` | Retrieves LbPair's fee info including `base fee`, `protocol fee` & `max fee` | `FeeInfo` |
|
|
206
|
+
| `getDynamicFee` | Retrieves LbPair's dynamic fee | `Decimal` |
|
|
207
|
+
| `getBinsAroundActiveBin` | retrieves a specified number of bins to the left and right of the active bin and returns them along with the active bin ID. | `Promise<{ activeBin: number; bins: BinLiquidity[] }>` |
|
|
208
|
+
| `getBinsBetweenMinAndMaxPrice` | Retrieves a list of bins within a specified price | `Promise<{ activeBin: number; bins: BinLiquidity[] }>` |
|
|
209
|
+
| `getBinsBetweenLowerAndUpperBound` | retrieves a list of bins between a lower and upper bin ID and returns the active bin ID and the list of bins. | `Promise<{ activeBin: number; bins: BinLiquidity[] }>` |
|
|
210
|
+
| `toPricePerLamport` | Converts a real price of bin to lamport price | `string` |
|
|
211
|
+
| `fromPricePerLamport` | converts a price per lamport value to a real price of bin | `string` |
|
|
212
|
+
| `getActiveBin` | Retrieves the active bin ID and its corresponding price | `Promise<{ binId: number; price: string }>` |
|
|
213
|
+
| `getPriceOfBinByBinId` | Get the price of a bin based on its bin ID | `string` |
|
|
214
|
+
| `getBinIdFromPrice` | get bin ID based on a given price and a boolean flag indicating whether to round down or up. | `number` |
|
|
215
|
+
| `getPositionsByUserAndLbPair` | Retrieves positions by user and LB pair, including active bin and user positions. | `Promise<{ activeBin: { binId: any; price: string; }; userPositions: Array<Position>;}>` |
|
|
216
|
+
| `initializePositionAndAddLiquidityByWeight` | Initializes a position and adds liquidity | `Promise<Transaction\|Transaction[]>` |
|
|
217
|
+
| `addLiquidityByWeight` | Add liquidity to existing position | `Promise<Transaction\|Transaction[]>` |
|
|
218
|
+
| `removeLiquidity` | function is used to remove liquidity from a position, with the option to claim rewards and close the position. | `Promise<Transaction\|Transaction[]>` |
|
|
219
|
+
| `closePosition` | Closes a position | `Promise<Transaction\|Transaction[]>` |
|
|
220
|
+
| `swapQuote` | Quote for a swap | `SwapQuote` |
|
|
221
|
+
| `swap` | Swap token within the LbPair | `Promise<Transaction>` |
|
|
222
|
+
| `claimLMReward` | Claim rewards for a specific position owned by a specific owner | `Promise<Transaction>` |
|
|
223
|
+
| `claimAllLMRewards` | Claim all liquidity mining rewards for a given owner and their positions. | `Promise<Transaction[]>` |
|
|
224
|
+
| `claimSwapFee` | Claim swap fees for a specific position owned by a specific owner | `Promise<Transaction>` |
|
|
225
|
+
| `claimAllSwapFee` | Claim swap fees for multiple positions owned by a specific owner | `Promise<Transaction>` |
|
|
226
|
+
| `claimAllRewards` | Claim swap fees and LM rewards for multiple positions owned by a specific owner | `Promise<Transaction[]>` |
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
```
|