bindings

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: 12 Imported by: 4

Documentation

Overview

Package bindings abstracts the complexity of interacting with smart contracts deployed on the Ethereum blockchain and potentially other compatible networks. It provides developers with a structured approach to manage contract bindings, execute contract calls, and subscribe to contract events, etc...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BindOptions

type BindOptions struct {
	Networks  []utils.Network // A list of networks on which the contract is deployed.
	NetworkID utils.NetworkID // The unique identifier for the blockchain network.
	Name      string          // The name of the binding, for identification purposes.
	Type      BindingType     // The type of binding, indicating the contract standard (e.g., ERC20, ERC721).
	Address   common.Address  // The blockchain address of the contract.
	ABI       string          // The JSON string representing the contract's Application Binary Interface.
}

BindOptions defines the configuration parameters required to establish a binding to a smart contract. It includes network-specific settings, contract metadata, and the contract's ABI (Application Binary Interface). These options are used to initialize and manage contract bindings, allowing for interactions with the contract across different blockchain networks.

func DefaultTokenBindOptions

func DefaultTokenBindOptions(address common.Address) []*BindOptions

DefaultTokenBindOptions generates a default set of BindOptions for ERC20 and ERC20Ownable tokens. It presets configurations such as networks, network IDs, types, contract addresses, and ABIs based on standard implementations. This function simplifies the setup process for common token types.

func (*BindOptions) Validate

func (b *BindOptions) Validate() error

Validate checks the integrity and completeness of the BindOptions. It ensures that all necessary information is provided and valid, such as the presence of at least one network, a valid network ID, a specified binding type, a non-zero contract address, and a non-empty ABI definition. This validation step is crucial before establishing a binding to a contract to prevent runtime errors and ensure reliable contract interaction.

type Binding

type Binding struct {
	Type    BindingType    // The type of the contract binding (e.g., ERC20, ERC721).
	Address common.Address // The blockchain address of the contract.
	RawABI  string         // The raw string representation of the contract's ABI.
	ABI     *abi.ABI       // The parsed contract ABI, providing programmatic access to its contents.
	// contains filtered or unexported fields
}

Binding represents a specific contract binding, including its network, type, address, and ABI. It serves as a central point for accessing contract-related data and functionalities, such as querying available methods and events defined in the contract ABI.

func (*Binding) EventExist

func (b *Binding) EventExist(eventName string) bool

EventExist checks if a specified event exists in the contract ABI.

func (*Binding) GetABI

func (b *Binding) GetABI() *abi.ABI

GetABI returns the parsed contract ABI.

func (*Binding) GetAddress

func (b *Binding) GetAddress() common.Address

GetAddress returns the blockchain address of the contract.

func (*Binding) GetAllEvents

func (b *Binding) GetAllEvents() []string

GetAllEvents returns all the event names defined in the contract ABI.

func (*Binding) GetAllMethods

func (b *Binding) GetAllMethods() []string

GetAllMethods returns all the method names defined in the contract ABI.

func (*Binding) GetNetwork

func (b *Binding) GetNetwork() utils.Network

GetNetwork returns the network where the contract is deployed.

func (*Binding) GetNetworkID

func (b *Binding) GetNetworkID() utils.NetworkID

GetNetworkID returns the unique identifier of the network.

func (*Binding) GetRawABI

func (b *Binding) GetRawABI() string

GetRawABI returns the raw string representation of the contract's ABI.

func (*Binding) GetType

func (b *Binding) GetType() BindingType

GetType returns the type of the contract binding.

func (*Binding) MethodExist

func (b *Binding) MethodExist(methodName string) bool

MethodExist checks if a specified method exists in the contract ABI.

type BindingType

type BindingType string

BindingType defines a custom type for representing different types of contract bindings. It is a string type that enables easy identification and differentiation of various contract standards and interfaces, such as ERC20, ERC20Ownable, etc. This type aids in the classification and management of contract interactions within the bindings package.

