Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce updated typegen #250

Closed
wants to merge 13 commits into from
3,414 changes: 2,209 additions & 1,205 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions evm/evm-typegen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@subsquid/evm-typegen",
"version": "3.3.0",
"version": "4.0.0",
"description": "CLI for generating typescript types and decode implementations for evm logs",
"license": "GPL-3.0-or-later",
"repository": "[email protected]:subsquid/squid.git",
Expand All @@ -16,21 +16,24 @@
"squid-evm-typegen": "bin/run.js"
},
"scripts": {
"build": "rm -rf lib && tsc"
"build": "rm -rf lib && tsc -p tsconfig.build.json"
},
"dependencies": {
"@subsquid/evm-utils": "1.0.0",
"@subsquid/http-client": "^1.3.2",
"@subsquid/logger": "^1.3.3",
"@subsquid/util-internal": "^3.0.0",
"@subsquid/util-internal-code-printer": "^1.2.2",
"@subsquid/util-internal-commander": "^1.3.2",
"commander": "^11.1.0"
},
"peerDependencies": {
"ethers": "^6.9.0"
"commander": "^11.1.0",
"solc": "^0.8.24",
"abitype": "^1.0.0",
"viem": "^2.7.11"
},
"devDependencies": {
"@types/node": "^18.18.14",
"typescript": "~5.3.2"
"types-solc": "^1.0.1",
"typescript": "~5.3.2",
"vitest": "^1.3.1"
}
}
135 changes: 0 additions & 135 deletions evm/evm-typegen/src/abi.support.ts

This file was deleted.

111 changes: 111 additions & 0 deletions evm/evm-typegen/src/devdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const solc = require("solc");

interface Devdoc {
details: string;
params: {
[name: string]: string;
};
returns: {
[name: string]: string;
};
}

function formatDevdoc(devdoc: Devdoc) {
if (!devdoc.details && !devdoc.params && !devdoc.returns) {
return undefined;
}
const details = devdoc.details ? ` * ${devdoc.details}\n` : "";
const params = devdoc.params
? `${Object.entries(devdoc.params ?? [])
.map(([name, details]) => ` * @param ${name} ${details}`)
.join("\n")}\n`
: "";
const returns = devdoc.returns
? `${Object.entries(devdoc.returns ?? [])
.map(
([name, details]) =>
` * @return ${name.match(/^_\d+$/) ? "" : name + " "}${details}`
)
.join("\n")}\n`
: "";
return `/**
${details}${params}${returns} */`;
}

export async function devdoc(
source: string | object,
settings: any,
compilerVersion = "latest"
) {
let sources: {
[name: string]: {
content: string;
};
};
if (typeof source === "string") {
sources = {
"Contract.sol": {
content: source,
},
};
} else {
sources = source as any;
}
const input = {
language: "Solidity",
sources,
settings: {
...settings,
outputSelection: {
"*": {
"*": ["devdoc"],
},
},
},
};

const output: any = await new Promise((resolve) =>
solc.loadRemoteVersion(compilerVersion, (err: any, solcSnapshot: any) => {
if (err) {
resolve(undefined);
}
const output = JSON.parse(solcSnapshot.compile(JSON.stringify(input)));
resolve(output);
})
);

if (!output) {
return {
methods: {},
events: {},
};
}

const getDocs = (get: "events" | "methods") =>
Object.values(output.contracts)
.map((x: any) => Object.values(x))
.flatMap((x: any) => x?.[0]?.devdoc?.[get] ?? [])
.reduce(
(acc: any, x: any) => ({
...acc,
...x,
}),
{}
);
const events = getDocs("events");
const methods = getDocs("methods");
const formattedMethods = Object.fromEntries(
Object.entries(methods)
.map(([name, doc]) => [name, formatDevdoc(doc as any)])
.filter(([_, doc]) => !!doc)
);
const formattedEvents = Object.fromEntries(
Object.entries(events)
.map(([name, doc]) => [name, formatDevdoc(doc as any)])
.filter(([_, doc]) => !!doc)
);
return {
methods: formattedMethods,
events: formattedEvents,
};
}
Loading
Loading