aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/utils/log_decoder.ts
blob: 98dd8eab6f6c3ed7196bce577c6e33e58a647dab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { LogWithDecodedArgs, RawLog } from '@0xproject/types';
import { AbiDecoder } from '@0xproject/utils';
import * as _ from 'lodash';
import * as Web3 from 'web3';

import { artifacts } from './artifacts';
import { Artifact } from './types';

export class LogDecoder {
    private _abiDecoder: AbiDecoder;
    constructor(networkId: number) {
        if (_.isUndefined(networkId)) {
            throw new Error('networkId not specified');
        }
        const abiArrays: Web3.AbiDefinition[][] = [];
        _.forEach(artifacts, (artifact: Artifact) => {
            const network = artifact.networks[networkId];
            if (_.isUndefined(network)) {
                throw new Error(`Artifact does not exist on network ${networkId}`);
            }
            abiArrays.push(network.abi);
        });
        this._abiDecoder = new AbiDecoder(abiArrays);
    }
    public tryToDecodeLogOrNoop<ArgsType>(log: Web3.LogEntry): LogWithDecodedArgs<ArgsType> | RawLog {
        const logWithDecodedArgs = this._abiDecoder.tryToDecodeLogOrNoop(log);
        return logWithDecodedArgs;
    }
}