From 0faa8b3231ddfc15723a4bdda0b6ed7aeb742bd4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 23 Nov 2018 14:03:48 +0100 Subject: Refactor contracts-core into contracts-multisig, contracts-core and contracts-test-utils --- contracts/test-utils/src/log_decoder.ts | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 contracts/test-utils/src/log_decoder.ts (limited to 'contracts/test-utils/src/log_decoder.ts') diff --git a/contracts/test-utils/src/log_decoder.ts b/contracts/test-utils/src/log_decoder.ts new file mode 100644 index 000000000..54666ea5f --- /dev/null +++ b/contracts/test-utils/src/log_decoder.ts @@ -0,0 +1,51 @@ +import { AbiDecoder, BigNumber } from '@0x/utils'; +import { Web3Wrapper } from '@0x/web3-wrapper'; +import { + AbiDefinition, + ContractArtifact, + DecodedLogArgs, + LogEntry, + LogWithDecodedArgs, + RawLog, + TransactionReceiptWithDecodedLogs, +} from 'ethereum-types'; +import * as _ from 'lodash'; + +import { constants } from './constants'; + +export class LogDecoder { + private readonly _web3Wrapper: Web3Wrapper; + private readonly _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('); + if (isWeb3BigNumber) { + log.args[argName] = new BigNumber(log.args[argName]); + } + } + } + constructor(web3Wrapper: Web3Wrapper, artifacts: { [contractName: string]: ContractArtifact }) { + this._web3Wrapper = web3Wrapper; + 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); + // tslint:disable-next-line:no-unnecessary-type-assertion + if (_.isUndefined((logWithDecodedArgsOrLog as LogWithDecodedArgs).args)) { + throw new Error(`Unable to decode log: ${JSON.stringify(log)}`); + } + 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 = _.map(tx.logs, log => this.decodeLogOrThrow(log)); + return tx; + } +} -- cgit v1.2.3