diff --git a/monitor/alerts.js b/monitor/alerts.js index 013a317a..fd290d29 100644 --- a/monitor/alerts.js +++ b/monitor/alerts.js @@ -13,67 +13,57 @@ const foreignProvider = new Web3.providers.HttpProvider(FOREIGN_RPC_URL) const web3Foreign = new Web3(foreignProvider) async function main() { - try { - const { - foreignDeposits, - homeDeposits, - homeWithdrawals, - foreignWithdrawals - } = await eventsInfo() + const { foreignDeposits, homeDeposits, homeWithdrawals, foreignWithdrawals } = await eventsInfo() - const xSignatures = foreignDeposits.filter(findDifferences(homeDeposits)) - const xAffirmations = homeWithdrawals.filter(findDifferences(foreignWithdrawals)) + const xSignatures = foreignDeposits.filter(findDifferences(homeDeposits)) + const xAffirmations = homeWithdrawals.filter(findDifferences(foreignWithdrawals)) - logger.debug('building misbehavior blocks') - const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) + logger.debug('building misbehavior blocks') + const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) - const baseRange = [false, false, false, false, false] - const xSignaturesMisbehavior = buildRangesObject( - xSignatures.map(findMisbehaviorRange(foreignBlockNumber)).reduce(mergeRanges, baseRange) - ) - const xAffirmationsMisbehavior = buildRangesObject( - xAffirmations.map(findMisbehaviorRange(homeBlockNumber)).reduce(mergeRanges, baseRange) - ) + const baseRange = [false, false, false, false, false] + const xSignaturesMisbehavior = buildRangesObject( + xSignatures.map(findMisbehaviorRange(foreignBlockNumber)).reduce(mergeRanges, baseRange) + ) + const xAffirmationsMisbehavior = buildRangesObject( + xAffirmations.map(findMisbehaviorRange(homeBlockNumber)).reduce(mergeRanges, baseRange) + ) - logger.debug('extracting most recent transactionHash') - const { transactionHash: xSignaturesMostRecentTxHash = '' } = - xSignatures.sort(sortEvents).reverse()[0] || {} - const { transactionHash: xAffirmationsMostRecentTxHash = '' } = - xAffirmations.sort(sortEvents).reverse()[0] || {} + logger.debug('extracting most recent transactionHash') + const { transactionHash: xSignaturesMostRecentTxHash = '' } = + xSignatures.sort(sortEvents).reverse()[0] || {} + const { transactionHash: xAffirmationsMostRecentTxHash = '' } = + xAffirmations.sort(sortEvents).reverse()[0] || {} - logger.debug('building transaction objects') - const foreignValidators = await Promise.all( - xSignatures.map(event => findTxSender(web3Foreign)(event)) - ) - const homeValidators = await Promise.all( - xAffirmations.map(event => findTxSender(web3Home)(event)) - ) + logger.debug('building transaction objects') + const foreignValidators = await Promise.all( + xSignatures.map(event => findTxSender(web3Foreign)(event)) + ) + const homeValidators = await Promise.all( + xAffirmations.map(event => findTxSender(web3Home)(event)) + ) - const xSignaturesTxs = xSignatures - .map(normalizeEventInformation) - .reduce(buildTxList(foreignValidators), {}) - const xAffirmationsTxs = xAffirmations - .map(normalizeEventInformation) - .reduce(buildTxList(homeValidators), {}) + const xSignaturesTxs = xSignatures + .map(normalizeEventInformation) + .reduce(buildTxList(foreignValidators), {}) + const xAffirmationsTxs = xAffirmations + .map(normalizeEventInformation) + .reduce(buildTxList(homeValidators), {}) - logger.debug('Done') + logger.debug('Done') - return { - executeSignatures: { - misbehavior: xSignaturesMisbehavior, - mostRecentTxHash: xSignaturesMostRecentTxHash, - transactions: xSignaturesTxs - }, - executeAffirmations: { - misbehavior: xAffirmationsMisbehavior, - mostRecentTxHash: xAffirmationsMostRecentTxHash, - transactions: xAffirmationsTxs - }, - lastChecked: Math.floor(Date.now() / 1000) - } - } catch (e) { - logger.error(e) - throw e + return { + executeSignatures: { + misbehavior: xSignaturesMisbehavior, + mostRecentTxHash: xSignaturesMostRecentTxHash, + transactions: xSignaturesTxs + }, + executeAffirmations: { + misbehavior: xAffirmationsMisbehavior, + mostRecentTxHash: xAffirmationsMostRecentTxHash, + transactions: xAffirmationsTxs + }, + lastChecked: Math.floor(Date.now() / 1000) } } diff --git a/monitor/checkWorker.js b/monitor/checkWorker.js index 604111e4..b2e10e9a 100644 --- a/monitor/checkWorker.js +++ b/monitor/checkWorker.js @@ -40,10 +40,8 @@ async function checkWorker() { JSON.stringify(vBalances, null, 4) ) logger.debug('Done') - return status } catch (e) { logger.error(e) - throw e } } checkWorker() diff --git a/monitor/checkWorker2.js b/monitor/checkWorker2.js index eaf16a35..be99c295 100644 --- a/monitor/checkWorker2.js +++ b/monitor/checkWorker2.js @@ -21,10 +21,8 @@ async function checkWorker2() { JSON.stringify(_alerts, null, 4) ) logger.debug('Done x2') - return evStats } catch (e) { - logger.error('checkWorker2.js', e) - throw e + logger.error(e) } } checkWorker2() diff --git a/monitor/eventsStats.js b/monitor/eventsStats.js index 4b936830..7bd36959 100644 --- a/monitor/eventsStats.js +++ b/monitor/eventsStats.js @@ -57,38 +57,33 @@ function compareTransferForeign(home) { } async function main() { - try { - const { - foreignDeposits, - homeDeposits, - homeWithdrawals, - foreignWithdrawals, - isExternalErc20 - } = await eventsInfo() + const { + foreignDeposits, + homeDeposits, + homeWithdrawals, + foreignWithdrawals, + isExternalErc20 + } = await eventsInfo() - const onlyInHomeDeposits = homeDeposits.filter(compareDepositsHome(foreignDeposits)) - const onlyInForeignDeposits = foreignDeposits - .concat([]) - .filter(compareDepositsForeign(homeDeposits)) + const onlyInHomeDeposits = homeDeposits.filter(compareDepositsHome(foreignDeposits)) + const onlyInForeignDeposits = foreignDeposits + .concat([]) + .filter(compareDepositsForeign(homeDeposits)) - const onlyInHomeWithdrawals = isExternalErc20 - ? homeWithdrawals.filter(compareTransferHome(foreignWithdrawals)) - : homeWithdrawals.filter(compareDepositsForeign(foreignWithdrawals)) - const onlyInForeignWithdrawals = isExternalErc20 - ? foreignWithdrawals.filter(compareTransferForeign(homeWithdrawals)) - : foreignWithdrawals.filter(compareDepositsHome(homeWithdrawals)) + const onlyInHomeWithdrawals = isExternalErc20 + ? homeWithdrawals.filter(compareTransferHome(foreignWithdrawals)) + : homeWithdrawals.filter(compareDepositsForeign(foreignWithdrawals)) + const onlyInForeignWithdrawals = isExternalErc20 + ? foreignWithdrawals.filter(compareTransferForeign(homeWithdrawals)) + : foreignWithdrawals.filter(compareDepositsHome(homeWithdrawals)) - logger.debug('Done') - return { - onlyInHomeDeposits, - onlyInForeignDeposits, - onlyInHomeWithdrawals, - onlyInForeignWithdrawals, - lastChecked: Math.floor(Date.now() / 1000) - } - } catch (e) { - logger.error(e) - throw e + logger.debug('Done') + return { + onlyInHomeDeposits, + onlyInForeignDeposits, + onlyInHomeWithdrawals, + onlyInForeignWithdrawals, + lastChecked: Math.floor(Date.now() / 1000) } } diff --git a/monitor/getBalances.js b/monitor/getBalances.js index 5d4a85c5..fa900308 100644 --- a/monitor/getBalances.js +++ b/monitor/getBalances.js @@ -26,107 +26,98 @@ const FOREIGN_NATIVE_TO_ERC_ABI = require('../contracts/build/contracts/ForeignB .abi async function main(bridgeMode) { - try { - if (bridgeMode === BRIDGE_MODES.ERC_TO_ERC) { - const foreignBridge = new web3Foreign.eth.Contract( - FOREIGN_ERC_TO_ERC_ABI, - FOREIGN_BRIDGE_ADDRESS - ) - const erc20Address = await foreignBridge.methods.erc20token().call() - const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) - logger.debug('calling erc20Contract.methods.balanceOf') - const foreignErc20Balance = await erc20Contract.methods - .balanceOf(FOREIGN_BRIDGE_ADDRESS) - .call() - const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_ERC_ABI, HOME_BRIDGE_ADDRESS) - logger.debug('calling homeBridge.methods.erc677token') - const tokenAddress = await homeBridge.methods.erc677token().call() - const tokenContract = new web3Home.eth.Contract(ERC677_ABI, tokenAddress) - logger.debug('calling tokenContract.methods.totalSupply()') - const totalSupply = await tokenContract.methods.totalSupply().call() - const foreignBalanceBN = new BN(foreignErc20Balance) - const foreignTotalSupplyBN = new BN(totalSupply) - const diff = foreignBalanceBN.minus(foreignTotalSupplyBN).toString(10) - logger.debug('Done') - return { - home: { - totalSupply: Web3Utils.fromWei(totalSupply) - }, - foreign: { - erc20Balance: Web3Utils.fromWei(foreignErc20Balance) - }, - balanceDiff: Number(Web3Utils.fromWei(diff)), - lastChecked: Math.floor(Date.now() / 1000) - } - } else if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC) { - logger.debug('calling web3Home.eth.getBalance') - const foreignBridge = new web3Foreign.eth.Contract( - FOREIGN_NATIVE_TO_ERC_ABI, - FOREIGN_BRIDGE_ADDRESS - ) - const erc20Address = await foreignBridge.methods.erc677token().call() - const homeBalance = await web3Home.eth.getBalance(HOME_BRIDGE_ADDRESS) - const tokenContract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) - logger.debug('calling tokenContract.methods.totalSupply()') - const totalSupply = await tokenContract.methods.totalSupply().call() - const homeBalanceBN = new BN(homeBalance) - const foreignTotalSupplyBN = new BN(totalSupply) - const diff = homeBalanceBN.minus(foreignTotalSupplyBN).toString(10) - logger.debug('Done') - return { - home: { - balance: Web3Utils.fromWei(homeBalance) - }, - foreign: { - totalSupply: Web3Utils.fromWei(totalSupply) - }, - balanceDiff: Number(Web3Utils.fromWei(diff)), - lastChecked: Math.floor(Date.now() / 1000) - } - } else if (bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) { - const foreignBridge = new web3Foreign.eth.Contract( - FOREIGN_ERC_TO_NATIVE_ABI, - FOREIGN_BRIDGE_ADDRESS - ) - const erc20Address = await foreignBridge.methods.erc20token().call() - const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) - logger.debug('calling erc20Contract.methods.balanceOf') - const foreignErc20Balance = await erc20Contract.methods - .balanceOf(FOREIGN_BRIDGE_ADDRESS) - .call() - - const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_NATIVE_ABI, HOME_BRIDGE_ADDRESS) - logger.debug('calling homeBridge.methods.blockRewardContract') - const blockRewardAddress = await homeBridge.methods.blockRewardContract().call() - const blockRewardContract = new web3Home.eth.Contract(BLOCK_REWARD_ABI, blockRewardAddress) - logger.debug('calling blockReward.methods.mintedTotally') - const mintedCoins = await blockRewardContract.methods.mintedTotally().call() - logger.debug('calling homeBridge.methods.totalBurntCoins') - const burntCoins = await homeBridge.methods.totalBurntCoins().call() - - const mintedCoinsBN = new BN(mintedCoins) - const burntCoinsBN = new BN(burntCoins) - const totalSupplyBN = mintedCoinsBN.minus(burntCoinsBN) - const foreignErc20BalanceBN = new BN(foreignErc20Balance) - - const diff = foreignErc20BalanceBN.minus(totalSupplyBN).toFixed() - logger.debug('Done') - return { - home: { - totalSupply: Web3Utils.fromWei(totalSupplyBN.toFixed()) - }, - foreign: { - erc20Balance: Web3Utils.fromWei(foreignErc20Balance) - }, - balanceDiff: Number(Web3Utils.fromWei(diff)), - lastChecked: Math.floor(Date.now() / 1000) - } - } else { - throw new Error(`Unrecognized bridge mode: '${bridgeMode}'`) + if (bridgeMode === BRIDGE_MODES.ERC_TO_ERC) { + const foreignBridge = new web3Foreign.eth.Contract( + FOREIGN_ERC_TO_ERC_ABI, + FOREIGN_BRIDGE_ADDRESS + ) + const erc20Address = await foreignBridge.methods.erc20token().call() + const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) + logger.debug('calling erc20Contract.methods.balanceOf') + const foreignErc20Balance = await erc20Contract.methods.balanceOf(FOREIGN_BRIDGE_ADDRESS).call() + const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_ERC_ABI, HOME_BRIDGE_ADDRESS) + logger.debug('calling homeBridge.methods.erc677token') + const tokenAddress = await homeBridge.methods.erc677token().call() + const tokenContract = new web3Home.eth.Contract(ERC677_ABI, tokenAddress) + logger.debug('calling tokenContract.methods.totalSupply()') + const totalSupply = await tokenContract.methods.totalSupply().call() + const foreignBalanceBN = new BN(foreignErc20Balance) + const foreignTotalSupplyBN = new BN(totalSupply) + const diff = foreignBalanceBN.minus(foreignTotalSupplyBN).toString(10) + logger.debug('Done') + return { + home: { + totalSupply: Web3Utils.fromWei(totalSupply) + }, + foreign: { + erc20Balance: Web3Utils.fromWei(foreignErc20Balance) + }, + balanceDiff: Number(Web3Utils.fromWei(diff)), + lastChecked: Math.floor(Date.now() / 1000) } - } catch (e) { - logger.error(e) - throw e + } else if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC) { + logger.debug('calling web3Home.eth.getBalance') + const foreignBridge = new web3Foreign.eth.Contract( + FOREIGN_NATIVE_TO_ERC_ABI, + FOREIGN_BRIDGE_ADDRESS + ) + const erc20Address = await foreignBridge.methods.erc677token().call() + const homeBalance = await web3Home.eth.getBalance(HOME_BRIDGE_ADDRESS) + const tokenContract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) + logger.debug('calling tokenContract.methods.totalSupply()') + const totalSupply = await tokenContract.methods.totalSupply().call() + const homeBalanceBN = new BN(homeBalance) + const foreignTotalSupplyBN = new BN(totalSupply) + const diff = homeBalanceBN.minus(foreignTotalSupplyBN).toString(10) + logger.debug('Done') + return { + home: { + balance: Web3Utils.fromWei(homeBalance) + }, + foreign: { + totalSupply: Web3Utils.fromWei(totalSupply) + }, + balanceDiff: Number(Web3Utils.fromWei(diff)), + lastChecked: Math.floor(Date.now() / 1000) + } + } else if (bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) { + const foreignBridge = new web3Foreign.eth.Contract( + FOREIGN_ERC_TO_NATIVE_ABI, + FOREIGN_BRIDGE_ADDRESS + ) + const erc20Address = await foreignBridge.methods.erc20token().call() + const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) + logger.debug('calling erc20Contract.methods.balanceOf') + const foreignErc20Balance = await erc20Contract.methods.balanceOf(FOREIGN_BRIDGE_ADDRESS).call() + + const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_NATIVE_ABI, HOME_BRIDGE_ADDRESS) + logger.debug('calling homeBridge.methods.blockRewardContract') + const blockRewardAddress = await homeBridge.methods.blockRewardContract().call() + const blockRewardContract = new web3Home.eth.Contract(BLOCK_REWARD_ABI, blockRewardAddress) + logger.debug('calling blockReward.methods.mintedTotally') + const mintedCoins = await blockRewardContract.methods.mintedTotally().call() + logger.debug('calling homeBridge.methods.totalBurntCoins') + const burntCoins = await homeBridge.methods.totalBurntCoins().call() + + const mintedCoinsBN = new BN(mintedCoins) + const burntCoinsBN = new BN(burntCoins) + const totalSupplyBN = mintedCoinsBN.minus(burntCoinsBN) + const foreignErc20BalanceBN = new BN(foreignErc20Balance) + + const diff = foreignErc20BalanceBN.minus(totalSupplyBN).toFixed() + logger.debug('Done') + return { + home: { + totalSupply: Web3Utils.fromWei(totalSupplyBN.toFixed()) + }, + foreign: { + erc20Balance: Web3Utils.fromWei(foreignErc20Balance) + }, + balanceDiff: Number(Web3Utils.fromWei(diff)), + lastChecked: Math.floor(Date.now() / 1000) + } + } else { + throw new Error(`Unrecognized bridge mode: '${bridgeMode}'`) } } diff --git a/monitor/getShortEventStats.js b/monitor/getShortEventStats.js index 8e8eaac5..9e5ccd01 100644 --- a/monitor/getShortEventStats.js +++ b/monitor/getShortEventStats.js @@ -19,79 +19,74 @@ const { getTokenType } = require('./utils/ercUtils') const { getPastEvents, getBlockNumber } = require('./utils/contract') async function main(bridgeMode) { - try { - const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode) - const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) - const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS) - const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC ? 'erc677token' : 'erc20token' - const erc20Address = await foreignBridge.methods[erc20MethodName]().call() - const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) - const tokenType = await getTokenType(foreignBridge, FOREIGN_BRIDGE_ADDRESS) + const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode) + const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) + const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS) + const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC ? 'erc677token' : 'erc20token' + const erc20Address = await foreignBridge.methods[erc20MethodName]().call() + const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) + const tokenType = await getTokenType(foreignBridge, FOREIGN_BRIDGE_ADDRESS) - logger.debug('getting last block numbers') - const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) - logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')") - const homeDeposits = await getPastEvents({ - contract: homeBridge, - event: 'UserRequestForSignature', - fromBlock: HOME_DEPLOYMENT_BLOCK, - toBlock: homeBlockNumber, - options: {} - }) + logger.debug('getting last block numbers') + const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) + logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')") + const homeDeposits = await getPastEvents({ + contract: homeBridge, + event: 'UserRequestForSignature', + fromBlock: HOME_DEPLOYMENT_BLOCK, + toBlock: homeBlockNumber, + options: {} + }) - logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')") - const foreignDeposits = await getPastEvents({ - contract: foreignBridge, - event: 'RelayedMessage', - fromBlock: FOREIGN_DEPLOYMENT_BLOCK, - toBlock: foreignBlockNumber, - options: {} - }) + logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')") + const foreignDeposits = await getPastEvents({ + contract: foreignBridge, + event: 'RelayedMessage', + fromBlock: FOREIGN_DEPLOYMENT_BLOCK, + toBlock: foreignBlockNumber, + options: {} + }) - logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')") - const homeWithdrawals = await getPastEvents({ - contract: homeBridge, - event: 'AffirmationCompleted', - fromBlock: HOME_DEPLOYMENT_BLOCK, - toBlock: homeBlockNumber, - options: {} - }) + logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')") + const homeWithdrawals = await getPastEvents({ + contract: homeBridge, + event: 'AffirmationCompleted', + fromBlock: HOME_DEPLOYMENT_BLOCK, + toBlock: homeBlockNumber, + options: {} + }) - logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')") - const foreignWithdrawals = - tokenType === ERC_TYPES.ERC20 - ? await getPastEvents({ - contract: erc20Contract, - event: 'Transfer', - fromBlock: FOREIGN_DEPLOYMENT_BLOCK, - toBlock: foreignBlockNumber, - options: { - filter: { to: FOREIGN_BRIDGE_ADDRESS } - } - }) - : await getPastEvents({ - contract: foreignBridge, - event: 'UserRequestForAffirmation', - fromBlock: FOREIGN_DEPLOYMENT_BLOCK, - toBlock: foreignBlockNumber, - options: {} - }) - logger.debug('Done') - return { - depositsDiff: homeDeposits.length - foreignDeposits.length, - withdrawalDiff: homeWithdrawals.length - foreignWithdrawals.length, - home: { - deposits: homeDeposits.length, - withdrawals: homeWithdrawals.length - }, - foreign: { - deposits: foreignDeposits.length, - withdrawals: foreignWithdrawals.length - } + logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')") + const foreignWithdrawals = + tokenType === ERC_TYPES.ERC20 + ? await getPastEvents({ + contract: erc20Contract, + event: 'Transfer', + fromBlock: FOREIGN_DEPLOYMENT_BLOCK, + toBlock: foreignBlockNumber, + options: { + filter: { to: FOREIGN_BRIDGE_ADDRESS } + } + }) + : await getPastEvents({ + contract: foreignBridge, + event: 'UserRequestForAffirmation', + fromBlock: FOREIGN_DEPLOYMENT_BLOCK, + toBlock: foreignBlockNumber, + options: {} + }) + logger.debug('Done') + return { + depositsDiff: homeDeposits.length - foreignDeposits.length, + withdrawalDiff: homeWithdrawals.length - foreignWithdrawals.length, + home: { + deposits: homeDeposits.length, + withdrawals: homeWithdrawals.length + }, + foreign: { + deposits: foreignDeposits.length, + withdrawals: foreignWithdrawals.length } - } catch (e) { - logger.error(e) - throw e } } module.exports = main diff --git a/monitor/validators.js b/monitor/validators.js index f68edd9a..d143fe66 100644 --- a/monitor/validators.js +++ b/monitor/validators.js @@ -47,109 +47,104 @@ async function getGasPrices(type) { } async function main(bridgeMode) { - try { - const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode) - const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) - const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS) - const homeValidatorsAddress = await homeBridge.methods.validatorContract().call() - const homeBridgeValidators = new web3Home.eth.Contract( - BRIDGE_VALIDATORS_ABI, - homeValidatorsAddress - ) + const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode) + const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) + const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS) + const homeValidatorsAddress = await homeBridge.methods.validatorContract().call() + const homeBridgeValidators = new web3Home.eth.Contract( + BRIDGE_VALIDATORS_ABI, + homeValidatorsAddress + ) - logger.debug('getting last block numbers') - const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) + logger.debug('getting last block numbers') + const [homeBlockNumber, foreignBlockNumber] = await getBlockNumber(web3Home, web3Foreign) - logger.debug('calling foreignBridge.methods.validatorContract().call()') - const foreignValidatorsAddress = await foreignBridge.methods.validatorContract().call() - const foreignBridgeValidators = new web3Foreign.eth.Contract( - BRIDGE_VALIDATORS_ABI, - foreignValidatorsAddress - ) + logger.debug('calling foreignBridge.methods.validatorContract().call()') + const foreignValidatorsAddress = await foreignBridge.methods.validatorContract().call() + const foreignBridgeValidators = new web3Foreign.eth.Contract( + BRIDGE_VALIDATORS_ABI, + foreignValidatorsAddress + ) - logger.debug('calling foreignBridgeValidators getValidatorList()') - const foreignValidators = await getValidatorList( - foreignValidatorsAddress, - web3Foreign.eth, - FOREIGN_DEPLOYMENT_BLOCK, - foreignBlockNumber - ) + logger.debug('calling foreignBridgeValidators getValidatorList()') + const foreignValidators = await getValidatorList( + foreignValidatorsAddress, + web3Foreign.eth, + FOREIGN_DEPLOYMENT_BLOCK, + foreignBlockNumber + ) - logger.debug('calling homeBridgeValidators getValidatorList()') - const homeValidators = await getValidatorList( - homeValidatorsAddress, - web3Home.eth, - HOME_DEPLOYMENT_BLOCK, - homeBlockNumber - ) + logger.debug('calling homeBridgeValidators getValidatorList()') + const homeValidators = await getValidatorList( + homeValidatorsAddress, + web3Home.eth, + HOME_DEPLOYMENT_BLOCK, + homeBlockNumber + ) - const homeBalances = {} - logger.debug('calling asyncForEach homeValidators homeBalances') - await asyncForEach(homeValidators, async v => { - homeBalances[v] = Web3Utils.fromWei(await web3Home.eth.getBalance(v)) - }) - const foreignVBalances = {} - const homeVBalances = {} - logger.debug('calling getGasPrices') - const gasPriceInGwei = await getGasPrices(GAS_PRICE_SPEED_TYPE) - const gasPrice = new Web3Utils.BN(Web3Utils.toWei(gasPriceInGwei.toString(10), 'gwei')) - const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT)) - let validatorsMatch = true - logger.debug('calling asyncForEach foreignValidators foreignVBalances') - await asyncForEach(foreignValidators, async v => { - const balance = await web3Foreign.eth.getBalance(v) - const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10) - foreignVBalances[v] = { - balance: Web3Utils.fromWei(balance), - leftTx: Number(leftTx), - gasPrice: gasPriceInGwei - } - if (!homeValidators.includes(v)) { - validatorsMatch = false - foreignVBalances[v].onlyOnForeign = true - } - }) - logger.debug('calling asyncForEach homeValidators homeVBalances') - await asyncForEach(homeValidators, async v => { - const gasPrice = new Web3Utils.BN(1) - const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT)) - const balance = await web3Home.eth.getBalance(v) - const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10) - homeVBalances[v] = { - balance: Web3Utils.fromWei(balance), - leftTx: Number(leftTx), - gasPrice: Number(gasPrice.toString(10)) - } - if (!foreignValidators.includes(v)) { - validatorsMatch = false - homeVBalances[v].onlyOnHome = true - } - }) - logger.debug('calling homeBridgeValidators.methods.requiredSignatures().call()') - const reqSigHome = await homeBridgeValidators.methods.requiredSignatures().call() - logger.debug('calling foreignBridgeValidators.methods.requiredSignatures().call()') - const reqSigForeign = await foreignBridgeValidators.methods.requiredSignatures().call() - logger.debug('Done') - return { - home: { - validators: { - ...homeVBalances - }, - requiredSignatures: Number(reqSigHome) - }, - foreign: { - validators: { - ...foreignVBalances - }, - requiredSignatures: Number(reqSigForeign) - }, - requiredSignaturesMatch: reqSigHome === reqSigForeign, - validatorsMatch, - lastChecked: Math.floor(Date.now() / 1000) + const homeBalances = {} + logger.debug('calling asyncForEach homeValidators homeBalances') + await asyncForEach(homeValidators, async v => { + homeBalances[v] = Web3Utils.fromWei(await web3Home.eth.getBalance(v)) + }) + const foreignVBalances = {} + const homeVBalances = {} + logger.debug('calling getGasPrices') + const gasPriceInGwei = await getGasPrices(GAS_PRICE_SPEED_TYPE) + const gasPrice = new Web3Utils.BN(Web3Utils.toWei(gasPriceInGwei.toString(10), 'gwei')) + const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT)) + let validatorsMatch = true + logger.debug('calling asyncForEach foreignValidators foreignVBalances') + await asyncForEach(foreignValidators, async v => { + const balance = await web3Foreign.eth.getBalance(v) + const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10) + foreignVBalances[v] = { + balance: Web3Utils.fromWei(balance), + leftTx: Number(leftTx), + gasPrice: gasPriceInGwei } - } catch (e) { - logger.error(e) - throw e + if (!homeValidators.includes(v)) { + validatorsMatch = false + foreignVBalances[v].onlyOnForeign = true + } + }) + logger.debug('calling asyncForEach homeValidators homeVBalances') + await asyncForEach(homeValidators, async v => { + const gasPrice = new Web3Utils.BN(1) + const txCost = gasPrice.mul(new Web3Utils.BN(GAS_LIMIT)) + const balance = await web3Home.eth.getBalance(v) + const leftTx = new Web3Utils.BN(balance).div(txCost).toString(10) + homeVBalances[v] = { + balance: Web3Utils.fromWei(balance), + leftTx: Number(leftTx), + gasPrice: Number(gasPrice.toString(10)) + } + if (!foreignValidators.includes(v)) { + validatorsMatch = false + homeVBalances[v].onlyOnHome = true + } + }) + logger.debug('calling homeBridgeValidators.methods.requiredSignatures().call()') + const reqSigHome = await homeBridgeValidators.methods.requiredSignatures().call() + logger.debug('calling foreignBridgeValidators.methods.requiredSignatures().call()') + const reqSigForeign = await foreignBridgeValidators.methods.requiredSignatures().call() + logger.debug('Done') + return { + home: { + validators: { + ...homeVBalances + }, + requiredSignatures: Number(reqSigHome) + }, + foreign: { + validators: { + ...foreignVBalances + }, + requiredSignatures: Number(reqSigForeign) + }, + requiredSignaturesMatch: reqSigHome === reqSigForeign, + validatorsMatch, + lastChecked: Math.floor(Date.now() / 1000) } }