Skip to content
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

(feat) Added script examples for the IBC Core Client module queries #218

Merged
merged 2 commits into from
Apr 23, 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
95 changes: 95 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/gogoproto/proto"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
eth "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
Expand Down Expand Up @@ -267,6 +268,17 @@ type ChainClient interface {
FetchIBCUnreceivedAcks(ctx context.Context, portId string, channelId string, packetAckSequences []uint64) (*ibcchanneltypes.QueryUnreceivedAcksResponse, error)
FetchIBCNextSequenceReceive(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryNextSequenceReceiveResponse, error)

// IBC Core Chain module
FetchIBCClientState(ctx context.Context, clientId string) (*ibcclienttypes.QueryClientStateResponse, error)
FetchIBCClientStates(ctx context.Context, pagination *query.PageRequest) (*ibcclienttypes.QueryClientStatesResponse, error)
FetchIBCConsensusState(ctx context.Context, clientId string, revisionNumber uint64, revisionHeight uint64, latestHeight bool) (*ibcclienttypes.QueryConsensusStateResponse, error)
FetchIBCConsensusStates(ctx context.Context, clientId string, pagination *query.PageRequest) (*ibcclienttypes.QueryConsensusStatesResponse, error)
FetchIBCConsensusStateHeights(ctx context.Context, clientId string, pagination *query.PageRequest) (*ibcclienttypes.QueryConsensusStateHeightsResponse, error)
FetchIBCClientStatus(ctx context.Context, clientId string) (*ibcclienttypes.QueryClientStatusResponse, error)
FetchIBCClientParams(ctx context.Context) (*ibcclienttypes.QueryClientParamsResponse, error)
FetchIBCUpgradedClientState(ctx context.Context) (*ibcclienttypes.QueryUpgradedClientStateResponse, error)
FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcclienttypes.QueryUpgradedConsensusStateResponse, error)

Close()
}

Expand Down Expand Up @@ -300,6 +312,7 @@ type chainClient struct {
distributionQueryClient distributiontypes.QueryClient
exchangeQueryClient exchangetypes.QueryClient
ibcChannelQueryClient ibcchanneltypes.QueryClient
ibcClientQueryClient ibcclienttypes.QueryClient
ibcTransferQueryClient ibctransfertypes.QueryClient
tendermintQueryClient tmservice.ServiceClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
Expand Down Expand Up @@ -398,6 +411,7 @@ func NewChainClient(
distributionQueryClient: distributiontypes.NewQueryClient(conn),
exchangeQueryClient: exchangetypes.NewQueryClient(conn),
ibcChannelQueryClient: ibcchanneltypes.NewQueryClient(conn),
ibcClientQueryClient: ibcclienttypes.NewQueryClient(conn),
ibcTransferQueryClient: ibctransfertypes.NewQueryClient(conn),
tendermintQueryClient: tmservice.NewServiceClient(conn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
Expand Down Expand Up @@ -2470,3 +2484,84 @@ func (c *chainClient) FetchIBCNextSequenceReceive(ctx context.Context, portId st

return res, err
}

// IBC Core Chain module
func (c *chainClient) FetchIBCClientState(ctx context.Context, clientId string) (*ibcclienttypes.QueryClientStateResponse, error) {
req := &ibcclienttypes.QueryClientStateRequest{
ClientId: clientId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.ClientState, req)

return res, err
}

func (c *chainClient) FetchIBCClientStates(ctx context.Context, pagination *query.PageRequest) (*ibcclienttypes.QueryClientStatesResponse, error) {
req := &ibcclienttypes.QueryClientStatesRequest{
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.ClientStates, req)

return res, err
}

func (c *chainClient) FetchIBCConsensusState(ctx context.Context, clientId string, revisionNumber uint64, revisionHeight uint64, latestHeight bool) (*ibcclienttypes.QueryConsensusStateResponse, error) {
req := &ibcclienttypes.QueryConsensusStateRequest{
ClientId: clientId,
RevisionNumber: revisionNumber,
RevisionHeight: revisionHeight,
LatestHeight: latestHeight,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.ConsensusState, req)

return res, err
}

func (c *chainClient) FetchIBCConsensusStates(ctx context.Context, clientId string, pagination *query.PageRequest) (*ibcclienttypes.QueryConsensusStatesResponse, error) {
req := &ibcclienttypes.QueryConsensusStatesRequest{
ClientId: clientId,
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.ConsensusStates, req)

return res, err
}

func (c *chainClient) FetchIBCConsensusStateHeights(ctx context.Context, clientId string, pagination *query.PageRequest) (*ibcclienttypes.QueryConsensusStateHeightsResponse, error) {
req := &ibcclienttypes.QueryConsensusStateHeightsRequest{
ClientId: clientId,
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.ConsensusStateHeights, req)

return res, err
}

func (c *chainClient) FetchIBCClientStatus(ctx context.Context, clientId string) (*ibcclienttypes.QueryClientStatusResponse, error) {
req := &ibcclienttypes.QueryClientStatusRequest{
ClientId: clientId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.ClientStatus, req)

return res, err
}

func (c *chainClient) FetchIBCClientParams(ctx context.Context) (*ibcclienttypes.QueryClientParamsResponse, error) {
req := &ibcclienttypes.QueryClientParamsRequest{}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.ClientParams, req)

return res, err
}

func (c *chainClient) FetchIBCUpgradedClientState(ctx context.Context) (*ibcclienttypes.QueryUpgradedClientStateResponse, error) {
req := &ibcclienttypes.QueryUpgradedClientStateRequest{}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.UpgradedClientState, req)

return res, err
}

func (c *chainClient) FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcclienttypes.QueryUpgradedConsensusStateResponse, error) {
req := &ibcclienttypes.QueryUpgradedConsensusStateRequest{}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcClientQueryClient.UpgradedConsensusState, req)

