service

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TopicNewBlock is the topic about received new blocks
	TopicNewBlock = "service:newBlock"
	// TopicChainData is the topic about latest chain data (ether price, gas price, etc.)
	TopicChainData = "service:chainData"
	// TopicTick is a topic that receives tick event periodically
	TopicTick = "service:tick"

	// UpdatePeriod is the time duration between two updates
	UpdatePeriod = 10 * time.Second
)
View Source
const (
	// TypeMainnet is the Ethereum Mainnet
	TypeMainnet = "mainnet"
	// TypeTestnet is all kinds of the testnets (Ropsten, Rinkeby, Goerli etc.)
	TypeTestnet = "testnet"
	// TypeDevnet is a local network for development purpose (Hardhat, Ganeche etc.)
	TypeDevnet = "devnet"
	// TypeUnknown is a unknown network
	TypeUnknown = "unknown"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

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

Account represents an account of Etheruem network.

func (*Account) AsContract

func (a *Account) AsContract() (*Contract, error)

AsContract upgrade this account object to a contract.

func (*Account) ClearCache added in v0.2.0

func (a *Account) ClearCache()

ClearCache will clear cached balance

func (*Account) GetAddress

func (a *Account) GetAddress() common.Address

GetAddress returns address of this account.

func (*Account) GetBalance

func (a *Account) GetBalance() common.BigInt

GetBalance returns cached balance of this account.

func (*Account) GetBalanceForce

func (a *Account) GetBalanceForce() (common.BigInt, error)

GetBalanceForce will query for current account's balance, store it in cache and return.

func (*Account) GetTransactions

func (a *Account) GetTransactions() (common.Transactions, error)

GetTransactions returns transactions of this account.

func (*Account) GetType

func (a *Account) GetType() AccountType

GetType returns type of this account, either Wallet or Contract.

func (*Account) IsContract

func (a *Account) IsContract() bool

IsContract returns true if this account is a smart contract.

func (*Account) UpdateBalance

func (a *Account) UpdateBalance() bool

UpdateBalance will update cache of current account's balance

type AccountType

type AccountType string

AccountType represents two types of Etheruem's account: EOA and SmartContract.

const (
	// TypeWallet is an EOA account.
	TypeWallet AccountType = "Wallet"
	// TypeContract is a SmartContract account.
	TypeContract = "Contract"
)

func (AccountType) String

func (at AccountType) String() string

type ChainData

type ChainData struct {
	Price    *decimal.Decimal
	GasPrice *big.Int
}

type Contract

type Contract struct {
	*Account
	// contains filtered or unexported fields
}

Contract represents a smart contract deployed on Ethereum network.

func (*Contract) Call

func (c *Contract) Call(method string, args ...any) ([]any, error)

Call invokes a constant method of this contract. The arguments should be unpacked into correct type.

func (*Contract) GetABI

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

GetABI returns ABI of this contract, may be nil if ABI is unknown.

func (*Contract) GetSource

func (c *Contract) GetSource() string

GetSource returns source of this contract, may be empty if source cannot be retrieved.

func (*Contract) HasABI

func (c *Contract) HasABI() bool

HasABI returns true if this contract has a known ABI.

func (*Contract) ImportABI

func (c *Contract) ImportABI(abiJson string) error

ImportABI generates ABI from a json representation of ABI.

func (*Contract) ParseCalldata added in v0.2.0

func (c *Contract) ParseCalldata(data []byte) (*abi.Method, []any, error)

ParseCalldata parses calldata into method name and arguments.

func (*Contract) Send added in v0.2.0

func (c *Contract) Send(signer *Signer, method string, args ...any) (common.Hash, error)

Send invokes a non-constant method of this contract. This method will sign and send the transaction to the network.

type Network

type Network struct {
	Name    string   `json:"name"`
	Title   string   `json:"title"`
	ChainId *big.Int `json:"chainId"`
}

func (Network) NetType

func (n Network) NetType() string

NetType returns type of this network.

There are 3 types of network:

  • Mainnet: a public network for serious applications
  • Testnet: a public network for testing
  • Devnet: a local network for development purpose

type Service

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

func NewService

func NewService(config *conf.Config) *Service

func (*Service) GetAccount

func (s *Service) GetAccount(address string) (*Account, error)

GetAccount returns an account of given address.

func (*Service) GetBlockHeight

func (s *Service) GetBlockHeight() (uint64, error)

GetBlockHeight returns the current block height.

func (*Service) GetCache added in v0.2.0

func (s *Service) GetCache(address common.Address, accountType AccountType) (any, bool)

func (*Service) GetContract

func (s *Service) GetContract(address common.Address) (*Contract, error)

GetContract returns a contract object of given address.

func (*Service) GetEthPrice

func (s *Service) GetEthPrice() (*decimal.Decimal, error)

GetEthPrice returns ETH price in USD.

func (*Service) GetGasPrice

func (s *Service) GetGasPrice() (common.BigInt, error)

GetGasPrice returns average gas price of last block.

func (*Service) GetLatestTransactions

func (s *Service) GetLatestTransactions(n int, nBlock int) (common.Transactions, error)

GetLatestTransactions returns last n transactions of at most nBlock blocks.

func (*Service) GetNetwork

func (s *Service) GetNetwork() Network

GetNetwork returns the network that provider is connected to.

func (*Service) GetProvider

func (s *Service) GetProvider() *provider.Provider

GetProvider returns underlying provider instance. Usually you don't need to tackle with provider directly.

func (*Service) GetSigner

func (s *Service) GetSigner(privateKey string) (*Signer, error)

GetSigner returns a signer which can sign transactions

func (*Service) GetTransactionHistory

func (s *Service) GetTransactionHistory(address common.Address) (common.Transactions, error)

GetTransactionHistory returns transactions related to specified account. This method relies on Etherscan API at chains other than local chain.

func (*Service) GetTransactionsByBlock

func (s *Service) GetTransactionsByBlock(block *common.Block) (common.Transactions, error)

GetTransactionsByBlock returns transactions of given block hash.

func (*Service) SetCache added in v0.2.0

func (s *Service) SetCache(address common.Address, accountType AccountType, value any, expiration time.Duration)

func (*Service) ToContract

func (s *Service) ToContract(account *Account) (*Contract, error)

ToContract upgrade an account object to a contract.

type Signer

type Signer struct {
	*Account
	PrivateKey *ecdsa.PrivateKey
}

func (*Signer) CallContract

func (s *Signer) CallContract(address common.Address, abi *abi.ABI, method string, args ...any) (common.Hash, error)

func (*Signer) TransferTo

func (s *Signer) TransferTo(address common.Address, amount common.BigInt) (common.Hash, error)

type Syncer

type Syncer struct {
	*sync.Mutex
	// contains filtered or unexported fields
}

Syncer is used to synchronize information from blockchain.

func NewSyncer

func NewSyncer(service *Service, eventBus EventBus.Bus) *Syncer

func (*Syncer) Start

func (s *Syncer) Start() error

Jump to

Keyboard shortcuts

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