-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
305 lines (232 loc) · 8.25 KB
/
utils.ts
File metadata and controls
305 lines (232 loc) · 8.25 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
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
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
import dotenv from 'dotenv';
import * as base58 from 'base-58';
import { PublicKey } from '@solana/web3.js';
dotenv.config({ path: path.resolve(__dirname, '../.env') });
// Initialize timer
export const timer = {
start: () => console.time('Pool detection'),
end: () => console.timeEnd('Pool detection')
};
// Configure colors
export const success = chalk.green.bold;
export const info = chalk.cyan;
export const warning = chalk.yellow;
export const error = chalk.red;
export const highlight = chalk.magentaBright;
export const RUG_CHECK_URL = process.env.RUG_CHECK_URL;
export const TOKEN_PRICE_URL = process.env.TOKEN_PRICE_URL;
export const SOL_PUBLIC_ADDRESS = "So11111111111111111111111111111111111111112";
const PUMPSWAP_TOKEN_URL= "https://swap-api.pump.fun/v1/pools/pair"
// (?)mintA=So11111111111111111111111111111111111111112&mintB=8vVvjJG4KZ4xhcUoa4koKhQbSr58PJSAXVnh7WM9pump&sort=liquidity&include_vol=true"
export interface MarketAccountData {
poolBump: number;
index: number;
creator: string;
baseMint: string;
quoteMint: string;
lpMint: string;
poolBaseTokenAccount: string;
poolQuoteTokenAccount: string;
lpSupply: string;
}
// Add new interface for fee estimation
export interface TradeFees {
buy: {
baseFee: number;
priorityFee: number;
totalFee: number;
};
sell: {
baseFee: number;
priorityFee: number;
totalFee: number;
};
}
// Update EnhancedMarketData interface
export interface EnhancedMarketData extends MarketAccountData {
creatorHoldings?: string;
freezeAuthority?: string;
price?: number;
detectedIn?: number;
fees?: TradeFees;
}
// Add these new interfaces
interface DashboardData {
mint: string;
price: number;
liquidity: number;
lpLockedPct: number;
topHolder: string;
holderPercentage: number;
freezeAuthority: string | null;
lastUpdated: Date;
risks: string[];
}
// Update the fee estimation constants and logic
export const REALISTIC_SWAP_CU = 300000; // Actual average for Jupiter swaps
export const MICRO_LAMPORTS_PER_CU = 10000; // 0.01 lamport/CU (1 lamport = 0.000000001 SOL)
export const WSS_ENDPOINT = process.env.SOLANA_NODE_WSS_ENDPOINT!;
export const RPC_ENDPOINT = process.env.PUBLIC_RPC_URL!;
export const PUMP_AMM_PROGRAM_ID = new PublicKey('pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA');
// Precomputed constants
export const MARKET_ACCOUNT_LENGTH = 211;
export const DISCRIMINATOR = Buffer.from([0xf1, 0x9a, 0x6d, 0x04, 0x11, 0xb1, 0x6d, 0xbc]);
export const MARKET_DISCRIMINATOR = base58.encode(DISCRIMINATOR);
export const QUOTE_MINT_SOL = 'So11111111111111111111111111111111111111112';
const fromRaw = (raw:any, decimals:number) => Number(raw) / 10 ** decimals;
export function getFormattedUTCTime(): string {
const now = new Date();
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const year = now.getUTCFullYear();
const month = monthNames[now.getUTCMonth()];
const day = String(now.getUTCDate()).padStart(2, '0');
const hours = String(now.getUTCHours()).padStart(2, '0');
const minutes = String(now.getUTCMinutes()).padStart(2, '0');
const seconds = String(now.getUTCSeconds()).padStart(2, '0');
return `${month} ${day}, ${year} ${hours}:${minutes}:${seconds} +UTC`;
}
export function formatNumberToKM(value: number): string {
if (value >= 1_000_000) {
return `${(value / 1_000_000).toFixed(1)} M`;
} else if (value >= 1_000) {
return `${(value / 1_000).toFixed(1)} K`;
} else {
return value.toFixed(1);
}
}
// 1st used
export async function fetchExistingMarketPubkeys(connection:any): Promise<Set<string>> {
const filters: any[] = [
{ dataSize: MARKET_ACCOUNT_LENGTH },
{ memcmp: { offset: 0, bytes: MARKET_DISCRIMINATOR } },
{ memcmp: { offset: 75, bytes: QUOTE_MINT_SOL } }
];
const accounts = await connection.getProgramAccounts(PUMP_AMM_PROGRAM_ID, { filters });
return new Set(accounts.map((account:any) => account.pubkey.toBase58()));
}
// 2nd used
export function parseMarketAccountData(data: Buffer): MarketAccountData | null {
try {
let offset = 8; // Skip discriminator
const poolBump = data.readUInt8(offset++);
const index = data.readUInt16LE(offset);
offset += 2;
const readPubkey = () => {
const pubkey = new PublicKey(data.subarray(offset, offset + 32));
offset += 32;
return pubkey.toBase58();
};
return {
poolBump,
index,
creator: readPubkey(),
baseMint: readPubkey(),
quoteMint: readPubkey(),
lpMint: readPubkey(),
poolBaseTokenAccount: readPubkey(),
poolQuoteTokenAccount: readPubkey(),
lpSupply: data.readBigUInt64LE(offset).toString()
};
} catch (error) {
console.error('Error parsing market account:', error);
return null;
}
}
/**
* Fetch and format Pump Swap pool info for token ↔ SOL.
*
* @param {string} tokenMint – the mint address of your token
* @returns {Promise<object>} – structured pool info
*/
export async function getPoolInfo(tokenMint:string) {
const trial = 10;
let pool = null;
for(let i=0;i<trial;i++){
const url = `https://swap-api.pump.fun/v1/pools/pair`
+ `?mintA=${tokenMint}`
+ `&mintB=${SOL_PUBLIC_ADDRESS}`
// + `&sort=liquidity`;
try{
const res = await fetch(url);
if (!res.ok) {
await new Promise((r) => setTimeout(r, 1000));
// console.error(`API error: ${res.status} ${res.statusText}`,res);
continue;
}
const [temp] = await res.json();
pool = temp;
if (!pool) {
console.error(`No pool found for token ${tokenMint}`,pool);
}
}catch(e){
console.error("FOUND ERR IN getPoolInfo : ",e);
}
return pool
}
return pool;
// // 2. Extract raw reserves & decimals
// const rawTokenReserve = BigInt(pool.baseReserves);
// const rawSOLReserve = BigInt(pool.quoteReserves);
// const tokenDec = pool.baseMintDecimals;
// const solDec = pool.quoteMintDecimals;
// // 3. Convert to human amounts
// const tokenReserve = fromRaw(rawTokenReserve, tokenDec);
// const solReserve = fromRaw(rawSOLReserve, solDec);
// // 4. Compute price in SOL: how many SOL per 1 token
// const priceInSOL = solReserve / tokenReserve;
// // 5. Compute USD price
// // Pool.liquidityUSD = total USD value of BOTH sides.
// // So USD per SOL ≈ (liquidityUSD/2) / solReserve
// // Then token USD price = priceInSOL × USD per SOL
// const liqUSD = Number(pool.liquidityUSD);
// const usdPerSOL = (liqUSD / 2) / solReserve;
// const priceInUSD = priceInSOL * usdPerSOL;
// // 6. Package and return!
// return {
// // poolAddress: pool.address,
// poolTimestamp: pool.timestamp,
// // solLiquidity: solReserve,
// // tokenLiquidity: formatNumberToKM(tokenReserve),
// priceInSOL: priceInSOL,
// priceInUSD: priceInUSD,
// // volumeUSD: formatNumberToKM(Number(pool.volumeUSD)),
// // lpMint: pool.lpMint,
// // isCanonical: pool.isCanonical,
// };
}
export const getTradeData = (filename:string)=>{
const fileContent = fs.readFileSync(filename, 'utf8');
const data = JSON.parse(fileContent);
if (!Array.isArray(data)) {
throw new Error('The JSON data is not an array.');
}
return data;
}
const getTotalProfit = ()=>{
const data = getTradeData('one-trade.json');
let totalProfit = 0;
data.forEach((trade)=>{
if(trade.compUSDEarned) totalProfit += trade.compUSDEarned
})
console.log(`Total Profit is $${totalProfit}`) // $3.7
}
const FetchMemePriceUSD = async(memeCoin:string)=>{
while(true){
try {
// const createdAt = new Date();
const priceResponse = await fetch(`${TOKEN_PRICE_URL}${memeCoin}/price`);
const price:number = await priceResponse.json();
// const elapsed = Math.floor((Date.now() - createdAt.getTime()) / 1000);
return price;
} catch (error) {
console.error(`Error fetching price for ${memeCoin}:`, error);
}
}
}
export function hasDecimal(number:number) {
return number % 1 !== 0;
}