From eb9a7557853ae050ab544a457bfb7c167fabfa1f Mon Sep 17 00:00:00 2001 From: Axe Date: Tue, 13 Jun 2023 11:33:43 +0100 Subject: [PATCH] Fix #227 - Reduce low balance alerts (#229) * add isAccountBalanceBelowMinimum to client * add 12 hours interval to balance report * if no threshold file, default to zero --- src/app.js | 1 + src/services/notificationJobs.js | 15 +++++++++++---- src/web3client/client.js | 9 +++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index b9ac337a..1644416a 100644 --- a/src/app.js +++ b/src/app.js @@ -218,6 +218,7 @@ class App { } catch (err) { this.logger.warn(`error loading thresholds.json`); await this.db.queries.updateThresholds({}); + this.config.SENTINEL_BALANCE_THRESHOLD = 0; } diff --git a/src/services/notificationJobs.js b/src/services/notificationJobs.js index 55812129..bccc61da 100644 --- a/src/services/notificationJobs.js +++ b/src/services/notificationJobs.js @@ -5,10 +5,13 @@ async function trigger (obj, ms) { await timeout(ms); await obj.sendReport(); } +// if balance report interval is <= 5 min will always send a balance alert because global report interval is 5 min +const BALANCE_REPORT_INTERVAL = 12 * 1000 * 60 * 60; // 12 hours class NotificationJobs { constructor(app) { this.app = app; + this._lastBalanceReportTime = Date.now(); } async sendReport () { @@ -17,10 +20,14 @@ class NotificationJobs { const healthData = `Healthy: ${healthcheck.healthy}\nChainId: ${healthcheck.network.chainId}`; this.app.notifier.sendNotification(healthData); } - const accountBalance = await this.app.client.getAccountBalance(); - if(new BN(accountBalance).lt(new BN(this.app.config.SENTINEL_BALANCE_THRESHOLD))) { - const balanceData = `Attention: Sentinel balance: ${wad4human(accountBalance)}`; - this.app.notifier.sendNotification(balanceData); + const currentTime = Date.now(); + if(currentTime - this._lastBalanceReportTime >= BALANCE_REPORT_INTERVAL) { + const balanceQuery = await this.app.client.isAccountBalanceBelowMinimum(); + if(balanceQuery.isBelow) { + this.app.notifier.sendNotification(`Attention: Sentinel balance: ${wad4human(balanceQuery.balance)}`); + // update the time of last balance report + this._lastBalanceReportTime = currentTime; + } } } diff --git a/src/web3client/client.js b/src/web3client/client.js index 18e696a2..42c0772b 100644 --- a/src/web3client/client.js +++ b/src/web3client/client.js @@ -8,6 +8,7 @@ const SuperfluidGovernance = require("@superfluid-finance/ethereum-contracts/bui const BatchContract = require("../abis/BatchLiquidator.json"); const TogaContract = require("@superfluid-finance/ethereum-contracts/build/contracts/TOGA.json"); const { wad4human } = require("@decentral.ee/web3-helpers"); +const BN = require("bn.js"); /* * Web3 and superfluid client: @@ -245,6 +246,14 @@ class Client { } } + async isAccountBalanceBelowMinimum () { + const balance = await this.getAccountBalance(); + return { + isBelow: new BN(balance).lt(new BN(this.app.config.SENTINEL_BALANCE_THRESHOLD)), + balance: balance + }; + } + getAccount () { return this.agentAccounts; }