aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/utils/log_decoder.ts
diff options
context:
space:
mode:
authorAmir Bandeali <abandeali1@gmail.com>2018-05-31 08:10:39 +0800
committerGitHub <noreply@github.com>2018-05-31 08:10:39 +0800
commitc0cf55b40bb4a13cfd94a506bf125f6eb57c6767 (patch)
treef2cb7cbb5ed24cdc16da99fce78b9cf2324a7e12 /packages/contracts/src/utils/log_decoder.ts
parente18d61b31a22519cd7d85ecffa62925ef7adc63d (diff)
parent5a840c88b5b767844cfcbf32b97b4123de1757fe (diff)
downloaddexon-sol-tools-c0cf55b40bb4a13cfd94a506bf125f6eb57c6767.tar
dexon-sol-tools-c0cf55b40bb4a13cfd94a506bf125f6eb57c6767.tar.gz
dexon-sol-tools-c0cf55b40bb4a13cfd94a506bf125f6eb57c6767.tar.bz2
dexon-sol-tools-c0cf55b40bb4a13cfd94a506bf125f6eb57c6767.tar.lz
dexon-sol-tools-c0cf55b40bb4a13cfd94a506bf125f6eb57c6767.tar.xz
dexon-sol-tools-c0cf55b40bb4a13cfd94a506bf125f6eb57c6767.tar.zst
dexon-sol-tools-c0cf55b40bb4a13cfd94a506bf125f6eb57c6767.zip
Merge pull request #639 from 0xProject/fix/contracts/multisigWrapper
Update LogDecoder
Diffstat (limited to 'packages/contracts/src/utils/log_decoder.ts')
-rw-r--r--packages/contracts/src/utils/log_decoder.ts52
1 files changed, 36 insertions, 16 deletions
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<ArgsType>(log: LogEntry): LogWithDecodedArgs<ArgsType> | 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<ArgsType>(log: LogEntry): LogWithDecodedArgs<ArgsType> | RawLog {
+ const logWithDecodedArgsOrLog = this._abiDecoder.tryToDecodeLogOrNoop(log);
if (_.isUndefined((logWithDecodedArgsOrLog as LogWithDecodedArgs<ArgsType>).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<TransactionReceiptWithDecodedLogs> {
+ 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;
+ }
+}