2019-05-08 15:12:02 +02:00
|
|
|
require('dotenv').config()
|
|
|
|
const logger = require('../logger')('eventsUtils')
|
2019-07-12 14:49:46 +02:00
|
|
|
const {
|
|
|
|
BRIDGE_MODES,
|
|
|
|
getBridgeABIs,
|
|
|
|
getBridgeMode,
|
2021-05-08 09:50:46 -06:00
|
|
|
HOME_ERC_TO_NATIVE_ABI,
|
2019-07-26 14:26:14 +02:00
|
|
|
ERC20_ABI,
|
2020-06-10 16:22:01 +03:00
|
|
|
ZERO_ADDRESS,
|
|
|
|
OLD_AMB_USER_REQUEST_FOR_SIGNATURE_ABI,
|
|
|
|
OLD_AMB_USER_REQUEST_FOR_AFFIRMATION_ABI
|
2019-07-12 14:49:46 +02:00
|
|
|
} = require('../../commons')
|
2019-10-29 16:22:02 -03:00
|
|
|
const { normalizeEventInformation } = require('./message')
|
2020-05-17 17:57:49 +03:00
|
|
|
const { filterTransferBeforeES } = require('./tokenUtils')
|
2020-05-07 20:42:27 +03:00
|
|
|
const { writeFile, readCacheFile } = require('./file')
|
2020-11-04 14:24:42 +03:00
|
|
|
const { web3Home, web3Foreign, getHomeBlockNumber, getForeignBlockNumber } = require('./web3')
|
|
|
|
const { getPastEvents } = require('./web3Cache')
|
2019-05-08 15:12:02 +02:00
|
|
|
|
2020-10-29 11:25:43 +03:00
|
|
|
const { COMMON_HOME_BRIDGE_ADDRESS, COMMON_FOREIGN_BRIDGE_ADDRESS, MONITOR_CACHE_EVENTS } = process.env
|
2020-11-04 14:24:42 +03:00
|
|
|
const MONITOR_HOME_START_BLOCK = Number(process.env.MONITOR_HOME_START_BLOCK) || 0
|
|
|
|
const MONITOR_FOREIGN_START_BLOCK = Number(process.env.MONITOR_FOREIGN_START_BLOCK) || 0
|
2019-05-08 15:12:02 +02:00
|
|
|
|
2020-05-07 20:42:27 +03:00
|
|
|
const cacheFilePath = '/tmp/cachedEvents.json'
|
2019-05-31 15:35:50 -03:00
|
|
|
async function main(mode) {
|
2020-05-07 20:42:27 +03:00
|
|
|
if (MONITOR_CACHE_EVENTS === 'true') {
|
|
|
|
logger.debug('checking existing events cache')
|
|
|
|
const cachedEvents = readCacheFile(cacheFilePath)
|
|
|
|
if (cachedEvents !== false) {
|
|
|
|
logger.debug('returning events stored in cache')
|
|
|
|
return cachedEvents
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 09:50:46 -06:00
|
|
|
const homeErcBridge = new web3Home.eth.Contract(HOME_ERC_TO_NATIVE_ABI, COMMON_HOME_BRIDGE_ADDRESS)
|
2019-05-31 15:35:50 -03:00
|
|
|
const bridgeMode = mode || (await getBridgeMode(homeErcBridge))
|
|
|
|
const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode)
|
2019-09-13 09:11:38 +02:00
|
|
|
const homeBridge = new web3Home.eth.Contract(HOME_ABI, COMMON_HOME_BRIDGE_ADDRESS)
|
|
|
|
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, COMMON_FOREIGN_BRIDGE_ADDRESS)
|
2021-05-08 09:50:46 -06:00
|
|
|
let isExternalErc20 = false
|
2019-09-18 16:45:13 -03:00
|
|
|
let erc20Contract
|
2019-12-19 06:39:41 -03:00
|
|
|
let erc20Address
|
2019-10-29 16:22:02 -03:00
|
|
|
let normalizeEvent = normalizeEventInformation
|
2019-09-18 16:45:13 -03:00
|
|
|
if (bridgeMode !== BRIDGE_MODES.ARBITRARY_MESSAGE) {
|
2021-05-08 09:50:46 -06:00
|
|
|
erc20Address = await foreignBridge.methods.erc20token().call()
|
|
|
|
isExternalErc20 = true
|
2019-09-18 16:45:13 -03:00
|
|
|
erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
|
2019-10-29 16:22:02 -03:00
|
|
|
} else {
|
|
|
|
normalizeEvent = e => e
|
2019-09-18 16:45:13 -03:00
|
|
|
}
|
2019-05-08 15:12:02 +02:00
|
|
|
|
2019-05-31 15:35:50 -03:00
|
|
|
logger.debug('getting last block numbers')
|
2020-11-04 14:24:42 +03:00
|
|
|
const homeBlockNumber = await getHomeBlockNumber()
|
|
|
|
const foreignBlockNumber = await getForeignBlockNumber()
|
|
|
|
const homeConfirmations = await homeBridge.methods.requiredBlockConfirmations().call()
|
|
|
|
const foreignConfirmations = await foreignBridge.methods.requiredBlockConfirmations().call()
|
|
|
|
const homeDelayedBlockNumber = homeBlockNumber - homeConfirmations
|
|
|
|
const foreignDelayedBlockNumber = foreignBlockNumber - foreignConfirmations
|
2020-10-05 15:48:36 +03:00
|
|
|
|
2020-06-10 16:22:01 +03:00
|
|
|
let homeToForeignRequests = []
|
|
|
|
let foreignToHomeRequests = []
|
|
|
|
let homeMigrationBlock = MONITOR_HOME_START_BLOCK
|
|
|
|
let foreignMigrationBlock = MONITOR_FOREIGN_START_BLOCK
|
|
|
|
|
|
|
|
if (bridgeMode === BRIDGE_MODES.ARBITRARY_MESSAGE) {
|
|
|
|
const oldHomeBridge = new web3Home.eth.Contract(OLD_AMB_USER_REQUEST_FOR_SIGNATURE_ABI, COMMON_HOME_BRIDGE_ADDRESS)
|
|
|
|
const oldForeignBridge = new web3Foreign.eth.Contract(
|
|
|
|
OLD_AMB_USER_REQUEST_FOR_AFFIRMATION_ABI,
|
|
|
|
COMMON_FOREIGN_BRIDGE_ADDRESS
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("calling oldHomeBridge.getPastEvents('UserRequestForSignature(bytes)')")
|
|
|
|
homeToForeignRequests = (await getPastEvents(oldHomeBridge, {
|
|
|
|
event: 'UserRequestForSignature',
|
|
|
|
fromBlock: MONITOR_HOME_START_BLOCK,
|
2020-11-04 14:24:42 +03:00
|
|
|
toBlock: homeDelayedBlockNumber,
|
|
|
|
chain: 'home'
|
2020-06-10 16:22:01 +03:00
|
|
|
})).map(normalizeEvent)
|
|
|
|
logger.debug(`found ${homeToForeignRequests.length} events`)
|
|
|
|
if (homeToForeignRequests.length > 0) {
|
2020-11-04 14:24:42 +03:00
|
|
|
homeMigrationBlock = Math.max(...homeToForeignRequests.map(x => x.blockNumber))
|
2020-06-10 16:22:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("calling oldForeignBridge.getPastEvents('UserRequestForAffirmation(bytes)')")
|
|
|
|
foreignToHomeRequests = (await getPastEvents(oldForeignBridge, {
|
|
|
|
event: 'UserRequestForAffirmation',
|
|
|
|
fromBlock: MONITOR_FOREIGN_START_BLOCK,
|
2020-11-04 14:24:42 +03:00
|
|
|
toBlock: foreignDelayedBlockNumber,
|
|
|
|
chain: 'foreign'
|
2020-06-10 16:22:01 +03:00
|
|
|
})).map(normalizeEvent)
|
|
|
|
logger.debug(`found ${foreignToHomeRequests.length} events`)
|
|
|
|
if (foreignToHomeRequests.length > 0) {
|
2020-11-04 14:24:42 +03:00
|
|
|
foreignMigrationBlock = Math.max(...foreignToHomeRequests.map(x => x.blockNumber))
|
2020-06-10 16:22:01 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 15:12:02 +02:00
|
|
|
|
2019-05-31 15:35:50 -03:00
|
|
|
logger.debug("calling homeBridge.getPastEvents('UserRequestForSignature')")
|
2020-06-10 16:22:01 +03:00
|
|
|
const homeToForeignRequestsNew = (await getPastEvents(homeBridge, {
|
2021-05-08 09:50:46 -06:00
|
|
|
event: 'UserRequestForSignature',
|
2020-06-10 16:22:01 +03:00
|
|
|
fromBlock: homeMigrationBlock,
|
2020-11-04 14:24:42 +03:00
|
|
|
toBlock: homeDelayedBlockNumber,
|
|
|
|
chain: 'home'
|
2019-10-29 16:22:02 -03:00
|
|
|
})).map(normalizeEvent)
|
2020-06-10 16:22:01 +03:00
|
|
|
homeToForeignRequests = [...homeToForeignRequests, ...homeToForeignRequestsNew]
|
2019-05-08 15:12:02 +02:00
|
|
|
|
2019-05-31 15:35:50 -03:00
|
|
|
logger.debug("calling foreignBridge.getPastEvents('RelayedMessage')")
|
2019-10-29 16:22:02 -03:00
|
|
|
const homeToForeignConfirmations = (await getPastEvents(foreignBridge, {
|
2021-05-08 09:50:46 -06:00
|
|
|
event: 'RelayedMessage',
|
2019-09-13 09:11:38 +02:00
|
|
|
fromBlock: MONITOR_FOREIGN_START_BLOCK,
|
2020-11-04 14:24:42 +03:00
|
|
|
toBlock: foreignBlockNumber,
|
|
|
|
chain: 'foreign',
|
|
|
|
safeToBlock: foreignDelayedBlockNumber
|
2019-10-29 16:22:02 -03:00
|
|
|
})).map(normalizeEvent)
|
2019-05-08 15:12:02 +02:00
|
|
|
|
2019-05-31 15:35:50 -03:00
|
|
|
logger.debug("calling homeBridge.getPastEvents('AffirmationCompleted')")
|
2019-10-29 16:22:02 -03:00
|
|
|
const foreignToHomeConfirmations = (await getPastEvents(homeBridge, {
|
2021-05-08 09:50:46 -06:00
|
|
|
event: 'AffirmationCompleted',
|
2019-09-13 09:11:38 +02:00
|
|
|
fromBlock: MONITOR_HOME_START_BLOCK,
|
2020-11-04 14:24:42 +03:00
|
|
|
toBlock: homeBlockNumber,
|
|
|
|
chain: 'home',
|
|
|
|
safeToBlock: homeDelayedBlockNumber
|
2019-10-29 16:22:02 -03:00
|
|
|
})).map(normalizeEvent)
|
2019-05-08 15:12:02 +02:00
|
|
|
|
2019-05-31 15:35:50 -03:00
|
|
|
logger.debug("calling foreignBridge.getPastEvents('UserRequestForAffirmation')")
|
2020-06-10 16:22:01 +03:00
|
|
|
const foreignToHomeRequestsNew = (await getPastEvents(foreignBridge, {
|
2021-05-08 09:50:46 -06:00
|
|
|
event: 'UserRequestForAffirmation',
|
2020-06-10 16:22:01 +03:00
|
|
|
fromBlock: foreignMigrationBlock,
|
2020-11-04 14:24:42 +03:00
|
|
|
toBlock: foreignDelayedBlockNumber,
|
|
|
|
chain: 'foreign'
|
2019-10-29 16:22:02 -03:00
|
|
|
})).map(normalizeEvent)
|
2020-06-10 16:22:01 +03:00
|
|
|
foreignToHomeRequests = [...foreignToHomeRequests, ...foreignToHomeRequestsNew]
|
|
|
|
|
2021-05-26 21:58:20 +07:00
|
|
|
let informationRequests
|
|
|
|
let informationResponses
|
|
|
|
if (bridgeMode === BRIDGE_MODES.ARBITRARY_MESSAGE) {
|
|
|
|
logger.debug("calling homeBridge.getPastEvents('UserRequestForInformation')")
|
|
|
|
informationRequests = (await getPastEvents(homeBridge, {
|
|
|
|
event: 'UserRequestForInformation',
|
|
|
|
fromBlock: MONITOR_HOME_START_BLOCK,
|
|
|
|
toBlock: homeDelayedBlockNumber,
|
|
|
|
chain: 'home'
|
|
|
|
})).map(normalizeEvent)
|
|
|
|
|
|
|
|
logger.debug("calling foreignBridge.getPastEvents('InformationRetrieved')")
|
|
|
|
informationResponses = (await getPastEvents(homeBridge, {
|
|
|
|
event: 'InformationRetrieved',
|
|
|
|
fromBlock: MONITOR_HOME_START_BLOCK,
|
|
|
|
toBlock: homeBlockNumber,
|
|
|
|
safeToBlock: homeDelayedBlockNumber,
|
|
|
|
chain: 'home'
|
|
|
|
})).map(normalizeEvent)
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:22:02 -03:00
|
|
|
if (isExternalErc20) {
|
|
|
|
logger.debug("calling erc20Contract.getPastEvents('Transfer')")
|
2019-11-06 16:49:01 -03:00
|
|
|
let transferEvents = (await getPastEvents(erc20Contract, {
|
2019-10-29 16:22:02 -03:00
|
|
|
event: 'Transfer',
|
|
|
|
fromBlock: MONITOR_FOREIGN_START_BLOCK,
|
2020-10-05 15:48:36 +03:00
|
|
|
toBlock: foreignDelayedBlockNumber,
|
2019-10-29 16:22:02 -03:00
|
|
|
options: {
|
|
|
|
filter: { to: COMMON_FOREIGN_BRIDGE_ADDRESS }
|
2020-11-04 14:24:42 +03:00
|
|
|
},
|
|
|
|
chain: 'foreign'
|
2019-10-29 16:22:02 -03:00
|
|
|
})).map(normalizeEvent)
|
|
|
|
|
2020-05-17 17:57:49 +03:00
|
|
|
let directTransfers = transferEvents
|
|
|
|
const tokensSwappedAbiExists = FOREIGN_ABI.filter(e => e.type === 'event' && e.name === 'TokensSwapped')[0]
|
|
|
|
if (tokensSwappedAbiExists) {
|
|
|
|
logger.debug('collecting half duplex tokens participated in the bridge balance')
|
|
|
|
logger.debug("calling foreignBridge.getPastEvents('TokensSwapped')")
|
|
|
|
const tokensSwappedEvents = await getPastEvents(foreignBridge, {
|
|
|
|
event: 'TokensSwapped',
|
|
|
|
fromBlock: MONITOR_FOREIGN_START_BLOCK,
|
2020-11-04 14:24:42 +03:00
|
|
|
toBlock: foreignBlockNumber,
|
|
|
|
chain: 'foreign',
|
|
|
|
safeToBlock: foreignDelayedBlockNumber
|
2020-05-17 17:57:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// Get token swap events emitted by foreign bridge
|
|
|
|
const bridgeTokensSwappedEvents = tokensSwappedEvents.filter(e => e.address === COMMON_FOREIGN_BRIDGE_ADDRESS)
|
|
|
|
|
|
|
|
// Get transfer events for each previous erc20
|
|
|
|
const uniqueTokenAddressesSet = new Set(bridgeTokensSwappedEvents.map(e => e.returnValues.from))
|
|
|
|
|
|
|
|
// Exclude chai token from previous erc20
|
|
|
|
try {
|
|
|
|
logger.debug('calling foreignBridge.chaiToken() to remove it from half duplex tokens list')
|
|
|
|
const chaiToken = await foreignBridge.methods.chaiToken().call()
|
|
|
|
uniqueTokenAddressesSet.delete(chaiToken)
|
|
|
|
} catch (e) {
|
|
|
|
logger.debug('call to foreignBridge.chaiToken() failed')
|
|
|
|
}
|
|
|
|
// Exclude dai token from previous erc20
|
|
|
|
try {
|
|
|
|
logger.debug('calling foreignBridge.erc20token() to remove it from half duplex tokens list')
|
|
|
|
const daiToken = await foreignBridge.methods.erc20token().call()
|
|
|
|
uniqueTokenAddressesSet.delete(daiToken)
|
|
|
|
} catch (e) {
|
|
|
|
logger.debug('call to foreignBridge.erc20token() failed')
|
|
|
|
}
|
|
|
|
|
|
|
|
const uniqueTokenAddresses = [...uniqueTokenAddressesSet]
|
|
|
|
await Promise.all(
|
|
|
|
uniqueTokenAddresses.map(async tokenAddress => {
|
|
|
|
const halfDuplexTokenContract = new web3Foreign.eth.Contract(ERC20_ABI, tokenAddress)
|
|
|
|
|
|
|
|
logger.debug('Half duplex token:', tokenAddress)
|
|
|
|
logger.debug("calling halfDuplexTokenContract.getPastEvents('Transfer')")
|
|
|
|
const halfDuplexTransferEvents = (await getPastEvents(halfDuplexTokenContract, {
|
|
|
|
event: 'Transfer',
|
|
|
|
fromBlock: MONITOR_FOREIGN_START_BLOCK,
|
2020-10-05 15:48:36 +03:00
|
|
|
toBlock: foreignDelayedBlockNumber,
|
2020-05-17 17:57:49 +03:00
|
|
|
options: {
|
|
|
|
filter: { to: COMMON_FOREIGN_BRIDGE_ADDRESS }
|
2020-11-04 14:24:42 +03:00
|
|
|
},
|
|
|
|
chain: 'foreign'
|
2020-05-17 17:57:49 +03:00
|
|
|
})).map(normalizeEvent)
|
|
|
|
|
|
|
|
// Remove events after the ES
|
|
|
|
logger.debug('filtering half duplex transfers happened before ES')
|
|
|
|
const validHalfDuplexTransfers = await filterTransferBeforeES(halfDuplexTransferEvents)
|
|
|
|
|
|
|
|
transferEvents = [...validHalfDuplexTransfers, ...transferEvents]
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// filter transfer that is part of a token swap
|
|
|
|
directTransfers = transferEvents.filter(
|
|
|
|
e =>
|
|
|
|
bridgeTokensSwappedEvents.findIndex(
|
|
|
|
t => t.transactionHash === e.referenceTx && e.recipient === ZERO_ADDRESS
|
|
|
|
) === -1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:22:02 -03:00
|
|
|
// Get transfer events that didn't have a UserRequestForAffirmation event in the same transaction
|
2020-05-17 17:57:49 +03:00
|
|
|
directTransfers = directTransfers.filter(
|
2019-10-29 16:22:02 -03:00
|
|
|
e => foreignToHomeRequests.findIndex(t => t.referenceTx === e.referenceTx) === -1
|
|
|
|
)
|
|
|
|
|
2020-05-17 17:57:49 +03:00
|
|
|
foreignToHomeRequests = [...foreignToHomeRequests, ...directTransfers]
|
2019-10-29 16:22:02 -03:00
|
|
|
}
|
|
|
|
|
2019-05-31 15:35:50 -03:00
|
|
|
logger.debug('Done')
|
2020-05-07 20:42:27 +03:00
|
|
|
const result = {
|
2019-09-18 16:45:13 -03:00
|
|
|
homeToForeignRequests,
|
|
|
|
homeToForeignConfirmations,
|
|
|
|
foreignToHomeConfirmations,
|
|
|
|
foreignToHomeRequests,
|
2021-05-26 21:58:20 +07:00
|
|
|
informationRequests,
|
|
|
|
informationResponses,
|
2019-09-18 16:45:13 -03:00
|
|
|
isExternalErc20,
|
2020-11-04 14:24:42 +03:00
|
|
|
bridgeMode,
|
2021-04-02 12:59:49 +03:00
|
|
|
homeBlockNumber,
|
|
|
|
foreignBlockNumber,
|
2020-11-04 14:24:42 +03:00
|
|
|
homeDelayedBlockNumber,
|
|
|
|
foreignDelayedBlockNumber
|
2019-05-08 15:12:02 +02:00
|
|
|
}
|
2020-05-07 20:42:27 +03:00
|
|
|
|
|
|
|
if (MONITOR_CACHE_EVENTS === 'true') {
|
|
|
|
logger.debug('saving obtained events into cache file')
|
2021-04-02 12:59:49 +03:00
|
|
|
writeFile(cacheFilePath, result, { useCwd: false })
|
2020-05-07 20:42:27 +03:00
|
|
|
}
|
|
|
|
return result
|
2019-05-08 15:12:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = main
|