Search code examples
smartcontractserc20ethers.jsetherscan

How to transfer ETH on Rinkeby using ethers.js


Is there anybody who can help me with transfer ETH programmatically using ethers.js?

I have some ETH on Rinkeby and want to transfer it to any address programmatically.

Balance on Rinkeby

Please let me know the address of ETH contract and how to transfer using ethers.js.


Solution

  • First, I suggest reading about the ethers.js library. This is a great library for working with evm networks.

    Then let's consider two options:

    1. sending with wallet like Metamask;
    2. sending without wallet using private key.

    Let's start with the wallet option. This example can be found on the ethers.js documentation site:

    // A Web3Provider wraps a standard Web3 provider, which is
    // what MetaMask injects as window.ethereum into each page
    const provider = new ethers.providers.Web3Provider(window.ethereum)
    
    // MetaMask requires requesting permission to connect users accounts
    Await provider.send("eth_requestAccounts", []);
    
    // The MetaMask plugin also allows signing transactions to
    // send ether and pay to change state within the blockchain.
    // For this, you need the account signer...
    const signer = provider.getSigner()
    
    // Sending 1 ETH
    const tx = signer.sendTransaction({
        to: destAddress,
        value: ethers.utils.parseEther("1.0")
    });
    

    Now an option if we don't have a wallet like Metamask, but we have a private key:

    // The JsonRpcProvider is a popular method for interacting with Ethereum
    const provider = new ethers.providers.JsonRpcProvider("ADDRESS OF RINKEBY RPC");
    
    // Create a new Wallet instance for privateKey and connected to the provider.
    const wallet = new Wallet("TOP SECRET PRIVATE KEY", provider);
    
    // Sending 1 ETH
    wallet.sendTransaction({
        to: destAddress,
        value: ethers.utils.parseEther("1.0")
    })