return res, err
}
39 changes: 39 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"

ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"

ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
Expand Down Expand Up @@ -656,3 +658,40 @@ func (c *MockChainClient) FetchIBCUnreceivedAcks(ctx context.Context, portId str
func (c *MockChainClient) FetchIBCNextSequenceReceive(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryNextSequenceReceiveResponse, error) {
return &ibcchanneltypes.QueryNextSequenceReceiveResponse{}, nil
}

// IBC Core Chain module
func (c *MockChainClient) FetchIBCClientState(ctx context.Context, clientId string) (*ibcclienttypes.QueryClientStateResponse, error) {
return &ibcclienttypes.QueryClientStateResponse{}, nil
}

func (c *MockChainClient) FetchIBCClientStates(ctx context.Context, pagination *query.PageRequest) (*ibcclienttypes.QueryClientStatesResponse, error) {
return &ibcclienttypes.QueryClientStatesResponse{}, nil
}

func (c *MockChainClient) FetchIBCConsensusState(ctx context.Context, clientId string, revisionNumber uint64, revisionHeight uint64, latestHeight bool) (*ibcclienttypes.QueryConsensusStateResponse, error) {
return &ibcclienttypes.QueryConsensusStateResponse{}, nil
}

func (c *MockChainClient) FetchIBCConsensusStates(ctx context.Context, clientId string, pagination *query.PageRequest) (*ibcclienttypes.QueryConsensusStatesResponse, error) {
return &ibcclienttypes.QueryConsensusStatesResponse{}, nil
}

func (c *MockChainClient) FetchIBCConsensusStateHeights(ctx context.Context, clientId string, pagination *query.PageRequest) (*ibcclienttypes.QueryConsensusStateHeightsResponse, error) {
return &ibcclienttypes.QueryConsensusStateHeightsResponse{}, nil
}

func (c *MockChainClient) FetchIBCClientStatus(ctx context.Context, clientId string) (*ibcclienttypes.QueryClientStatusResponse, error) {
return &ibcclienttypes.QueryClientStatusResponse{}, nil
}

func (c *MockChainClient) FetchIBCClientParams(ctx context.Context) (*ibcclienttypes.QueryClientParamsResponse, error) {
return &ibcclienttypes.QueryClientParamsResponse{}, nil
}

func (c *MockChainClient) FetchIBCUpgradedClientState(ctx context.Context) (*ibcclienttypes.QueryUpgradedClientStateResponse, error) {
return &ibcclienttypes.QueryUpgradedClientStateResponse{}, nil
}

func (c *MockChainClient) FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcclienttypes.QueryUpgradedConsensusStateResponse, error) {
return &ibcclienttypes.QueryUpgradedConsensusStateResponse{}, nil
}
69 changes: 69 additions & 0 deletions examples/chain/ibc/client/query/1_ClientState/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"context"
"fmt"

"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"

"os"
)

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)
}

clientId := "07-tendermint-0"
ctx := context.Background()

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

fmt.Print(res)

}
71 changes: 71 additions & 0 deletions examples/chain/ibc/client/query/2_ClientStates/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/types/query"

"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"

"os"
)

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)
}

pagination := query.PageRequest{Offset: 2, Limit: 4}
ctx := context.Background()

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

fmt.Print(res)

}
73 changes: 73 additions & 0 deletions examples/chain/ibc/client/query/3_ConsensusState/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"context"
"fmt"

"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"

"os"
)

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)
}

clientId := "07-tendermint-0"
revisionNumber := uint64(0)
revisionHeight := uint64(7379538)
latestHeight := false

ctx := context.Background()

res, err := chainClient.FetchIBCConsensusState(ctx, clientId, revisionNumber, revisionHeight, latestHeight)
if err != nil {
fmt.Println(err)
}

fmt.Print(res)

}
Loading
Loading