Skip to content

Instantly share code, notes, and snippets.

@duanescarlett
Last active April 10, 2022 00:37
Show Gist options
  • Save duanescarlett/a14504f65e333dd84d6e8e0004cd48e2 to your computer and use it in GitHub Desktop.
Save duanescarlett/a14504f65e333dd84d6e8e0004cd48e2 to your computer and use it in GitHub Desktop.
Send Ether to another wallet
ethers = require('ethers')
require('dotenv').config()
async function main () {
// Connect to an EVM network
const provider = new ethers.providers.JsonRpcProvider(`https://polygon-mainnet.g.alchemy.com/v2/${process.env.RPC_KEY}`)
// Get the gas price
const gasPrice = provider.getGasPrice()
// Create a wallet object from private key
const wallet = new ethers.Wallet(`${process.env.PRIVATE_KEY}`)
// Get a signer
const signer = wallet.connect(provider)
// Get the address of the receiving wallet
const receiver = '0x0fC4319EB388751E315eFfB7Dbb4c3Ca2d83E656'
// Look up the current block number
let block = await provider.getBlockNumber()
// Create the transaction object
const tx = {
from: wallet.address,
to: receiver,
value: ethers.utils.parseUnits('1', 'ether'),
gasPrice: gasPrice,
gasLimit: ethers.utils.hexlify(300000),
nonce: provider.getTransactionCount(wallet.address, block)
}
// Send the transaction
const transaction = await signer.sendTransaction(tx);
// Wait to get the transaction result
let result = await transaction.wait()
console.log(result)
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment