import { isUndefined } from "lodash" ; import { AbiType , DecodedLogEntry } from "@joincivil/typescript-types" ; import Web3 from "web3" ; import { Log } from "web3-core" ; import { AbiItem } from "web3-utils" ; import { JsonRpcPayload } from "web3-core-helpers" ; type JsonRpcResponse = any ; const stubProvider = { send : ( payload , callback ) => ( { } ) , host : "x" , supportsSubscriptions : ( ) => false , sendBatch : async ( methods , moduleInstance ) => Promise . reject ( [ ] ) , connected : false , disconnect : ( ) => true , } ; export class AbiDecoder { private savedABIs : any [ ] = [ ] ; private methodIds : { [ signatureHash ] : AbiItem ; } = { } ; constructor ( abiArrays ) { abiArrays . forEach ( this . addABI . bind ( this ) ) ; } public tryToDecodeLogOrNoop < LogType extends DecodedLogEntry = DecodedLogEntry > ( log ) { const methodId = log . topics [ 0 ] ; const event = this . methodIds [ methodId as string ] ; if ( isUndefined ( event ) ) { return log ; } const decoded = new Web3 ( stubProvider ) . eth . abi . decodeLog ( event . inputs ! , log . data , log . topics ) ; return ( { ... log , event : event . name as any , args : decoded , } as any ) as LogType ; } private addABI ( abiArray ) { abiArray . forEach ( abi => { if ( abi . type === AbiType . Event ) { const abiTypes = abi . inputs ! . map ( input => input . type ) ; const signature = `${ abi . name }(${ abiTypes . join ( "," ) })` ; const signatureHash = new Web3 ( stubProvider ) . utils . sha3 ( signature ) ; this . methodIds [ signatureHash ] = abi ; } } ) ; this . savedABIs = this . savedABIs . concat ( abiArray ) ; } }