Skip to content

Commit

Permalink
add is method to types
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Jan 23, 2024
1 parent b65bc54 commit 184ba7b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions evm/evm-typegen/src/abi.support.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import assert from 'assert'
import * as ethers from 'ethers'


export interface LogRecord {
export interface EventRecord {
topics: string[]
data: string
}

export type LogRecord = EventRecord

export class LogEvent<Args> {
private fragment: ethers.EventFragment
Expand All @@ -17,11 +16,19 @@ export class LogEvent<Args> {
this.fragment = fragment
}

decode(rec: LogRecord): Args {
is(rec: EventRecord): boolean {
return rec.topics[0] === this.topic
}

decode(rec: EventRecord): Args {
return this.abi.decodeEventLog(this.fragment, rec.data, rec.topics) as any as Args
}
}

export interface FuncRecord {
sighash?: string
input: string
}

export class Func<Args extends any[], FieldArgs, Result> {
private fragment: ethers.FunctionFragment
Expand All @@ -32,7 +39,15 @@ export class Func<Args extends any[], FieldArgs, Result> {
this.fragment = fragment
}

decode(input: ethers.BytesLike): Args & FieldArgs {
is(rec: FuncRecord): boolean {
let sighash = rec.sighash ? rec.sighash : rec.input.slice(0, 6)
return sighash === this.sighash
}

decode(input: ethers.BytesLike): Args & FieldArgs
decode(rec: FuncRecord): Args & FieldArgs
decode(inputOrRec: ethers.BytesLike | FuncRecord): Args & FieldArgs {
const input = ethers.isBytesLike(inputOrRec) ? inputOrRec : inputOrRec.input
return this.abi.decodeFunctionData(this.fragment, input) as any as Args & FieldArgs
}

Expand Down

0 comments on commit 184ba7b

Please sign in to comment.