const (
	Erc20        BindingType = "ERC20"
	Erc20Ownable BindingType = "ERC20Ownable"
)

BindingType is a custom type that defines various supported token standards. Currently, it includes ERC20 and ERC20Ownable types, but it can be extended to support more standards in the future.

func (BindingType) String

func (b BindingType) String() string

String is a receiver method of the BindingType type that converts the BindingType value back into a string. This method provides a straightforward way to obtain the string representation of a BindingType, facilitating logging, debugging, and any other scenarios where a string representation is more convenient or necessary.

type CreatorInformation

type CreatorInformation struct {
	ContractCreator common.Address `json:"creator"` // The Ethereum address of the contract creator.
	CreationHash    common.Hash    `json:"hash"`    // The hash of the transaction used to create the contract.
}

CreatorInformation holds data related to the creation of a smart contract on the Ethereum blockchain. It includes the address of the contract creator and the transaction hash of the contract creation transaction. This struct is typically used to retrieve and store information about how and by whom a contract was deployed to the blockchain.

type Manager

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

Manager acts as a central registry for smart contract bindings. It enables the configuration, management, and interaction with smart contracts across different networks. The Manager maintains a client pool for network communications, a context for managing lifecycle events, and a mutex for thread-safe operation.

func NewManager

func NewManager(ctx context.Context, clientPool *clients.ClientPool) (*Manager, error)

NewManager creates a new Manager instance with a specified context and client pool. It ensures that the contract standards are loaded before initialization. This constructor is suitable for production use where interaction with real network clients is required.

func (*Manager) BindingExist

func (m *Manager) BindingExist(network utils.Network, name BindingType) bool

BindingExist checks whether a specific binding exists within the Manager, aiding in conditional logic and binding management.

func (*Manager) CallContractMethod

func (m *Manager) CallContractMethod(ctx context.Context, network utils.Network, bindingType BindingType, toAddr common.Address, methodName string, params ...interface{}) (any, error)

CallContractMethod executes a method call on a smart contract, handling the data packing, RPC call execution, and results unpacking.

func (*Manager) CallContractMethodUnpackMap

func (m *Manager) CallContractMethodUnpackMap(ctx context.Context, network utils.Network, bindingType BindingType, toAddr common.Address, methodName string, params ...interface{}) (map[string]any, error)

CallContractMethodUnpackMap executes a contract method call and unpacks the results into a map, providing a flexible interface for handling contract outputs.

func (*Manager) GetBinding

func (m *Manager) GetBinding(network utils.Network, name BindingType) (*Binding, error)

GetBinding retrieves a specific contract binding by its network and type, enabling contract interactions.

func (*Manager) GetBindings

func (m *Manager) GetBindings(network utils.Network) map[BindingType]*Binding

GetBindings returns all bindings registered under a specific network, providing a comprehensive overview of available contract interactions.

func (*Manager) GetClient

func (m *Manager) GetClient() *clients.ClientPool

GetClient returns the client pool associated with the Manager, allowing for direct network interactions.

func (*Manager) GetContractCreator

func (m *Manager) GetContractCreator(ctx context.Context, network utils.Network, contract common.Address) (*CreatorInformation, error)

GetContractCreator queries the Ethereum blockchain to find the creator of a specified smart contract. This method utilizes the Ethereum JSON-RPC API to request creator information, which includes both the creator's address and the transaction hash of the contract's creation. It's a valuable tool for auditing and tracking the origins of contracts on the network. WORKS ONLY WITH ERIGON NODE - OR NODES THAT SUPPORT OTTERSCAN!

func (*Manager) RegisterBinding

func (m *Manager) RegisterBinding(network utils.Network, networkID utils.NetworkID, name BindingType, address common.Address, rawABI string) (*Binding, error)

