import { EventType , Network } from "./index" ; import { Log , Receipt } from "./receipt" ; import { Trace } from "./trace" ; import { Transaction } from "./transaction" ; import { keccak256 } from "./utils" ; import { ethers } from '.' ; import _ from "lodash" ; export interface TxEventBlock { hash : string ; number : number ; timestamp : number ; } export type LogDescription = ethers . utils . LogDescription & { address : string ; } ; export class TransactionEvent { constructor ( readonly type , readonly network , readonly transaction , readonly receipt , readonly traces = [ ] , readonly addresses , readonly block ) { } get hash ( ) { return this . transaction . hash ; } get from ( ) { return this . transaction . from ; } get to ( ) { return this . transaction . to ; } get status ( ) { return this . receipt . status ; } get gasUsed ( ) { return this . receipt . gasUsed ; } get gasPrice ( ) { return this . transaction . gasPrice ; } get logs ( ) { return this . receipt . logs ; } get timestamp ( ) { return this . block . timestamp ; } get blockNumber ( ) { return this . block . number ; } get blockHash ( ) { return this . block . hash ; } filterEvent ( eventSignature , contractAddress = '' ) { const eventTopic = keccak256 ( eventSignature ) . toLowerCase ( ) ; contractAddress = contractAddress . toLowerCase ( ) ; const events = this . receipt . logs . filter ( log => log . topics . length && log . topics [ 0 ] . toLowerCase ( ) === eventTopic && ( contractAddress . length ? log . address . toLowerCase ( ) === contractAddress : true ) ) ; return events ; } filterLog ( eventAbi , contractAddress = '' ) { eventAbi = _ . isArray ( eventAbi ) ? eventAbi : [ eventAbi ] ; let logs = this . receipt . logs ; if ( contractAddress ) { contractAddress = contractAddress . toLowerCase ( ) ; logs = logs . filter ( log => log . address . toLowerCase ( ) === contractAddress ) ; } const results = [ ] ; const iface = new ethers . utils . Interface ( eventAbi ) ; for ( const log of logs ) { try { const parsedLog = iface . parseLog ( log ) ; results . push ( Object . assign ( parsedLog , { address : log . address } ) ) ; } catch ( e ) { } } return results ; } filterFunction ( functionAbi , contractAddress = '' ) { functionAbi = _ . isArray ( functionAbi ) ? functionAbi : [ functionAbi ] ; let sources = [ this . transaction ] ; if ( this . traces . length ) { sources = this . traces . map ( ( { action } ) => ( { data : action . input , value : action . value , to : action . to } ) ) ; } if ( contractAddress ) { contractAddress = contractAddress . toLowerCase ( ) ; sources = sources . filter ( source => source . to ?. toLowerCase ( ) === contractAddress ) ; } const results = [ ] ; const iface = new ethers . utils . Interface ( functionAbi ) ; for ( const source of sources ) { try { results . push ( iface . parseTransaction ( source ) ) ; } catch ( e ) { } } return results ; } }