// Import ethers.js
import { ethers } from 'ethers';
require("dotenv").config(); // To use environment variables
// Boilerplate Script
async function main() {
try {
// Step 1: Set up the provider
const rpcUrl = process.env.RPC_URL; // RPC URL for your blockchain
if (!rpcUrl) throw new Error("RPC_URL is not defined in .env");
const provider = new ethers.JsonRpcProvider(rpcUrl);
// Step 2: Configure the wallet (signer)
const privateKey = process.env.PRIVATE_KEY; // Private key stored in environment variables
if (!privateKey) throw new Error("PRIVATE_KEY is not defined in .env");
const wallet = new ethers.Wallet(privateKey, provider);
console.log(`Connected wallet: ${wallet.address}`);
// Step 3: Define the transaction
const tx = {
to: "0xRecipientAddressHere", // Replace with the recipient's address
value: ethers.parseEther("0.1"), // Amount in ETH to send (e.g., 0.1 ETH)
gasLimit: 21000, // Standard gas limit for a simple ETH transfer
maxFeePerGas: ethers.parseUnits("20", "gwei"), // Max fee per gas
maxPriorityFeePerGas: ethers.parseUnits("2", "gwei"), // Priority fee
};
console.log("Prepared transaction:", tx);
// Step 4: Sign and send the transaction
console.log("Sending transaction...");
const txResponse = await wallet.sendTransaction(tx);
console.log(`Transaction submitted: ${txResponse.hash}`);
// Step 5: Wait for transaction to be mined
console.log("Waiting for transaction to be mined...");
const receipt = await txResponse.wait();
console.log("Transaction mined:", receipt);
console.log(`Transaction successful! Block Number: ${receipt.blockNumber}`);
} catch (error) {
console.error("Error occurred:", error.message);
}
}
// Execute the script
main();