contractevent

package module
v0.0.0-...-2417588 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2022 License: MIT Imports: 24 Imported by: 0

README

 合约事件监听

监听链上事件,可以保存到数据库,可以推送到指定服务。

当前只支持EVM的链,ethereum、polygon、BSC......

示例代码可以查看examples文件夹。

说明

使用库
import (
    contractevent "github.com/lengzhao/contract_event"
)

event, err := contractevent.NewEvent(sub, client, func(alias string, info map[string]interface{}) error {
    log.Println("new event:", alias, info)
    return nil
})
...
err = event.Run(sub.StartBlock, sub.StartBlock+1)
if err != nil {
    log.Fatal("fail to run:", err)
}
Event
  1. 通过event := NewEvent(...),创建订阅
    1. 参数SubscriptionConf
      1. Alias:可以是任意字符串
      2. Contract:合约地址,可以多个
      3. ABIFile:智能合约的abi文件
        1. 可以直接用erc20/erc721/erc1155作为配置项的值,将使用默认自带的abi
        2. 可以自己修改abi中参数的名称,从而实现自定义收到的数据
      4. EventName:要监听的事件
        1. 如果为空,则表示监听合约的所有事件
        2. 不允许监听无法识别的事件
      5. Filter:要过滤的参数,map[string]string
        1. key就是abi事件的参数名
        2. value默认为hex字符串,要匹配的值
          1. 比如from:0x...,用于监听指定地址的转出事件
          2. 如果参数为int/uint的,也支持数字
          3. 比如要监听指定tokenID的NFT
        3. 如果本身abi参数有indexed修饰,则会在查询节点时,就增加该过滤
    2. type EventCallback func(alias string, info map[string]interface{}) error
      1. 回调函数,监听到的事件,将通过回调通知到业务模块
      2. alias就是配置中的Alias
      3. info携带了具体的事件内容
        1. 包含基础内容:KAlias/KBlock/KTX等信息
        2. 包含事件对应的具体信息,如Transfer
          1. from/to/amount
  2. 通过event.Run()执行查询指定范围区块的事件
    1. 结果将通过callback通知到业务模块
    2. 如果callback返回error,表示异常,将退出
DBItem
  1. 可以将数据存储到数据库,不同的alias存储在不同的表里
  2. tx+logIndex创建了索引,所以不允许重复
  3. 例子可以查看examples/2.save

Documentation

Index

Constants

View Source
const (
	ABIERC20   = "erc20"
	ABIERC721  = "erc721"
	ABIERC1155 = "erc1155"
)
View Source
const (
	KAlias       = "alias"
	KBlock       = "block"
	KBlockNumber = "block_number"
	KContract    = "contract"
	KTX          = "tx"
	KLogIndex    = "log_index"
	KTopic       = "topic"
	KEventName   = "event_name"
	KRawData     = "raw_data"
)

Variables

This section is empty.

Functions

func CreateBlockRecord

func CreateBlockRecord(db *gorm.DB) error

func CreateEventTable

func CreateEventTable(db *gorm.DB, alias string) error

func CreateNotifyRecord

func CreateNotifyRecord(db *gorm.DB) error

func DeleteItem

func DeleteItem(db *gorm.DB, alias string, id uint) error

func GetABIData

func GetABIData(name string) []byte

func GetBlockRecord

func GetBlockRecord(db *gorm.DB, alias string) (uint64, error)

func GetNotifyRecord

func GetNotifyRecord(db *gorm.DB, alias string) (uint, error)

func HttpRouter

func HttpRouter(router *gin.RouterGroup, db *gorm.DB)

func InsertItem

func InsertItem(db *gorm.DB, alias string, item DBItem) (uint, error)

func ItemsTotal

func ItemsTotal(db *gorm.DB, alias string) (uint, error)

func NewDB

func NewDB(conf DBConf) (*gorm.DB, error)

func SetBlockRecord

func SetBlockRecord(db *gorm.DB, alias string, blockID uint64) error

func SetNotifyRecord

func SetNotifyRecord(db *gorm.DB, alias string, nid uint) error

Types

type BlockRecord

type BlockRecord struct {
	gorm.Model
	Alias   string `gorm:"uniqueIndex;column:alias"`
	BlockID uint64 `gorm:"column:block_id"`
}

type ChainConfig

type ChainConfig struct {
	RPCNode    string `yaml:"rpc_node,omitempty"`
	DelayBlock uint64 `yaml:"delay_block,omitempty"`
}

type Config

type Config struct {
	Chain ChainConfig        `yaml:"chain,omitempty"`
	DB    DBConf             `yaml:"db,omitempty"`
	Subs  []SubscriptionConf `yaml:"subscriptions,omitempty"`
	Http  ServerConfig       `yaml:"http,omitempty"`
}

type DBConf

type DBConf struct {
	Engine   string `yaml:"engine,omitempty"`
	DSN      string `yaml:"dsn,omitempty"`
	LogLevel int    `yaml:"log_level,omitempty"`
}

type DBItem

type DBItem struct {
	gorm.Model
	TX       string `gorm:"column:tx"`
	LogIndex uint   `gorm:"column:log_index"`
	Others   []byte
}

func ListItems

func ListItems(db *gorm.DB, alias string, offest, limit int) ([]DBItem, error)

type Event

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

func NewEvent

func NewEvent(conf SubscriptionConf, client *ethclient.Client, cb EventCallback) (*Event, error)

func NewEventWithDB

func NewEventWithDB(conf SubscriptionConf, client *ethclient.Client, db *gorm.DB) (*Event, error)

func (*Event) Run

func (e *Event) Run(start, end uint64) error

type EventCallback

type EventCallback func(alias string, info map[string]interface{}) error

type Manager

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

func NewManager

func NewManager(conf Config) (*Manager, error)

func (*Manager) Close

func (m *Manager) Close()

func (*Manager) Run

func (m *Manager) Run()

type NotifyRecord

type NotifyRecord struct {
	gorm.Model
	Alias      string `gorm:"uniqueIndex;column:alias"`
	NotifiedID uint   `gorm:"column:nid"`
}

type NotifyTask

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

func NewNotifyTask

func NewNotifyTask(db *gorm.DB, alias, webHook string) *NotifyTask

func (*NotifyTask) Run

func (t *NotifyTask) Run(limit uint) error

type RespItems

type RespItems struct {
	Alias  string                   `json:"alias,omitempty"`
	Offset int                      `json:"offset,omitempty"`
	Total  uint                     `json:"total,omitempty"`
	Items  []map[string]interface{} `json:"items,omitempty"`
}

type ServerConfig

type ServerConfig struct {
	Port       int    `yaml:"port,omitempty"`
	PrefixPath string `yaml:"prefix_path,omitempty"`
}

type SubscriptionConf

type SubscriptionConf struct {
	Alias        string            `yaml:"alias"`
	Contract     []string          `yaml:"contract"`
	ABIFile      string            `yaml:"abi_file"`
	EventName    string            `yaml:"event_name"`
	Filter       map[string]string `yaml:"filter"`
	StartBlock   uint64            `yaml:"start_block"`
	BlocksPerReq uint64            `yaml:"blocks_per_req"`
	WaitPerReq   int64             `yaml:"wait_per_req"`
	WebHook      string            `yaml:"web_hook"`
}

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

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