Compare commits

...

2 Commits

Author SHA1 Message Date
Kirill Fedoseev
65dc698022 Fix interaction with the legacy v1 contracts 2020-12-08 15:33:02 +03:00
Kirill Fedoseev
c254e35d11 Find out-of-limits transfers in monitor 2020-12-08 15:07:41 +03:00
3 changed files with 207 additions and 3 deletions

@ -79,6 +79,67 @@ const homeV1Abi = [
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [],
name: 'foreignDailyLimit',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [],
name: 'foreignMaxPerTx',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [],
name: 'getCurrentDay',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [
{
name: '',
type: 'uint256'
}
],
name: 'totalExecutedPerDay',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
]
@ -182,6 +243,67 @@ const foreignViAbi = [
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [],
name: 'homeDailyLimit',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [],
name: 'homeMaxPerTx',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [],
name: 'getCurrentDay',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [
{
name: '',
type: 'uint256'
}
],
name: 'totalExecutedPerDay',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
]

@ -0,0 +1,70 @@
require('dotenv').config()
const BN = require('bignumber.js')
const { fromWei } = require('web3').utils
const logger = require('./logger')('getRequestsOutOfLimits')
const { BRIDGE_MODES, 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)
// replace the required methods by their legacy versions
if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC_V1) {
homeBridge.methods.executionDailyLimit = homeBridge.methods.foreignDailyLimit
homeBridge.methods.executionMaxPerTx = homeBridge.methods.foreignMaxPerTx
foreignBridge.methods.executionDailyLimit = foreignBridge.methods.homeDailyLimit
foreignBridge.methods.executionMaxPerTx = foreignBridge.methods.homeMaxPerTx
}
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
}
}
}