1

I have followed this guide and wrote the same code but instead of getting the latest block, I am getting this,

Subscription Failed =>  The method newBlocks_newBlocks does not exist/is not available

Code :

package main

import (
    "github.com/ethereum/go-ethereum/rpc"
    "time"
    "fmt"
    "context"
)

type Block struct{
    Number string
}

func main(){
    clientSubscription()

}
func clientSubscription(){
    client, err := rpc.Dial("ws://localhost:8546")
    //client, err := rpc.Dial("ws://115.186.176.139:8547")
    if err != nil {
        fmt.Println("Error Connecting to Server",err)

    }
    subch:=make(chan Block)
    go func(){
        for i:=0; ; i++{
            if i>0 {
                time.Sleep(2*time.Second)
            }
            subscribeBlocks(client,subch)
        }
    }()

    for block:=range subch{
        fmt.Println("Latest Block => ",block.Number)
    }

}

func subscribeBlocks(client *rpc.Client, subch chan Block)  {
    ctx,cancel:=context.WithTimeout(context.Background(),10*time.Second)
    defer cancel()

    blockSubscription,err:=client.EthSubscribe(ctx,subch,"newBlocks")
    if err!=nil{
        fmt.Println("Subscription Failed => ",err)
        return
    }

    var latestBlock Block
    if err:=client.CallContext(ctx,&latestBlock,"eth_getBlockByNumber","latest"); err!=nil{
        fmt.Println("Cannot get the latest Block => ",err)
        return
    }

    subch <-latestBlock

    fmt.Println("connection lost",<-blockSubscription.Err())
}

Any idea what is going on?

1 Answer 1

2

Here's a full working example of how to subscribe to latest blocks using the go-ethereum package in Go.

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/ethclient"
)

func main() {
    client, err := ethclient.Dial("wss://ropsten.infura.io/ws")
    if err != nil {
        log.Fatal(err)
    }

    headers := make(chan *types.Header)
    sub, err := client.SubscribeNewHead(context.Background(), headers)
    if err != nil {
        log.Fatal(err)
    }

    for {
        select {
        case err := <-sub.Err():
            log.Fatal(err)
        case header := <-headers:
            block, err := client.BlockByHash(context.Background(), header.Hash())
            if err != nil {
                log.Fatal(err)
            }

            fmt.Println(block.Hash().Hex())        // 0xbc10defa8dda384c96a17640d84de5578804945d347072e091b4e5f390ddea7f
            fmt.Println(block.Number().Uint64())   // 3477413
            fmt.Println(block.Time().Uint64())     // 1529525947
            fmt.Println(block.Nonce())             // 130524141876765836
            fmt.Println(len(block.Transactions())) // 7
        }
    }
}

For more examples, check out the the Ethereum Development with Go book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.