From 5a840c88b5b767844cfcbf32b97b4123de1757fe Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 30 May 2018 10:00:58 -0700 Subject: Change logDecoder back into class, remove awaitTransactionMined from multiSigWrapper --- packages/contracts/src/utils/log_decoder.ts | 52 ++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'packages/contracts/src/utils/log_decoder.ts') diff --git a/packages/contracts/src/utils/log_decoder.ts b/packages/contracts/src/utils/log_decoder.ts index d2e65d176..32819b657 100644 --- a/packages/contracts/src/utils/log_decoder.ts +++ b/packages/contracts/src/utils/log_decoder.ts @@ -1,19 +1,23 @@ import { ContractArtifact } from '@0xproject/sol-compiler'; -import { AbiDefinition, LogEntry, LogWithDecodedArgs, RawLog } from '@0xproject/types'; +import { + AbiDefinition, + LogEntry, + LogWithDecodedArgs, + RawLog, + TransactionReceiptWithDecodedLogs, +} from '@0xproject/types'; import { AbiDecoder, BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { artifacts } from './artifacts'; +import { constants } from './constants'; -const abiArrays: AbiDefinition[][] = []; -_.forEach(artifacts, (artifact: ContractArtifact) => { - const compilerOutput = artifact.compilerOutput; - abiArrays.push(compilerOutput.abi); -}); -const abiDecoder = new AbiDecoder(abiArrays); - -export const logDecoder = { - wrapLogBigNumbers(log: any): any { +export class LogDecoder { + private _web3Wrapper: Web3Wrapper; + private _contractAddress: string; + private _abiDecoder: AbiDecoder; + public static wrapLogBigNumbers(log: any): any { const argNames = _.keys(log.args); for (const argName of argNames) { const isWeb3BigNumber = _.startsWith(log.args[argName].constructor.toString(), 'function BigNumber('); @@ -21,13 +25,29 @@ export const logDecoder = { log.args[argName] = new BigNumber(log.args[argName]); } } - }, - decodeLogOrThrow(log: LogEntry): LogWithDecodedArgs | RawLog { - const logWithDecodedArgsOrLog = abiDecoder.tryToDecodeLogOrNoop(log); + } + constructor(web3Wrapper: Web3Wrapper, contractAddress: string) { + this._web3Wrapper = web3Wrapper; + this._contractAddress = contractAddress; + const abiArrays: AbiDefinition[][] = []; + _.forEach(artifacts, (artifact: ContractArtifact) => { + const compilerOutput = artifact.compilerOutput; + abiArrays.push(compilerOutput.abi); + }); + this._abiDecoder = new AbiDecoder(abiArrays); + } + public decodeLogOrThrow(log: LogEntry): LogWithDecodedArgs | RawLog { + const logWithDecodedArgsOrLog = this._abiDecoder.tryToDecodeLogOrNoop(log); if (_.isUndefined((logWithDecodedArgsOrLog as LogWithDecodedArgs).args)) { throw new Error(`Unable to decode log: ${JSON.stringify(log)}`); } - logDecoder.wrapLogBigNumbers(logWithDecodedArgsOrLog); + LogDecoder.wrapLogBigNumbers(logWithDecodedArgsOrLog); return logWithDecodedArgsOrLog; - }, -}; + } + public async getTxWithDecodedLogsAsync(txHash: string): Promise { + const tx = await this._web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); + tx.logs = _.filter(tx.logs, log => log.address === this._contractAddress); + tx.logs = _.map(tx.logs, log => this.decodeLogOrThrow(log)); + return tx; + } +} -- cgit v1.2.3