Using 'ethers' Library as an EVM Bot

BTb4...tBoy
7 Jan 2024
79

I mentioned in my previous article that how you can use EVM bots for your projects. Now I am going to code a new bot that I can understand better what ethers library is.

What is Ethers?

The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem. You can see the doc here *

We are going to create a new wallet for testing purpose. For testing, you can use Ganache.


Ganache creates a blockchain in your computer for testing purpose. It is easy and fast for testing. I recommend to use it. You can google it and install it.We will use Ganache RPC in our project.

You can create a new project and an index.js file in it. And then add ethers library. Don't forget to run 'npm init' and 'npm install ethers' to install the lastest version. Then add your dependencies and start coding.

require("dotenv").config();
const { ethers } = require("ethers");


const provider = new ethers.providers.JsonRpcProvider("HTTP://0.0.0.0:4727");
const mnemonic = process.env.MNEMONIC;

async function send_from_zero() {

}

send_from_zero();


Let's see what is going on. We use Ganache as provider. If you use it on mainnet or testnet you can use relevant RPC. We get mnemonics from .env file. It is a file that you create and write your wallet mnemonics in it. It is safer than using it here. Because you can add .env file to .gitignore file so that you can prevent it sending to your github repo. And also, you can get help from forums so you can share your code easily. You can write the mnemonics that is written in Ganache. Don't forget to install 'dotenv' using 'npm install dotenv'.

Our first function send_from_zero . It is going to send the amount to your sub accounts.

require("dotenv").config();
const { ethers } = require("ethers");


const provider = new ethers.providers.JsonRpcProvider("HTTP://0.0.0.0:4727");
const mnemonic = process.env.MNEMONIC;

async function send_from_zero() {
    try {     

   const signer = this.provider.getSigner();    
  const accounts = await this.provider.listAccounts();   


     
 for (var i = 1; i < accounts.length; i++) {     
   const tx = signer.sendTransaction({       
   to: accounts[i],       
   value: ethers.utils.parseEther("1"),    
    });    

    (await tx).wait();    
   
console.log( `${accounts[i]} gets 1 ether`);
  }   
 } catch (error) {     
console.log(error);
 }
}

send_from_zero();


You will send 1 ether from your first account to other accounts.

We will do the opposite of this functions. We create a new function and send from other accounts to our first account.

require("dotenv").config();
const { ethers } = require("ethers");


const provider = new ethers.providers.JsonRpcProvider("HTTP://0.0.0.0:4727");
const mnemonic = process.env.MNEMONIC;

async send_from_all_to_zero() {   

 const accounts = await this.provider.listAccounts();   

 for (var i = 1; i < accounts.length; i++) {     
 let path = `m/44'/60'/0'/0/${i}`;      
 let senderAccount = ethers.Wallet.fromMnemonic(mnemonic, path); 
   
 let walletsigner = await senderAccount.connect(this.provider); 
  
 const tx = await walletsigner.sendTransaction({    
   to: accounts[0],       
   value: ethers.utils.parseEther("1"),     
   });     

 (await tx).wait();    
 
  }
}

  send_from_all_to_zero();


You will get 1 ether from each your account to your first account.



*https://docs.ethers.org/v5/


Write & Read to Earn with BULB

Learn More

Enjoy this blog? Subscribe to thertln

10 Comments

B
No comments yet.
Most relevant comments are displayed, so some may have been filtered out.