-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathmeteora.ts
342 lines (298 loc) · 12.9 KB
/
meteora.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { Solana } from '../../chains/solana/solana';
import { PublicKey } from '@solana/web3.js';
import DLMM, { getPriceOfBinByBinId } from '@meteora-ag/dlmm';
import { MeteoraConfig } from './meteora.config';
import { logger } from '../../services/logger';
import { convertDecimals } from '../../services/base';
import { MeteoraPoolInfo, PositionInfo, BinLiquidity } from '../../services/clmm-interfaces';
import { LbPair } from '@meteora-ag/dlmm';
import { percentRegexp } from '../../services/config-manager-v2';
export class Meteora {
private static _instances: { [name: string]: Meteora };
private static readonly MAX_BINS = 70;
private solana: Solana;
public config: MeteoraConfig.NetworkConfig;
private dlmmPools: Map<string, DLMM> = new Map();
private dlmmPoolPromises: Map<string, Promise<DLMM>> = new Map();
private constructor() {
this.config = MeteoraConfig.config;
this.solana = null; // Initialize as null since we need to await getInstance
}
/** Gets singleton instance of Meteora */
public static async getInstance(network: string): Promise<Meteora> {
if (!Meteora._instances) {
Meteora._instances = {};
}
if (!Meteora._instances[network]) {
const instance = new Meteora();
await instance.init(network);
Meteora._instances[network] = instance;
}
return Meteora._instances[network];
}
/** Initializes Meteora instance */
private async init(network: string) {
try {
this.solana = await Solana.getInstance(network); // Get initialized Solana instance
this.dlmmPools = new Map();
this.dlmmPoolPromises = new Map();
logger.info("Initializing Meteora");
} catch (error) {
logger.error("Failed to initialize Meteora:", error);
throw error;
}
}
/** Gets DLMM pool instance */
async getDlmmPool(poolAddress: string): Promise<DLMM> {
// Check if we already have the pool instance
if (this.dlmmPools.has(poolAddress)) {
return this.dlmmPools.get(poolAddress);
}
// Check if we have a pending promise for this pool
if (this.dlmmPoolPromises.has(poolAddress)) {
return this.dlmmPoolPromises.get(poolAddress);
}
// Create a promise for the DLMM instance
const dlmmPoolPromise = DLMM.create(
this.solana.connection,
new PublicKey(poolAddress),
{ cluster: this.solana.network as any }
).then(async (dlmmPool) => {
await dlmmPool.refetchStates();
this.dlmmPools.set(poolAddress, dlmmPool);
this.dlmmPoolPromises.delete(poolAddress);
return dlmmPool;
});
this.dlmmPoolPromises.set(poolAddress, dlmmPoolPromise);
return dlmmPoolPromise;
}
/** Gets Meteora pools with optional token filtering */
async getPools(
limit: number = 100,
tokenMintA?: string,
tokenMintB?: string
): Promise<{ publicKey: PublicKey; account: LbPair }[]> {
const timeoutMs = 10000;
try {
logger.info('Fetching Meteora pools...');
const lbPairsPromise = DLMM.getLbPairs(this.solana.connection, {
cluster: this.solana.network as any
});
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('getPools timed out')), timeoutMs);
});
let lbPairs = (await Promise.race([lbPairsPromise, timeoutPromise])) as {
publicKey: PublicKey;
account: LbPair
}[];
// Only apply token filtering if tokens are provided
if (tokenMintA && tokenMintB) {
lbPairs = lbPairs.filter(pair => {
const tokenXMint = pair.account.tokenXMint.toBase58();
const tokenYMint = pair.account.tokenYMint.toBase58();
return (tokenXMint === tokenMintA && tokenYMint === tokenMintB) ||
(tokenXMint === tokenMintB && tokenYMint === tokenMintA);
});
} else if (tokenMintA) {
lbPairs = lbPairs.filter(pair => {
const tokenXMint = pair.account.tokenXMint.toBase58();
const tokenYMint = pair.account.tokenYMint.toBase58();
return tokenXMint === tokenMintA || tokenYMint === tokenMintA;
});
}
const returnLength = Math.min(lbPairs.length, limit);
logger.info(`Found ${lbPairs.length} matching Meteora pools, returning first ${returnLength}`);
// console.log(JSON.stringify(lbPairs[0], null, 2));
return lbPairs.slice(0, returnLength);
} catch (error) {
logger.error('Failed to fetch Meteora pools:', error);
return []; // Return empty array instead of throwing
}
}
/** Gets comprehensive pool information */
async getPoolInfo(poolAddress: string): Promise<MeteoraPoolInfo | null> {
try {
const dlmmPool = await this.getDlmmPool(poolAddress);
if (!dlmmPool) {
logger.error(`Pool not found: ${poolAddress}`);
return null;
}
const [reserveXBalance, reserveYBalance] = await Promise.all([
this.solana.connection.getTokenAccountBalance(dlmmPool.lbPair.reserveX),
this.solana.connection.getTokenAccountBalance(dlmmPool.lbPair.reserveY)
]);
const feeInfo = await dlmmPool.getFeeInfo();
const activeBin = await dlmmPool.getActiveBin();
const dynamicFee = dlmmPool.getDynamicFee();
if (!activeBin || !activeBin.price || !activeBin.pricePerToken) {
logger.error(`Invalid active bin data for pool: ${poolAddress}`);
return null;
}
return {
address: poolAddress,
baseTokenAddress: dlmmPool.tokenX.publicKey.toBase58(),
quoteTokenAddress: dlmmPool.tokenY.publicKey.toBase58(),
binStep: dlmmPool.lbPair.binStep,
feePct: Number(feeInfo.baseFeeRatePercentage),
dynamicFeePct: Number(dynamicFee),
price: Number(activeBin.pricePerToken),
baseTokenAmount: reserveXBalance.value.uiAmount,
quoteTokenAmount: reserveYBalance.value.uiAmount,
activeBinId: activeBin.binId,
minBinId: dlmmPool.lbPair.parameters.minBinId,
maxBinId: dlmmPool.lbPair.parameters.maxBinId,
bins: await this.getPoolLiquidity(poolAddress),
};
} catch (error) {
logger.error(`Error getting pool info for ${poolAddress}:`, error);
return null;
}
}
async getPoolLiquidity(poolAddress: string): Promise<BinLiquidity[]> {
const dlmmPool = await this.getDlmmPool(poolAddress);
if (!dlmmPool) {
throw new Error(`Pool not found: ${poolAddress}`);
}
const binData = await dlmmPool.getBinsAroundActiveBin(
Meteora.MAX_BINS - 1,
Meteora.MAX_BINS - 1
);
return binData.bins.map(bin => ({
binId: bin.binId,
price: Number(bin.pricePerToken),
baseTokenAmount: Number(convertDecimals(bin.xAmount, dlmmPool.tokenX.decimal)),
quoteTokenAmount: Number(convertDecimals(bin.yAmount, dlmmPool.tokenY.decimal))
}));
}
/** Gets all positions for a pool */
async getPositionsInPool(poolAddress: string, wallet: PublicKey): Promise<PositionInfo[]> {
const dlmmPool = await this.getDlmmPool(poolAddress);
if (!dlmmPool) {
throw new Error(`Pool not found: ${poolAddress}`);
}
const { userPositions } = await dlmmPool.getPositionsByUserAndLbPair(wallet);
const activeBin = await dlmmPool.getActiveBin();
if (!activeBin || !activeBin.price || !activeBin.pricePerToken) {
throw new Error(`Invalid active bin data for pool: ${poolAddress}`);
}
return userPositions.map(({ publicKey, positionData }) => {
// Get prices from bin IDs
const lowerPrice = getPriceOfBinByBinId(positionData.lowerBinId, dlmmPool.lbPair.binStep);
const upperPrice = getPriceOfBinByBinId(positionData.upperBinId, dlmmPool.lbPair.binStep);
// Adjust for decimal difference (tokenX.decimal - tokenY.decimal)
const decimalDiff = dlmmPool.tokenX.decimal - dlmmPool.tokenY.decimal; // 9 - 6 = 3
const adjustmentFactor = Math.pow(10, decimalDiff);
const adjustedLowerPrice = Number(lowerPrice) * adjustmentFactor;
const adjustedUpperPrice = Number(upperPrice) * adjustmentFactor;
return {
address: publicKey.toString(),
poolAddress,
baseTokenAddress: dlmmPool.tokenX.publicKey.toBase58(),
quoteTokenAddress: dlmmPool.tokenY.publicKey.toBase58(),
baseTokenAmount: Number(convertDecimals(positionData.totalXAmount, dlmmPool.tokenX.decimal)),
quoteTokenAmount: Number(convertDecimals(positionData.totalYAmount, dlmmPool.tokenY.decimal)),
baseFeeAmount: Number(convertDecimals(positionData.feeX, dlmmPool.tokenX.decimal)),
quoteFeeAmount: Number(convertDecimals(positionData.feeY, dlmmPool.tokenY.decimal)),
lowerBinId: positionData.lowerBinId,
upperBinId: positionData.upperBinId,
lowerPrice: adjustedLowerPrice,
upperPrice: adjustedUpperPrice,
price: Number(activeBin.pricePerToken),
};
});
}
/** Gets raw position data without parsing */
async getRawPosition(positionAddress: string, wallet: PublicKey) {
const allPositions = await DLMM.getAllLbPairPositionsByUser(
this.solana.connection,
wallet
);
const [matchingPosition] = Array.from(allPositions.values())
.map(position => ({
position: position.lbPairPositionsData.find(
lbPosition => lbPosition.publicKey.toBase58() === positionAddress
),
info: position
}))
.filter(x => x.position);
if (!matchingPosition) {
return null;
}
// console.log(matchingPosition);
return matchingPosition;
}
/** Gets position information */
async getPositionInfo(positionAddress: string, wallet: PublicKey): Promise<PositionInfo> {
const { position, info } = await this.getRawPosition(positionAddress, wallet);
if (!position) {
throw new Error('Position not found');
}
const dlmmPool = await this.getDlmmPool(info.publicKey.toBase58());
const activeBin = await dlmmPool.getActiveBin();
if (!activeBin || !activeBin.price || !activeBin.pricePerToken) {
throw new Error(`Invalid active bin data for pool: ${info.publicKey.toBase58()}`);
}
// Get prices from bin IDs
const lowerPrice = getPriceOfBinByBinId(position.positionData.lowerBinId, dlmmPool.lbPair.binStep);
const upperPrice = getPriceOfBinByBinId(position.positionData.upperBinId, dlmmPool.lbPair.binStep);
// Adjust for decimal difference (tokenX.decimal - tokenY.decimal)
const decimalDiff = dlmmPool.tokenX.decimal - dlmmPool.tokenY.decimal;
const adjustmentFactor = Math.pow(10, decimalDiff);
const adjustedLowerPrice = Number(lowerPrice) * adjustmentFactor;
const adjustedUpperPrice = Number(upperPrice) * adjustmentFactor;
return {
address: positionAddress,
poolAddress: info.publicKey.toString(),
baseTokenAddress: dlmmPool.tokenX.publicKey.toBase58(),
quoteTokenAddress: dlmmPool.tokenY.publicKey.toBase58(),
baseTokenAmount: Number(convertDecimals(position.positionData.totalXAmount, dlmmPool.tokenX.decimal)),
quoteTokenAmount: Number(convertDecimals(position.positionData.totalYAmount, dlmmPool.tokenY.decimal)),
baseFeeAmount: Number(convertDecimals(position.positionData.feeX, dlmmPool.tokenX.decimal)),
quoteFeeAmount: Number(convertDecimals(position.positionData.feeY, dlmmPool.tokenY.decimal)),
lowerBinId: position.positionData.lowerBinId,
upperBinId: position.positionData.upperBinId,
lowerPrice: adjustedLowerPrice,
upperPrice: adjustedUpperPrice,
price: Number(activeBin.pricePerToken),
};
}
// async getBinLiquidity(poolAddress: string, binId: number): Promise<{xAmount: number, yAmount: number}> {
// const dlmmPool = await this.getDlmmPool(poolAddress);
// if (!dlmmPool) {
// throw new Error(`Pool not found: ${poolAddress}`);
// }
// return dlmmPool.getBinLiquidity(binId);
// }
/** Converts price range to bin IDs */
async getPriceToBinIds(
poolAddress: string,
lowerPrice: number,
upperPrice: number,
padBins: number = 1
): Promise<{minBinId: number, maxBinId: number}> {
const dlmmPool = await this.getDlmmPool(poolAddress);
if (!dlmmPool) {
throw new Error(`Pool not found: ${poolAddress}`);
}
const lowerPricePerLamport = dlmmPool.toPricePerLamport(lowerPrice);
const upperPricePerLamport = dlmmPool.toPricePerLamport(upperPrice);
const minBinId = dlmmPool.getBinIdFromPrice(Number(lowerPricePerLamport), true) - padBins;
const maxBinId = dlmmPool.getBinIdFromPrice(Number(upperPricePerLamport), false) + padBins;
if (maxBinId - minBinId > Meteora.MAX_BINS) {
throw new Error(`Position range too wide. Maximum ${Meteora.MAX_BINS} bins allowed`);
}
return { minBinId, maxBinId };
}
/** Gets slippage percentage from config */
getSlippagePct(): number {
const allowedSlippage = this.config.allowedSlippage;
const nd = allowedSlippage.match(percentRegexp);
let slippage = 0.0;
if (nd) {
slippage = Number(nd[1]) / Number(nd[2]);
} else {
logger.error('Failed to parse slippage value:', allowedSlippage);
}
return slippage * 100;
}
}