Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const web3 = require("@solana/web3.js");
- const WebSocket = require("ws");
- const fs = require("fs");
- const rpcEndpoint = process.argv[2];
- const wsEndpoint = process.argv[3];
- const privateKeyPath = process.argv[4];
- if (!privateKeyPath) {
- console.error("Arguments: RPC_URL WS_URL PRIVATE_KEY_PATH");
- process.exit(1);
- }
- const connection = new web3.Connection(rpcEndpoint);
- let latestBlockHash;
- let missedSlotCounter = 1;
- // Your wallet details
- const secretKeyString = fs.readFileSync(privateKeyPath, { encoding: "utf8" });
- const senderSecretKey = Uint8Array.from(JSON.parse(secretKeyString));
- const senderKeypair = web3.Keypair.fromSecretKey(senderSecretKey);
- const recipientPublicKey = senderKeypair.publicKey;
- console.log(
- "Start benchmarking %s as %s",
- rpcEndpoint,
- senderKeypair.publicKey,
- );
- setInterval(async () => {
- console.log("Getting blockhash");
- const blockhash = await connection.getLatestBlockhash("confirmed");
- latestBlockHash = blockhash;
- console.log("Got blockhash:", latestBlockHash);
- }, 10000);
- // Function to send SOL transfer
- async function sendSolTransfer(transferAmount) {
- const solTransferMessage = new web3.TransactionMessage({
- payerKey: senderKeypair.publicKey,
- recentBlockhash: latestBlockHash.blockhash,
- instructions: [
- web3.ComputeBudgetProgram.setComputeUnitLimit({
- units: 30000,
- }),
- web3.ComputeBudgetProgram.setComputeUnitPrice({
- microLamports: 100,
- }),
- web3.SystemProgram.transfer({
- fromPubkey: senderKeypair.publicKey,
- toPubkey: recipientPublicKey,
- lamports: transferAmount,
- }),
- ],
- }).compileToV0Message();
- const solTransferTransaction = new web3.VersionedTransaction(
- solTransferMessage,
- );
- solTransferTransaction.sign([senderKeypair]);
- const txHash = await connection.sendTransaction(solTransferTransaction, {
- skipPreflight: true,
- commitment: "confirmed",
- });
- console.log("Send tx:", txHash);
- }
- // Connect to Solana WebSocket and subscribe to slot updates
- const ws = new WebSocket(wsEndpoint);
- ws.on("open", function open() {
- ws.send(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "slotSubscribe" }));
- });
- ws.on("message", function incoming(data) {
- const response = JSON.parse(data);
- if (response && response.method === "slotNotification") {
- if (missedSlotCounter % 40 === 0) {
- sendSolTransfer(response.params.result.slot % 1000); // Send SOL transfer on new slot
- missedSlotCounter = 0;
- }
- ++missedSlotCounter;
- console.log("New slot:", response.params.result);
- console.log("Will fire after", 40 - (missedSlotCounter % 100));
- }
- });
- ws.on("close", function close() {
- console.log("Disconnected from WebSocket");
- });
- ws.on("error", function error(error) {
- console.error("WebSocket error:", error);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement