Skip to content

feat/tendermint_module_queries #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync/atomic"
"time"

"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"

distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -234,6 +236,15 @@ type ChainClient interface {
FetchTraderDerivativeConditionalOrders(ctx context.Context, subaccountId string, marketId string) (*exchangetypes.QueryTraderDerivativeConditionalOrdersResponse, error)
FetchMarketAtomicExecutionFeeMultiplier(ctx context.Context, marketId string) (*exchangetypes.QueryMarketAtomicExecutionFeeMultiplierResponse, error)

// Tendermint module
FetchNodeInfo(ctx context.Context) (*tmservice.GetNodeInfoResponse, error)
FetchSyncing(ctx context.Context) (*tmservice.GetSyncingResponse, error)
FetchLatestBlock(ctx context.Context) (*tmservice.GetLatestBlockResponse, error)
FetchBlockByHeight(ctx context.Context, height int64) (*tmservice.GetBlockByHeightResponse, error)
FetchLatestValidatorSet(ctx context.Context) (*tmservice.GetLatestValidatorSetResponse, error)
FetchValidatorSetByHeight(ctx context.Context, height int64, pagination *query.PageRequest) (*tmservice.GetValidatorSetByHeightResponse, error)
ABCIQuery(ctx context.Context, path string, data []byte, height int64, prove bool) (*tmservice.ABCIQueryResponse, error)

Close()
}

Expand Down Expand Up @@ -269,6 +280,7 @@ type chainClient struct {
chainStreamClient chainstreamtypes.StreamClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
distributionQueryClient distributiontypes.QueryClient
tendermintQueryClient tmservice.ServiceClient
subaccountToNonce map[ethcommon.Hash]uint32

closed int64
Expand Down Expand Up @@ -364,6 +376,7 @@ func NewChainClient(
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
distributionQueryClient: distributiontypes.NewQueryClient(conn),
tendermintQueryClient: tmservice.NewServiceClient(conn),
subaccountToNonce: make(map[ethcommon.Hash]uint32),
}

Expand Down Expand Up @@ -2017,3 +2030,50 @@ func (c *chainClient) FetchMarketAtomicExecutionFeeMultiplier(ctx context.Contex
}
return c.exchangeQueryClient.MarketAtomicExecutionFeeMultiplier(ctx, req)
}

// Tendermint module

func (c *chainClient) FetchNodeInfo(ctx context.Context) (*tmservice.GetNodeInfoResponse, error) {
req := &tmservice.GetNodeInfoRequest{}
return c.tendermintQueryClient.GetNodeInfo(ctx, req)
}

func (c *chainClient) FetchSyncing(ctx context.Context) (*tmservice.GetSyncingResponse, error) {
req := &tmservice.GetSyncingRequest{}
return c.tendermintQueryClient.GetSyncing(ctx, req)
}

func (c *chainClient) FetchLatestBlock(ctx context.Context) (*tmservice.GetLatestBlockResponse, error) {
req := &tmservice.GetLatestBlockRequest{}
return c.tendermintQueryClient.GetLatestBlock(ctx, req)
}

func (c *chainClient) FetchBlockByHeight(ctx context.Context, height int64) (*tmservice.GetBlockByHeightResponse, error) {
req := &tmservice.GetBlockByHeightRequest{
Height: height,
}
return c.tendermintQueryClient.GetBlockByHeight(ctx, req)
}

func (c *chainClient) FetchLatestValidatorSet(ctx context.Context) (*tmservice.GetLatestValidatorSetResponse, error) {
req := &tmservice.GetLatestValidatorSetRequest{}
return c.tendermintQueryClient.GetLatestValidatorSet(ctx, req)
}

func (c *chainClient) FetchValidatorSetByHeight(ctx context.Context, height int64, pagination *query.PageRequest) (*tmservice.GetValidatorSetByHeightResponse, error) {
req := &tmservice.GetValidatorSetByHeightRequest{
Height: height,
Pagination: pagination,
}
return c.tendermintQueryClient.GetValidatorSetByHeight(ctx, req)
}

func (c *chainClient) ABCIQuery(ctx context.Context, path string, data []byte, height int64, prove bool) (*tmservice.ABCIQueryResponse, error) {
req := &tmservice.ABCIQueryRequest{
Path: path,
Data: data,
Height: height,
Prove: prove,
}
return c.tendermintQueryClient.ABCIQuery(ctx, req)
Comment on lines +2036 to +2078
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of methods FetchNodeInfo, FetchSyncing, FetchLatestBlock, FetchBlockByHeight, FetchLatestValidatorSet, FetchValidatorSetByHeight, and ABCIQuery to interact with the Tendermint service are crucial for expanding the querying capabilities of the ChainClient interface. However, it's noted that these added lines were not covered by tests.

Would you like assistance in creating test cases for these new methods to ensure they function as expected and to maintain high code quality?

}
32 changes: 32 additions & 0 deletions client/chain/chain_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"time"

"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"

distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
Expand Down Expand Up @@ -546,3 +548,33 @@ func (c *MockChainClient) FetchTraderDerivativeConditionalOrders(ctx context.Con
func (c *MockChainClient) FetchMarketAtomicExecutionFeeMultiplier(ctx context.Context, marketId string) (*exchangetypes.QueryMarketAtomicExecutionFeeMultiplierResponse, error) {
return &exchangetypes.QueryMarketAtomicExecutionFeeMultiplierResponse{}, nil
}

// Tendermint module

func (c *MockChainClient) FetchNodeInfo(ctx context.Context) (*tmservice.GetNodeInfoResponse, error) {
return &tmservice.GetNodeInfoResponse{}, nil
}

func (c *MockChainClient) FetchSyncing(ctx context.Context) (*tmservice.GetSyncingResponse, error) {
return &tmservice.GetSyncingResponse{}, nil
}

func (c *MockChainClient) FetchLatestBlock(ctx context.Context) (*tmservice.GetLatestBlockResponse, error) {
return &tmservice.GetLatestBlockResponse{}, nil
}

func (c *MockChainClient) FetchBlockByHeight(ctx context.Context, height int64) (*tmservice.GetBlockByHeightResponse, error) {
return &tmservice.GetBlockByHeightResponse{}, nil
}

func (c *MockChainClient) FetchLatestValidatorSet(ctx context.Context) (*tmservice.GetLatestValidatorSetResponse, error) {
return &tmservice.GetLatestValidatorSetResponse{}, nil
}

func (c *MockChainClient) FetchValidatorSetByHeight(ctx context.Context, height int64, pagination *query.PageRequest) (*tmservice.GetValidatorSetByHeightResponse, error) {
return &tmservice.GetValidatorSetByHeightResponse{}, nil
}

func (c *MockChainClient) ABCIQuery(ctx context.Context, path string, data []byte, height int64, prove bool) (*tmservice.ABCIQueryResponse, error) {
return &tmservice.ABCIQueryResponse{}, nil
}
Comment on lines +554 to +580
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The added functions for interacting with the Tendermint module in the MockChainClient struct are a good addition for testing purposes. However, it's important to ensure these functions are covered by unit tests to validate their behavior and integration with the rest of the SDK. Consider adding tests for these functions to improve the SDK's test coverage and reliability.

Would you like me to help generate unit tests for these new functions or open a GitHub issue to track this task?

69 changes: 69 additions & 0 deletions examples/chain/tendermint/query/1_GetNodeInfo/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"context"
"encoding/json"
"fmt"

"os"

"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}

ctx := context.Background()

res, err := chainClient.FetchNodeInfo(ctx)
if err != nil {
fmt.Println(err)
}

str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))

}
Comment on lines +1 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example code for fetching node information is well-structured and follows Go conventions. Similar to the previous examples, adding error handling after printing the result in line 67 would improve the robustness of the example.

-	fmt.Print(string(str))
+	fmt.Println(string(str))
+	if err != nil {
+		// Handle the error appropriately
+		fmt.Println("Error fetching node information:", err)
+	}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)
func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)
if err != nil {
panic(err)
}
ctx := context.Background()
res, err := chainClient.FetchNodeInfo(ctx)
if err != nil {
fmt.Println(err)
}
str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))
}
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)
func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)
if err != nil {
panic(err)
}
ctx := context.Background()
res, err := chainClient.FetchNodeInfo(ctx)
if err != nil {
fmt.Println(err)
}
str, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(str))
if err != nil {
// Handle the error appropriately
fmt.Println("Error fetching node information:", err)
}
}

69 changes: 69 additions & 0 deletions examples/chain/tendermint/query/2_GetSyncing/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"context"
"encoding/json"
"fmt"

"os"

"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}

ctx := context.Background()

res, err := chainClient.FetchSyncing(ctx)
if err != nil {
fmt.Println(err)
}

str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))

}
Comment on lines +1 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example code for checking the syncing status of a Tendermint blockchain is clear and follows Go conventions. However, similar to the previous file, consider adding error handling after printing the result in line 67 to ensure that any issues during the operation are appropriately managed.

-	fmt.Print(string(str))
+	fmt.Println(string(str))
+	if err != nil {
+		// Handle the error appropriately
+		fmt.Println("Error fetching the syncing status:", err)
+	}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)
func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)
if err != nil {
panic(err)
}
ctx := context.Background()
res, err := chainClient.FetchSyncing(ctx)
if err != nil {
fmt.Println(err)
}
str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))
}
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)
func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)
if err != nil {
panic(err)
}
ctx := context.Background()
res, err := chainClient.FetchSyncing(ctx)
if err != nil {
fmt.Println(err)
}
str, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(str))
if err != nil {
// Handle the error appropriately
fmt.Println("Error fetching the syncing status:", err)
}
}

69 changes: 69 additions & 0 deletions examples/chain/tendermint/query/3_GetLatestBlock/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"context"
"encoding/json"
"fmt"

"os"

"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}

ctx := context.Background()

res, err := chainClient.FetchLatestBlock(ctx)
if err != nil {
fmt.Println(err)
}

str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))

}
Comment on lines +1 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example code for fetching the latest block is clear and follows Go conventions. As with the other examples, adding error handling after printing the result in line 67 would enhance the example's error management.

-	fmt.Print(string(str))
+	fmt.Println(string(str))
+	if err != nil {
+		// Handle the error appropriately
+		fmt.Println("Error fetching the latest block:", err)
+	}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)
func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)
if err != nil {
panic(err)
}
ctx := context.Background()
res, err := chainClient.FetchLatestBlock(ctx)
if err != nil {
fmt.Println(err)
}
str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))
}
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)
func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)
if err != nil {
panic(err)
}
ctx := context.Background()
res, err := chainClient.FetchLatestBlock(ctx)
if err != nil {
fmt.Println(err)
}
str, _ := json.MarshalIndent(res, "", " ")
fmt.Println(string(str))
if err != nil {
// Handle the error appropriately
fmt.Println("Error fetching the latest block:", err)
}
}

Loading