tovarish-relayer/lib/services/check.js

43 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-11-13 04:42:13 +03:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DISABLE_LOW_BALANCE = exports.CHECK_BALANCE = void 0;
exports.checkProviders = checkProviders;
const process_1 = __importDefault(require("process"));
const ethers_1 = require("ethers");
const core_1 = require("@tornado/core");
// Can use 0 to use network on low balance
exports.CHECK_BALANCE = (0, ethers_1.parseEther)(process_1.default.env.CHECK_BALANCE || '0.001');
exports.DISABLE_LOW_BALANCE = true;
async function checkProviders(relayerConfig, logger) {
const { enabledNetworks, rpcUrls, rewardAccount } = relayerConfig;
const disabledNetworks = (await Promise.all(enabledNetworks.map(async (netId) => {
try {
const config = (0, core_1.getConfig)(netId);
const rpcUrl = rpcUrls[netId];
const provider = await (0, core_1.getProvider)(rpcUrl, {
netId,
});
const balance = await provider.getBalance(rewardAccount);
const symbol = config.nativeCurrency.toUpperCase();
if (balance < exports.CHECK_BALANCE) {
logger.error(`Network ${netId} has lower balance than 0.001 ${symbol} and thus disabled (Balance: ${(0, ethers_1.formatEther)(balance)} ${symbol})`);
if (exports.DISABLE_LOW_BALANCE) {
return netId;
}
}
else {
logger.info(`Network ${netId} connected with ${rpcUrl} (Balance: ${(0, ethers_1.formatEther)(balance)} ${config.nativeCurrency.toUpperCase()})`);
}
}
catch (err) {
logger.error(`Failed to connect with ${netId} provider, make sure you have configured correct RPC url`);
throw err;
}
}))).filter((n) => n);
relayerConfig.enabledNetworks = relayerConfig.enabledNetworks.filter((n) => !disabledNetworks.includes(n));
logger.info(`Enabled Networks: ${relayerConfig.enabledNetworks.join(', ')}`);
}