contracts

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package contracts provides a set of utilities and listeners for working with Solidity contracts. It includes a ContractListener, which is a listener for the Solidity parser that extracts information about contracts, such as the contract name, implemented interfaces, imported contracts, pragmas, and comments. The ContractListener is designed to be used in conjunction with the Solidity parser to provide a convenient interface for working with Solidity contracts.

The ContractListener extracts information from Solidity contracts by traversing the Solidity parse tree and capturing relevant information during the parsing process. It identifies pragma directives, import directives, contract definitions, inheritance specifiers, using directives, and comments within the contract source code. Additionally, it can detect SPDX license identifiers if present.

The extracted contract information can be accessed using the provided getter methods, which return various aspects of the contract, such as the contract name, implemented interfaces, imported contracts, pragmas, comments, is proxy, and SPDX license. The ContractListener also provides a convenience method, ToStruct, that returns a struct containing all the extracted information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContractInfo added in v0.1.4

type ContractInfo struct {
	Comments        []string `json:"comments"`         // Comments associated with the contract
	License         string   `json:"license"`          // License information of the contract
	Pragmas         []string `json:"pragmas"`          // Pragmas specified in the contract
	Imports         []string `json:"imports"`          // Imported dependencies of the contract
	Name            string   `json:"name"`             // Name of the contract
	Implements      []string `json:"implements"`       // Interfaces implemented by the contract
	IsProxy         bool     `json:"is_proxy"`         // Whether the contract is a proxy
	ProxyConfidence int16    `json:"proxy_confidence"` // Confidence in the proxy detection
	IsContract      bool     `json:"is_contract"`      // Whether the contract is a contract
	IsInterface     bool     `json:"is_interface"`     // Whether the contract is an interface
	IsLibrary       bool     `json:"is_library"`       // Whether the contract is a library
	IsAbstract      bool     `json:"is_abstract"`      // Whether the contract is abstract
}

ContractInfo contains information about a contract

type ContractListener

type ContractListener struct {
	*parser.BaseSolidityParserListener // BaseSolidityParserListener is the base listener from the Solidity parser.
	// contains filtered or unexported fields
}

ContractListener is a listener for the Solidity parser that extracts information about contracts, including the contract name, implemented interfaces, imported contracts, pragmas, and comments. It also extracts the SPDX license identifier if present. This listener is designed to be used in conjunction with the Solidity parser to provide a convenient interface for working with Solidity contracts.

func NewContractListener

func NewContractListener(parser *parser.SolidityParser) *ContractListener

NewContractListener creates a new ContractListener. It takes a SolidityParser as an argument.

func (*ContractListener) EnterContractDefinition

func (l *ContractListener) EnterContractDefinition(ctx *parser.ContractDefinitionContext)

EnterContractDefinition is called when the parser enters a contract definition. It extracts the contract name from the context and sets it to the contractName field.

func (*ContractListener) EnterEveryRule

func (l *ContractListener) EnterEveryRule(ctx antlr.ParserRuleContext)

EnterEveryRule is called when the parser enters any rule in the grammar. It is used to search for license and any comments that code has. ANTLR parser by default have comments disabled to be parsed as tokens, so we need to search for them manually using the CommonTokenStream.

func (*ContractListener) EnterFallbackFunctionDefinition added in v0.1.4

func (l *ContractListener) EnterFallbackFunctionDefinition(ctx *parser.FallbackFunctionDefinitionContext)

EnterFallbackFunctionDefinition is called when the parser enters a fallback function definition. It checks if there's a delegatecall in the fallback function. We use this later on to determine if the contract is a proxy.

func (*ContractListener) EnterFunctionDefinition added in v0.1.4

func (l *ContractListener) EnterFunctionDefinition(ctx *parser.FunctionDefinitionContext)

EnterFunctionDefinition is called when the parser enters a function definition. It checks if there are any modifiers in the function definition and if there's a proxy modifier.

func (*ContractListener) EnterImportDirective

func (l *ContractListener) EnterImportDirective(ctx *parser.ImportDirectiveContext)

EnterImportDirective is called when the parser enters an import directive. It extracts the import path from the context and adds it to the imports slice.

