我想通过以下的代码片段来监听部署在链上的合约事件:
package main
import (
"context"
"fmt"
"log"
"strings"
"math/big"
"github.com/PlatONnetwork/PlatON-Go/crypto"
"github.com/PlatONnetwork/PlatON-Go"
"github.com/PlatONnetwork/PlatON-Go/common"
"github.com/PlatONnetwork/PlatON-Go/accounts/abi"
"github.com/PlatONnetwork/PlatON-Go/core/types"
"github.com/PlatONnetwork/PlatON-Go/ethclient"
)
func main() {
client, err := ethclient.Dial("wss://devnetopenapi.platon.network/ws")
if err != nil {
log.Fatal(err)
}
contractAddress := common.MustBech32ToAddress("lat1xt80enwhxtj2tspr48cv8q9t2sp6shx6klfvmf")
query := platon.FilterQuery{
Addresses: []common.Address{contractAddress},
}
logs := make(chan types.Log)
sub, err := client.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
log.Fatal(err)
}
for {
select {
case err := <-sub.Err():
log.Fatal(err)
case vLog := <-logs:
fmt.Println(vLog) // 每次事件发生时, 会读到两次
}
}
}
以上代码run起来有两个问题:
- websocket连接过大概60s就会自动断开, 提示: websocket: close 1006 (abnormal closure): unexpected EOF
- 每次事件发生时, 事件chan都会读到两次消息.
请问以上问题应该如何解决呢?
THX