From 6256b1977e8c807491179b7eae582bdd473f928f Mon Sep 17 00:00:00 2001 From: naggiba <69507625+naggiba@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:23:47 +0200 Subject: [PATCH] Update simple-send.ts --- scripts/simple-send.ts | 98 ++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 43 deletions(-) diff --git a/scripts/simple-send.ts b/scripts/simple-send.ts index 88de972..03fe3e8 100755 --- a/scripts/simple-send.ts +++ b/scripts/simple-send.ts @@ -1,65 +1,77 @@ #!/usr/bin/env -S node --require ts-node/register +// Import necessary modules from ethers import {providers, Wallet, utils, BigNumber} from 'ethers'; const {JsonRpcProvider} = providers; -const cfg = config() +// Function to read configuration from environment variables +function config() { + // Check and throw errors if required environment variables are not set + if (!process.env.PRIVATE_KEY) + throw new Error('Must pass PRIVATE_KEY'); + if (!process.env.HTTP_ENDPOINT) + throw new Error('Must pass HTTP_ENDPOINT'); + if (!process.env.TRANSACTION_TO) + throw new Error('Must pass TRANSACTION_TO'); + // Return configuration object + return { + privateKey: process.env.PRIVATE_KEY, + httpEndpoint: process.env.HTTP_ENDPOINT, + transactionTo: process.env.TRANSACTION_TO, + gasLimit: process.env.GAS_LIMIT, + }; +} + +// Main function to execute the script +(async () => { + const cfg = config(); // Read configuration -const provider = new JsonRpcProvider(cfg.httpEndpoint) -const wallet = new Wallet(cfg.privateKey).connect(provider) + // Initialize provider and wallet + const provider = new JsonRpcProvider(cfg.httpEndpoint); + const wallet = new Wallet(cfg.privateKey).connect(provider); -;(async () => { - const address = await wallet.getAddress() - console.log(`Sending from ${address}`) + // Fetch and log wallet address + const address = await wallet.getAddress(); + console.log(`Sending from ${address}`); - const balance = await wallet.getBalance() - console.log(`Balance: ${utils.formatEther(balance.toString())}`) + // Fetch and log wallet balance + const balance = await wallet.getBalance(); + console.log(`Balance: ${utils.formatEther(balance.toString())}`); - const gasPrice = await wallet.getGasPrice() + // Fetch and log gas price + const gasPrice = await wallet.getGasPrice(); - let gasLimit - if (cfg.gasLimit) { - gasLimit = BigNumber.from(cfg.gasLimit) - } else { + // Calculate gas limit + let gasLimit; + if (cfg.gasLimit) { + gasLimit = BigNumber.from(cfg.gasLimit); + } else { gasLimit = await wallet.estimateGas({ to: cfg.transactionTo, gasPrice, - }) - } + }); + } - console.log(`Using Gas Price: ${gasPrice.toString()}`) - console.log(`Using Gas Limit: ${gasLimit.toString()}`) + console.log(`Using Gas Price: ${gasPrice.toString()}`); + console.log(`Using Gas Limit: ${gasLimit.toString()}`); - const value = balance.sub(gasPrice.mul(gasLimit)) - console.log(`Sweeping balance ${utils.formatEther(value.toString())}`) - console.log(`Sending to ${cfg.transactionTo}`) + // Calculate value to be sent + const value = balance.sub(gasPrice.mul(gasLimit)); + console.log(`Sweeping balance ${utils.formatEther(value.toString())}`); + console.log(`Sending to ${cfg.transactionTo}`); - const tx = await wallet.sendTransaction({ + // Send transaction + const tx = await wallet.sendTransaction({ to: cfg.transactionTo, value, gasPrice, gasLimit, - }) + }); - console.log('Waiting for receipt') - const receipt = await tx.wait() - console.log(receipt) + console.log('Waiting for receipt'); + const receipt = await tx.wait(); + console.log(receipt); })().catch(err => { - console.log(err); - process.exit(1); -}) - -function config() { - if (!process.env.PRIVATE_KEY) - throw new Error('Must pass PRIVATE_KEY') - if (!process.env.HTTP_ENDPOINT) - throw new Error('Must pass HTTP_ENDPOINT') - if (!process.env.TRANSACTION_TO) - throw new Error('Must pass TRANSACTION_TO') - return { - privateKey: process.env.PRIVATE_KEY, - httpEndpoint: process.env.HTTP_ENDPOINT, - transactionTo: process.env.TRANSACTION_TO, - gasLimit: process.env.GAS_LIMIT, - } -} + console.log(err); + process.exit(1); +});