From 9a78d5b87ad15cb9a883c497d4588baa2271df5f Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 14 Mar 2024 17:21:14 -0300 Subject: [PATCH] (feat) Added support for all queries in the chain 'tendermint' module --- client/chain/chain.go | 60 ++++++++++++++++ client/chain/chain_test_support.go | 32 +++++++++ .../tendermint/query/1_GetNodeInfo/example.go | 69 ++++++++++++++++++ .../tendermint/query/2_GetSyncing/example.go | 69 ++++++++++++++++++ .../query/3_GetLatestBlock/example.go | 69 ++++++++++++++++++ .../query/4_GetBlockByHeight/example.go | 70 ++++++++++++++++++ .../query/5_GetLatestValidatorSet/example.go | 67 +++++++++++++++++ .../6_GetValidatorSetByHeight/example.go | 71 +++++++++++++++++++ 8 files changed, 507 insertions(+) create mode 100644 examples/chain/tendermint/query/1_GetNodeInfo/example.go create mode 100644 examples/chain/tendermint/query/2_GetSyncing/example.go create mode 100644 examples/chain/tendermint/query/3_GetLatestBlock/example.go create mode 100644 examples/chain/tendermint/query/4_GetBlockByHeight/example.go create mode 100644 examples/chain/tendermint/query/5_GetLatestValidatorSet/example.go create mode 100644 examples/chain/tendermint/query/6_GetValidatorSetByHeight/example.go diff --git a/client/chain/chain.go b/client/chain/chain.go index 058c57df..e3cd4097 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -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" @@ -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() } @@ -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 @@ -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), } @@ -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) +} diff --git a/client/chain/chain_test_support.go b/client/chain/chain_test_support.go index 5ae0cfb6..a22fe440 100644 --- a/client/chain/chain_test_support.go +++ b/client/chain/chain_test_support.go @@ -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" @@ -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 +} diff --git a/examples/chain/tendermint/query/1_GetNodeInfo/example.go b/examples/chain/tendermint/query/1_GetNodeInfo/example.go new file mode 100644 index 00000000..b5392b32 --- /dev/null +++ b/examples/chain/tendermint/query/1_GetNodeInfo/example.go @@ -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)) + +} diff --git a/examples/chain/tendermint/query/2_GetSyncing/example.go b/examples/chain/tendermint/query/2_GetSyncing/example.go new file mode 100644 index 00000000..0d136f24 --- /dev/null +++ b/examples/chain/tendermint/query/2_GetSyncing/example.go @@ -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)) + +} diff --git a/examples/chain/tendermint/query/3_GetLatestBlock/example.go b/examples/chain/tendermint/query/3_GetLatestBlock/example.go new file mode 100644 index 00000000..21434356 --- /dev/null +++ b/examples/chain/tendermint/query/3_GetLatestBlock/example.go @@ -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)) + +} diff --git a/examples/chain/tendermint/query/4_GetBlockByHeight/example.go b/examples/chain/tendermint/query/4_GetBlockByHeight/example.go new file mode 100644 index 00000000..efcca72f --- /dev/null +++ b/examples/chain/tendermint/query/4_GetBlockByHeight/example.go @@ -0,0 +1,70 @@ +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() + + height := int64(23040174) + res, err := chainClient.FetchBlockByHeight(ctx, height) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/tendermint/query/5_GetLatestValidatorSet/example.go b/examples/chain/tendermint/query/5_GetLatestValidatorSet/example.go new file mode 100644 index 00000000..66142f4a --- /dev/null +++ b/examples/chain/tendermint/query/5_GetLatestValidatorSet/example.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "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.FetchLatestValidatorSet(ctx) + if err != nil { + fmt.Println(err) + } + + fmt.Print(res.String()) + +} diff --git a/examples/chain/tendermint/query/6_GetValidatorSetByHeight/example.go b/examples/chain/tendermint/query/6_GetValidatorSetByHeight/example.go new file mode 100644 index 00000000..419abf97 --- /dev/null +++ b/examples/chain/tendermint/query/6_GetValidatorSetByHeight/example.go @@ -0,0 +1,71 @@ +package main + +import ( + "context" + "fmt" + + "github.com/cosmos/cosmos-sdk/types/query" + + "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() + + height := int64(23040174) + pagination := query.PageRequest{Offset: 2, Limit: 10} + res, err := chainClient.FetchValidatorSetByHeight(ctx, height, &pagination) + if err != nil { + fmt.Println(err) + } + + fmt.Print(res.String()) + +}