forked from piotrostr/pump-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.ts
132 lines (123 loc) · 4.11 KB
/
listen.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
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
import { PUMP_WEBSOCKET_URL } from "./consts";
import { NewCoinCreatedSchema } from "./new-coin";
import { TradeCreatedSchema } from "./trade";
enum MessageType {
TRADE_CREATED = `42["tradeCreated"`,
COIN_CREATED = `42["newCoinCreated"`,
}
interface PumpBuyRequest {
mint: string;
bonding_curve: string;
associated_bonding_curve: string;
virtual_token_reserves: string;
virtual_sol_reserves: string;
real_token_reserves: string;
real_sol_reserves: string;
}
function _addBaseHandlers(ws: WebSocket) {
ws.onopen = function () {
console.log("Connection established");
ws.send("40");
};
ws.onclose = function () {
console.log("Connection closed");
};
ws.onerror = function (event) {
console.log("Error: " + event);
};
}
export async function listenOnTrades() {
const ws = new WebSocket(PUMP_WEBSOCKET_URL);
_addBaseHandlers(ws);
ws.onmessage = function (event) {
let data = event.data as string;
if (data == "2") {
ws.send("3");
console.log("Heartbeat sent");
} else if (data.startsWith(MessageType.TRADE_CREATED)) {
// dont do anything, we dont care about trades for now
let jsonParsable = data
.replace(`42["tradeCreated",`, "")
.replace("]", "");
let trade = TradeCreatedSchema.parse(JSON.parse(jsonParsable));
console.log({
signature: trade.signature,
sol_amount: trade.sol_amount,
token_amount: trade.token_amount,
is_buy: trade.is_buy,
timestamp: trade.timestamp,
name: trade.name,
symbol: trade.symbol,
usd_market_cap: trade.usd_market_cap,
});
}
};
}
export async function listenOnNewListings(sniperUrl: string) {
const ws = new WebSocket(PUMP_WEBSOCKET_URL);
_addBaseHandlers(ws);
ws.onmessage = function (event) {
let data = event.data as string;
if (data == "2") {
ws.send("3");
console.log("Heartbeat sent");
} else if (data.startsWith(MessageType.COIN_CREATED)) {
let jsonParsable = data
.replace(`42["newCoinCreated",`, "")
.replace("]", "");
let coin = NewCoinCreatedSchema.parse(JSON.parse(jsonParsable));
const msg = {
mint: coin.mint,
x: coin.twitter,
websites: coin.website,
telegram: coin.telegram,
at: new Date(coin.created_timestamp).toLocaleString(),
current: new Date().toLocaleString(),
};
console.debug(Date.now(), msg);
console.debug(
`got the msg in ${Date.now() - coin.created_timestamp} ms after creation`,
);
return;
// logs might be skipped to see later if that brings speed
// improvement, both rust and ts service
if (msg.at !== msg.current) {
// red X emoji in the log for skip
console.debug("❌Too late");
}
// skip if token doesnt have telegram or twitter
// there is so many tokens, I might just filter like this
if (!coin.website || !coin.telegram || !coin.twitter) {
console.debug("❌No telegram or twitter or website, skipping");
return;
}
// check if set of the three is not a single element
if (new Set([coin.website, coin.telegram, coin.twitter]).size === 1) {
console.debug("❌Website, telegram and twitter are the same, skipping");
return;
}
console.debug("✅Token has telegram, twitter and website, on time too!");
const pumpBuyRequest: PumpBuyRequest = {
mint: coin.mint,
bonding_curve: coin.bonding_curve,
associated_bonding_curve: coin.associated_bonding_curve,
virtual_token_reserves: String(coin.virtual_token_reserves),
virtual_sol_reserves: String(coin.virtual_sol_reserves),
real_token_reserves: String(coin.real_token_reserves),
real_sol_reserves: String(coin.real_sol_reserves),
};
const url = `${sniperUrl}/pump-buy`;
fetch(url, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(pumpBuyRequest),
})
.then((_) => {})
.catch((error) => {
console.error(url, error);
});
}
};
}