-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
55 lines (52 loc) · 1.71 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
55
require('dotenv').config()
const { RPC_URL = 'https://cloudflare-eth.com',
BLOCK_EXPLORER = 'etherscan.io',
ADDRESS,
THRESHOLD,
INTERVAL = 300,
TELEGRAM_NOTIFIER_BOT_TOKEN,
TELEGRAM_NOTIFIER_CHAT_ID
} = process.env
const fetch = require('node-fetch')
const Web3 = require('web3')
const web3 = new Web3(RPC_URL)
const { toBN, toWei, fromWei } = require('web3-utils')
async function main() {
try {
let balance = await web3.eth.getBalance(ADDRESS)
if( toBN(balance).lt(toBN(toWei(THRESHOLD))) ) {
const response = await fetch(`https://api.telegram.org/bot${TELEGRAM_NOTIFIER_BOT_TOKEN}/sendMessage`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: TELEGRAM_NOTIFIER_CHAT_ID,
text: `https://${BLOCK_EXPLORER}/address/${ADDRESS} balance is below ${THRESHOLD} ETH. \nit's ${fromWei(balance)}`
}) // body data type must match 'Content-Type' header
})
console.log('resp', await response.json())
} else {
console.log('everything is ok', `${ADDRESS} balance is ${fromWei(balance)}`)
}
} catch (e) {
console.log(e)
} finally {
setTimeout(main, INTERVAL * 1000)
}
}
async function healthcheck() {
try {
await fetch(`https://api.telegram.org/bot${TELEGRAM_NOTIFIER_BOT_TOKEN}/getMe`)
await web3.eth.getBalance(ADDRESS)
} catch (e) {
console.log(e)
process.exit(101)
}
process.exit(0)
}
if (process.argv.includes('healthcheck')) {
healthcheck()
} else {
main()
}