Documentation
¶
Overview ¶
Package solgo provides a suite of tools for parsing, analyzing, and interacting with Solidity contracts. It includes a contextual parser that maintains a stack of contexts as it parses a contract, allowing it to keep track of the current context (e.g., within a contract definition, function definition, etc.). It also includes a contract listener that extracts information about contracts as they are parsed, including the contract name, implemented interfaces, imported contracts, pragmas, and comments. Additionally, it includes a syntax error listener that listens for syntax errors in contracts and categorizes them by severity. The package also provides functionality for generating and working with Ethereum contract ABIs (Application Binary Interfaces). This includes parsing contract definitions to extract ABI information, normalizing type names, and handling complex types like mappings. These tools can be used together to provide a comprehensive interface for working with Solidity contracts, making it easier to understand their structure, identify potential issues, and interact with them on the Ethereum network.
Index ¶
- Variables
- type ListenerName
- type Node
- type Parser
- func (s *Parser) GetAllListeners() map[ListenerName]antlr.ParseTreeListener
- func (s *Parser) GetContextualParser() *syntaxerrors.ContextualParser
- func (s *Parser) GetInput() io.Reader
- func (s *Parser) GetInputStream() *antlr.InputStream
- func (s *Parser) GetLexer() *parser.SolidityLexer
- func (s *Parser) GetListener(name ListenerName) (antlr.ParseTreeListener, error)
- func (s *Parser) GetParser() *parser.SolidityParser
- func (s *Parser) GetSources() *Sources
- func (s *Parser) GetTokenStream() *antlr.CommonTokenStream
- func (s *Parser) GetTree() antlr.ParseTree
- func (s *Parser) IsListenerRegistered(name ListenerName) bool
- func (s *Parser) Parse() []syntaxerrors.SyntaxError
- func (s *Parser) RegisterListener(name ListenerName, listener antlr.ParseTreeListener) error
- type SourceUnit
- type Sources
- func (s *Sources) ArePrepared() bool
- func (s *Sources) GetCombinedSource() string
- func (s *Sources) GetLocalSource(partialPath string, relativeTo string) (*SourceUnit, error)
- func (s *Sources) GetSolidityVersion() (string, error)
- func (s *Sources) GetSourceUnitByName(name string) *SourceUnit
- func (s *Sources) GetSourceUnitByNameAndSize(name string, size int) *SourceUnit
- func (s *Sources) GetSourceUnitByPath(path string) *SourceUnit
- func (s *Sources) GetUnits() []*SourceUnit
- func (s *Sources) Prepare() error
- func (s *Sources) SortContracts() error
- func (s *Sources) SourceUnitExists(name string) bool
- func (s *Sources) SourceUnitExistsIn(name string, units []*SourceUnit) bool
- func (s *Sources) ToProto() *sources_pb.Sources
- func (s *Sources) TruncateDir(path string) error
- func (s *Sources) Validate() error
- func (s *Sources) WriteToDir(path string) error
Constants ¶
This section is empty.
Variables ¶
var ErrPathFound = errors.New("path found")
Functions ¶
This section is empty.
Types ¶
type ListenerName ¶
type ListenerName string
ListenerName represents the name of a listener.
const ( ListenerAbi ListenerName = "abi" ListenerContractInfo ListenerName = "contract_info" ListenerAst ListenerName = "ast" ListenerSyntaxErrors ListenerName = "syntax_errors" )
Predefined listener names.
func (ListenerName) String ¶ added in v0.1.4
func (l ListenerName) String() string
type Node ¶ added in v0.2.1
Node represents a unit of source code in Solidity with its dependencies.
type Parser ¶ added in v0.1.6
type Parser struct {
// contains filtered or unexported fields
}
Parser is a struct that encapsulates the functionality for parsing and analyzing Solidity contracts.
func NewParser ¶ added in v0.1.6
New creates a new instance of SolGo. It takes a context and an io.Reader from which the Solidity contract is read. It initializes an input stream, lexer, token stream, and parser, and sets up error listeners.
func NewParserFromSources ¶ added in v0.1.6
NewParserFromSources creates a new instance of parser from a reader. It takes a context and an io.Reader from which the Solidity contract is read. It initializes an input stream, lexer, token stream, and parser, and sets up error listeners.
func (*Parser) GetAllListeners ¶ added in v0.1.6
func (s *Parser) GetAllListeners() map[ListenerName]antlr.ParseTreeListener
func (*Parser) GetContextualParser ¶ added in v0.1.6
func (s *Parser) GetContextualParser() *syntaxerrors.ContextualParser
GetContextualParser returns the ContextualParser which wraps the Solidity parser.
func (*Parser) GetInput ¶ added in v0.1.6
GetInput returns the raw input reader from which the Solidity contract is read.
func (*Parser) GetInputStream ¶ added in v0.1.6
func (s *Parser) GetInputStream() *antlr.InputStream
GetInputStream returns the ANTLR input stream which is used by the lexer.
func (*Parser) GetLexer ¶ added in v0.1.6
func (s *Parser) GetLexer() *parser.SolidityLexer
GetLexer returns the Solidity lexer which tokenizes the input stream.
func (*Parser) GetListener ¶ added in v0.1.6
func (s *Parser) GetListener(name ListenerName) (antlr.ParseTreeListener, error)
func (*Parser) GetParser ¶ added in v0.1.6
func (s *Parser) GetParser() *parser.SolidityParser
GetParser returns the Solidity parser which parses the token stream.
func (*Parser) GetSources ¶ added in v0.1.6
GetSources returns the sources of the Solidity contract.
func (*Parser) GetTokenStream ¶ added in v0.1.6
func (s *Parser) GetTokenStream() *antlr.CommonTokenStream
GetTokenStream returns the stream of tokens produced by the lexer.
func (*Parser) GetTree ¶ added in v0.1.6
func (s *Parser) GetTree() antlr.ParseTree
GetTree returns the root of the parse tree that results from parsing the Solidity contract.
func (*Parser) IsListenerRegistered ¶ added in v0.1.6
func (s *Parser) IsListenerRegistered(name ListenerName) bool
func (*Parser) Parse ¶ added in v0.1.6
func (s *Parser) Parse() []syntaxerrors.SyntaxError
Parse initiates the parsing process. It walks the parse tree with all registered listeners and returns any syntax errors that were encountered during parsing.
func (*Parser) RegisterListener ¶ added in v0.1.6
func (s *Parser) RegisterListener(name ListenerName, listener antlr.ParseTreeListener) error
type SourceUnit ¶ added in v0.1.6
type SourceUnit struct { Name string `yaml:"name" json:"name"` Path string `yaml:"path" json:"path"` Content string `yaml:"content" json:"content"` }
SourceUnit represents a unit of source code in Solidity. It includes the name, path, and content of the source code.
func (*SourceUnit) GetContent ¶ added in v0.2.3
func (s *SourceUnit) GetContent() string
GetContent returns the content of the SourceUnit.
func (*SourceUnit) GetName ¶ added in v0.2.3
func (s *SourceUnit) GetName() string
GetName returns the name of the SourceUnit.
func (*SourceUnit) GetPath ¶ added in v0.2.3
func (s *SourceUnit) GetPath() string
GetPath returns the path of the SourceUnit.
func (*SourceUnit) String ¶ added in v0.2.3
func (s *SourceUnit) String() string
String returns a string representation of the SourceUnit.
func (*SourceUnit) ToProto ¶ added in v0.2.0
func (s *SourceUnit) ToProto() *sources_pb.SourceUnit
ToProto converts a SourceUnit to a protocol buffer SourceUnit.
type Sources ¶ added in v0.1.6
type Sources struct { SourceUnits []*SourceUnit `yaml:"source_units" json:"source_units"` EntrySourceUnitName string `yaml:"entry_source_unit" json:"base_source_unit"` MaskLocalSourcesPath bool `yaml:"mask_local_sources_path" json:"mask_local_sources_path"` LocalSourcesPath string `yaml:"local_sources_path" json:"local_sources_path"` // contains filtered or unexported fields }
Sources represents a collection of SourceUnit. It includes a slice of SourceUnit and the name of the entry source unit.
func NewSourcesFromMetadata ¶ added in v0.2.0
func NewSourcesFromMetadata(md *metadata.ContractMetadata) *Sources
NewSourcesFromMetadata creates a Sources from a metadata package ContractMetadata. This is a helper function that ensures easier integration when working with the metadata package.
func (*Sources) ArePrepared ¶ added in v0.2.0
ArePrepared returns true if the Sources has been prepared.
func (*Sources) GetCombinedSource ¶ added in v0.1.6
GetCombinedSource combines the content of all SourceUnits in the Sources into a single string, separated by two newlines.
func (*Sources) GetLocalSource ¶ added in v0.1.6
func (s *Sources) GetLocalSource(partialPath string, relativeTo string) (*SourceUnit, error)
GetLocalSource attempts to find a local source file that matches the given partial path. It searches relative to the provided path and returns a SourceUnit representing the found source. If no matching source is found, it returns nil.
The function replaces any instance of "@openzeppelin" in the partial path with the actual path to the openzeppelin-contracts repository. It then walks the file tree starting from "./sources/", checking each file against the new path.
If the new path contains "../", it removes this and looks for the file in the parent directory. If a match is found, it creates a new SourceUnit with the name and path of the file, and returns it.
If no "../" is present in the new path, it simply creates a new SourceUnit with the name and path.
After a SourceUnit is created, the function checks if the file at the path exists. If it does, it reads the file content and assigns it to the SourceUnit's Content field. If the file does not exist, it returns an error.
If the walk function encounters an error other than ErrPathFound, it returns the error. If the source is still nil after the walk, it returns nil.
func (*Sources) GetSolidityVersion ¶ added in v0.2.2
GetSolidityVersion extracts the highest Solidity version from all source units.
func (*Sources) GetSourceUnitByName ¶ added in v0.1.6
func (s *Sources) GetSourceUnitByName(name string) *SourceUnit
GetSourceUnitByName returns the SourceUnit with the given name from the Sources. If no such SourceUnit exists, it returns nil.
func (*Sources) GetSourceUnitByNameAndSize ¶ added in v0.2.3
func (s *Sources) GetSourceUnitByNameAndSize(name string, size int) *SourceUnit
GetSourceUnitByNameAndSize returns the SourceUnit with the given name and size from the Sources. If no such SourceUnit exists, it returns nil.
func (*Sources) GetSourceUnitByPath ¶ added in v0.1.6
func (s *Sources) GetSourceUnitByPath(path string) *SourceUnit
GetSourceUnitByPath returns the SourceUnit with the given path from the Sources. If no such SourceUnit exists, it returns nil.
func (*Sources) GetUnits ¶ added in v0.2.3
func (s *Sources) GetUnits() []*SourceUnit
GetUnits returns the SourceUnits in the Sources.
func (*Sources) Prepare ¶ added in v0.1.6
Prepare validates and prepares the Sources. It checks if each SourceUnit has either a path or content and a name. If a SourceUnit has a path but no content, it reads the content from the file at the path.
func (*Sources) SortContracts ¶ added in v0.2.1
SortContracts sorts the SourceUnits based on their dependencies.
func (*Sources) SourceUnitExists ¶ added in v0.1.6
SourceUnitExists returns true if a SourceUnit with the given name exists in the Sources.
func (*Sources) SourceUnitExistsIn ¶ added in v0.1.6
func (s *Sources) SourceUnitExistsIn(name string, units []*SourceUnit) bool
SourceUnitExistsIn returns true if a SourceUnit with the given name exists in the given slice of SourceUnits.
func (*Sources) ToProto ¶ added in v0.2.0
func (s *Sources) ToProto() *sources_pb.Sources
ToProto converts a Sources to a protocol buffer Sources.
func (*Sources) TruncateDir ¶ added in v0.2.0
TruncateDir removes all files and subdirectories within the specified directory.
func (*Sources) Validate ¶ added in v0.2.2
Validate checks the integrity of the Sources object. It ensures that: - There is at least one SourceUnit. - Each SourceUnit has a name and either a path or content. - If a SourceUnit has a path, the file at that path exists. - The entry source unit name is valid.
func (*Sources) WriteToDir ¶ added in v0.2.0
WriteToDir writes each SourceUnit's content to a file in the specified directory.
Directories
¶
Path | Synopsis |
---|---|
Package abi provides functionality for parsing and manipulating Solidity contract ABIs (Application Binary Interfaces).
|
Package abi provides functionality for parsing and manipulating Solidity contract ABIs (Application Binary Interfaces). |
Package ast provides an Abstract Syntax Tree (AST) representation for Solidity contracts.
|
Package ast provides an Abstract Syntax Tree (AST) representation for Solidity contracts. |
Package audit provides a comprehensive suite of tools for auditing smart contracts.
|
Package audit provides a comprehensive suite of tools for auditing smart contracts. |
Package bytecode provides tools for decoding and analyzing Ethereum contract, transaction, event and log bytecode.
|
Package bytecode provides tools for decoding and analyzing Ethereum contract, transaction, event and log bytecode. |
Package cfg offers a comprehensive toolkit for constructing and visualizing control flow graphs (CFGs) of Solidity smart contracts.
|
Package cfg offers a comprehensive toolkit for constructing and visualizing control flow graphs (CFGs) of Solidity smart contracts. |
Package detector provides helpers for accessing solgo packages.
|
Package detector provides helpers for accessing solgo packages. |
Package ir provides an intermediate representation of the AST.
|
Package ir provides an intermediate representation of the AST. |
Package metadata provides functionality for interacting with IPFS/SWARM and retrieving contract metadata.
|
Package metadata provides functionality for interacting with IPFS/SWARM and retrieving contract metadata. |
Package opcode offers tools for constructing and visualizing opcode execution trees, representing sequences of instructions.
|
Package opcode offers tools for constructing and visualizing opcode execution trees, representing sequences of instructions. |
Package standards provides structures and functions to represent and manipulate Ethereum Improvement Proposals (EIPs) and Ethereum standards.
|
Package standards provides structures and functions to represent and manipulate Ethereum Improvement Proposals (EIPs) and Ethereum standards. |
Package syntaxerrors provides tools for detecting and handling syntax errors in Solidity contracts.
|
Package syntaxerrors provides tools for detecting and handling syntax errors in Solidity contracts. |
Package validation provides utilities for verifying Ethereum based smart contracts.
|
Package validation provides utilities for verifying Ethereum based smart contracts. |