abi

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 13 Imported by: 2

Documentation

Overview

Package abi 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 formats.

Package abi provides tools for building and parsing Ethereum ABI (Application Binary Interface) data.

Package abi provides tools for building and parsing Ethereum ABI (Application Binary Interface) data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder facilitates the construction of Ethereum ABIs.

func NewBuilderFromSources

func NewBuilderFromSources(ctx context.Context, sources *solgo.Sources) (*Builder, error)

NewBuilderFromSources initializes a new ABI builder using the provided sources. It sets up the necessary IR builder based on the given sources.

func (*Builder) Build

func (b *Builder) Build() (err error)

Build constructs the ABIs from the sources.

func (*Builder) GetAstBuilder

func (b *Builder) GetAstBuilder() *ast.ASTBuilder

GetAstBuilder returns the AST builder.

func (*Builder) GetEntryContract

func (b *Builder) GetEntryContract() *Contract

GetEntryContract retrieves the entry contract of the ABI.

func (*Builder) GetEventAsAbi added in v0.3.2

func (b *Builder) GetEventAsAbi(unit *ir.Event) ([]*Method, error)

func (*Builder) GetFunctionAsABI added in v0.3.2

func (b *Builder) GetFunctionAsABI(unit *ir.Function) ([]*Method, error)

func (*Builder) GetParser

func (b *Builder) GetParser() *ir.Builder

GetParser returns the underlying intermediate representation parser.

func (*Builder) GetRoot

func (b *Builder) GetRoot() *Root

GetRoot retrieves the root of the ABI.

func (*Builder) GetSources

func (b *Builder) GetSources() *solgo.Sources

GetSources returns the source files being processed.

func (*Builder) GetTypeResolver

func (b *Builder) GetTypeResolver() *TypeResolver

GetTypeResolver returns the type resolver for the ABI.

func (*Builder) Parse

func (b *Builder) Parse() (errs []error)

Parse processes the source files and returns any syntax or build errors.

func (*Builder) ToABI

func (p *Builder) ToABI(contract *Contract) (*abi.ABI, error)

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

func (*Builder) ToJSON

func (b *Builder) ToJSON(d any) ([]byte, error)

ToJSON returns the JSON representation of the ABI. If the provided data is not nil, it marshals the data; otherwise, it marshals the root.

func (*Builder) ToJSONPretty

func (b *Builder) ToJSONPretty() ([]byte, error)

ToJSONPretty returns a prettified JSON representation of the ABI.

func (*Builder) ToProto

func (b *Builder) ToProto() *abi_pb.Root

ToProto converts the ABI to its protocol buffer representation.

func (*Builder) ToProtoPretty

func (b *Builder) ToProtoPretty() ([]byte, error)

ToProtoPretty returns a prettified JSON representation of the protocol buffer version of the ABI.

type Contract

type Contract []*Method

Contract represents a collection of Ethereum contract methods.

func (*Contract) GetMethodByName added in v0.3.1

func (c *Contract) GetMethodByName(name string) *Method

func (*Contract) GetMethodByType added in v0.3.1

func (c *Contract) GetMethodByType(typeName string) *Method

func (*Contract) ToProto

func (c *Contract) ToProto() *abi_pb.Contract

ToProto converts the Contract into its protocol buffer representation.

type Method

type Method struct {
	Components      []MethodIO `json:"components,omitempty"` // Components of the parameter, used if it's a struct or tuple type.
	Inputs          []MethodIO `json:"inputs"`               // Input parameters of the function.
	Outputs         []MethodIO `json:"outputs"`              // Output parameters of the function.
	Name            string     `json:"name"`                 // Name of the function.
	Type            string     `json:"type"`                 // Type of the method (always "function" for functions).
	StateMutability string     `json:"stateMutability"`      // State mutability of the function (e.g., pure, view, nonpayable, payable).
}

Method represents a contract function.

func (*Method) ToJSON added in v0.3.2

func (m *Method) ToJSON() (json.RawMessage, error)

func (*Method) ToProto

func (m *Method) ToProto() *abi_pb.Method