RegisterBinding adds a new contract binding to the Manager. It processes the contract's ABI and initializes a Binding struct, ensuring that each binding is uniquely registered within its network context.

func (*Manager) WatchEvents

func (m *Manager) WatchEvents(network utils.Network, bindingType BindingType, eventName string, ch chan<- types.Log) (ethereum.Subscription, error)

WatchEvents establishes a subscription to listen for specific contract events, facilitating real-time application responses to on-chain activities.

type Token

type Token struct {
	*Manager
	// contains filtered or unexported fields
}

Token encapsulates data and functions for interacting with a blockchain token. It contains network information, execution context, and binding options to interact with the smart contract representing the token.

func NewSimpleToken

func NewSimpleToken(ctx context.Context, network utils.Network, manager *Manager, opts []*BindOptions) (*Token, error)

NewSimpleToken is a simplified version of NewToken that also initializes a new Token instance but does not perform binding registrations. It's intended for scenarios where the bindings are already registered or not needed.

func NewToken

func NewToken(ctx context.Context, network utils.Network, manager *Manager, opts []*BindOptions) (*Token, error)

NewToken initializes a new Token instance. It requires a context, network, Manager instance, and a slice of BindOptions. The function validates the provided BindOptions and registers the bindings with the Manager. It returns a pointer to a Token instance or an error if the initialization fails.

func (*Token) Allowance

func (t *Token) Allowance(ctx context.Context, owner, from common.Address, spender common.Address) (*big.Int, error)

Allowance checks the amount of tokens that an owner allowed a spender to use on their behalf. It's a crucial component for enabling delegated token spending in ERC-20 tokens, supporting functionalities like token approvals for automated market makers or decentralized exchanges.

func (*Token) BalanceOf

func (t *Token) BalanceOf(ctx context.Context, from common.Address, address common.Address) (*big.Int, error)

BalanceOf queries the balance of a specific address within an ERC-20 token contract. It calls the balanceOf method of the contract and returns the balance as a *big.Int. This function is essential for determining the token balance of a wallet or contract address.

func (*Token) GetAddress

func (t *Token) GetAddress() common.Address

GetAddress returns the primary address of the token's smart contract as specified in the first BindOptions entry.

func (*Token) GetBinding

func (t *Token) GetBinding(network utils.Network, bindingType BindingType) (*Binding, error)

GetBinding retrieves a specific binding based on the network and binding type. It utilizes the Manager's GetBinding method to find the requested binding and returns it.

func (*Token) GetDecimals

func (t *Token) GetDecimals(ctx context.Context, from common.Address) (uint8, error)

GetDecimals calls the decimals() function in the ERC-20 contract.

func (*Token) GetName

func (t *Token) GetName(ctx context.Context, from common.Address) (string, error)

GetName queries the name of the token by calling the name() function in the ERC-20 contract. It returns the token name or an error if the call fails or the result cannot be asserted as a string.

func (*Token) GetOptionsByNetwork

func (t *Token) GetOptionsByNetwork(network utils.Network) *BindOptions

GetOptionsByNetwork retrieves the BindOptions associated with a specific network. This function is useful for accessing network-specific configurations like contract addresses and ABIs, facilitating multi-network support within the same application.

func (*Token) GetSymbol

func (t *Token) GetSymbol(ctx context.Context, from common.Address) (string, error)

GetSymbol calls the symbol() function in the ERC-20 contract.

func (*Token) GetTotalSupply

func (t *Token) GetTotalSupply(ctx context.Context, from common.Address) (*big.Int, error)

GetTotalSupply calls the totalSupply() function in the ERC-20 contract.

func (*Token) Owner

func (t *Token) Owner(ctx context.Context, from common.Address) (common.Address, error)

Owner retrieves the owner address of an ERC20Ownable token by calling the owner() contract method. It returns the owner's address or an error if the call fails or the result cannot be asserted to common.Address.

Jump to

Keyboard shortcuts

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