Avoid reading addresses from a file when sending balances

This commit is contained in:
Prathamesh Musale 2023-03-30 19:07:01 +05:30
parent 8d6d13ecf8
commit abb868eaf0

View File

@ -1,5 +1,3 @@
import fs from 'fs'
import { task } from 'hardhat/config'
import '@nomiclabs/hardhat-ethers'
@ -8,24 +6,15 @@ task('send-balance', 'Sends Ether to a specified Ethereum account')
.addParam('amount', 'The amount of Ether to send, in Ether')
.addParam('privateKey', 'The private key of the sender')
.setAction(async ({ to, amount, privateKey }, { ethers }) => {
const fileContent = fs.readFileSync('/l2-accounts/keys.json', 'utf-8')
const keySet = JSON.parse(fileContent)
// Get the dest account address from the json file if key present
let address: string = to
if (to in keySet) {
address = keySet[to].address
}
// Open the wallet using sender's private key
const wallet = new ethers.Wallet(privateKey, ethers.provider)
// Send amount to the specified address
const tx = await wallet.sendTransaction({
to: address,
to,
value: ethers.utils.parseEther(amount),
})
console.log(`Balance sent to: ${address}, from: ${wallet.address}`)
console.log(`Balance sent to: ${to}, from: ${wallet.address}`)
console.log(`Transaction hash: ${tx.hash}`)
})