diff options
Diffstat (limited to 'packages/contracts/src/utils/log_decoder.ts')
-rw-r--r-- | packages/contracts/src/utils/log_decoder.ts | 52 |
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; + } +} |