Created
April 1, 2023 03:56
-
-
Save ZacharyCouchman/ce471fa14a0bfa0eacf311996fa0d8e0 to your computer and use it in GitHub Desktop.
Send ETH from one wallet to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from "ethers"; | |
import { formatEther, parseEther } from "ethers/lib/utils.js"; | |
// As this demo is for sending funds, connect to the goerli test network to not spend real money | |
const rpcUrl = "https://eth-goerli.g.alchemy.com/v2/demo"; | |
const wallet1PublicAddress = ""; // add the wallet address you want to send goerli ETH from | |
const wallet1PrivateKey = ""; // Do not expose this Private key. Only use a test wallet for this demo. | |
const wallet2PublicAddress = ""; // add the wallet address you want to send funds to | |
async function sendATransaction() { | |
const provider = new ethers.providers.JsonRpcProvider(rpcUrl); | |
const wallet = new ethers.Wallet(wallet1PrivateKey, provider); | |
const wallet1Balance = await provider.getBalance(wallet1PublicAddress); | |
const wallet2Balance = await provider.getBalance(wallet2PublicAddress); | |
console.log(`Wallet 1 balance before transaction: ${formatEther(wallet1Balance)}`); | |
console.log(`Wallet 2 balance before transaction: ${formatEther(wallet2Balance)}`); | |
const txRequest = { | |
to: wallet2PublicAddress, | |
value: parseEther("0.001"), | |
} | |
const txResponse = await wallet.sendTransaction(txRequest); | |
console.log("Transaction response is:") | |
console.log(txResponse); | |
console.log("Waiting for transaction to be confirmed."); | |
console.log(`You can check the status of this transaction on Etherscan at https://goerli.etherscan.io/tx/${txResponse.hash}`); | |
const txReceipt = await provider.waitForTransaction(txResponse.hash); | |
const wallet1BalanceAfter = await provider.getBalance(wallet1PublicAddress); | |
const wallet2BalanceAfter = await provider.getBalance(wallet2PublicAddress); | |
console.log(`Wallet 1 balance after transaction: ${formatEther(wallet1BalanceAfter)}`); | |
console.log(`Wallet 2 balance after transaction: ${formatEther(wallet2BalanceAfter)}`); | |
} | |
sendATransaction(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment