blockchain

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidNetwork = errors.New("invalid Network type")

Functions

func SetLogger

func SetLogger(logger *logger.Logger)

Types

type Blockchain

type Blockchain struct {
	Clients     []*EthClient
	Contracts   []*Contract
	Network     Network
	PersistPath string
	// contains filtered or unexported fields
}

func (*Blockchain) ClientByNetwork

func (bc *Blockchain) ClientByNetwork() *EthClient

func (*Blockchain) Close

func (bc *Blockchain) Close()

func (*Blockchain) ContractByID

func (bc *Blockchain) ContractByID(id string) *Contract

func (*Blockchain) Init

func (bc *Blockchain) Init() error

func (*Blockchain) MonitorMeta added in v0.1.7

func (bc *Blockchain) MonitorMeta() *MonitorInfo

func (*Blockchain) RunMonitor added in v0.1.7

func (bc *Blockchain) RunMonitor() error

func (*Blockchain) SetDefault

func (bc *Blockchain) SetDefault()

func (*Blockchain) Watch added in v0.1.7

func (bc *Blockchain) Watch(options *WatchOptions, h EventHandler) (Subscription, error)

type Contract

type Contract struct {
	// ID human readable identifier in configuration
	ID string
	// Network deployed network
	Network Network
	// Address contract address
	Address common.Address
	// Events care about
	Events []*Event
	// contains filtered or unexported fields
}

func (*Contract) Init

func (c *Contract) Init() error

func (*Contract) ParseTxLog

func (c *Contract) ParseTxLog(name string, log *types.Log, result any) (err error)

func (*Contract) Topic

func (c *Contract) Topic(name string) (common.Hash, bool)

type EthClient

type EthClient struct {
	Endpoint string
	Network  Network

	*ethclient.Client `evn:"-"`
	// contains filtered or unexported fields
}

func (*EthClient) ChainEndpoint

func (c *EthClient) ChainEndpoint() string

func (*EthClient) ChainID

func (c *EthClient) ChainID(ctx context.Context) (chainID *big.Int, err error)

func (*EthClient) Init

func (c *EthClient) Init() error

type Event

type Event struct {
	Name string
	ABI  string
	// contains filtered or unexported fields
}

func (*Event) Init

func (e *Event) Init() error

type EventHandler added in v0.1.2

type EventHandler func(Subscription, *Contract, string, *types.Log)

type Meta

type Meta struct {
	ID        string           `json:"id"`
	Network   Network          `json:"network"`
	Contracts []common.Address `json:"contracts"`
	Topics    [][]common.Hash  `json:"topics"`
	// contains filtered or unexported fields
}

func NewMeta added in v0.1.7

func NewMeta(network Network, contracts ...*Contract) *Meta

func (*Meta) BlockKeyPrefixLowerBound added in v0.1.2

func (m *Meta) BlockKeyPrefixLowerBound(blk uint64) []byte

func (*Meta) BlockKeyPrefixUpperBound added in v0.1.2

func (m *Meta) BlockKeyPrefixUpperBound(blk uint64) []byte

func (*Meta) LogKey added in v0.1.7

func (m *Meta) LogKey(l *types.Log) []byte

func (*Meta) MonitorRangeEndKey added in v0.1.7

func (m *Meta) MonitorRangeEndKey() []byte

func (*Meta) MonitorRangeFromKey added in v0.1.7

func (m *Meta) MonitorRangeFromKey() []byte

func (*Meta) ParseLogKey added in v0.1.7

func (m *Meta) ParseLogKey(key string) (*Contract, string, error)

func (*Meta) String added in v0.1.2

func (m *Meta) String() string

func (*Meta) WatcherRangeEndKey added in v0.1.7

func (m *Meta) WatcherRangeEndKey(sub string) []byte

func (*Meta) WatcherRangeFromKey added in v0.1.7

func (m *Meta) WatcherRangeFromKey(sub string) []byte

type Monitor

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

func NewMonitor added in v0.1.7

func NewMonitor(network Network, contracts ...*Contract) *Monitor

func (*Monitor) CurrentBlock

func (m *Monitor) CurrentBlock() uint64

func (*Monitor) Endpoint

func (m *Monitor) Endpoint() string

func (*Monitor) Init

func (m *Monitor) Init() error

func (*Monitor) StartAt added in v0.1.2

func (m *Monitor) StartAt() uint64

func (*Monitor) Stop

func (m *Monitor) Stop()

func (*Monitor) Watch

func (m *Monitor) Watch(opts *WatchOptions, h EventHandler) (Subscription, error)

func (*Monitor) WatchList

func (m *Monitor) WatchList() []string

func (*Monitor) WithEthClient

func (m *Monitor) WithEthClient(c MonitorClient) *Monitor

func (*Monitor) WithInterval added in v0.1.2

func (m *Monitor) WithInterval(d time.Duration) *Monitor

func (*Monitor) WithPersistence

func (m *Monitor) WithPersistence(p TxPersistence) *Monitor

func (*Monitor) WithStartBlock added in v0.1.2

func (m *Monitor) WithStartBlock(from uint64) *Monitor

type MonitorClient

type MonitorClient interface {
	ChainID(context.Context) (*big.Int, error)
	ChainEndpoint() string
	BlockNumber(context.Context) (uint64, error)
	FilterLogs(context.Context, ethereum.FilterQuery) ([]types.Log, error)
}

type MonitorInfo

type MonitorInfo struct {
	Contracts map[string]struct {
		Address common.Address         `json:"address"`
		Events  map[string]common.Hash `json:"events"`
	} `json:"contracts"`
	From     uint64 `json:"from"`
	End      uint64 `json:"end"`
	Watchers []*struct {
		Sub  string `json:"sub"`
		From uint64 `json:"from"`
		End  uint64 `json:"end"`
	} `json:"watchers"`
}

type Network

type Network int
const (
	NETWORK_UNKNOWN       Network = 0
	NETWORK__IOTX_MAINNET Network = 4689
	NETWORK__IOTX_TESTNET Network = 4690
)

func ParseNetworkFromString

func ParseNetworkFromString(s string) (Network, error)

func (Network) Int

func (v Network) Int() int

func (Network) MarshalText

func (v Network) MarshalText() ([]byte, error)

func (Network) String

func (v Network) String() string

func (*Network) UnmarshalText

func (v *Network) UnmarshalText(data []byte) error

type PebbleKVStore

type PebbleKVStore interface {
	Load(k []byte) ([]byte, error)
	Store(k []byte, v []byte) error
	LoadAndDelete([]byte) (value []byte, loaded bool, err error)
	LoadOrStore([]byte, []byte) (actual []byte, loaded bool, err error)
	Delete([]byte) error
	Keys(prefix []byte) (keys [][]byte, err error)
}

type Persist

type Persist struct {
	Path string
	// contains filtered or unexported fields
}

func (*Persist) BatchSet

func (p *Persist) BatchSet(kvs ...[2][]byte) (err error)

func (*Persist) Close

func (p *Persist) Close() error

func (*Persist) DB

func (p *Persist) DB() *pebble.DB

func (*Persist) Delete

func (p *Persist) Delete(k []byte) error

func (*Persist) GetUint64

func (p *Persist) GetUint64(k []byte) (uint64, error)

func (*Persist) Init

func (p *Persist) Init() error

func (*Persist) InsertLogs

func (p *Persist) InsertLogs(meta Meta, logs ...*types.Log) error

func (*Persist) IsZero

func (p *Persist) IsZero() bool

func (*Persist) Keys

func (p *Persist) Keys(prefix []byte) ([][]byte, error)

func (*Persist) Load

func (p *Persist) Load(k []byte) ([]byte, error)

func (*Persist) LoadAndDelete

func (p *Persist) LoadAndDelete(k []byte) (v []byte, loaded bool, err error)

func (*Persist) LoadJSONValue

func (p *Persist) LoadJSONValue(k []byte, v any) error

func (*Persist) LoadOrStore

func (p *Persist) LoadOrStore(k, v []byte) (actual []byte, loaded bool, err error)

func (*Persist) LoadTextValue

func (p *Persist) LoadTextValue(k []byte, v any) error

func (*Persist) MonitorRange added in v0.1.7

func (p *Persist) MonitorRange(meta Meta) (from uint64, end uint64, err error)

func (*Persist) QueryTxByHeightRange

func (p *Persist) QueryTxByHeightRange(meta Meta, from, to uint64) ([]*result, error)

QueryTxByHeightRange query tx logs the block number between from and to [from,to]

func (*Persist) RemoveWatcher added in v0.1.2

func (p *Persist) RemoveWatcher(meta Meta, id string) error

func (*Persist) SetUint64

func (p *Persist) SetUint64(k []byte, v uint64) error

func (*Persist) Store

func (p *Persist) Store(k, v []byte) error

func (*Persist) StoreJSONValue

func (p *Persist) StoreJSONValue(k []byte, v any) error

func (*Persist) StoreTextValue

func (p *Persist) StoreTextValue(k []byte, v any) error

func (*Persist) UpdateMonitorRange added in v0.1.7

func (p *Persist) UpdateMonitorRange(meta Meta, from, end uint64) error

func (*Persist) UpdateWatcherRange added in v0.1.7

func (p *Persist) UpdateWatcherRange(meta Meta, id string, from, end uint64) error

func (*Persist) WatcherRange added in v0.1.7

func (p *Persist) WatcherRange(meta Meta, id string) (uint64, uint64, error)

type Subscription

type Subscription interface {
	ID() string
	Err() <-chan error
	Unsubscribe()
}

type TxPersistence

type TxPersistence interface {
	// MonitorRange query persisted monitor range
	MonitorRange(Meta) (uint64, uint64, error)
	// UpdateMonitorRange update persisted monitor range
	UpdateMonitorRange(Meta, uint64, uint64) error
	// QueryWatcher query watcher state by meta id and subscriber id
	WatcherRange(Meta, string) (uint64, uint64, error)
	// UpdateWatcher update watcher state
	UpdateWatcherRange(Meta, string, uint64, uint64) error
	// RemoveWatcher remove watcher state
	RemoveWatcher(Meta, string) error
	// QueryTxByHeightRange query tx logs by blockchain meta and block number range
	QueryTxByHeightRange(Meta, uint64, uint64) ([]*result, error)
	// InsertLogs insert tx logs with blockchain meta
	InsertLogs(Meta, ...*types.Log) error
	// Close
	Close() error
}

type WatchOptions

type WatchOptions struct {
	// SubID the unique id of subscriber, required field
	SubID string `json:"id"`
	// Start of the queried range(nil == latest) and this can be overwritten
	// by persisted record
	Start *uint64 `json:"start"`
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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