Skip to content

Commit

Permalink
Fix #227 - Reduce low balance alerts (#229)
Browse files Browse the repository at this point in the history
* add isAccountBalanceBelowMinimum to client

* add 12 hours interval to balance report

* if no threshold file, default to zero
  • Loading branch information
ngmachado committed Jun 13, 2023
1 parent 94a9e83 commit eb9a755
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
15 changes: 11 additions & 4 deletions src/services/notificationJobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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;
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/web3client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit eb9a755

Please sign in to comment.