Return back filter for tx with TokensSwapped (#335)
This commit is contained in:
parent
84508e2b84
commit
f90f888ae4
@ -11,9 +11,11 @@ const {
|
|||||||
ERC20_ABI,
|
ERC20_ABI,
|
||||||
ERC677_BRIDGE_TOKEN_ABI,
|
ERC677_BRIDGE_TOKEN_ABI,
|
||||||
getTokenType,
|
getTokenType,
|
||||||
getPastEvents
|
getPastEvents,
|
||||||
|
ZERO_ADDRESS
|
||||||
} = require('../../commons')
|
} = require('../../commons')
|
||||||
const { normalizeEventInformation } = require('./message')
|
const { normalizeEventInformation } = require('./message')
|
||||||
|
const { filterTransferBeforeES } = require('./tokenUtils')
|
||||||
const { writeFile, readCacheFile } = require('./file')
|
const { writeFile, readCacheFile } = require('./file')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -109,12 +111,79 @@ async function main(mode) {
|
|||||||
}
|
}
|
||||||
})).map(normalizeEvent)
|
})).map(normalizeEvent)
|
||||||
|
|
||||||
|
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,
|
||||||
|
toBlock: foreignBlockNumber
|
||||||
|
})
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
toBlock: foreignBlockNumber,
|
||||||
|
options: {
|
||||||
|
filter: { to: COMMON_FOREIGN_BRIDGE_ADDRESS }
|
||||||
|
}
|
||||||
|
})).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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Get transfer events that didn't have a UserRequestForAffirmation event in the same transaction
|
// Get transfer events that didn't have a UserRequestForAffirmation event in the same transaction
|
||||||
transferEvents = transferEvents.filter(
|
directTransfers = directTransfers.filter(
|
||||||
e => foreignToHomeRequests.findIndex(t => t.referenceTx === e.referenceTx) === -1
|
e => foreignToHomeRequests.findIndex(t => t.referenceTx === e.referenceTx) === -1
|
||||||
)
|
)
|
||||||
|
|
||||||
foreignToHomeRequests = [...foreignToHomeRequests, ...transferEvents]
|
foreignToHomeRequests = [...foreignToHomeRequests, ...directTransfers]
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('Done')
|
logger.debug('Done')
|
||||||
|
27
monitor/utils/tokenUtils.js
Normal file
27
monitor/utils/tokenUtils.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// https://etherscan.io/tx/0xd0c3c92c94e05bc71256055ce8c4c993e047f04e04f3283a04e4cb077b71f6c6
|
||||||
|
const blockNumberHalfDuplexDisabled = 9884448
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the event was before the bridge stopped supporting half duplex transfers.
|
||||||
|
*/
|
||||||
|
async function transferBeforeES(event) {
|
||||||
|
return event.blockNumber < blockNumberHalfDuplexDisabled
|
||||||
|
}
|
||||||
|
|
||||||
|
async function filterTransferBeforeES(array) {
|
||||||
|
const newArray = []
|
||||||
|
// Iterate events from newer to older
|
||||||
|
for (let i = array.length - 1; i >= 0; i--) {
|
||||||
|
const beforeES = await transferBeforeES(array[i])
|
||||||
|
if (beforeES) {
|
||||||
|
// add element to first position so the new array will have the same order
|
||||||
|
newArray.unshift(array[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newArray
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
filterTransferBeforeES,
|
||||||
|
blockNumberHalfDuplexDisabled
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user