4

I am trying to make a simple js bot that checks every block for eth(or the main token of the chain) and sends it to another wallet.

I have a working bot:

const { ethers } = require('ethers')

const provider = new ethers.providers.JsonRpcProvider("")

const addressReceiver = ''

const privateKeys = [""]


const bot = async =>{
    provider.on('block', async () => {
        console.log('Listening to 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('0.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! transferred -->${ethers.utils.formatEther(balance)}`);
                } catch(e){
                    console.log(`error: ${e}`);
                }
            }
        }
    })
}
bot();

But this has a set transaction buffer that ends up leaving some eth in the wallet after it the bot runs. I want to estimate fees and then subtract those fees from the total taken out. Something like this :

const {
    ethers
} = require('ethers')

const provider = new ethers.providers.JsonRpcProvider("")

const addressReceiver = ''

const privateKeys = [""]

const bot = async =>{
    provider.on('block', async () => {
        console.log('Listening to 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 gasLimit = await provider.estimateGas({
                to: addressReceiver,
                value: await provider.getBalance(target.address),
                gasLimit: 21000,
                gasPrice: ethers.utils.parseUnits('10', 'gwei'),
                nonce: await provider.getTransactionCount(privateKeys[i])
            })


            if (balance.sub(gasLimit) > 0) {
                console.log("New Account with Eth!");
                const amount = balance.sub(gasLimit);
                try {
                    await target.sendTransaction({
                        to: addressReceiver,
                        value: amount
                    });
                    console.log(`Success! transferred -->${ethers.utils.formatEther(balance)}`);
                } catch (e) {
                    console.log(`error: ${e}`);
                }
            }
        }
    })

}
bot();

But this throws an ENS name not configured error.

owenk455
  • 43
  • 1
  • 3

1 Answers1

1

Few issues here:

  1. ENS name not figured is probably because addressReceiver was an ENS name (ends with .eth), not an address (starts with 0x). Use an address.

  2. amount = balance.sub(gasLimit) is not right. gasLimit is the amount of gas used, not in eth. You'll need to multiply that by fee per gas to get eth. The easiest way to figure out the exact fee is to set tx.maxFeePerGas and tx.maxPriorityFeePerGas to be the same values. That will cause you to overpay most of the time. Then the new code will be amount = balance.sub(tx.maxFeePerGas.mul(gasLimit))

John Tseng
  • 6,262
  • 2
  • 27
  • 35