-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptimizer.js
More file actions
82 lines (72 loc) · 2.9 KB
/
optimizer.js
File metadata and controls
82 lines (72 loc) · 2.9 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
import { ARBITRAGE_CONSTANTS } from './utils/constants.js';
export class ArbitrageOptimizer {
/**
* @param {import('./utils/types.js').PoolMetrics[]} poolMetrics
* @param {import('./utils/types.js').NetworkConditions} networkConditions
*/
constructor(poolMetrics, networkConditions) {
this.poolMetrics = poolMetrics;
this.networkConditions = networkConditions;
}
/**
* Find optimal arbitrage route between pools
* @returns {Promise<import('./utils/types.js').ArbitrageRoute | null>}
*/
async findOptimalRoute() {
let bestRoute = null;
let maxProfit = 0;
// Compare all pool pairs
for (let i = 0; i < this.poolMetrics.length; i++) {
for (let j = i + 1; j < this.poolMetrics.length; j++) {
const pool1 = this.poolMetrics[i];
const pool2 = this.poolMetrics[j];
const priceDiff = Math.abs(pool1.price - pool2.price);
if (priceDiff === 0) continue;
// Calculate potential profit considering fees
const tradingFees = this.calculateTradingFees(pool1, pool2);
const networkFees = this.calculateNetworkFees();
const totalFees = tradingFees + networkFees;
// Estimate profit based on price difference
const baseAmount = Math.min(
pool1.activeLiquidity.solAmount,
pool2.activeLiquidity.solAmount
);
const estimatedProfit = (priceDiff * baseAmount) - totalFees;
if (estimatedProfit > maxProfit && estimatedProfit > ARBITRAGE_CONSTANTS.MIN_PROFIT_THRESHOLD) {
maxProfit = estimatedProfit;
bestRoute = {
pool1: pool1.address,
pool2: pool2.address,
expectedProfit: estimatedProfit,
fees: {
tradingFees,
networkFees
}
};
}
}
}
return bestRoute;
}
/**
* Calculate trading fees for a route
* @param {import('./utils/types.js').PoolMetrics} pool1
* @param {import('./utils/types.js').PoolMetrics} pool2
* @returns {number}
*/
calculateTradingFees(pool1, pool2) {
// Combined fees from both pools
const pool1Fees = pool1.fees.baseFee + pool1.fees.hostFee;
const pool2Fees = pool2.fees.baseFee + pool2.fees.hostFee;
return pool1Fees + pool2Fees;
}
/**
* Calculate network fees including priority fees
* @returns {number}
*/
calculateNetworkFees() {
// Base network fee + priority fee based on congestion
const baseFee = 5000; // Base fee in lamports
return baseFee + this.networkConditions.recommendedPriorityFee;
}
}