-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjupiter.ts
66 lines (57 loc) · 2.04 KB
/
jupiter.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
type SwapMode = "ExactIn" | "ExactOut";
export async function getQuote(
inputMint: string,
outputMint: string,
amount: number,
slippageBps: number,
onlyDirectRouteParam?: boolean,
swapModeParam?: SwapMode,
excludeDex?: string[],
metisEndpointParam?: string
) {
const onlyDirectRoute = onlyDirectRouteParam || false;
const swapMode = swapModeParam || "ExactIn";
const metisEndpoint = metisEndpointParam || process.env.JUPITER_ENDPOINT;
const excludeDexes = excludeDex ? excludeDex.join(",") : "";
const response = await fetch(
`${metisEndpoint}/quote?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount}&slippageBps=${slippageBps}&swapMode=${swapMode}&onlyDirectRoutes=${onlyDirectRoute}` +
(excludeDexes ? `&excludeDexes=${excludeDexes}` : "")
);
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`Error fetching quote: ${response.statusText} (Status: ${response.status})
URL: ${response.url}
Response Body: ${errorBody}`);
}
const data = await response.json();
return data;
}
export async function jupMesure(TokenIn: string, TokenOut: string) {
const timeLimit = 400; // Adjusted time limit
const startTime = performance.now();
let currentAmount = 1000000000;
let priceStable = true;
let initialProfit = 0;
let quote = await getQuote(TokenIn, TokenOut, currentAmount, 0, false);
initialProfit = (Number(quote.outAmount) - Number(quote.inAmount)) / 1e9;
while (priceStable) {
const currentTime = performance.now();
if (currentTime - startTime > timeLimit) {
console.log("Time limit exceeded.");
return null;
}
const newQuote = await getQuote(TokenIn, TokenOut, currentAmount, 0, false);
const newProfit =
(Number(newQuote.outAmount) - Number(newQuote.inAmount)) / 1e9;
if (newProfit != initialProfit) {
console.log(
"self hosted jupiter price updated in : ",
performance.now() - startTime,
" ms"
);
// await pause(300);
// process.exit();
break;
}
}
}