to be
a problem slover

subscribe new block

func TestSubscribeNewBlock(t *testing.T) {
    client, err := ethclient.DialContext(context.Background(), "ws://10.136.0.32:9546")
    if err != nil {
        t.Fatal(err)
    }
    ch := make(chan *types.Header)
    sub, err := client.SubscribeNewHead(context.Background(), ch)
    if err != nil {
        t.Fatal(err)
    }
    for {
        select {
        case header := <-ch:
            t.Log(header.Number)
        case err = <-sub.Err():
            if err != nil {
                t.Error(err.Error())
            }
            return
        }
    }
}

reference

https://goethereumbook.org/block-subscribe/

https://github.com/ethereum/go-ethereum/blob/master/rpc/client_example_test.go

赞(0) 打赏
欢迎转载,注明出处:刘世明的博客 » subscribe new block

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下作者

支付宝扫一扫打赏

微信扫一扫打赏