lib

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const InterfaceTemplate string = `` /* 1487-byte string literal not displayed */

This is the Go template used to generate Solidity interfaces to contracts with a given ABI. The template is meant to be applied to InterfaceSpecification structs.

Variables

View Source
var VERSION string = "0.2.3"

The current version of solface.

Functions

func CompoundSingleValue

func CompoundSingleValue(val Value, typeCounter, nameCounter *int) (Value, []CompoundType)

Recursively creates the compound types required to represent the given value. If the top-level compound type has members which are also compound types themselves, they will be included in the second return value. The first return value is a transformation of the original value represented using the new compound types.

func FindCompoundTypes

func FindCompoundTypes(abi DecodedABI) ([]ItemValueIndex, []ItemValueIndex, []ItemValueIndex, []ItemValueIndex)

Finds all the compound types that need to be defined in order to interface with a contract with the given decoded ABI.

Return values specify which items in the following arrays are compound types (by index): 1. Event inputs 2. Function inputs 3. Function outputs 4. Error inputs

func GenerateInterface

func GenerateInterface(interfaceName, license, pragma string, abi DecodedABI, annotations Annotations, includeAnnotations bool, writer io.Writer) error

Generates a Solidity interface for the given ABI (with the given parameters). The specification is generated by applying the specification to a Go template.

func GenerateName

func GenerateName(nameCounter *int) string

Generates a fresh name for an anonymous attribute.

func GenerateType

func GenerateType(typeCounter *int, internalType string) string

Generates a fresh name for an anonymous compound type.

func MethodSelector

func MethodSelector(function FunctionItem) []byte

Calculates the 4-byte method selector for a given ABI function.

func ParseInternalType

func ParseInternalType(internalType string) string

Parses the name of an internal type and either returns that name (for structs) or "Compound" (for any other type). For nested structs (e.g. structs defined in other contracts or interfaces), this only returns the final component of the name.

func SolidityTypeRequiresLocation

func SolidityTypeRequiresLocation(solidityType string) bool

This function returns true if the given Solidity type requires a location modifier ("memory", "storage", "calldata") when used as a function parameter or return value.

Types

type Annotations

type Annotations struct {
	InterfaceID       []byte
	FunctionSelectors [][]byte
}

Represents annotations for an ABI.

func Annotate

func Annotate(decodedABI DecodedABI) (Annotations, error)

Generates annotations for a decoded ABI.

type CompoundType

type CompoundType struct {
	TypeName string
	Members  []NamedValue
}

Represents a compound type.

type DecodedABI

type DecodedABI struct {
	Events    []EventItem
	Functions []FunctionItem
	Errors    []ErrorItem
}

Represents a parsed ABI, usable in the rest of solface.

func Decode

func Decode(rawJSON []byte) (DecodedABI, error)

type DecodedABIWithCompundTypes

type DecodedABIWithCompundTypes struct {
	OriginalABI   DecodedABI
	CompoundTypes []CompoundType
	EnrichedABI   DecodedABI
}

Represents a decoded ABI along with the compound types that need to be defined in a Solidity interface to a contract exposing that ABI.

func ResolveCompounds

func ResolveCompounds(abi DecodedABI) DecodedABIWithCompundTypes

Transitively resolves all compound types comprising the parameters and return values of all items in the given decoded ABI.

type ErrorItem

type ErrorItem struct {
	Type   string
	Name   string
	Inputs []Value
}

Represents an exception/error in an ABI.

type EventArgument

type EventArgument struct {
	Value
	Indexed bool
}

Represents a parameter for an event in an ABI.

type EventItem

type EventItem struct {
	Type      string
	Name      string `json:"name"`
	Inputs    []EventArgument
	Anonymous bool
}

Represents a log event in an ABI.

type FunctionItem

type FunctionItem struct {
	Type            string
	Name            string  `json:"name,omitempty"`
	Inputs          []Value `json:"inputs,omitempty"`
	Outputs         []Value `json:"outputs,omitempty"`
	StateMutability string  `json:"stateMutability,omitempty"`
}

Represents a smart contract method in an ABI.

type InterfaceSpecification

type InterfaceSpecification struct {
	Name               string
	ABI                DecodedABI
	Annotations        Annotations
	IncludeAnnotations bool
	CompoundTypes      []CompoundType
	SolfaceVersion     string
	License            string
	Pragma             string
}

InterfaceSpecification specifies certain details about the Solidity interface that should be generated.

  1. Name: The name of the Solidity interface.
  2. ABI: The ABI that the interface is being generated for.
  3. Annotations: A list of annotations (interface ID, method selectors) for the interface.
  4. IncludeAnnotations: Whether or not to include the annotations in the generated interface.
  5. CompoundTypes: The compound types that need to be defined in the interface.
  6. SolfaceVersion: The version of solface that generated the interface.
  7. License: The SPDX license identifier to be generated at the top of the output - if empty, this will not be included.
  8. Pragma: The Solidity pragma to be generated at the top of the output - if empty, this will not be included.

type ItemValueIndex

type ItemValueIndex struct {
	ItemIndex  int
	ValueIndex int
}

Represents an ordered pair of array indices, the first index representing a position in the ABI array, and the second index representing a position in the parameters array for that ABI item.

type NamedValue

type NamedValue struct {
	Name  string
	Value Value
}

Represents a named parameter in an ABI item.

type TypeDeclaration

type TypeDeclaration struct {
	Type string
}

Represents a type declaration in an ABI.

type Value

type Value struct {
	Name         string
	Type         string
	InternalType string `json:"internalType,omitempty"`
	Components   []Value
}

Represents a value in an ABI.

func (Value) IsCompoundType

func (v Value) IsCompoundType() bool

Returns true if the given value is a compound type (i.e. composed of other types like a struct or array) and false otherwise.

Jump to

Keyboard shortcuts

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