diff --git a/docs/run-node/interact-node.md b/docs/run-node/interact-node.md index 270326ad0..13cfb86d5 100644 --- a/docs/run-node/interact-node.md +++ b/docs/run-node/interact-node.md @@ -109,16 +109,28 @@ Assuming the state at that block has not yet been pruned by the node, this query The following snippet shows how to query the state using gRPC inside a Go program. The idea is to create a gRPC connection, and use the Protobuf-generated client code to query the gRPC server. +#### Install cosmos sdk + +Add below line to `go.mod` to replace protobuf, read more [#8469](https://github.com/cosmos/cosmos-sdk/issues/8469) + +``` +replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 +``` + +```bash +$ go get github.com/cosmos/cosmos-sdk@main +``` + ```go import ( "context" "fmt" - "google.golang.org/grpc" + "google.golang.org/grpc" - "github.com/cosmos/cosmo-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/tx" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" ) func queryState() error { @@ -128,20 +140,23 @@ func queryState() error { } // Create a connection to the gRPC server. - grpcConn := grpc.Dial( + grpcConn, err := grpc.Dial( "127.0.0.1:9090", // your gRPC server address. grpc.WithInsecure(), // The Cosmos SDK doesn't support any transport security mechanism. // This instantiates a general gRPC codec which handles proto bytes. We pass in a nil interface registry // if the request/response types contain interface instead of 'nil' you should pass the application specific codec. grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec())), ) + if err != nil { + return err + } defer grpcConn.Close() // This creates a gRPC client to query the x/bank service. bankClient := banktypes.NewQueryClient(grpcConn) bankRes, err := bankClient.Balance( context.Background(), - &banktypes.QueryBalanceRequest{Address: myAddress, Denom: "atom"}, + &banktypes.QueryBalanceRequest{Address: myAddress.String(), Denom: "atom"}, ) if err != nil { return err @@ -167,8 +182,10 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/metadata" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" - "github.com/cosmos/cosmos-sdk/types/tx" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" ) func queryState() error { @@ -177,13 +194,13 @@ func queryState() error { var header metadata.MD bankRes, err = bankClient.Balance( metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, "12"), // Add metadata to request - &banktypes.QueryBalanceRequest{Address: myAddress, Denom: denom}, + &banktypes.QueryBalanceRequest{Address: myAddress.String(), Denom: "atom"}, grpc.Header(&header), // Retrieve header from response ) if err != nil { return err } - blockHeight = header.Get(grpctypes.GRPCBlockHeightHeader) + blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader) fmt.Println(blockHeight) // Prints the block height (12)