Skip to content

Commit

Permalink
chore: refactor parser types
Browse files Browse the repository at this point in the history
  • Loading branch information
0xGabi committed Oct 28, 2023
1 parent 01119d7 commit 1c4075e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/internal/utils/artifact/generateAragonArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function generateAragonArtifact(
flatCode: string
): AragonArtifact {
const iface = new ethers.utils.Interface(abi)

const contractFunctions = parseContractFunctions(flatCode, contractName, {
onlyTargetContract: true,
})
Expand All @@ -40,7 +40,7 @@ export function generateAragonArtifact(
roles: parsedFn.roles.map((role) => role.id),
notice: parsedFn.notice,
abi:
iface.getFunction(parsedFn.sig).format("json") ||
iface.getFunction(parsedFn.sig).format('json') ||
(parsedFn.sig === 'fallback()' ? abiFallback : null),
sig: parsedFn.sig,
})),
Expand Down
65 changes: 34 additions & 31 deletions src/internal/utils/ast/parseContractFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FunctionDefinition,
ModifierInvocation,
VariableDeclaration,
} from '@solidity-parser/parser/dist/ast-types'
} from '@solidity-parser/parser/src/ast-types'

import { parseFunctionsNotices } from './parseFunctionNotices'
import { AragonContractFunction } from './types'
Expand Down Expand Up @@ -69,7 +69,7 @@ function parseFunctionSignatureFromNode(node: FunctionDefinition): string {
* Using an isolated function to use a switch / return structure
*/
(nodeParam: VariableDeclaration): string => {
switch (nodeParam.typeName.type) {
switch (nodeParam.typeName?.type) {
case 'ElementaryTypeName':
return nodeParam.typeName.name
case 'ArrayTypeName':
Expand Down Expand Up @@ -140,7 +140,7 @@ export function parseContractFunctions(
) {
const returnParam = node.returnParameters[0]
if (
returnParam.typeName.type === 'ArrayTypeName' &&
returnParam.typeName?.type === 'ArrayTypeName' &&
returnParam.typeName.baseTypeName.type === 'ElementaryTypeName' &&
returnParam.typeName.baseTypeName.name === 'uint256'
)
Expand All @@ -162,34 +162,37 @@ export function parseContractFunctions(
function parseContract(node: ContractDefinition): void {
// Parse functions
for (const subNode of node.subNodes) {
if (
subNode.type === 'FunctionDefinition' &&
// Ignore constructors
!subNode.isConstructor &&
// Only consider functions that modify state and are public / external
subNode.visibility !== 'internal' &&
subNode.visibility !== 'private' &&
subNode.stateMutability !== 'view' &&
subNode.stateMutability !== 'pure' &&
subNode.stateMutability !== 'constant'
) {
// Check the modifiers
functions.push({
name: subNode.name || '',
notice: '',
// Parse parameters for signature, some functions may be overloaded
sig: parseFunctionSignatureFromNode(subNode),
// Parse the auth modifiers
roles: subNode.modifiers
.filter((modNode) => ['auth', 'authP'].includes(modNode.name))
.map((authModNode) => ({
id: parseRoleIdFromNode(authModNode),
paramCount: parseRoleParamCountFromNode(
authModNode,
authHelperFunctions
),
})),
})
if (subNode.type === 'FunctionDefinition') {
const functionNode = subNode as FunctionDefinition

if (
// Ignore constructors
!functionNode.isConstructor &&
// Only consider functions that modify state and are public / external
functionNode.visibility !== 'internal' &&
functionNode.visibility !== 'private' &&
functionNode.stateMutability !== 'view' &&
functionNode.stateMutability !== 'pure' &&
functionNode.stateMutability !== 'constant'
) {
// Check the modifiers
functions.push({
name: functionNode.name || '',
notice: '',
// Parse parameters for signature, some functions may be overloaded
sig: parseFunctionSignatureFromNode(functionNode),
// Parse the auth modifiers
roles: functionNode.modifiers
.filter((modNode) => ['auth', 'authP'].includes(modNode.name))
.map((authModNode) => ({
id: parseRoleIdFromNode(authModNode),
paramCount: parseRoleParamCountFromNode(
authModNode,
authHelperFunctions
),
})),
})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/utils/ast/parseContructor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, visit } from '@solidity-parser/parser'
import { FunctionDefinition } from '@solidity-parser/parser/dist/ast-types'
import { FunctionDefinition } from '@solidity-parser/parser/src/ast-types'

/**
* Returns true if a contract has a constructor, otherwise false.
Expand Down
5 changes: 3 additions & 2 deletions src/internal/utils/ast/parseGlobalVariableAssignments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, visit } from '@solidity-parser/parser'
import { StateVariableDeclaration } from '@solidity-parser/parser/dist/ast-types'
import { StateVariableDeclaration } from '@solidity-parser/parser/src/ast-types'

/**
* Finds global storage variable declarations with initialized values, e.g 'int a = 1'.
Expand All @@ -17,7 +17,8 @@ export function parseGlobalVariableAssignments(sourceCode: string): string[] {
!variable.isDeclaredConst &&
variable.expression
) {
variables.push(variable.name)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
variables.push(variable.name!)
}
},
})
Expand Down
5 changes: 4 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export interface AragonArtifactFunction {
* The function's ABI element is included for convenience of the client
* null if ABI is not found for this signature
*/
abi: utils.Fragment | null
abi:
| string
| { payable: boolean; stateMutability: string; type: string }
| null
}

export interface RoleWithBytes extends Role {
Expand Down

0 comments on commit 1c4075e

Please sign in to comment.