5

I've set up a private local Ethereum node with just a genesis block I made, and I have this Go code I want to run on the private local network:

package main

    import (
        "fmt"
        "math/big"

        "github.com/ethereum/go-ethereum/rpc"
    )

    type Block struct {
        Number *big.Int
    }

    func main() {
        // Connect the client
        client, err := rpc.Dial("http://localhost:8000")
        if err != nil {
            panic(err)
        }

        var lastBlock Block
        if err := client.Call(&lastBlock,
            "eth_getBlockByNumber",
            "latest"); err != nil {
            fmt.Println("can't get latest block:", err)
            return
        }

        // Print events from the subscription as they arrive.
        fmt.Println("latest block:", lastBlock.Number)
    }

It compiles fine, but when I run it I get this error: can't get latest block: missing value for required argument 1

I'm passing in "latest", which is a valid argument for the RPC call according to the docs.

I tracked down the error output to this file and it seems the string is getting converted to nil and the RPC call is failing. Does anyone know what's going on here or how to fix this weird issue?

3

I made it work by adding a true parameter in err = client.Call(&lastBlock, "eth_getBlockByNumber", "latest", true) and changing *bit.Int to string

Like this:

package main

import(
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/rpc"
)

type Block struct {
    Number string
}

func main() {
  // Connect the client
  client, err := rpc.Dial("http://localhost:8000")
  if err != nil {
    log.Fatalf("could not create ipc client: %v", err)
  }

  var lastBlock Block
  err = client.Call(&lastBlock, "eth_getBlockByNumber", "latest", true)
  if err != nil {
      fmt.Println("can't get latest block:", err)
      return
  }

  // Print events from the subscription as they arrive.
  fmt.Printf("latest block: %v\n", lastBlock.Number)
}

The result should be in hexa (like 0x5c67)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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