Skip to content

Instantly share code, notes, and snippets.

@marekkirejczyk
Created May 3, 2020 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marekkirejczyk/3f03b10117469307e71a50f6d791ccb3 to your computer and use it in GitHub Desktop.
Save marekkirejczyk/3f03b10117469307e71a50f6d791ccb3 to your computer and use it in GitHub Desktop.
findEventFragment
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