to be
a problem slover

go 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

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

评论 2

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

    binance3周前 (04-06)回复
  2. #2

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.com/si-LK/register?ref=LBF8F65G

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

支付宝扫一扫打赏

微信扫一扫打赏