Configuré un nodo Ethereum local privado con solo un bloque de génesis que hice, y tengo este código Go que quiero ejecutar en la red local privada:
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)
}
Se compila bien, pero cuando lo ejecuto me sale este error:can't get latest block: missing value for required argument 1
Estoy pasando "latest"
, que es un argumento válido para la llamada RPC según los documentos .
Localicé el resultado del error en este archivo y parece que la cadena se está convirtiendo nil
y la llamada RPC está fallando. ¿Alguien sabe qué está pasando aquí o cómo solucionar este extraño problema?
Lo hice funcionar agregando un true
parámetro err = client.Call(&lastBlock, "eth_getBlockByNumber", "latest", true)
y cambiando *bit.Int
astring
Como esto:
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)
}
El resultado debe estar en hexa (como 0x5c67)