Advertisement
Guest User

Untitled

a guest
Jan 20th, 2024
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.86 KB | Cryptocurrency | 0 0
  1. const web3 = require("@solana/web3.js");
  2. const WebSocket = require("ws");
  3.  
  4. const fs = require("fs");
  5.  
  6. const rpcEndpoint = process.argv[2];
  7. const wsEndpoint = process.argv[3];
  8. const privateKeyPath = process.argv[4];
  9.  
  10. if (!privateKeyPath) {
  11.   console.error("Arguments: RPC_URL WS_URL PRIVATE_KEY_PATH");
  12.   process.exit(1);
  13. }
  14.  
  15. const connection = new web3.Connection(rpcEndpoint);
  16.  
  17. let latestBlockHash;
  18. let missedSlotCounter = 1;
  19.  
  20. // Your wallet details
  21. const secretKeyString = fs.readFileSync(privateKeyPath, { encoding: "utf8" });
  22. const senderSecretKey = Uint8Array.from(JSON.parse(secretKeyString));
  23. const senderKeypair = web3.Keypair.fromSecretKey(senderSecretKey);
  24. const recipientPublicKey = senderKeypair.publicKey;
  25.  
  26. console.log(
  27.   "Start benchmarking %s as %s",
  28.   rpcEndpoint,
  29.   senderKeypair.publicKey,
  30. );
  31.  
  32. setInterval(async () => {
  33.   console.log("Getting blockhash");
  34.   const blockhash = await connection.getLatestBlockhash("confirmed");
  35.   latestBlockHash = blockhash;
  36.   console.log("Got blockhash:", latestBlockHash);
  37. }, 10000);
  38.  
  39. // Function to send SOL transfer
  40. async function sendSolTransfer(transferAmount) {
  41.   const solTransferMessage = new web3.TransactionMessage({
  42.     payerKey: senderKeypair.publicKey,
  43.     recentBlockhash: latestBlockHash.blockhash,
  44.     instructions: [
  45.       web3.ComputeBudgetProgram.setComputeUnitLimit({
  46.         units: 30000,
  47.       }),
  48.       web3.ComputeBudgetProgram.setComputeUnitPrice({
  49.         microLamports: 100,
  50.       }),
  51.       web3.SystemProgram.transfer({
  52.         fromPubkey: senderKeypair.publicKey,
  53.         toPubkey: recipientPublicKey,
  54.         lamports: transferAmount,
  55.       }),
  56.     ],
  57.   }).compileToV0Message();
  58.  
  59.   const solTransferTransaction = new web3.VersionedTransaction(
  60.     solTransferMessage,
  61.   );
  62.   solTransferTransaction.sign([senderKeypair]);
  63.   const txHash = await connection.sendTransaction(solTransferTransaction, {
  64.     skipPreflight: true,
  65.     commitment: "confirmed",
  66.   });
  67.   console.log("Send tx:", txHash);
  68. }
  69.  
  70. // Connect to Solana WebSocket and subscribe to slot updates
  71. const ws = new WebSocket(wsEndpoint);
  72.  
  73. ws.on("open", function open() {
  74.   ws.send(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "slotSubscribe" }));
  75. });
  76.  
  77. ws.on("message", function incoming(data) {
  78.   const response = JSON.parse(data);
  79.  
  80.   if (response && response.method === "slotNotification") {
  81.     if (missedSlotCounter % 40 === 0) {
  82.       sendSolTransfer(response.params.result.slot % 1000); // Send SOL transfer on new slot
  83.       missedSlotCounter = 0;
  84.     }
  85.     ++missedSlotCounter;
  86.     console.log("New slot:", response.params.result);
  87.     console.log("Will fire after", 40 - (missedSlotCounter % 100));
  88.   }
  89. });
  90.  
  91. ws.on("close", function close() {
  92.   console.log("Disconnected from WebSocket");
  93. });
  94.  
  95. ws.on("error", function error(error) {
  96.   console.error("WebSocket error:", error);
  97. });
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement