abis

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: 8 Imported by: 0

Documentation

Overview

Package abis provides functionality for parsing and manipulating Solidity contract ABIs (Application Binary Interfaces). It includes features for normalizing type names, parsing mapping types, handling struct types, detecting mapping and struct types, and converting ABIs to JSON and Go-ethereum ABI formats.

The package consists of several key components:

  • The AbiListener struct is a listener for the Solidity parser that constructs an ABI as it walks the parse tree. It extends the BaseSolidityParserListener and uses an AbiParser to build the ABI.

  • The AbiParser struct is responsible for parsing a Solidity contract ABI and converting it into an ABI object that can be easily manipulated. It maintains an internal representation of the ABI and provides methods for injecting various contract elements, such as constructors, functions, events, errors, state variables, and fallback/receive functions, into the ABI. It also handles the resolution of struct components and supports the parsing of mapping and enum types.

  • The package includes functions for normalizing type names in Solidity to their canonical forms. For example, it can convert "uint" to "uint256" and "addresspayable" to "address".

  • The package provides functions for detecting mapping types, struct types, and enum types in Solidity. It can determine if a given type name represents a mapping, if a type name corresponds to a defined struct, or if a type is an enumerated type.

  • The package offers a function for parsing mapping types in Solidity ABI. It can extract the key and value types from a mapping type string of the form "mapping(keyType => valueType)".

  • The AbiParser can convert the internal ABI representation into a JSON string or an Ethereum/go-ethereum ABI object. It provides methods for converting the ABI to these formats.

This package is part of a larger system for working with Solidity contracts and their ABIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ABI added in v0.1.4

type ABI []IMethod

ABI represents a contract ABI, which is a list of contract methods, events, and constructors.

type AbiListener

type AbiListener struct {
	*parser.BaseSolidityParserListener // Base listener that does nothing, to be extended by AbiListener
	// contains filtered or unexported fields
}

AbiListener is a listener for the Solidity parser that constructs an ABI as it walks the parse tree. It embeds the BaseSolidityParserListener and uses an AbiParser to construct the ABI.

func NewAbiListener

func NewAbiListener() *AbiListener

NewAbiListener creates a new AbiListener with a new AbiParser. It returns a pointer to the newly created AbiListener.

func (*AbiListener) EnterConstructorDefinition

func (l *AbiListener) EnterConstructorDefinition(ctx *parser.ConstructorDefinitionContext)

EnterConstructorDefinition is called when the parser enters a constructor definition. It injects the constructor into the ABI.

func (*AbiListener) EnterContractDefinition

func (l *AbiListener) 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. Later on we use contract name to define internalType of the tuple/struct type.

func (*AbiListener) EnterEnumDefinition

func (p *AbiListener) EnterEnumDefinition(ctx *parser.EnumDefinitionContext)

EnterEnumDefinition is a method of the AbiParser struct that is called when the parser encounters an enum definition in the Solidity code. It takes a context of type EnumDefinitionContext, which contains the information about the enum definition in the code. The method uses this context to extract the enum's name and its values, and stores them in the AbiParser's map of defined enums for future reference. This allows the parser to recognize the enum type when it is used later in the code.

func (*AbiListener) EnterErrorDefinition

func (l *AbiListener) EnterErrorDefinition(ctx *parser.ErrorDefinitionContext)

EnterErrorDefinition is called when the parser enters an error definition. It injects the error into the ABI.

func (*AbiListener) EnterEventDefinition

func (l *AbiListener) EnterEventDefinition(ctx *parser.EventDefinitionContext)

EnterEventDefinition is called when the parser enters an event definition. It injects the event into the ABI.

func (*AbiListener) EnterFallbackFunctionDefinition

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

EnterFallbackFunctionDefinition is called when the parser enters a fallback function definition. It injects the fallback function into the ABI.

func (*AbiListener) EnterFunctionDefinition

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

EnterFunctionDefinition is called when the parser enters a function definition. It injects the function into the ABI.

func (*AbiListener) EnterInterfaceDefinition added in v0.1.4

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

func (*AbiListener) EnterLibraryDefinition added in v0.1.4

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

