Sign and Broadcast Transaction

To sign and broadcast a transaction to the Ethereum network, follow these steps:

  1. Retrieve unsigned serialized transactions in Base64 encrypted format.

  2. Sign and broadcast the transaction using the following code:

    require("dotenv").config();
    const ethers = require("ethers");
    
    async function signAndBroadcast() {
      console.log("Started");
    
      // Enter the serialized transaction
      const rawTransaction = process.env.RAW_TRANSACTION;
    
      // Enter the private key of the address used to transfer the stake amount
      const privateKey = process.env.PRIVATE_KEY;
    
      // Enter the selected RPC URL
      const rpcURL = process.env.RPC_URL;
    
      // Initialize the provider using the RPC URL
      const provider = new ethers.providers.JsonRpcProvider(rpcURL);
    
      // Initialize a new Wallet instance
      const wallet = new ethers.Wallet(privateKey, provider);
    
      // Parse the raw transaction
      const tx = ethers.utils.parseTransaction(rawTransaction);
    
      tx.nonce = await provider.getTransactionCount(wallet.address);
    
      // Enter the max fee per gas and prirorty fee
      tx.maxFeePerGas = ethers.utils.parseUnits(
        process.env.MAX_FEE_PER_GAS_IN_GWEI,
        "gwei"
      );
      tx.maxPriorityFeePerGas = ethers.utils.parseUnits(
        process.env.MAX_PRIORITY_FEE_IN_GWEI,
        "gwei"
      );
    
      // Sign the transaction
      const signedTransaction = await wallet.signTransaction(tx);
    
      // Send the signed transaction
      const transactionResponse = await provider.sendTransaction(signedTransaction);
    
      return transactionResponse;
    }
    
    signAndBroadcast()
      .then((transactionResponse) => {
        console.log(
          "Transaction broadcasted, transaction hash:",
          transactionResponse.hash
        );
      })
      .catch((error) => {
        console.error("Error:", error);
      })
      .finally(() => {
        console.log("Finished");
      });
    
    

What's Next?