-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoolAnalyzer.js
More file actions
164 lines (140 loc) · 5.18 KB
/
poolAnalyzer.js
File metadata and controls
164 lines (140 loc) · 5.18 KB
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
import pkg from '@meteora-ag/dlmm';
const { DLMM } = pkg;
import { PublicKey } from '@solana/web3.js';
import Decimal from 'decimal.js';
import dotenv from 'dotenv';
dotenv.config();
// Correct Meteora DLMM Program ID on mainnet
const METEORA_PROGRAM_ID = new PublicKey('LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo');
export class PoolAnalyzer {
/**
* @param {Connection} connection - Solana connection
*/
constructor(connection) {
this.connection = connection;
}
/**
* Find all SOL-USDC pools on Meteora
* @returns {Promise<PublicKey[]>} Array of pool addresses
*/
async findSolUsdcPools() {
// Get all program accounts and filter for SOL-USDC pairs
const accounts = await this.connection.getProgramAccounts(METEORA_PROGRAM_ID, {
filters: [
// Add specific filters for SOL-USDC pairs later
]
});
return accounts.map(acc => acc.pubkey);
}
/**
* Analyze specific pool metrics
* @param {DLMM} dlmm - DLMM instance
* @returns {Promise<Object|null>}
*/
async analyzePool(dlmm) {
try {
const binStep = dlmm.lbPair.binStep;
const { baseFeeRate, hostFeeRate } = await dlmm.getFeeInfo();
const activeBins = await dlmm.getActiveBins();
// Calculate active liquidity
const activeLiquidity = this.calculateActiveLiquidity(activeBins);
// Get current price from weighted average of active bins
const price = this.calculateWeightedPrice(activeBins);
// Calculate TVL
const tvl = this.calculateTVL(activeLiquidity, price);
// Skip pools with insufficient TVL
if (tvl < 1000) { // MIN_POOL_TVL constant
return null;
}
return {
address: dlmm.lbPair.address.toString(),
binStep,
tvl,
activeLiquidity,
fees: {
baseFee: Number(baseFeeRate),
hostFee: Number(hostFeeRate)
},
price,
effectiveSpread: this.calculateEffectiveSpread(baseFeeRate, binStep)
};
} catch (error) {
console.error(`Error analyzing pool ${dlmm.lbPair.address}:`, error);
return null;
}
}
/**
* Calculate active liquidity from bins
* @param {any[]} activeBins - Array of active bin data
* @returns {Object} Active liquidity in both tokens
*/
calculateActiveLiquidity(activeBins) {
return activeBins.reduce((acc, bin) => {
acc.solAmount += BigInt(bin.amountX);
acc.usdcAmount += BigInt(bin.amountY);
return acc;
}, { solAmount: 0n, usdcAmount: 0n });
}
/**
* Calculate weighted average price from active bins
* @param {any[]} activeBins - Array of active bin data
* @returns {number} Weighted average price
*/
calculateWeightedPrice(activeBins) {
let totalWeight = new Decimal(0);
let weightedSum = new Decimal(0);
for (const bin of activeBins) {
const binLiquidity = new Decimal(bin.amountY.toString())
.div(new Decimal(10 ** 6)); // USDC decimals
const binPrice = new Decimal(bin.price.toString());
weightedSum = weightedSum.plus(binPrice.times(binLiquidity));
totalWeight = totalWeight.plus(binLiquidity);
}
return totalWeight.isZero() ? 0 : weightedSum.div(totalWeight).toNumber();
}
/**
* Calculate pool TVL in USD
* @param {Object} activeLiquidity - Active liquidity amounts
* @param {number} price - Current SOL price
* @returns {number} TVL in USD
*/
calculateTVL(activeLiquidity, price) {
const solValue = new Decimal(activeLiquidity.solAmount.toString())
.div(10 ** 9) // SOL decimals
.times(price);
const usdcValue = new Decimal(activeLiquidity.usdcAmount.toString())
.div(10 ** 6); // USDC decimals
return solValue.plus(usdcValue).toNumber();
}
/**
* Calculate effective spread based on fees and bin step
* @param {number} baseFee - Base fee rate
* @param {number} binStep - Bin step
* @returns {number} Effective spread
*/
calculateEffectiveSpread(baseFee, binStep) {
// Effective spread combines base fee and bin step impact
return Number(baseFee) + (binStep / 10000); // Simplified approximation
}
/**
* Analyze all SOL-USDC pools
* @returns {Promise<Array>}
*/
async analyzePools() {
const pools = await this.findSolUsdcPools();
const poolMetrics = [];
for (const poolAddress of pools) {
try {
const dlmm = await DLMM.create(this.connection, poolAddress);
const metrics = await this.analyzePool(dlmm);
if (metrics) {
poolMetrics.push(metrics);
}
} catch (error) {
console.error(`Error analyzing pool ${poolAddress}:`, error);
continue;
}
}
return poolMetrics;
}
}