Mutate ERC20 ABI to handle WETH Deposit and Withdrawl

This commit is contained in:
Jacob Evans
2019-05-10 16:09:09 +02:00
parent f944b95e32
commit c5f6ebee18
2 changed files with 20 additions and 12 deletions

View File

@@ -26,19 +26,16 @@ export class CollisionResistanceAbiDecoder {
this._restAbiDecoder = new AbiDecoder(abis);
}
public tryToDecodeLogOrNoop<ArgsType extends DecodedLogArgs>(log: LogEntry): LogWithDecodedArgs<ArgsType> | 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<DecodedLogArgs>).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 {

View File

@@ -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);
}
/**