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

fix(common): include only errors defined in the contract #2194

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-plants-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/common": patch
---

Prevented errors not included in the contract (but present in the file) from being included in the interface by `contractToInterface`
100 changes: 59 additions & 41 deletions packages/common/src/codegen/utils/contractToInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { parse, visit } from "@solidity-parser/parser";
import type { SourceUnit, TypeName, VariableDeclaration } from "@solidity-parser/parser/dist/src/ast-types";
import type {
ContractDefinition,
SourceUnit,
TypeName,
VariableDeclaration,
} from "@solidity-parser/parser/dist/src/ast-types";
import { MUDError } from "../../errors";

export interface ContractInterfaceFunction {
Expand Down Expand Up @@ -36,52 +41,55 @@ export function contractToInterface(
} {
const ast = parse(data);

let withContract = false;
const contractNode = findContractNode(parse(data), contractName);
let symbolImports: SymbolImport[] = [];
const functions: ContractInterfaceFunction[] = [];
const errors: ContractInterfaceError[] = [];

visit(ast, {
ContractDefinition({ name }) {
if (name === contractName) {
withContract = true;
}
},
FunctionDefinition(
{ name, visibility, parameters, stateMutability, returnParameters, isConstructor, isFallback, isReceiveEther },
parent
) {
if (parent !== undefined && parent.type === "ContractDefinition" && parent.name === contractName) {
try {
// skip constructor and fallbacks
if (isConstructor || isFallback || isReceiveEther) return;
// forbid default visibility (this check might be unnecessary, modern solidity already disallows this)
if (visibility === "default") throw new MUDError(`Visibility is not specified`);

if (visibility === "external" || visibility === "public") {
functions.push({
name: name === null ? "" : name,
parameters: parameters.map(parseParameter),
stateMutability: stateMutability || "",
returnParameters: returnParameters === null ? [] : returnParameters.map(parseParameter),
});

for (const { typeName } of parameters.concat(returnParameters ?? [])) {
const symbols = typeNameToSymbols(typeName);
symbolImports = symbolImports.concat(symbolsToImports(ast, symbols));
}
}
} catch (error: unknown) {
if (error instanceof MUDError) {
error.message = `Function "${name}" in contract "${contractName}": ${error.message}`;
if (!contractNode) {
throw new MUDError(`Contract not found: ${contractName}`);
}

visit(contractNode, {
FunctionDefinition({
name,
visibility,
parameters,
stateMutability,
returnParameters,
isConstructor,
isFallback,
isReceiveEther,
}) {
try {
// skip constructor and fallbacks
if (isConstructor || isFallback || isReceiveEther) return;
// forbid default visibility (this check might be unnecessary, modern solidity already disallows this)
if (visibility === "default") throw new MUDError(`Visibility is not specified`);

if (visibility === "external" || visibility === "public") {
functions.push({
name: name === null ? "" : name,
parameters: parameters.map(parseParameter),
stateMutability: stateMutability || "",
returnParameters: returnParameters === null ? [] : returnParameters.map(parseParameter),
});

for (const { typeName } of parameters.concat(returnParameters ?? [])) {
const symbols = typeNameToSymbols(typeName);
symbolImports = symbolImports.concat(symbolsToImports(ast, symbols));
}
throw error;
}
} catch (error: unknown) {
if (error instanceof MUDError) {
error.message = `Function "${name}" in contract "${contractName}": ${error.message}`;
}
throw error;
}
},
CustomErrorDefinition({ name, parameters }) {
errors.push({
name: name === null ? "" : name,
name,
parameters: parameters.map(parseParameter),
});

Expand All @@ -92,17 +100,27 @@ export function contractToInterface(
},
});

if (!withContract) {
throw new MUDError(`Contract not found: ${contractName}`);
}

return {
functions,
errors,
symbolImports,
};
}

function findContractNode(ast: SourceUnit, contractName: string): ContractDefinition | undefined {
let contract = undefined;

visit(ast, {
ContractDefinition(node) {
if (node.name === contractName) {
contract = node;
}
},
});

return contract;
}

function parseParameter({ name, typeName, storageLocation }: VariableDeclaration): string {
let typedNameWithLocation = "";

Expand Down
Loading