How to use EVM Bot?

BTb4...tBoy
5 Jan 2024
459

Bots are use for automation. It is used for automation in blockchain, too. We can use them to interact with contracts or just use it with ethers library.

First of all, I used it with Engram Testnet. Because in this testnet, there was a 'Tx Spamming' object. so I created a contract on Engram Testnet. You can use this example on any EVM. Just change related sections.

Start With Deploying a Simple Contract


In solidity, I coded a simple contract and deployed it on Engram Testnet. After it, you get a contract address and an ABI. With this address and ABI, you can interact the contract.

const contractABI = [  {    
inputs: [],    name: "set",    outputs: [],    stateMutability: "nonpayable",    type: "function",  },  {    inputs: [],    name: "get",    outputs: [      {        internalType: "uint256",        name: "",        type: "uint256",      },    ],    stateMutability: "view",    type: "function",    constant: true, 

 },];

const contractAddress = "0xaD91C679aaAf72b8805c172819C246d9C35CEF54";



It is a simple contract, only set and get function. It is increasing the storeData when you use set function and, reads it when you use get function.

Now, let's start with our bot. You should have javascript development envirenment on your computer, like node.js, npm, and visual studio code. After it, create a new project and start npm in it.

npm init


Then create an index.js file. and install ether.js library.

npm install ethers



write your dependencies and api to your index file.

const { ethers } = require("ethers");

const contractABI = [ 'ABI'];

const contractAddress = "Address";


Write a wait function.

const bekle = (ms) => new Promise((resolve) => setTimeout(resolve, ms));



Now we are coding a start function. It is going to be async because we want it to work in a squence. You should change the RPC and MNEMONIC.

async function get() {  

const provider = new ethers.providers.JsonRpcProvider(    "https://tokioswift.engram.tech"  );  

const mnemonic = process.env.MNEMONIC;  
//for sending from account 0  

let path = `m/44'/60'/0'/0/0`;  

let senderAccount = ethers.Wallet.fromMnemonic(mnemonic, path);  

let walletsigner = await senderAccount.connect(provider);  

const contract = new ethers.Contract(   
 contractAddress,    
 contractABI,   
 walletsigner  );  

console.log(`${senderAccount.address} is ready. Please wait`);  

await bekle(1000);  

while (true) {    
try {      

const transaction = await contract.set({  
      from: walletsigner.account,     
 });     

 await transaction.wait();     
 console.log(`Transaction completed: ${transaction}`);      
let storedData = await contract.get();      
console.log(`Stored Data: ${storedData}`);    
} catch (error) {      
console.log(`HATA: ${error}`);    
}   
 await bekle(5000);  
}
}


It is a looping that runs set function of the contract and waits 5 seconds and starts over again. You can run this code with:

node index.js


You can get the whole code from my github.

Write & Read to Earn with BULB

Learn More

Enjoy this blog? Subscribe to thertln

7 Comments

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