Skip to content

Commit

Permalink
(feat) Added support for IBC Core Connection module. Included example…
Browse files Browse the repository at this point in the history
… scripts
  • Loading branch information
abel committed Apr 25, 2024
1 parent 31417bc commit 01e7ce1
Show file tree
Hide file tree
Showing 8 changed files with 552 additions and 40 deletions.
122 changes: 94 additions & 28 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"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"
ibcconnectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/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 @@ -279,6 +280,14 @@ type ChainClient interface {
FetchIBCUpgradedClientState(ctx context.Context) (*ibcclienttypes.QueryUpgradedClientStateResponse, error)
FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcclienttypes.QueryUpgradedConsensusStateResponse, error)

// IBC Core Connection module
FetchIBCConnection(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionResponse, error)
FetchIBCConnections(ctx context.Context, pagination *query.PageRequest) (*ibcconnectiontypes.QueryConnectionsResponse, error)
FetchIBCClientConnections(ctx context.Context, clientId string) (*ibcconnectiontypes.QueryClientConnectionsResponse, error)
FetchIBCConnectionClientState(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionClientStateResponse, error)
FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error)
FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error)

Close()
}

Expand All @@ -305,20 +314,21 @@ type chainClient struct {

sessionEnabled bool

authQueryClient authtypes.QueryClient
authzQueryClient authztypes.QueryClient
bankQueryClient banktypes.QueryClient
chainStreamClient chainstreamtypes.StreamClient
distributionQueryClient distributiontypes.QueryClient
exchangeQueryClient exchangetypes.QueryClient
ibcChannelQueryClient ibcchanneltypes.QueryClient
ibcClientQueryClient ibcclienttypes.QueryClient
ibcTransferQueryClient ibctransfertypes.QueryClient
tendermintQueryClient tmservice.ServiceClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
txClient txtypes.ServiceClient
wasmQueryClient wasmtypes.QueryClient
subaccountToNonce map[ethcommon.Hash]uint32
authQueryClient authtypes.QueryClient
authzQueryClient authztypes.QueryClient
bankQueryClient banktypes.QueryClient
chainStreamClient chainstreamtypes.StreamClient
distributionQueryClient distributiontypes.QueryClient
exchangeQueryClient exchangetypes.QueryClient
ibcChannelQueryClient ibcchanneltypes.QueryClient
ibcClientQueryClient ibcclienttypes.QueryClient
ibcConnectionQueryClient ibcconnectiontypes.QueryClient
ibcTransferQueryClient ibctransfertypes.QueryClient
tendermintQueryClient tmservice.ServiceClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
txClient txtypes.ServiceClient
wasmQueryClient wasmtypes.QueryClient
subaccountToNonce map[ethcommon.Hash]uint32

closed int64
canSign bool
Expand Down Expand Up @@ -404,20 +414,21 @@ func NewChainClient(

sessionEnabled: stickySessionEnabled,

authQueryClient: authtypes.NewQueryClient(conn),
authzQueryClient: authztypes.NewQueryClient(conn),
bankQueryClient: banktypes.NewQueryClient(conn),
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
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),
txClient: txtypes.NewServiceClient(conn),
wasmQueryClient: wasmtypes.NewQueryClient(conn),
subaccountToNonce: make(map[ethcommon.Hash]uint32),
authQueryClient: authtypes.NewQueryClient(conn),
authzQueryClient: authztypes.NewQueryClient(conn),
bankQueryClient: banktypes.NewQueryClient(conn),
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
distributionQueryClient: distributiontypes.NewQueryClient(conn),
exchangeQueryClient: exchangetypes.NewQueryClient(conn),
ibcChannelQueryClient: ibcchanneltypes.NewQueryClient(conn),
ibcClientQueryClient: ibcclienttypes.NewQueryClient(conn),
ibcConnectionQueryClient: ibcconnectiontypes.NewQueryClient(conn),
ibcTransferQueryClient: ibctransfertypes.NewQueryClient(conn),
tendermintQueryClient: tmservice.NewServiceClient(conn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
txClient: txtypes.NewServiceClient(conn),
wasmQueryClient: wasmtypes.NewQueryClient(conn),
subaccountToNonce: make(map[ethcommon.Hash]uint32),
}

if cc.canSign {
Expand Down Expand Up @@ -2565,3 +2576,58 @@ func (c *chainClient) FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcc

return res, err
}

// IBC Core Connection module
func (c *chainClient) FetchIBCConnection(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionResponse, error) {
req := &ibcconnectiontypes.QueryConnectionRequest{
ConnectionId: connectionId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.Connection, req)

return res, err

Check warning on line 2587 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2581-L2587

Added lines #L2581 - L2587 were not covered by tests
}

func (c *chainClient) FetchIBCConnections(ctx context.Context, pagination *query.PageRequest) (*ibcconnectiontypes.QueryConnectionsResponse, error) {
req := &ibcconnectiontypes.QueryConnectionsRequest{
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.Connections, req)

return res, err

Check warning on line 2596 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2590-L2596

Added lines #L2590 - L2596 were not covered by tests
}

func (c *chainClient) FetchIBCClientConnections(ctx context.Context, clientId string) (*ibcconnectiontypes.QueryClientConnectionsResponse, error) {
req := &ibcconnectiontypes.QueryClientConnectionsRequest{
ClientId: clientId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ClientConnections, req)

return res, err

Check warning on line 2605 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2599-L2605

Added lines #L2599 - L2605 were not covered by tests
}

func (c *chainClient) FetchIBCConnectionClientState(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionClientStateResponse, error) {
req := &ibcconnectiontypes.QueryConnectionClientStateRequest{
ConnectionId: connectionId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ConnectionClientState, req)

return res, err

Check warning on line 2614 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2608-L2614

Added lines #L2608 - L2614 were not covered by tests
}

func (c *chainClient) FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error) {
req := &ibcconnectiontypes.QueryConnectionConsensusStateRequest{
ConnectionId: connectionId,
RevisionNumber: revisionNumber,
RevisionHeight: revisionHeight,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ConnectionConsensusState, req)

return res, err

Check warning on line 2625 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2617-L2625

Added lines #L2617 - L2625 were not covered by tests
}

