modulepackage
0.0.0-20241108013353-87f866ac40b7
Repository: https://github.com/lengzhao/contract_event.git
Documentation: pkg.go.dev
# 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
- 通过
event := NewEvent(...)
,创建订阅- 参数
SubscriptionConf
- Alias:可以是任意字符串
- Contract:合约地址,可以多个
- ABIFile:智能合约的abi文件
- 可以直接用
erc20
/erc721
/erc1155
作为配置项的值,将使用默认自带的abi - 可以自己修改abi中参数的名称,从而实现自定义收到的数据
- 可以直接用
- EventName:要监听的事件
- 如果为空,则表示监听合约的所有事件
- 不允许监听无法识别的事件
- Filter:要过滤的参数,
map[string]string
- key就是abi事件的参数名
- value默认为hex字符串,要匹配的值
- 比如from:0x...,用于监听指定地址的转出事件
- 如果参数为int/uint的,也支持数字
- 比如要监听指定tokenID的NFT
- 如果本身abi参数有indexed修饰,则会在查询节点时,就增加该过滤
type EventCallback func(alias string, info map[string]interface{}) error
- 回调函数,监听到的事件,将通过回调通知到业务模块
- alias就是配置中的Alias
- info携带了具体的事件内容
- 包含基础内容:
KAlias
/KBlock
/KTX
等信息 - 包含事件对应的具体信息,如
Transfer
:- from/to/amount
- 包含基础内容:
- 参数
- 通过
event.Run()
执行查询指定范围区块的事件- 结果将通过callback通知到业务模块
- 如果callback返回error,表示异常,将退出
DBItem
- 可以将数据存储到数据库,不同的alias存储在不同的表里
- tx+logIndex创建了索引,所以不允许重复
- 例子可以查看
examples/2.save
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author