From c5f6ebee188e35a4e7fc8b9363d04ca90e4f31ee Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Fri, 10 May 2019 16:09:09 +0200 Subject: [PATCH] Mutate ERC20 ABI to handle WETH Deposit and Withdrawl --- .../collision_resistant_abi_decoder.ts | 17 +++++++---------- .../src/order_watcher/order_watcher.ts | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/order-watcher/src/order_watcher/collision_resistant_abi_decoder.ts b/packages/order-watcher/src/order_watcher/collision_resistant_abi_decoder.ts index 52f28cd4a3..2ea7969470 100644 --- a/packages/order-watcher/src/order_watcher/collision_resistant_abi_decoder.ts +++ b/packages/order-watcher/src/order_watcher/collision_resistant_abi_decoder.ts @@ -26,19 +26,16 @@ export class CollisionResistanceAbiDecoder { this._restAbiDecoder = new AbiDecoder(abis); } public tryToDecodeLogOrNoop(log: LogEntry): LogWithDecodedArgs | RawLog { - let maybeDecodedLog = log; if (this._knownERC20Tokens.has(log.address)) { - maybeDecodedLog = this._erc20AbiDecoder.tryToDecodeLogOrNoop(log); + const maybeDecodedERC20Log = this._erc20AbiDecoder.tryToDecodeLogOrNoop(log); + return maybeDecodedERC20Log; } else if (this._knownERC721Tokens.has(log.address)) { - maybeDecodedLog = this._erc721AbiDecoder.tryToDecodeLogOrNoop(log); + const maybeDecodedERC721Log = this._erc721AbiDecoder.tryToDecodeLogOrNoop(log); + return maybeDecodedERC721Log; + } else { + const maybeDecodedLog = this._restAbiDecoder.tryToDecodeLogOrNoop(log); + return maybeDecodedLog; } - // Fall back to the supplied ABIs for decoding if the ERC20/ERC721 decoding fails - // This ensures we hit events like Deposit and Withdraw given WETH can be a known ERC20Token - const isLogDecoded = ((maybeDecodedLog as any) as LogWithDecodedArgs).event !== undefined; - if (!isLogDecoded) { - maybeDecodedLog = this._restAbiDecoder.tryToDecodeLogOrNoop(log); - } - return maybeDecodedLog; } // Hints the ABI decoder that a particular token address is ERC20 and events from it should be decoded as ERC20 events public addERC20Token(address: string): void { diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index 78c619859f..c298cbcac4 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -119,10 +119,20 @@ export class OrderWatcher { }; this._provider = provider; + const wethDepositEvent = _.find( + artifacts.WETH9.compilerOutput.abi, + item => item.type === 'event' && item.name === 'Deposit', + ); + const wethWithdrawEvent = _.find( + artifacts.WETH9.compilerOutput.abi, + item => item.type === 'event' && item.name === 'Withdrawal', + ); + // Combine ERC20 and WETH events into a single ABI. This is used as the default ERC20 ABI when decoding + const combinedWETHERC20Abi = [...artifacts.ERC20Token.compilerOutput.abi, wethDepositEvent, wethWithdrawEvent]; this._collisionResistantAbiDecoder = new CollisionResistanceAbiDecoder( - artifacts.ERC20Token.compilerOutput.abi, + combinedWETHERC20Abi, artifacts.ERC721Token.compilerOutput.abi, - [artifacts.WETH9.compilerOutput.abi, artifacts.Exchange.compilerOutput.abi], + [artifacts.Exchange.compilerOutput.abi], ); const contractWrappers = new ContractWrappers(provider, { networkId, @@ -150,6 +160,7 @@ export class OrderWatcher { this._cleanupJobInterval = config.cleanupJobIntervalMs; const zrxTokenAddress = assetDataUtils.decodeERC20AssetData(orderFilledCancelledFetcher.getZRXAssetData()) .tokenAddress; + this._collisionResistantAbiDecoder.addERC20Token(zrxTokenAddress); this._dependentOrderHashesTracker = new DependentOrderHashesTracker(zrxTokenAddress); } /**