-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.js
More file actions
127 lines (117 loc) · 4.26 KB
/
Copy pathutils.js
File metadata and controls
127 lines (117 loc) · 4.26 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
const { Connection, PublicKey } = require("@solana/web3.js");
const axios = require("axios");
const {
SOLANA_RPC_URL,
PRIORITY_FEE_PROVIDER_URL,
PRIORITY_FEE_PROVIDER_METHOD,
PRIORITY_FEE_ACCOUNT_KEYS,
PRIORITY_FEE_PERCENTILE,
PRIORITY_FEE_MIN_MICROLAMPORTS,
PRIORITY_FEE_MAX_MICROLAMPORTS,
} = require("./config");
const connection = new Connection(SOLANA_RPC_URL);
async function getTokenInfo(mint) {
const mintAccount = new PublicKey(mint);
const mintInfo = await connection.getParsedAccountInfo(mintAccount);
if (!mintInfo.value || !mintInfo.value.data || !mintInfo.value.data.parsed) {
throw new Error(`❌ Failed to fetch token info for mint: ${mint}`);
}
const { decimals } = mintInfo.value.data.parsed.info;
return { decimals };
}
async function getAveragePriorityFee() {
const priorityFees = await connection.getRecentPrioritizationFees();
if (priorityFees.length === 0) {
return { microLamports: 0, solAmount: 0 }; // No data
}
const recentFees = priorityFees.slice(-150); // Get fees from last 150 slots
const averageFee =
recentFees.reduce((sum, fee) => sum + fee.prioritizationFee, 0) /
recentFees.length;
const microLamports = Math.ceil(averageFee);
const solAmount = microLamports / 1e15; // micro-lamports -> lamports (/1e6) -> SOL (/1e9)
return { microLamports, solAmount };
}
function pickPercentile(values, percentile) {
if (!Array.isArray(values) || values.length === 0) return 0;
const sorted = values.slice().sort((a, b) => a - b);
const idx = Math.min(
sorted.length - 1,
Math.max(0, Math.floor((percentile / 100) * (sorted.length - 1)))
);
return sorted[idx];
}
async function getPercentilePriorityFee() {
const priorityFees = await connection.getRecentPrioritizationFees();
if (!priorityFees || priorityFees.length === 0) {
const floor = PRIORITY_FEE_MIN_MICROLAMPORTS;
return { microLamports: floor, solAmount: floor / 1e15 };
}
const recentFees = priorityFees
.slice(-150)
.map((f) => f.prioritizationFee)
.filter((v) => Number.isFinite(v) && v >= 0);
if (recentFees.length === 0) {
const floor = PRIORITY_FEE_MIN_MICROLAMPORTS;
return { microLamports: floor, solAmount: floor / 1e15 };
}
let picked = Math.ceil(pickPercentile(recentFees, PRIORITY_FEE_PERCENTILE));
if (Number.isFinite(PRIORITY_FEE_MIN_MICROLAMPORTS)) {
picked = Math.max(picked, PRIORITY_FEE_MIN_MICROLAMPORTS);
}
if (Number.isFinite(PRIORITY_FEE_MAX_MICROLAMPORTS)) {
picked = Math.min(picked, PRIORITY_FEE_MAX_MICROLAMPORTS);
}
return { microLamports: picked, solAmount: picked / 1e15 };
}
async function getProviderEstimatedPriorityFee(
percentileOverride,
accountKeysOverride
) {
if (!PRIORITY_FEE_PROVIDER_URL) return null;
try {
const effectivePercentile = Number.isFinite(percentileOverride)
? percentileOverride
: PRIORITY_FEE_PERCENTILE;
const effectiveAccountKeys =
Array.isArray(accountKeysOverride) && accountKeysOverride.length
? accountKeysOverride
: PRIORITY_FEE_ACCOUNT_KEYS.length
? PRIORITY_FEE_ACCOUNT_KEYS
: undefined;
const params = [
{ percentile: effectivePercentile, accountKeys: effectiveAccountKeys },
];
const body = {
jsonrpc: "2.0",
id: 1,
method: PRIORITY_FEE_PROVIDER_METHOD,
params,
};
const resp = await axios.post(PRIORITY_FEE_PROVIDER_URL, body, {
timeout: 2500,
headers: { "Content-Type": "application/json" },
});
const result = resp.data?.result;
if (!result) return null;
// Normalize common provider shapes
let microLamports = null;
if (typeof result === "number") microLamports = Math.ceil(result);
else if (result?.microLamports)
microLamports = Math.ceil(result.microLamports);
else if (result?.priorityFeeEstimate)
microLamports = Math.ceil(result.priorityFeeEstimate);
if (!Number.isFinite(microLamports)) return null;
microLamports = Math.max(microLamports, PRIORITY_FEE_MIN_MICROLAMPORTS);
microLamports = Math.min(microLamports, PRIORITY_FEE_MAX_MICROLAMPORTS);
return { microLamports, solAmount: microLamports / 1e15 };
} catch {
return null;
}
}
module.exports = {
getTokenInfo,
getAveragePriorityFee,
getPercentilePriorityFee,
getProviderEstimatedPriorityFee,
};