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 NewSourcesFromEtherScan(entryContractName string, sc interface{}) (*Sources, error)
- func NewSourcesFromMetadata(md *metadata.ContractMetadata) *Sources
- func NewSourcesFromPath(entrySourceUnitName, path string) (*Sources, error)
- func NewSourcesFromProto(entryContractName string, sc *sources_pb.Sources) (*Sources, error)
- func (s *Sources) AppendSource(source *SourceUnit)
- 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) HasUnits() bool
- func (s *Sources) Prepare() error
- func (s *Sources) ReplaceSource(old *SourceUnit, newSource *SourceUnit)
- func (s *Sources) SortContracts() error
- func (s *Sources) SourceUnitExists(name string) bool
- func (s *Sources) SourceUnitExistsIn(name string, units []*SourceUnit) bool
- func (s *Sources) SourceUnitPathExists(name string) bool
- func (s *Sources) SourceUnitPathExistsIn(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 ¶
func (l ListenerName) String() string
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a struct that encapsulates the functionality for parsing and analyzing Solidity contracts.
func NewParser ¶
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 ¶
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 ¶
func (s *Parser) GetAllListeners() map[ListenerName]antlr.ParseTreeListener
func (*Parser) GetContextualParser ¶
func (s *Parser) GetContextualParser() *syntaxerrors.ContextualParser
GetContextualParser returns the ContextualParser which wraps the Solidity parser.
func (*Parser) GetInput ¶
GetInput returns the raw input reader from which the Solidity contract is read.
func (*Parser) GetInputStream ¶
func (s *Parser) GetInputStream() *antlr.InputStream
GetInputStream returns the ANTLR input stream which is used by the lexer.
func (*Parser) GetLexer ¶
func (s *Parser) GetLexer() *parser.SolidityLexer
GetLexer returns the Solidity lexer which tokenizes the input stream.
func (*Parser) GetListener ¶
func (s *Parser) GetListener(name ListenerName) (antlr.ParseTreeListener, error)
func (*Parser) GetParser ¶
func (s *Parser) GetParser() *parser.SolidityParser
GetParser returns the Solidity parser which parses the token stream.
func (*Parser) GetSources ¶
GetSources returns the sources of the Solidity contract.
func (*Parser) GetTokenStream ¶
func (s *Parser) GetTokenStream() *antlr.CommonTokenStream
GetTokenStream returns the stream of tokens produced by the lexer.
func (*Parser) GetTree ¶
func (s *Parser) GetTree() antlr.ParseTree
GetTree returns the root of the parse tree that results from parsing the Solidity contract.
func (*Parser) IsListenerRegistered ¶
func (s *Parser) IsListenerRegistered(name ListenerName) bool
func (*Parser) Parse ¶
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 ¶
func (s *Parser) RegisterListener(name ListenerName, listener antlr.ParseTreeListener) error
type SourceUnit ¶
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) GetBasePath ¶ added in v0.3.1
func (s *SourceUnit) GetBasePath() string
GetBasePath returns the base path of the SourceUnit.
func (*SourceUnit) GetContent ¶
func (s *SourceUnit) GetContent() string
GetContent returns the content of the SourceUnit.
func (*SourceUnit) GetName ¶
func (s *SourceUnit) GetName() string
GetName returns the name of the SourceUnit.
func (*SourceUnit) GetPath ¶
func (s *SourceUnit) GetPath() string
GetPath returns the path of the SourceUnit.
func (*SourceUnit) String ¶
func (s *SourceUnit) String() string
String returns a string representation of the SourceUnit.
func (*SourceUnit) ToProto ¶
func (s *SourceUnit) ToProto() *sources_pb.SourceUnit
ToProto converts a SourceUnit to a protocol buffer SourceUnit.
type Sources ¶
type Sources struct { SourceUnits []*SourceUnit `yaml:"source_units" json:"source_units"` EntrySourceUnitName string `yaml:"entry_source_unit" json:"base_source_unit"` LocalSources bool `yaml:"local_sources" json:"local_sources"` 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 represent a collection of SourceUnit. It includes a slice of SourceUnit and the name of the entry source unit.
func NewSourcesFromEtherScan ¶
NewSourcesFromEtherScan creates a Sources from an EtherScan response. This is a helper function that ensures easier integration when working with the EtherScan provider. This includes BscScan, and other equivalent from the same family.
func NewSourcesFromMetadata ¶
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 NewSourcesFromPath ¶ added in v0.3.2
func NewSourcesFromProto ¶ added in v0.3.2
func NewSourcesFromProto(entryContractName string, sc *sources_pb.Sources) (*Sources, error)
func (*Sources) AppendSource ¶ added in v0.3.1
func (s *Sources) AppendSource(source *SourceUnit)
AppendSource appends a SourceUnit to the Sources. If a SourceUnit with the same name already exists, it replaces it unless the new SourceUnit has less content.
func (*Sources) ArePrepared ¶
ArePrepared returns true if the Sources has been prepared.
func (*Sources) GetCombinedSource ¶
GetCombinedSource combines the content of all SourceUnits in the Sources into a single string, separated by two newlines.
func (*Sources) GetLocalSource ¶
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 ¶
GetSolidityVersion extracts the highest Solidity version from all source units.
func (*Sources) GetSourceUnitByName ¶
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 ¶
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 ¶
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 ¶
func (s *Sources) GetUnits() []*SourceUnit
GetUnits returns the SourceUnits in the Sources.
func (*Sources) Prepare ¶
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) ReplaceSource ¶ added in v0.3.1
func (s *Sources) ReplaceSource(old *SourceUnit, newSource *SourceUnit)
ReplaceSource replaces an old SourceUnit with a new one. If the old SourceUnit is not found, nothing happens.
func (*Sources) SortContracts ¶
SortContracts sorts the SourceUnits based on their dependencies.
func (*Sources) SourceUnitExists ¶
SourceUnitExists returns true if a SourceUnit with the given name exists in the Sources.
func (*Sources) SourceUnitExistsIn ¶
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) SourceUnitPathExists ¶ added in v0.3.2
SourceUnitExists returns true if a SourceUnit with the given name exists in the Sources.
func (*Sources) SourceUnitPathExistsIn ¶ added in v0.3.2
func (s *Sources) SourceUnitPathExistsIn(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 ¶
func (s *Sources) ToProto() *sources_pb.Sources
ToProto converts a Sources to a protocol buffer Sources.
func (*Sources) TruncateDir ¶
TruncateDir removes all files and subdirectories within the specified directory.
func (*Sources) Validate ¶
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 ¶
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). |
The package is designed to be flexible and adaptable to various Ethereum-compatible networks like Ethereum mainnet, Binance Smart Chain (BSC), and Polygon.
|
The package is designed to be flexible and adaptable to various Ethereum-compatible networks like Ethereum mainnet, Binance Smart Chain (BSC), and Polygon. |
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 bindings abstracts the complexity of interacting with smart contracts deployed on the Ethereum blockchain and potentially other compatible networks.
|
Package bindings abstracts the complexity of interacting with smart contracts deployed on the Ethereum blockchain and potentially other compatible networks. |
Package bytecode provides tools for decoding and analyzing Ethereum contract, transaction, events, and log bytecode.
|
Package bytecode provides tools for decoding and analyzing Ethereum contract, transaction, events, and log bytecode. |
Package cfg provides structures and functions for building and manipulating control flow graphs (CFGs) of Solidity contracts.
|
Package cfg provides structures and functions for building and manipulating control flow graphs (CFGs) of Solidity contracts. |
Package clients provides tools for managing and interacting with Ethereum clients.
|
Package clients provides tools for managing and interacting with Ethereum clients. |
Package integrates various Ethereum-related functionalities including interaction with blockchain clients, fetching contract metadata from external providers such as etherscan, token discovery, bytecode validation, auditing, etc...
|
Package integrates various Ethereum-related functionalities including interaction with blockchain clients, fetching contract metadata from external providers such as etherscan, token discovery, bytecode validation, auditing, etc... |
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 observers provides tools for managing and interacting with Ethereum onchain data such as blocks, transactions, events and logs.
|
Package observers provides tools for managing and interacting with Ethereum onchain data such as blocks, transactions, events and logs. |
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. |
providers
|
|
bitquery
Package bitquery provides functions to query blockchain contract creation information using the Ethereum blockchain.
|
Package bitquery provides functions to query blockchain contract creation information using the Ethereum blockchain. |
etherscan
Package etherscan provides a client for interacting with Etherscan's API, enabling the retrieval of blockchain data and supporting rate-limited API key rotation.
|
Package etherscan provides a client for interacting with Etherscan's API, enabling the retrieval of blockchain data and supporting rate-limited API key rotation. |
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 storage provides tools for interacting with smart contract storages on the Ethereum based networks.
|
Package storage provides tools for interacting with smart contract storages on the Ethereum based networks. |
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 tokens provides a high-level abstraction for interacting with Ethereum tokens.
|
Package tokens provides a high-level abstraction for interacting with Ethereum tokens. |
Package validation provides utilities for verifying Ethereum based smart contracts.
|
Package validation provides utilities for verifying Ethereum based smart contracts. |