Find out-of-limits transfers in monitor

This commit is contained in:
Kirill Fedoseev 2020-12-08 15:07:41 +03:00
parent 21581b3c01
commit c254e35d11
2 changed files with 77 additions and 3 deletions

@ -0,0 +1,62 @@
require('dotenv').config()
const BN = require('bignumber.js')
const { fromWei } = require('web3').utils
const logger = require('./logger')('getRequestsOutOfLimits')
const { getBridgeABIs } = require('../commons')
const { web3Home, web3Foreign } = require('./utils/web3')
const { COMMON_HOME_BRIDGE_ADDRESS, COMMON_FOREIGN_BRIDGE_ADDRESS } = process.env
function outOfRemainingQuota(remainingLimit) {
let limit = remainingLimit
return event => {
if (limit.lt(event.value)) {
return true
}
limit = limit.minus(event.value)
return false
}
}
async function checkOutOfLimitsStats(contract, requests) {
logger.debug('calling contract.getCurrentDay()')
const day = await contract.methods.getCurrentDay().call()
logger.debug('calling contract.executionDailyLimit()')
const executionDailyLimit = new BN(await contract.methods.executionDailyLimit().call())
logger.debug('calling contract.executionMaxPerTx()')
const executionMaxPerTx = new BN(await contract.methods.executionMaxPerTx().call())
logger.debug('calling contract.totalExecutedPerDay()')
const executedPerDay = new BN(await contract.methods.totalExecutedPerDay(day).call())
const remainingExecutionDailyLimit = executionDailyLimit.minus(executedPerDay)
// value > executionMaxPerTx
const requestsAboveMaxPerTx = requests.filter(event => executionMaxPerTx.lt(event.value))
// value <= executionMaxPerTx && remainingExecutionDailyLimit < value
const requestsAboveDailyLimit = requests
.filter(event => executionMaxPerTx.gte(event.value))
.filter(outOfRemainingQuota(remainingExecutionDailyLimit))
return {
aboveExecutionMaxPerTx: requestsAboveMaxPerTx.length,
aboveExecutionDailyLimit: requestsAboveDailyLimit.length,
aboveExecutionDailyLimitAmount: fromWei(BN.sum(0, ...requestsAboveDailyLimit.map(e => e.value)).toFixed()),
remainingExecutionDailyLimit: fromWei(remainingExecutionDailyLimit.toFixed())
}
}
async function main(bridgeMode, unprocessedRequests) {
const { homeRequests, foreignRequests } = unprocessedRequests
const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode)
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, COMMON_FOREIGN_BRIDGE_ADDRESS)
const homeBridge = new web3Home.eth.Contract(HOME_ABI, COMMON_HOME_BRIDGE_ADDRESS)
return {
home: await checkOutOfLimitsStats(homeBridge, foreignRequests),
foreign: await checkOutOfLimitsStats(foreignBridge, homeRequests)
}
}
module.exports = main

@ -8,6 +8,7 @@ const {
manuallyProcessedAMBHomeToForeignRequests
} = require('./utils/message')
const { BRIDGE_MODES } = require('../commons')
const getRequestsOutOfLimits = require('./getRequestsOutOfLimits')
const { getHomeTxSender } = require('./utils/web3Cache')
const {
@ -46,8 +47,11 @@ async function main(bridgeMode, eventsInfo) {
depositsDiff: homeToForeignRequests.length - homeToForeignConfirmations.length,
withdrawalDiff: foreignToHomeConfirmations.length - foreignToHomeRequests.length
}
if (MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST || MONITOR_HOME_TO_FOREIGN_BLOCK_LIST) {
const onlyInHomeDeposits = homeToForeignRequests.filter(eventWithoutReference(homeToForeignConfirmations))
const onlyInForeignWithdrawals = foreignToHomeRequests.filter(eventWithoutReference(foreignToHomeConfirmations))
if (MONITOR_HOME_TO_FOREIGN_ALLOWANCE_LIST || MONITOR_HOME_TO_FOREIGN_BLOCK_LIST) {
if (MONITOR_HOME_TO_FOREIGN_CHECK_SENDER === 'true') {
for (let i = 0; i < onlyInHomeDeposits.length; i++) {
onlyInHomeDeposits[i].sender = await getHomeTxSender(onlyInHomeDeposits[i].transactionHash)
@ -60,15 +64,23 @@ async function main(bridgeMode, eventsInfo) {
stats.unclaimedDiff = unclaimedPool.length
stats.unclaimedBalance = Web3Utils.fromWei(BN.sum(0, ...unclaimedPool.map(e => e.value)).toFixed())
}
const limitsStats = await getRequestsOutOfLimits(bridgeMode, {
homeRequests: onlyInHomeDeposits,
foreignRequests: onlyInForeignWithdrawals
})
return {
...stats,
home: {
deposits: homeToForeignRequests.length,
withdrawals: foreignToHomeConfirmations.length
withdrawals: foreignToHomeConfirmations.length,
outOfLimits: limitsStats.home
},
foreign: {
deposits: homeToForeignConfirmations.length,
withdrawals: foreignToHomeRequests.length
withdrawals: foreignToHomeRequests.length,
outOfLimits: limitsStats.foreign
}
}
}