func (*AbiListener) EnterReceiveFunctionDefinition

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

EnterReceiveFunctionDefinition is called when the parser enters a receive function definition. It injects the receive function into the ABI.

func (*AbiListener) EnterStateVariableDeclaration

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

EnterStateVariableDeclaration is called when the parser enters a state variable declaration. It injects the state variable into the ABI.

func (*AbiListener) EnterStructDefinition

func (l *AbiListener) EnterStructDefinition(ctx *parser.StructDefinitionContext)

EnterStructDefinition is called when the parser enters a struct definition. It appends the struct to the ABI for future resolution.

func (*AbiListener) ExitStructDefinition

func (l *AbiListener) ExitStructDefinition(ctx *parser.StructDefinitionContext)

ExitStructDefinition is called when the parser exits a struct definition. It resolves the components of the struct in the ABI.

func (*AbiListener) GetParser

func (l *AbiListener) GetParser() *AbiParser

GetParser returns the AbiParser associated with the AbiListener.

type AbiParser

type AbiParser struct {
	// contains filtered or unexported fields
}

AbiParser is a parser that can parse a Solidity contract ABI and convert it into an ABI object that can be easily manipulated.

func (*AbiParser) AppendStruct

func (p *AbiParser) AppendStruct(ctx *parser.StructDefinitionContext) error

AppendStruct injects a struct definition into internal struct mapping for future use by functions. Structs are not part of the ABI in meaning that you get a view function immediately without declaring it like in regular types. Instead, structs are used as input and output types for functions and only there they are visible. Current function will only store initial function definitions and because we have forward declarations, we will need to process additionally all structs after all declarations are processed to ensure nested structs are processed correctly.

func (*AbiParser) InjectConstructor

func (p *AbiParser) InjectConstructor(ctx *parser.ConstructorDefinitionContext) error

InjectConstructor injects a constructor definition into the ABI. It takes a ConstructorDefinitionContext (from the parser) as input.

func (*AbiParser) InjectContract

func (p *AbiParser) InjectContract(ctx *parser.ContractDefinitionContext) error

InjectContract injects a contract definition into the ABI. It takes a ContractDefinitionContext (from the parser) as input.

func (*AbiParser) InjectEnum

func (p *AbiParser) InjectEnum(ctx *parser.EnumDefinitionContext) error

InjectEnum injects an enum definition into the ABI. It takes an EnumDefinitionContext (from the parser) as input.

func (*AbiParser) InjectError

func (p *AbiParser) InjectError(ctx *parser.ErrorDefinitionContext) error

InjectError injects an error definition into the ABI. It takes an ErrorDefinitionContext (from the parser) as input.

func (*AbiParser) InjectEvent

func (p *AbiParser) InjectEvent(ctx *parser.EventDefinitionContext) error

InjectEvent injects an event definition into the ABI. It takes an EventDefinitionContext (from the parser) as input.

func (*AbiParser) InjectFallback

func (p *AbiParser) InjectFallback(ctx *parser.FallbackFunctionDefinitionContext) error

InjectFallback injects a fallback function definition into the ABI. It takes a FallbackFunctionDefinitionContext (from the parser) as input.

func (*AbiParser) InjectFunction

func (p *AbiParser) InjectFunction(ctx *parser.FunctionDefinitionContext) error

InjectFunction injects a function definition into the ABI. It takes a FunctionDefinitionContext (from the parser) as input.

func (*AbiParser) InjectModifier

func (p *AbiParser) InjectModifier(ctx *parser.ModifierDefinitionContext) error

InjectModifier injects a modifier definition into the ABI. It takes a ModifierDefinitionContext (from the parser) as input.

func (*AbiParser) InjectReceive

func (p *AbiParser) InjectReceive(ctx *parser.ReceiveFunctionDefinitionContext) error

InjectReceive injects a receive function definition into the ABI. It takes a ReceiveFunctionDefinitionContext (from the parser) as input.

func (*AbiParser) InjectStateVariable

func (p *AbiParser) InjectStateVariable(ctx *parser.StateVariableDeclarationContext) error

