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 ¶
- type BindOptions
- type Binding
- func (b *Binding) EventExist(eventName string) bool
- func (b *Binding) GetABI() *abi.ABI
- func (b *Binding) GetAddress() common.Address
- func (b *Binding) GetAllEvents() []string
- func (b *Binding) GetAllMethods() []string
- func (b *Binding) GetNetwork() utils.Network
- func (b *Binding) GetNetworkID() utils.NetworkID
- func (b *Binding) GetRawABI() string
- func (b *Binding) GetType() BindingType
- func (b *Binding) MethodExist(methodName string) bool
- type BindingType
- type CreatorInformation
- type Manager
- func (m *Manager) BindingExist(network utils.Network, name BindingType) bool
- func (m *Manager) CallContractMethod(ctx context.Context, network utils.Network, bindingType BindingType, ...) (any, error)
- func (m *Manager) CallContractMethodUnpackMap(ctx context.Context, network utils.Network, bindingType BindingType, ...) (map[string]any, error)
- func (m *Manager) GetBinding(network utils.Network, name BindingType) (*Binding, error)
- func (m *Manager) GetBindings(network utils.Network) map[BindingType]*Binding
- func (m *Manager) GetClient() *clients.ClientPool
- func (m *Manager) GetContractCreator(ctx context.Context, network utils.Network, contract common.Address) (*CreatorInformation, error)
- func (m *Manager) GetTransactionBySenderAndNonce(ctx context.Context, network utils.Network, sender common.Address, nonce int64) (*common.Hash, error)
- func (m *Manager) RegisterBinding(network utils.Network, networkID utils.NetworkID, name BindingType, ...) (*Binding, error)
- func (m *Manager) TraceCallMany(ctx context.Context, network utils.Network, sender common.Address, nonce int64) (*common.Hash, error)
- func (m *Manager) WatchEvents(network utils.Network, bindingType BindingType, eventName string, ...) (ethereum.Subscription, error)
- type Token
- func (t *Token) Allowance(ctx context.Context, owner, from common.Address, spender common.Address) (*big.Int, error)
- func (t *Token) BalanceOf(ctx context.Context, from common.Address, address common.Address) (*big.Int, error)
- func (t *Token) GetAddress() common.Address
- func (t *Token) GetBinding(network utils.Network, bindingType BindingType) (*Binding, error)
- func (t *Token) GetDecimals(ctx context.Context, from common.Address) (uint8, error)
- func (t *Token) GetName(ctx context.Context, from common.Address) (string, error)
- func (t *Token) GetOptionsByNetwork(network utils.Network) *BindOptions
- func (t *Token) GetSymbol(ctx context.Context, from common.Address) (string, error)
- func (t *Token) GetTotalSupply(ctx context.Context, from common.Address) (*big.Int, error)
- func (t *Token) Owner(ctx context.Context, from common.Address) (common.Address, error)
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 ¶
EventExist checks if a specified event exists in the contract ABI.
func (*Binding) GetAddress ¶
GetAddress returns the blockchain address of the contract.
func (*Binding) GetAllEvents ¶
GetAllEvents returns all the event names defined in the contract ABI.
func (*Binding) GetAllMethods ¶
GetAllMethods returns all the method names defined in the contract ABI.
func (*Binding) GetNetwork ¶
GetNetwork returns the network where the contract is deployed.
func (*Binding) GetNetworkID ¶
GetNetworkID returns the unique identifier of the network.
func (*Binding) GetType ¶
func (b *Binding) GetType() BindingType
GetType returns the type of the contract binding.
func (*Binding) MethodExist ¶
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 ¶
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 ¶
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 QUICKNODE PROVIDER - OR NODES THAT SUPPORT OTTERSCAN!
func (*Manager) GetTransactionBySenderAndNonce ¶ added in v0.3.5
func (m *Manager) GetTransactionBySenderAndNonce(ctx context.Context, network utils.Network, sender common.Address, nonce int64) (*common.Hash, error)
GetTransactionBySenderAndNonce retrieves a transaction hash based on a specific sender's address and nonce. This function also utilizes the Ethereum JSON-RPC API and requires a node that supports specific transaction lookup by sender and nonce, which is particularly useful for tracking transaction sequences and debugging transaction flows.
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) TraceCallMany ¶ added in v0.3.5
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 ¶
GetAddress returns the primary address of the token's smart contract as specified in the first BindOptions entry.
func (*Token) GetBinding ¶
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 ¶
GetDecimals calls the decimals() function in the ERC-20 contract.
func (*Token) GetName ¶
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) GetTotalSupply ¶
GetTotalSupply calls the totalSupply() function in the ERC-20 contract.