Documentation ¶
Index ¶
- type Contract
- func (c *Contract) ABI() *abi.ABI
- func (c *Contract) Addr() common.Address
- func (c *Contract) ID() uint64
- func (c *Contract) IsValid() bool
- func (c *Contract) Name() string
- func (c *Contract) RawABI() string
- func (c *Contract) Standard() utils.Standard
- func (c *Contract) ToABI() (abi.ABI, error)
- func (c *Contract) Validate() error
- type Manager
- func (m *Manager) GetByAddr(network utils.NetworkID, address common.Address) (*Contract, error)
- func (m *Manager) GetByID(network utils.NetworkID, id uint64) (*Contract, error)
- func (m *Manager) GetByStandard(network utils.NetworkID, standard utils.Standard) (*Contract, error)
- func (m *Manager) List() map[utils.NetworkID][]*Contract
- func (m *Manager) ListByNetworkId(networkId utils.NetworkID) []*Contract
- func (m *Manager) LoadDefaults() error
- func (m *Manager) Register(network utils.NetworkID, contract *Contract) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Contract ¶
type Contract struct {
// contains filtered or unexported fields
}
Contract represents an Ethereum smart contract, including its address, name, standard, and ABI.
func NewContract ¶
func NewContract(id uint64, addr common.Address, name string, standard utils.Standard, rawAbi string) (*Contract, error)
NewContract creates a new Contract instance, validates it, and returns an error if validation fails.
Example:
addr := common.HexToAddress("0x123...") contract, err := contracts.NewContract(addr, "MyContract", utils.Erc20, contractABI) if err != nil { log.Fatalf("Failed to create contract: %v", err) }
func (*Contract) ABI ¶
ABI returns the parsed ABI of the contract.
Example:
fmt.Println(contract.ABI())
func (*Contract) Addr ¶
Addr returns the Ethereum address of the contract.
Example:
fmt.Println(contract.Addr().Hex())
func (*Contract) IsValid ¶
IsValid checks if the contract is valid and ready to be used. This is a helper method to check if the contract's key fields (addr, abi, and standard) are valid.
Example:
if contract.IsValid() { // Use the contract }
func (*Contract) Name ¶
Name returns the name of the contract.
Example:
fmt.Println(contract.Name())
func (*Contract) RawABI ¶
RawABI returns the raw ABI string of the contract.
Example:
fmt.Println(contract.RawAbi())
func (*Contract) Standard ¶
Standard returns the standard of the contract (e.g., ERC-20, ERC-721).
Example:
fmt.Println(contract.Standard())
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages a registry of contracts, allowing for thread-safe operations.
func NewManager ¶
func NewManager() *Manager
NewManager creates and returns a new Manager with an initialized registry.
func NewManagerWithDefaults ¶
NewManagerWithDefaults creates and returns a new Manager with an initialized registry and loaded default contracts.
func (*Manager) GetByAddr ¶
GetByAddr retrieves the contract for the given network ID and associated address. It returns an error if no contract is registered for the given network.
Example:
contract, err := manager.GetByAddr(networkID, common.HexToAddress("0x0")) if err != nil { log.Fatalf("Failed to get contract: %v", err) }
func (*Manager) GetByID ¶
GetByID retrieves the contract for the given network ID and associated contract ID. It returns an error if no contract is registered for the given network.
Example:
contract, err := manager.GetByAddr(networkID, 1) if err != nil { log.Fatalf("Failed to get contract: %v", err) }
func (*Manager) GetByStandard ¶
func (m *Manager) GetByStandard(network utils.NetworkID, standard utils.Standard) (*Contract, error)
GetByStandard retrieves the contract for the given network ID and associated contract standard. It returns an error if no contract is registered for the given network.
Example:
contract, err := manager.GetByStandard(networkID, utils.Erc20) if err != nil { log.Fatalf("Failed to get contract: %v", err) }
func (*Manager) List ¶
List returns a copy of all registered contracts in the registry.
Example:
contracts := manager.List() for id, contract := range contracts { fmt.Printf("Network: %d, Contract: %v\n", id, contract) }
func (*Manager) ListByNetworkId ¶
ListByNetworkId returns a copy of all registered contracts in the registry under specified network id.
Example:
contracts := manager.ListByNetworkId(utils.EthereumNetworkId) for id, contract := range contracts { fmt.Printf("Network: %d, Contract: %v\n", id, contract) }
func (*Manager) LoadDefaults ¶
LoadDefaults loads default contracts for different networks into the manager.
func (*Manager) Register ¶
Register adds a new contract to the registry for the given network ID. It returns an error if the contract is already registered or if the contract is invalid.
Example:
err := manager.Register(networkID, contract) if err != nil { log.Fatalf("Failed to register contract: %v", err) }