2018-04-30 20:13:49 +03:00
// Description:
// Script that monitors the balance of the wallet used by
// the bot to do payouts and sends a Slack DM if it falls
// under a threshold
//
// Dependencies:
//
// Author:
// iSasFTW
const slackHelper = require ( '../lib/slack' )
const ethers = require ( 'ethers' )
const options = getOptions ( process . env . CHECK _BOT _BALANCE _CONFIG )
const botName = 'check-bot-balance'
const checkIntervalInSecs = parseInt ( options . options . check _interval _in _secs )
const minWarningAgeInMillisecs = 24 * 60 * 60 * 1000
var isCheckingBalance = false
module . exports = robot => {
2018-07-03 11:40:44 +02:00
if ( ! options || ! options . accounts || options . accounts . length === 0 ) {
2018-07-03 12:11:26 +02:00
robot . log . debug ( ` ${ botName } - No accounts configured. Disabling script ` )
2018-04-30 20:13:49 +03:00
return
}
robot . log . info ( ` ${ botName } - Repeating script every ${ checkIntervalInSecs } seconds ` )
if ( process . env . DISABLE _DELAY ) {
setTimeout ( ( ) => checkBotBalance ( robot ) , 1 * 1000 )
}
setInterval ( ( ) => checkBotBalance ( robot ) , checkIntervalInSecs * 1000 )
}
function getOptions ( optionsString ) {
return JSON . parse ( optionsString . split ( ` ' ` ) . join ( ` " ` ) )
}
async function checkBotBalance ( robot ) {
if ( isCheckingBalance ) {
return
}
isCheckingBalance = true
try {
for ( const account of options . accounts ) {
try {
const lastWarningTimestamp = account . lastWarningTimestamp || 0
const warningThresholdTimestamp = lastWarningTimestamp + minWarningAgeInMillisecs
const now = ( new Date ( ) ) . getTime ( )
// Make sure we don't flood Slack with repeated notifications
if ( now > warningThresholdTimestamp ) {
const provider = ethers . providers . getDefaultProvider ( account . network _id )
const balance = await provider . getBalance ( account . address )
const minBalance = ethers . utils . parseUnits ( account . min _balance . toString ( ) , 'ether' )
// Format balance to ether, check if is under threshold
if ( balance . lt ( minBalance ) ) {
// Send slack message
2018-05-01 10:35:10 +02:00
const slackChannelID = options . slack . notification . channel _id
2018-05-02 08:18:21 +02:00
const msg = ` <!here> URGENT: ${ account . name } account ETH will run out soon, current balance is ${ ethers . utils . formatEther ( balance ) } ETH (threshold: ${ ethers . utils . formatEther ( minBalance ) } ETH) \n https://etherscan.io/address/ ${ account . address } `
2018-05-01 10:35:10 +02:00
2018-05-02 08:18:21 +02:00
robot . log . info ( ` Notifying Slack channel with ID ${ slackChannelID } about bot balance ` )
2018-05-01 10:35:10 +02:00
2018-05-02 08:18:21 +02:00
robot . slackWeb . chat . postMessage ( slackChannelID , msg , { unfurl _links : false , as _user : slackHelper . BotUserName } )
2018-05-01 10:09:44 +02:00
account . lastWarningTimestamp = now
2018-04-30 20:13:49 +03:00
}
}
} catch ( error ) {
robot . log . error ( ` ${ botName } - Error while checking ${ account . name } account balance: ${ error } ` )
}
}
} finally {
isCheckingBalance = false
}
2018-05-01 10:35:10 +02:00
}