Skip to content

Commit

Permalink
Merge pull request #217 from InjectiveLabs/feat/ibc_core_channel_modu…
Browse files Browse the repository at this point in the history
…le_queries

(feat) Added support for IBC Core Channel module queries. Added also …
  • Loading branch information
aarmoa authored Apr 16, 2024
2 parents ac5aaab + 4a698ee commit 4c14bf7
Show file tree
Hide file tree
Showing 15 changed files with 1,163 additions and 2 deletions.
161 changes: 159 additions & 2 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"sync/atomic"
"time"

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"
Expand All @@ -39,6 +37,8 @@ import (
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
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"
ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
eth "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
Expand Down Expand Up @@ -252,6 +252,21 @@ type ChainClient interface {
FetchEscrowAddress(ctx context.Context, portId string, channelId string) (*ibctransfertypes.QueryEscrowAddressResponse, error)
FetchTotalEscrowForDenom(ctx context.Context, denom string) (*ibctransfertypes.QueryTotalEscrowForDenomResponse, error)

// IBC Core Channel module
FetchIBCChannel(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryChannelResponse, error)
FetchIBCChannels(ctx context.Context, pagination *query.PageRequest) (*ibcchanneltypes.QueryChannelsResponse, error)
FetchIBCConnectionChannels(ctx context.Context, connection string, pagination *query.PageRequest) (*ibcchanneltypes.QueryConnectionChannelsResponse, error)
FetchIBCChannelClientState(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryChannelClientStateResponse, error)
FetchIBCChannelConsensusState(ctx context.Context, portId string, channelId string, revisionNumber uint64, revisionHeight uint64) (*ibcchanneltypes.QueryChannelConsensusStateResponse, error)
FetchIBCPacketCommitment(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketCommitmentResponse, error)
FetchIBCPacketCommitments(ctx context.Context, portId string, channelId string, pagination *query.PageRequest) (*ibcchanneltypes.QueryPacketCommitmentsResponse, error)
FetchIBCPacketReceipt(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketReceiptResponse, error)
FetchIBCPacketAcknowledgement(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketAcknowledgementResponse, error)
FetchIBCPacketAcknowledgements(ctx context.Context, portId string, channelId string, packetCommitmentSequences []uint64, pagination *query.PageRequest) (*ibcchanneltypes.QueryPacketAcknowledgementsResponse, error)
FetchIBCUnreceivedPackets(ctx context.Context, portId string, channelId string, packetCommitmentSequences []uint64) (*ibcchanneltypes.QueryUnreceivedPacketsResponse, error)
FetchIBCUnreceivedAcks(ctx context.Context, portId string, channelId string, packetAckSequences []uint64) (*ibcchanneltypes.QueryUnreceivedAcksResponse, error)
FetchIBCNextSequenceReceive(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryNextSequenceReceiveResponse, error)

Close()
}

Expand Down Expand Up @@ -284,6 +299,7 @@ type chainClient struct {
chainStreamClient chainstreamtypes.StreamClient
distributionQueryClient distributiontypes.QueryClient
exchangeQueryClient exchangetypes.QueryClient
ibcChannelQueryClient ibcchanneltypes.QueryClient
ibcTransferQueryClient ibctransfertypes.QueryClient
tendermintQueryClient tmservice.ServiceClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
Expand Down Expand Up @@ -381,6 +397,7 @@ func NewChainClient(
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
distributionQueryClient: distributiontypes.NewQueryClient(conn),
exchangeQueryClient: exchangetypes.NewQueryClient(conn),
ibcChannelQueryClient: ibcchanneltypes.NewQueryClient(conn),
ibcTransferQueryClient: ibctransfertypes.NewQueryClient(conn),
tendermintQueryClient: tmservice.NewServiceClient(conn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
Expand Down Expand Up @@ -2313,3 +2330,143 @@ func (c *chainClient) FetchTotalEscrowForDenom(ctx context.Context, denom string

return res, err
}

// IBC Core Channel module
func (c *chainClient) FetchIBCChannel(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryChannelResponse, error) {
req := &ibcchanneltypes.QueryChannelRequest{
PortId: portId,
ChannelId: channelId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.Channel, req)

return res, err
}

func (c *chainClient) FetchIBCChannels(ctx context.Context, pagination *query.PageRequest) (*ibcchanneltypes.QueryChannelsResponse, error) {
req := &ibcchanneltypes.QueryChannelsRequest{
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.Channels, req)

return res, err
}

func (c *chainClient) FetchIBCConnectionChannels(ctx context.Context, connection string, pagination *query.PageRequest) (*ibcchanneltypes.QueryConnectionChannelsResponse, error) {
req := &ibcchanneltypes.QueryConnectionChannelsRequest{
Connection: connection,
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.ConnectionChannels, req)

return res, err
}

func (c *chainClient) FetchIBCChannelClientState(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryChannelClientStateResponse, error) {
req := &ibcchanneltypes.QueryChannelClientStateRequest{
PortId: portId,
ChannelId: channelId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.ChannelClientState, req)

return res, err
}

func (c *chainClient) FetchIBCChannelConsensusState(ctx context.Context, portId string, channelId string, revisionNumber uint64, revisionHeight uint64) (*ibcchanneltypes.QueryChannelConsensusStateResponse, error) {
req := &ibcchanneltypes.QueryChannelConsensusStateRequest{
PortId: portId,
ChannelId: channelId,
RevisionNumber: revisionNumber,
RevisionHeight: revisionHeight,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.ChannelConsensusState, req)

return res, err
}

func (c *chainClient) FetchIBCPacketCommitment(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketCommitmentResponse, error) {
req := &ibcchanneltypes.QueryPacketCommitmentRequest{
PortId: portId,
ChannelId: channelId,
Sequence: sequence,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.PacketCommitment, req)

return res, err
}

func (c *chainClient) FetchIBCPacketCommitments(ctx context.Context, portId string, channelId string, pagination *query.PageRequest) (*ibcchanneltypes.QueryPacketCommitmentsResponse, error) {
req := &ibcchanneltypes.QueryPacketCommitmentsRequest{
PortId: portId,
ChannelId: channelId,
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.PacketCommitments, req)

return res, err
}

func (c *chainClient) FetchIBCPacketReceipt(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketReceiptResponse, error) {
req := &ibcchanneltypes.QueryPacketReceiptRequest{
PortId: portId,
ChannelId: channelId,
Sequence: sequence,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.PacketReceipt, req)

return res, err
}

func (c *chainClient) FetchIBCPacketAcknowledgement(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketAcknowledgementResponse, error) {
req := &ibcchanneltypes.QueryPacketAcknowledgementRequest{
PortId: portId,
ChannelId: channelId,
Sequence: sequence,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.PacketAcknowledgement, req)

return res, err
}

func (c *chainClient) FetchIBCPacketAcknowledgements(ctx context.Context, portId string, channelId string, packetCommitmentSequences []uint64, pagination *query.PageRequest) (*ibcchanneltypes.QueryPacketAcknowledgementsResponse, error) {
req := &ibcchanneltypes.QueryPacketAcknowledgementsRequest{
PortId: portId,
ChannelId: channelId,
Pagination: pagination,
PacketCommitmentSequences: packetCommitmentSequences,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.PacketAcknowledgements, req)

return res, err
}

func (c *chainClient) FetchIBCUnreceivedPackets(ctx context.Context, portId string, channelId string, packetCommitmentSequences []uint64) (*ibcchanneltypes.QueryUnreceivedPacketsResponse, error) {
req := &ibcchanneltypes.QueryUnreceivedPacketsRequest{
PortId: portId,
ChannelId: channelId,
PacketCommitmentSequences: packetCommitmentSequences,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.UnreceivedPackets, req)

return res, err
}

func (c *chainClient) FetchIBCUnreceivedAcks(ctx context.Context, portId string, channelId string, packetAckSequences []uint64) (*ibcchanneltypes.QueryUnreceivedAcksResponse, error) {
req := &ibcchanneltypes.QueryUnreceivedAcksRequest{
PortId: portId,
ChannelId: channelId,
PacketAckSequences: packetAckSequences,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.UnreceivedAcks, req)

return res, err
}

func (c *chainClient) FetchIBCNextSequenceReceive(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryNextSequenceReceiveResponse, error) {
req := &ibcchanneltypes.QueryNextSequenceReceiveRequest{
PortId: portId,
ChannelId: channelId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcChannelQueryClient.NextSequenceReceive, req)

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

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"
Expand Down Expand Up @@ -601,3 +603,56 @@ func (c *MockChainClient) FetchEscrowAddress(ctx context.Context, portId string,
func (c *MockChainClient) FetchTotalEscrowForDenom(ctx context.Context, denom string) (*ibctransfertypes.QueryTotalEscrowForDenomResponse, error) {
return &ibctransfertypes.QueryTotalEscrowForDenomResponse{}, nil
}

// IBC Core Channel module
func (c *MockChainClient) FetchIBCChannel(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryChannelResponse, error) {
return &ibcchanneltypes.QueryChannelResponse{}, nil
}

func (c *MockChainClient) FetchIBCChannels(ctx context.Context, pagination *query.PageRequest) (*ibcchanneltypes.QueryChannelsResponse, error) {
return &ibcchanneltypes.QueryChannelsResponse{}, nil
}

func (c *MockChainClient) FetchIBCConnectionChannels(ctx context.Context, connection string, pagination *query.PageRequest) (*ibcchanneltypes.QueryConnectionChannelsResponse, error) {
return &ibcchanneltypes.QueryConnectionChannelsResponse{}, nil
}

func (c *MockChainClient) FetchIBCChannelClientState(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryChannelClientStateResponse, error) {
return &ibcchanneltypes.QueryChannelClientStateResponse{}, nil
}

func (c *MockChainClient) FetchIBCChannelConsensusState(ctx context.Context, portId string, channelId string, revisionNumber uint64, revisionHeight uint64) (*ibcchanneltypes.QueryChannelConsensusStateResponse, error) {
return &ibcchanneltypes.QueryChannelConsensusStateResponse{}, nil
}

func (c *MockChainClient) FetchIBCPacketCommitment(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketCommitmentResponse, error) {
return &ibcchanneltypes.QueryPacketCommitmentResponse{}, nil
}

func (c *MockChainClient) FetchIBCPacketCommitments(ctx context.Context, portId string, channelId string, pagination *query.PageRequest) (*ibcchanneltypes.QueryPacketCommitmentsResponse, error) {
return &ibcchanneltypes.QueryPacketCommitmentsResponse{}, nil
}

func (c *MockChainClient) FetchIBCPacketReceipt(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketReceiptResponse, error) {
return &ibcchanneltypes.QueryPacketReceiptResponse{}, nil
}

func (c *MockChainClient) FetchIBCPacketAcknowledgement(ctx context.Context, portId string, channelId string, sequence uint64) (*ibcchanneltypes.QueryPacketAcknowledgementResponse, error) {
return &ibcchanneltypes.QueryPacketAcknowledgementResponse{}, nil
}

func (c *MockChainClient) FetchIBCPacketAcknowledgements(ctx context.Context, portId string, channelId string, packetCommitmentSequences []uint64, pagination *query.PageRequest) (*ibcchanneltypes.QueryPacketAcknowledgementsResponse, error) {
return &ibcchanneltypes.QueryPacketAcknowledgementsResponse{}, nil
}

func (c *MockChainClient) FetchIBCUnreceivedPackets(ctx context.Context, portId string, channelId string, packetCommitmentSequences []uint64) (*ibcchanneltypes.QueryUnreceivedPacketsResponse, error) {
return &ibcchanneltypes.QueryUnreceivedPacketsResponse{}, nil
}

func (c *MockChainClient) FetchIBCUnreceivedAcks(ctx context.Context, portId string, channelId string, packetAckSequences []uint64) (*ibcchanneltypes.QueryUnreceivedAcksResponse, error) {
return &ibcchanneltypes.QueryUnreceivedAcksResponse{}, nil
}

func (c *MockChainClient) FetchIBCNextSequenceReceive(ctx context.Context, portId string, channelId string) (*ibcchanneltypes.QueryNextSequenceReceiveResponse, error) {
return &ibcchanneltypes.QueryNextSequenceReceiveResponse{}, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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)
}

portId := "transfer"
channelId := "channel-126"
sequences := []uint64{1, 2}
pagination := query.PageRequest{Offset: 2, Limit: 4}
ctx := context.Background()

res, err := chainClient.FetchIBCPacketAcknowledgements(ctx, portId, channelId, sequences, &pagination)
if err != nil {
fmt.Println(err)
}

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

}
Loading

0 comments on commit 4c14bf7

Please sign in to comment.