func (*ContractListener) EnterInheritanceSpecifier

func (l *ContractListener) EnterInheritanceSpecifier(ctx *parser.InheritanceSpecifierContext)

EnterInheritanceSpecifier is called when the parser enters an inheritance specifier. It extracts the name of the inherited contract/interface and adds it to the implements slice.

func (*ContractListener) EnterInterfaceDefinition added in v0.1.5

func (l *ContractListener) EnterInterfaceDefinition(ctx *parser.InterfaceDefinitionContext)

func (*ContractListener) EnterLibraryDefinition added in v0.1.5

func (l *ContractListener) EnterLibraryDefinition(ctx *parser.LibraryDefinitionContext)

func (*ContractListener) EnterPragmaDirective

func (l *ContractListener) EnterPragmaDirective(ctx *parser.PragmaDirectiveContext)

EnterPragmaDirective is called when the parser enters a pragma directive. It extracts all pragma tokens from the context and adds them to the pragmas slice.

func (*ContractListener) EnterReceiveFunctionDefinition added in v0.1.4

func (l *ContractListener) EnterReceiveFunctionDefinition(ctx *parser.ReceiveFunctionDefinitionContext)

EnterReceiveFunctionDefinition is called when the parser enters a receive function definition. It checks if there's a delegatecall in the receive function. We use this later on to determine if the contract is a proxy.

func (*ContractListener) EnterStateVariableDeclaration added in v0.1.4

func (l *ContractListener) EnterStateVariableDeclaration(ctx *parser.StateVariableDeclarationContext)

EnterStateVariableDeclaration is called when the parser enters a state variable declaration. It checks if there's a state variable that could be storing the implementation address. We use this later on to determine if the contract is a proxy.

func (*ContractListener) EnterUsingDirective

func (l *ContractListener) EnterUsingDirective(ctx *parser.UsingDirectiveContext)

EnterUsingDirective is called when the parser enters a using directive. It extracts the name of the library and adds it to the libraries slice.

func (*ContractListener) ExitContractDefinition added in v0.1.4

func (l *ContractListener) ExitContractDefinition(ctx *parser.ContractDefinitionContext)

ExitContractDefinition is called when the parser exits a contract definition. It checks if the contract is a proxy and sets the IsProxy field to true if it is. It also sets the ProxyConfidence field based on current dummy algorithm.

func (*ContractListener) GetComments

func (l *ContractListener) GetComments() []string

GetComments returns a slice of all comments in the contract.

func (*ContractListener) GetImplements

func (l *ContractListener) GetImplements() []string

GetImplements returns a slice of all interfaces that the contract implements.

func (*ContractListener) GetImports

func (l *ContractListener) GetImports() []string

GetImports returns a slice of all contracts that the contract imports.

func (*ContractListener) GetIsProxy added in v0.1.4

func (l *ContractListener) GetIsProxy() bool

GetIsProxy returns true if the contract is a proxy, false otherwise.

func (*ContractListener) GetLicense

func (l *ContractListener) GetLicense() string

GetLicense returns the SPDX license identifier, if present.

func (*ContractListener) GetName

func (l *ContractListener) GetName() string

GetName returns the name of the contract.

func (*ContractListener) GetPragmas

func (l *ContractListener) GetPragmas() []string

GetPragmas returns a slice of all pragma directives in the contract.

func (*ContractListener) GetProxyConfidence added in v0.1.4

func (l *ContractListener) GetProxyConfidence() int16

GetProxyConfidence returns the confidence of the proxy detection algorithm.

func (*ContractListener) IsAbstract added in v0.1.5

func (l *ContractListener) IsAbstract() bool

func (*ContractListener) IsContract added in v0.1.5

func (l *ContractListener) IsContract() bool

func (*ContractListener) IsInterface added in v0.1.5

func (l *ContractListener) IsInterface() bool

func (*ContractListener) IsLibrary added in v0.1.5

func (l *ContractListener) IsLibrary() bool

func (*ContractListener) ToStruct

func (l *ContractListener) ToStruct() ContractInfo

GetInfoForTests returns a map of all information extracted from the contract. This is used for testing purposes only

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL