diff --git a/package.json b/package.json index 67f5bd0..2b9e857 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,19 @@ { "name": "relay", - "version": "4.0.1", + "version": "4.0.2", "description": "Relayer for Tornado.cash privacy solution. https://tornado.cash", "scripts": { "server": "node src/server.js", "worker": "node src/worker", "treeWatcher": "node src/treeWatcher", "priceWatcher": "node src/priceWatcher", + "healthWatcher": "node src/healthWatcher", "eslint": "eslint --ext .js --ignore-path .gitignore .", "prettier:check": "npx prettier --check . --config .prettierrc", "prettier:fix": "npx prettier --write . --config .prettierrc", "lint": "yarn eslint && yarn prettier:check", "test": "mocha", - "start": "yarn server & yarn priceWatcher & yarn treeWatcher & yarn worker " + "start": "yarn server & yarn priceWatcher & yarn treeWatcher & yarn worker & yarn healthWatcher" }, "author": "tornado.cash", "license": "MIT", diff --git a/src/config.js b/src/config.js index 2ab6575..8906371 100644 --- a/src/config.js +++ b/src/config.js @@ -24,4 +24,5 @@ module.exports = { [jobType.MINING_REWARD]: 800000, [jobType.MINING_WITHDRAW]: 800000, }, + minimumBalance: 1000000000000000000, } diff --git a/src/healthWatcher.js b/src/healthWatcher.js new file mode 100644 index 0000000..55f2ed3 --- /dev/null +++ b/src/healthWatcher.js @@ -0,0 +1,27 @@ +const Web3 = require('web3') +const Redis = require('ioredis') +const { toBN, fromWei } = require('web3-utils') + +const { setSafeInterval } = require('./utils') +const { redisUrl, httpRpcUrl, privateKey, minimumBalance } = require('./config') + +const web3 = new Web3(httpRpcUrl) +const redis = new Redis(redisUrl) + +async function main() { + try { + const { address } = web3.eth.accounts.privateKeyToAccount(privateKey) + const balance = await web3.eth.getBalance(address) + + if (toBN(balance).lt(toBN(minimumBalance))) { + throw new Error(`Not enough balance, less than ${fromWei(minimumBalance.toString())} ETH`) + } + + await redis.hset('health', { status: true, error: '' }) + } catch (e) { + console.error('healthWatcher', e.message) + await redis.hset('health', { status: false, error: e.message }) + } +} + +setSafeInterval(main, 30 * 1000) diff --git a/src/status.js b/src/status.js index 3dc03d8..df4f0f6 100644 --- a/src/status.js +++ b/src/status.js @@ -6,6 +6,10 @@ const redis = new Redis(redisUrl) async function status(req, res) { const ethPrices = await redis.hgetall('prices') + const health = await redis.hgetall('health') + + const { waiting: currentQueue } = await queue.queue.getJobCounts() + res.json({ rewardAccount, instances: instances[`netId${netId}`], @@ -14,6 +18,8 @@ async function status(req, res) { tornadoServiceFee, miningServiceFee, version, + health, + currentQueue, }) }