@ethersproject/abi#EventFragment TypeScript Examples

The following examples show how to use @ethersproject/abi#EventFragment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: main.ts    From squid with GNU General Public License v3.0 4 votes vote down vote up
function generateTsFromAbi(inputPathRaw: string, outputPathRaw: string): void {
    const inputPath = path.parse(inputPathRaw);
    const outputPath = path.parse(outputPathRaw);

    if (inputPath.ext !== ".json") {
        throw new Error("invalid abi file extension");
    }

    if (outputPath.ext !== ".ts") {
        throw new Error("invalid output file extension");
    }

    const rawABI = JSON.parse(fs.readFileSync(inputPathRaw, { encoding: "utf-8" }));

    const output = new Output();

    output.line("import * as ethers from \"ethers\";");
    output.line();
    output.line("export const abi = new ethers.utils.Interface(getJsonAbi());");
    output.line();

    // validate the abi
    const abi = new Interface(rawABI);

    const typeNames: { [key: string]: number } = {};

    const abiEvents: Array<AbiEvent> = Object.values(abi.events).map((event: EventFragment): AbiEvent => {
        let signature = `${event.name}(`;
        let eventTypeName = `${event.name}`;

        if(typeNames[event.name]) {
            eventTypeName += typeNames[event.name].toString();
            typeNames[event.name]++;
        } else {
            eventTypeName += "0";
            typeNames[event.name] = 1;
        }

        if(event.inputs.length > 0) {
            signature += event.inputs[0].type;
        }

        for (let i=1; i<event.inputs.length; ++i) {
            const input = event.inputs[i];
            signature += `,${input.type}`;
        }

        signature += ")";

        return {
            signature,
            eventTypeName,
            inputs: event.inputs,
        };
    });

    for(const decl of abiEvents) {
        output.block(`export interface ${decl.eventTypeName}Event`, () => {
            for (const input of decl.inputs) {
                output.line(`${input.name}: ${getType(input)};`);
            }
        });
        output.line("");
    }

    output.block("export interface EvmEvent", () => {
        output.line("data: string;");
        output.line("topics: string[];");
    });

    output.line();

    output.block("export const events =", () => {
        for(const decl of abiEvents) {
            output.block(`"${decl.signature}": `, () => {
                output.line(`topic: abi.getEventTopic("${decl.signature}"),`);
                output.block(`decode(data: EvmEvent): ${decl.eventTypeName}Event`, () => {
                    output.line(`const result = abi.decodeEventLog(`);
                    output.indentation(() => {
                        output.line(`abi.getEvent("${decl.signature}"),`);
                        output.line(`data.data || "",`);
                        output.line("data.topics");
                    });
                    output.line(");");
                    output.block("return ", () => {
                        for (let i=0; i<decl.inputs.length; ++i) {
                            const input = decl.inputs[i];
                            output.line(`${input.name}: ${`result[${i}]`},`);
                        }
                    });
                });
            });
            output.line(",");
        }
    });

    output.line();

    output.block("function getJsonAbi(): any", () => {
        `return ${JSON.stringify(rawABI, null, 2)}`.split('\n').forEach(line => {
            output.line(line)
        });
    });

    fs.writeFileSync(outputPathRaw, output.toString());
}