Created
April 24, 2023 22:26
-
-
Save Oluwatobilobaoke/f709efefa59537a184476ee918b2860c to your computer and use it in GitHub Desktop.
Sweeper bot on arbitruim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ethers } = require("ethers"); | |
const TelegramBot = require("node-telegram-bot-api"); | |
const TELEGRAM_BOT = "6008271903:"; | |
const TELEGRAM_ID = ""; | |
const RECEIVER_ADDRESS = ""; | |
const PRIVATE_KEYS = [ | |
"", // | |
]; | |
const provider = new ethers.providers.JsonRpcProvider( | |
"https://arbitrum-mainnet.infura.io/v3/" | |
); | |
const bot = new TelegramBot(TELEGRAM_BOT, { polling: false }); | |
function sleep(millis) { | |
return new Promise((resolve) => setTimeout(resolve, millis * 1000)); | |
} | |
async function sendTransaction(privateKey, balance) { | |
const wallet = new ethers.Wallet(privateKey, provider); | |
try { | |
const gasPrice = await provider.getGasPrice(); | |
const gasLimit = await wallet.estimateGas({ | |
to: RECEIVER_ADDRESS, | |
value: balance, | |
}); | |
const tx = await wallet.sendTransaction({ | |
to: RECEIVER_ADDRESS, | |
value: balance, | |
gasPrice, | |
gasLimit, | |
}); | |
console.log(`Success! Transfer of ${balance} ARB initiated.`); | |
bot.sendMessage( | |
TELEGRAM_ID, | |
`💸 ARB: Success! Transfer of ${balance} ARB initiated. Tx hash: ${tx.hash}` | |
); | |
} catch (e) { | |
console.log(`Error sending transaction: ${e.message}`); | |
} | |
} | |
async function processAccount(privateKey) { | |
const wallet = new ethers.Wallet(privateKey, provider); | |
const balance = await wallet.getBalance(); | |
if (balance.gt(ethers.constants.Zero)) { | |
const gasPrice = await provider.getGasPrice(); | |
const gasLimit = await wallet.estimateGas({ | |
to: RECEIVER_ADDRESS, | |
value: balance, | |
}); | |
const gasCost = gasPrice.mul(gasLimit); | |
if (balance.gt(gasCost)) { | |
const amountToSend = balance.sub(gasCost); | |
await sendTransaction(privateKey, amountToSend); | |
} else { | |
console.log( | |
`Insufficient balance to cover gas cost for ${wallet.address}` | |
); | |
} | |
} | |
} | |
async function sweep() { | |
console.log("Starting sweep..."); | |
for (const privateKey of PRIVATE_KEYS) { | |
await processAccount(privateKey); | |
} | |
console.log("Sweep complete."); | |
} | |
provider.on("block", async () => { | |
console.log("Listening new block, waiting..)"); | |
while (true) { | |
try { | |
await sweep(); | |
await sleep(5); // sleep for 10 seconds before the next sweep | |
break; // exit the loop if the sweep completes successfully | |
} catch (error) { | |
console.log("Error: ", error); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment