forked from Block-Logic/ping-thing-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ping-thing-client-jupiter.ts
390 lines (326 loc) · 12.8 KB
/
ping-thing-client-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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import {
setTransactionMessageLifetimeUsingBlockhash,
createKeyPairFromBytes,
getAddressFromPublicKey,
signTransaction,
sendTransactionWithoutConfirmingFactory,
createSolanaRpcSubscriptions_UNSTABLE,
getSignatureFromTransaction,
compileTransaction,
createSolanaRpc,
SOLANA_ERROR__TRANSACTION_ERROR__BLOCKHASH_NOT_FOUND,
isSolanaError,
type Signature,
type Commitment,
getTransactionDecoder,
getCompiledTransactionMessageDecoder,
decompileTransactionMessageFetchingLookupTables,
} from "@solana/web3.js";
import dotenv from "dotenv";
import bs58 from "bs58";
import { createRecentSignatureConfirmationPromiseFactory } from "@solana/transaction-confirmation";
import { sleep } from "./utils/misc.js";
import { watchBlockhash } from "./utils/blockhash.js";
import { watchSlotSent } from "./utils/slot.js";
import { setMaxListeners } from "events";
import axios from "axios";
import { safeRace } from "@solana/promises";
dotenv.config();
// Catch interrupts & exit
process.on("SIGINT", function () {
console.log(`Caught interrupt signal`, "\n");
process.exit();
});
const RPC_ENDPOINT = process.env.RPC_ENDPOINT;
const WS_ENDPOINT = process.env.WS_ENDPOINT;
const SLEEP_MS_RPC = process.env.SLEEP_MS_RPC ? parseInt(process.env.SLEEP_MS_RPC) : 2000;
const SLEEP_MS_LOOP = process.env.SLEEP_MS_LOOP ? parseInt(process.env.SLEEP_MS_LOOP) : 0;
const VA_API_KEY = process.env.VA_API_KEY;
const VERBOSE_LOG = process.env.VERBOSE_LOG === "true" ? true : false;
const COMMITMENT_LEVEL = process.env.COMMITMENT || "confirmed";
const SKIP_VALIDATORS_APP = process.env.SKIP_VALIDATORS_APP || false;
const SWAP_TOKEN_FROM = "So11111111111111111111111111111111111111112"; // SOL
const SWAP_TOKEN_TO = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; // USDC
const SWAP_AMOUNT = 1000;
const JUPITER_ENDPOINT = `${RPC_ENDPOINT}/jupiter`;
const PRIORITY_FEE_PERCENTILE = process.env.PRIORITY_FEE_PERCENTILE || 5000;
if (VERBOSE_LOG) console.log(`Starting script`);
// RPC connection for HTTP API calls, equivalent to `const c = new Connection(RPC_ENDPOINT)`
const rpcConnection = createSolanaRpc(RPC_ENDPOINT!);
// RPC connection for websocket connection
const rpcSubscriptions = createSolanaRpcSubscriptions_UNSTABLE(WS_ENDPOINT!);
let USER_KEYPAIR;
const TX_RETRY_INTERVAL = 2000;
// Global blockhash value fetching constantly in a loop
const gBlockhash = { value: null, updated_at: 0, lastValidBlockHeight: BigInt(0) };
// Record new slot on `firstShredReceived` fetched from a slot subscription
const gSlotSent = { value: null, updated_at: 0 };
// main ping thing function
async function pingThing() {
USER_KEYPAIR = await createKeyPairFromBytes(
bs58.decode(process.env.WALLET_PRIVATE_KEYPAIR!)
);
// Pre-define loop constants & variables
const FAKE_SIGNATURE =
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
// Run inside a loop that will exit after 3 consecutive failures
const MAX_TRIES = 3;
let tryCount = 0;
// Infinite loop to keep this running forever
while (true) {
await sleep(SLEEP_MS_LOOP);
let blockhash;
let lastValidBlockHeight;
let slotSent;
let slotLanded;
let signature;
let txStart;
let txSendAttempts = 1;
let tempResponse;
let quoteResponse;
let jupiterSwapTransaction;
// Wait for fresh slot and blockhash
while (true) {
if (
Date.now() - gBlockhash.updated_at < 10000 &&
Date.now() - gSlotSent.updated_at < 50
) {
blockhash = gBlockhash.value;
lastValidBlockHeight = gBlockhash.lastValidBlockHeight;
slotSent = gSlotSent.value;
break;
}
await sleep(1);
}
try {
try {
if (VERBOSE_LOG)
console.log(
`fetching jupiter swap quote`
);
// Get quote for swap
tempResponse = await axios.get(
`${JUPITER_ENDPOINT}/quote?inputMint=${SWAP_TOKEN_FROM}&outputMint=${SWAP_TOKEN_TO}&amount=${SWAP_AMOUNT}&slippageBps=50`
);
// Throw error if response is not ok
if (!(tempResponse.status >= 200) && tempResponse.status < 300) {
throw new Error(
`Failed to fetch jupiter swap quote: ${tempResponse.status}`
);
}
quoteResponse = tempResponse.data;
if (VERBOSE_LOG)
console.log(`fetched jupiter swap quote`);
// get priority fees from teh improved priority fees api
// https://docs.triton.one/chains/solana/improved-priority-fees-api
const priorityFeeApiResult = await axios.post(`${RPC_ENDPOINT}`, {
method: "getRecentPrioritizationFees",
jsonrpc: "2.0",
params: [
["JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4"],
{
percentile: parseInt(PRIORITY_FEE_PERCENTILE.toString()),
},
],
id: "1",
});
// get the fees array from the response array
const fees: number[] = priorityFeeApiResult.data.result.map((i: { slot: number, prioritizationFee: number }) => i.prioritizationFee);
const medianFees = fees.sort()[Math.floor(fees.length / 2)]
if (VERBOSE_LOG)
console.log(`fetched global priority fees for jupiter`);
const userPublicKey = await getAddressFromPublicKey(USER_KEYPAIR.publicKey);
// Get swap transaction
tempResponse = await axios.post(`${JUPITER_ENDPOINT}/swap`, {
quoteResponse: quoteResponse,
userPublicKey: userPublicKey.toString(),
wrapAndUnwrapSol: true,
dynamicComputeUnitLimit: true,
prioritizationFeeLamports: medianFees,
});
// throw error if response is not ok
if (!(tempResponse.status >= 200) && tempResponse.status < 300) {
throw new Error(
`failed to fetch jupiter swap transaction: ${tempResponse.status}`
);
}
if (VERBOSE_LOG)
console.log(
`fetched jupiter swap transaction`
);
jupiterSwapTransaction = tempResponse.data;
const swapTransactionBuffer = Buffer.from(
jupiterSwapTransaction.swapTransaction,
"base64"
);
// ---- Start: Decode and parse the transaction ----
const transactionDecoder = getTransactionDecoder()
const decodedTx = transactionDecoder.decode(swapTransactionBuffer)
const compiledTransactionMessageDecoder = getCompiledTransactionMessageDecoder()
const compiledTransactionMessage = compiledTransactionMessageDecoder.decode(decodedTx.messageBytes)
const txMessage = await decompileTransactionMessageFetchingLookupTables(compiledTransactionMessage, rpcConnection)
// Set blockhash
const finalTxWithBlockhash = setTransactionMessageLifetimeUsingBlockhash({ blockhash: blockhash!, lastValidBlockHeight: lastValidBlockHeight }, txMessage)
// Sign the tx
const transactionSignedWithFeePayer = await signTransaction(
[USER_KEYPAIR],
compileTransaction(finalTxWithBlockhash)
);
signature = getSignatureFromTransaction(transactionSignedWithFeePayer);
// ---- End: Decode and parse the transaction ----
// Note the timestamp we begin sending the tx, we'll compare it with the
// timestamp when the tx is confirmed to mesaure the tx latency
txStart = Date.now();
console.log(`Sending ${signature}`);
// The tx sendinng and confirming startegy of the Ping Thing is as follow:
// 1. Send the tranaction
// 2. Subscribe to the tx signature and listen for dersied commitment change
// 3. Send the tx again if not confrmed within 2000ms
// 4. Stop sending when tx is confirmed
// Create a sender factory that sends a transaction and doesn't wait for confirmation
const mSendTransaction = sendTransactionWithoutConfirmingFactory({
rpc: rpcConnection,
});
// Create a promise factory that has the logic for a the tx to be confirmed
const getRecentSignatureConfirmationPromise =
createRecentSignatureConfirmationPromiseFactory({
rpc: rpcConnection,
rpcSubscriptions,
});
setMaxListeners(100);
// Incase we want to abort the promise that's waiting for a tx to be confirmed
const abortController = new AbortController();
while (true) {
try {
// Send the tx
await mSendTransaction(transactionSignedWithFeePayer, {
commitment: "confirmed",
maxRetries: 0n,
skipPreflight: true,
});
// Wait for tx confirmation promise, if it doesn't resolve in 2000ms, trigger a resend of the tx. We're handling client side retris here
// safeRace is a memory safe version of `Promise.all` that doesn't leak memory
await safeRace([
getRecentSignatureConfirmationPromise({
signature,
commitment: "confirmed",
abortSignal: abortController.signal,
}),
sleep(TX_RETRY_INTERVAL).then(() => {
throw new Error("Tx Send Timeout");
}),
]);
console.log(`Confirmed tx ${signature}`);
break;
} catch (e) {
console.log(
`Tx not confirmed after ${TX_RETRY_INTERVAL * txSendAttempts++
}ms, resending`
);
}
}
} catch (e: any) {
// Log and loop if we get a bad blockhash.
if (
isSolanaError(e, SOLANA_ERROR__TRANSACTION_ERROR__BLOCKHASH_NOT_FOUND)
) {
// Same as `if (e.message.includes("Blockhash not found")) {`
console.log(`ERROR: Blockhash not found`);
continue;
}
// If the transaction expired on the chain. Make a log entry and send
// to VA. Otherwise log and loop.
if (e.name === "TransactionExpiredBlockheightExceededError") {
console.log(
`ERROR: Blockhash expired/block height exceeded. TX failure sent to VA.`
);
} else {
console.log(`ERROR: ${e.name}`);
console.log(e.message);
console.log(e);
console.log(JSON.stringify(e));
continue;
}
// Need to submit a fake signature to pass the import filters
signature = FAKE_SIGNATURE as Signature;
}
const txEnd = Date.now();
// Sleep a little here to ensure the signature is on an RPC node.
await sleep(SLEEP_MS_RPC);
if (signature !== FAKE_SIGNATURE) {
// Capture the slotLanded
let txLanded = await rpcConnection
.getTransaction(signature, {
commitment: COMMITMENT_LEVEL as Commitment,
maxSupportedTransactionVersion: 0,
})
.send();
if (txLanded === null) {
console.log(
signature,
`ERROR: tx is not found on RPC within ${SLEEP_MS_RPC}ms. Not sending to VA.`
);
continue;
}
slotLanded = txLanded.slot;
}
// Don't send if the slot latency is negative
if (slotLanded! < slotSent!) {
console.log(
signature,
`ERROR: Slot ${slotLanded} < ${slotSent}. Not sending to VA.`
);
continue;
}
// Prepare the payload to send to validators.app
const vAPayload = JSON.stringify({
time: txEnd - txStart!,
signature,
transaction_type: "transfer",
success: signature !== FAKE_SIGNATURE,
application: "web3",
commitment_level: COMMITMENT_LEVEL,
slot_sent: BigInt(slotSent!).toString(),
slot_landed: BigInt(slotLanded!).toString(),
});
if (VERBOSE_LOG) {
console.log(vAPayload);
}
if (!SKIP_VALIDATORS_APP) {
// Send the payload to validators.app
const vaResponse = await axios.post(
"https://www.validators.app/api/v1/ping-thing/mainnet",
vAPayload,
{
headers: {
"Content-Type": "application/json",
Token: VA_API_KEY,
},
}
);
// throw error if response is not ok
if (!(vaResponse.status >= 200 && vaResponse.status <= 299)) {
throw new Error(`Failed to update validators: ${vaResponse.status}`);
}
if (VERBOSE_LOG) {
console.log(
`VA Response ${vaResponse.status} ${JSON.stringify(
vaResponse.data
)}`
);
}
}
// Reset the try counter
tryCount = 0;
} catch (e) {
console.log(`ERROR`);
console.log(e);
if (++tryCount === MAX_TRIES) throw e;
}
}
}
Promise.all([
watchBlockhash(gBlockhash, rpcConnection),
watchSlotSent(gSlotSent, rpcSubscriptions),
pingThing(),
]);