func (c *chainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error) {
req := &ibcconnectiontypes.QueryConnectionParamsRequest{}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ConnectionParams, req)

return res, err

Check warning on line 2632 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2628-L2632

Added lines #L2628 - L2632 were not covered by tests
}
44 changes: 32 additions & 12 deletions client/chain/chain_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@ 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"

"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"
"google.golang.org/grpc"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types"
tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -30,8 +21,12 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
eth "github.com/ethereum/go-ethereum/common"
"google.golang.org/grpc"
)

type MockChainClient struct {
Expand Down Expand Up @@ -695,3 +690,28 @@ func (c *MockChainClient) FetchIBCUpgradedClientState(ctx context.Context) (*ibc
func (c *MockChainClient) FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcclienttypes.QueryUpgradedConsensusStateResponse, error) {
return &ibcclienttypes.QueryUpgradedConsensusStateResponse{}, nil
}

// IBC Core Connection module
func (c *MockChainClient) FetchIBCConnection(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionResponse, error) {
return &ibcconnectiontypes.QueryConnectionResponse{}, nil

Check warning on line 696 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L695-L696

Added lines #L695 - L696 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnections(ctx context.Context, pagination *query.PageRequest) (*ibcconnectiontypes.QueryConnectionsResponse, error) {
return &ibcconnectiontypes.QueryConnectionsResponse{}, nil

Check warning on line 700 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L699-L700

Added lines #L699 - L700 were not covered by tests
}

func (c *MockChainClient) FetchIBCClientConnections(ctx context.Context, clientId string) (*ibcconnectiontypes.QueryClientConnectionsResponse, error) {
return &ibcconnectiontypes.QueryClientConnectionsResponse{}, nil

Check warning on line 704 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L703-L704

Added lines #L703 - L704 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnectionClientState(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionClientStateResponse, error) {
return &ibcconnectiontypes.QueryConnectionClientStateResponse{}, nil

Check warning on line 708 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L707-L708

Added lines #L707 - L708 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error) {
return &ibcconnectiontypes.QueryConnectionConsensusStateResponse{}, nil

Check warning on line 712 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L711-L712

Added lines #L711 - L712 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error) {
return &ibcconnectiontypes.QueryConnectionParamsResponse{}, nil

Check warning on line 716 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L715-L716

Added lines #L715 - L716 were not covered by tests
}
71 changes: 71 additions & 0 deletions examples/chain/ibc/connection/query/1_Connection/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"context"
"encoding/json"
"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)
}

connectionId := "connection-0"
ctx := context.Background()

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

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

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

import (
"context"
"encoding/json"
"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.FetchIBCConnections(ctx, &pagination)
if err != nil {
fmt.Println(err)
}

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

}

0 comments on commit 01e7ce1

Please sign in to comment.