aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/test-utils/src/log_decoder.ts
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-12-06 04:33:34 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-12-06 04:33:34 +0800
commitf9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac (patch)
treee1b93ec089efa842372b885d51046c845728dd75 /contracts/test-utils/src/log_decoder.ts
parent5c29b918df4ac8b0f7914e8da10fa1ae530ff4e8 (diff)
parent08eb0b91b6d0f0dc90ae920a18ca5dd080bf235c (diff)
downloaddexon-sol-tools-f9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac.tar
dexon-sol-tools-f9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac.tar.gz
dexon-sol-tools-f9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac.tar.bz2
dexon-sol-tools-f9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac.tar.lz
dexon-sol-tools-f9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac.tar.xz
dexon-sol-tools-f9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac.tar.zst
dexon-sol-tools-f9e73d2a6f6b7c3126c0c10286c2f79c4fd2a7ac.zip
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/website/instant-configurator
Diffstat (limited to 'contracts/test-utils/src/log_decoder.ts')
-rw-r--r--contracts/test-utils/src/log_decoder.ts51
1 files changed, 51 insertions, 0 deletions
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<ArgsType extends DecodedLogArgs>(log: LogEntry): LogWithDecodedArgs<ArgsType> | RawLog {
+ const logWithDecodedArgsOrLog = this._abiDecoder.tryToDecodeLogOrNoop(log);
+ // tslint:disable-next-line:no-unnecessary-type-assertion
+ if (_.isUndefined((logWithDecodedArgsOrLog as LogWithDecodedArgs<ArgsType>).args)) {
+ throw new Error(`Unable to decode log: ${JSON.stringify(log)}`);
+ }
+ LogDecoder.wrapLogBigNumbers(logWithDecodedArgsOrLog);
+ return logWithDecodedArgsOrLog;
+ }
+ public async getTxWithDecodedLogsAsync(txHash: string): Promise<TransactionReceiptWithDecodedLogs> {
+ const tx = await this._web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS);
+ tx.logs = _.map(tx.logs, log => this.decodeLogOrThrow(log));
+ return tx;
+ }
+}