ToProto converts the Method to its protobuf representation.

type MethodIO

type MethodIO struct {
	Indexed         bool       `json:"indexed,omitempty"`         // Indicates if the parameter is indexed. Only used by events.
	InternalType    string     `json:"internalType,omitempty"`    // Represents the internal Solidity type of the parameter.
	Name            string     `json:"name"`                      // Name of the parameter.
	Type            string     `json:"type"`                      // Type of the parameter.
	Components      []MethodIO `json:"components,omitempty"`      // Components of the parameter, used if it's a struct or tuple type.
	StateMutability string     `json:"stateMutability,omitempty"` // State mutability of the function (e.g., pure, view, nonpayable, payable).
	Inputs          []MethodIO `json:"inputs,omitempty"`          // Input parameters of the function.
	Outputs         []MethodIO `json:"outputs,omitempty"`         // Output parameters of the function.
}

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

func (*MethodIO) ToProto

func (m *MethodIO) ToProto() *abi_pb.MethodIO

ToProto converts the MethodIO to its protobuf representation.

type Root

type Root struct {
	EntryContractId   int64                `json:"entry_contract_id"`   // ID of the entry contract.
	EntryContractName string               `json:"entry_contract_name"` // Name of the entry contract.
	ContractsCount    int32                `json:"contracts_count"`     // Count of contracts in the ABI.
	Contracts         map[string]*Contract `json:"contracts"`           // Map of contract names to their respective Contract structures.
	// contains filtered or unexported fields
}

Root represents the root of a Solidity contract's ABI structure.

func (*Root) GetContractByName

func (r *Root) GetContractByName(name string) *Contract

GetContractByName retrieves a contract by its name from the ABI. Returns nil if the contract is not found.

func (*Root) GetContracts

func (r *Root) GetContracts() map[string]*Contract

GetContracts returns the map of contracts.

func (*Root) GetContractsAsSlice

func (r *Root) GetContractsAsSlice() []*Contract

GetContractsAsSlice returns the slice contracts.

func (*Root) GetContractsCount

func (r *Root) GetContractsCount() int32

GetContractsCount returns the total number of contracts in the ABI.

func (*Root) GetEntryContract

func (r *Root) GetEntryContract() *Contract

GetEntryContract retrieves the entry contract from the ABI.

func (*Root) GetEntryId

func (r *Root) GetEntryId() int64

GetEntryId returns the entry contract ID.

func (*Root) GetEntryName

func (r *Root) GetEntryName() string

GetEntryName returns the entry contract name.

func (*Root) GetIR

func (r *Root) GetIR() *ir.RootSourceUnit

GetIR returns the underlying IR node of the RootSourceUnit.

func (*Root) HasContracts

func (r *Root) HasContracts() bool

HasContracts checks if the ABI has any contracts. Returns true if there are one or more contracts, otherwise false.

func (*Root) ToProto

func (r *Root) ToProto() *abi_pb.Root

ToProto converts the Root structure to its protobuf representation.

type Type

type Type struct {
	Name         string
	Type         string
	InternalType string
	Outputs      []Type
	Components   []MethodIO
}

Type represents a type within the Ethereum ABI.

type TypeResolver

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

TypeResolver provides methods to resolve and discover types within the ABI.

func (*TypeResolver) ResolveMappingType

func (t *TypeResolver) ResolveMappingType(typeName *ast.TypeDescription) ([]MethodIO, []MethodIO)

ResolveMappingType resolves the input and output types for a given mapping type. It returns slices of MethodIO for inputs and outputs respectively.

func (*TypeResolver) ResolveStructType

func (t *TypeResolver) ResolveStructType(typeName *ast.TypeDescription) MethodIO

ResolveStructType resolves the type of a given struct and returns its MethodIO representation.

func (*TypeResolver) ResolveType

func (t *TypeResolver) ResolveType(typeName *ast.TypeDescription) string

ResolveType determines the type of a given typeName based on its identifier. It returns a string representation of the type.

Jump to

Keyboard shortcuts

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