contracts

package
v0.0.0-...-5068cfe Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

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

func (c *Contract) ABI() *abi.ABI

ABI returns the parsed ABI of the contract.

Example:

fmt.Println(contract.ABI())

func (*Contract) Addr

func (c *Contract) Addr() common.Address

Addr returns the Ethereum address of the contract.

Example:

fmt.Println(contract.Addr().Hex())

func (*Contract) ID

func (c *Contract) ID() uint64

ID returns the contract internal ID.

Example:

fmt.Println(contract.ID())

func (*Contract) IsValid

func (c *Contract) IsValid() bool

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

func (c *Contract) Name() string

Name returns the name of the contract.

Example:

fmt.Println(contract.Name())

func (*Contract) RawABI

func (c *Contract) RawABI() string

RawABI returns the raw ABI string of the contract.

Example:

fmt.Println(contract.RawAbi())

func (*Contract) Standard

func (c *Contract) Standard() utils.Standard

Standard returns the standard of the contract (e.g., ERC-20, ERC-721).

Example:

fmt.Println(contract.Standard())

func (*Contract) ToABI

func (c *Contract) ToABI() (abi.ABI, error)

ToABI parses and returns the contract's ABI as an abi.ABI object, or an error if the ABI is invalid.

Example:

parsedABI, err := contract.ToABI()
if err != nil {
    log.Fatalf("Failed to parse contract ABI: %v", err)
}

func (*Contract) Validate

func (c *Contract) Validate() error

Validate checks if the contract has a valid address, standard, and ABI, and parses the ABI.

Example:

if err := contract.Validate(); err != nil {
    log.Fatalf("Invalid contract: %v", err)
}

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

func NewManagerWithDefaults() (*Manager, error)

NewManagerWithDefaults creates and returns a new Manager with an initialized registry and loaded default contracts.

func (*Manager) GetByAddr

func (m *Manager) GetByAddr(network utils.NetworkID, address common.Address) (*Contract, error)

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

func (m *Manager) GetByID(network utils.NetworkID, id uint64) (*Contract, error)

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

func (m *Manager) List() map[utils.NetworkID][]*Contract

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

func (m *Manager) ListByNetworkId(networkId utils.NetworkID) []*Contract

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

func (m *Manager) LoadDefaults() error

LoadDefaults loads default contracts for different networks into the manager.

func (*Manager) Register

func (m *Manager) Register(network utils.NetworkID, contract *Contract) error

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)
}

Jump to

Keyboard shortcuts

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