InjectStateVariable injects a state variable definition into the ABI. It takes a StateVariableDeclarationContext (from the parser) as input.

func (*AbiParser) ResolveStruct

func (p *AbiParser) ResolveStruct(ctx *parser.StructDefinitionContext) error

ResolveStruct iterates over the defined structs in the AbiParser and resolves their components. If a component is of a struct type, it retrieves the components of the nested struct and updates the component's type to "tuple". The component's InternalType is also updated to reflect the struct's name and the contract it belongs to. If a struct component cannot be resolved, it logs a debug message and returns an error. This function is useful for resolving nested structs and should be called after all structs have been defined.

func (*AbiParser) ToABI

func (p *AbiParser) ToABI() (*abi.ABI, error)

ToABI converts the ABI object into an ethereum/go-ethereum ABI object.

func (*AbiParser) ToJSON

func (p *AbiParser) ToJSON() (string, error)

ToJSON converts the ABI object into a JSON string.

func (*AbiParser) ToStruct

func (p *AbiParser) ToStruct() ABI

ToStruct returns the ABI object.

type ContractDefinition added in v0.1.4

type ContractDefinition struct {
	IsAbstract bool `json:"abstract"`
}

ContractDefinition represents a contract definition.

type IMethod added in v0.1.4

type IMethod interface{}

IMethod is an interface that represents a contract method, event, or constructor.

type Method added in v0.1.4

type Method struct {
	Inputs          []MethodIO `json:"inputs"`          // The input parameters of the function
	Outputs         []MethodIO `json:"outputs"`         // The output parameters of the function
	Name            string     `json:"name"`            // The name of the function
	Type            string     `json:"type"`            // The type of the method (always "function" for functions)
	StateMutability string     `json:"stateMutability"` // The state mutability of the function (pure, view, nonpayable, payable)
}

Method represents a contract function.

type MethodConstructor added in v0.1.4

type MethodConstructor struct {
	Inputs  []MethodIO `json:"inputs"`            // The input parameters of the constructor
	Type    string     `json:"type"`              // The type of the method (always "constructor" for constructors)
	Outputs []MethodIO `json:"outputs,omitempty"` // The output parameters of the constructor (always empty for constructors)
}

MethodConstructor represents a contract constructor.

type MethodEvent added in v0.1.4

type MethodEvent struct {
	Anonymous bool       `json:"anonymous"`      // Whether the event is anonymous
	Inputs    []MethodIO `json:"inputs"`         // The input parameters of the event
	Name      string     `json:"name,omitempty"` // The name of the event
	Type      string     `json:"type"`           // The type of the method (always "event" for events)
}

MethodEvent represents a contract event.

type MethodFallbackOrReceive added in v0.1.4

type MethodFallbackOrReceive struct {
	Type            string `json:"type"`                      // The type of the method (either "fallback" or "receive")
	StateMutability string `json:"stateMutability,omitempty"` // The state mutability of the function (nonpayable for fallback functions, payable for receive functions)
}

MethodFallbackOrReceive represents a contract fallback or receive function.

type MethodIO added in v0.1.4

type MethodIO struct {
	Indexed      bool       `json:"indexed,omitempty"`    // Used only by the events
	InternalType string     `json:"internalType"`         // The internal Solidity type of the parameter
	Name         string     `json:"name"`                 // The name of the parameter
	Type         string     `json:"type"`                 // The type of the parameter
	Components   []MethodIO `json:"components,omitempty"` // Components of the parameter, if it's a struct or tuple type
}

MethodIO represents an input or output parameter of a contract method or event.

type MethodVariable added in v0.1.4

type MethodVariable struct {
	Inputs          []MethodIO `json:"inputs"`          // The input parameters of the variable (always empty for variables)
	Outputs         []MethodIO `json:"outputs"`         // The output parameters of the variable (always contains one element representing the variable itself)
	Name            string     `json:"name"`            // The name of the variable
	Type            string     `json:"type"`            // The type of the method (always "function" for variables)
	StateMutability string     `json:"stateMutability"` // The state mutability of the variable (always "view" for variables)
}

MethodVariable represents a contract state variable.

Jump to

Keyboard shortcuts

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