Created
May 3, 2020 06:32
-
-
Save marekkirejczyk/3f03b10117469307e71a50f6d791ccb3 to your computer and use it in GitHub Desktop.
findEventFragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Contract} from "ethers" | |
export class AmbiguousEvent extends Error { | |
constructor(eventName: string, candidates: string[]) { | |
super(`Ambiguous event ${eventName}, could be ${candidates.join(' or ')}`); | |
} | |
}; | |
export const findEventFragment = (contract: Contract, eventName: string) => { | |
const candidates: [string, object][] | |
= Object.entries(contract.interface.events).filter(([key]) => key.startsWith(eventName)); | |
if (!candidates.length) { | |
return undefined; | |
} else if (candidates.length === 1) { | |
return candidates[0][1]; | |
} | |
const candidateNames = candidates.map(c => c[0]); | |
throw new AmbiguousEvent(eventName, candidateNames); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment