-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
54 lines (48 loc) · 1.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const ethers = require('ethers')
require('dotenv').config();
const {
ADDRESS_RECEIVER,
GAS_LIMIT,
PRIVATE_KEY,
ETH_RPC_URL
} = process.env;
const provider = new ethers.providers.JsonRpcProvider(ETH_RPC_URL)
const increaseGasBy = 15000000000
async function getCurrentGasPrice() {
try {
const currentGasPrice = await provider.getGasPrice();
return (Number(currentGasPrice) + increaseGasBy) + ''
}
catch (err) {
console.error(err);
}
}
const bot = async () => {
provider.on("block", async () => {
console.log("New block minted")
const _target = new ethers.Wallet(PRIVATE_KEY)
const target = _target.connect(provider)
const balance = await provider.getBalance(target.address)
const currentGasPrice = await getCurrentGasPrice();
const balanceinEther = ethers.utils.formatEther(balance)
if (Number(balanceinEther) > 0 && Number(currentGasPrice) > 0) {
let withdrawAmount = Number(balance) - (Number(GAS_LIMIT) * Number(currentGasPrice));
if (Number(withdrawAmount) > 0) {
console.log(`Ether received ${balanceinEther}`)
try {
await target.sendTransaction({
to: ADDRESS_RECEIVER,
value: withdrawAmount.toString(),
gasPrice: currentGasPrice.toString(),
gasLimit: GAS_LIMIT.toString()
})
console.log(` Success! transfered -->${ethers.utils.formatEther(balance)}`)
} catch (e) {
console.log(e)
}
}
}
}
)
}
bot()