Skip to content

Commit

Permalink
add back in ICS test
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed May 10, 2024
1 parent b12bf27 commit 9146757
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
21 changes: 0 additions & 21 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,27 +589,6 @@ func (c *CosmosChain) ExportState(ctx context.Context, height int64) (string, er
return c.getFullNode().ExportState(ctx, height)
}

// GetBalance fetches the current balance for a specific account address and denom.
// Implements Chain interface
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (sdkmath.Int, error) {
params := &bankTypes.QueryBalanceRequest{Address: address, Denom: denom}
grpcAddress := c.getFullNode().hostGRPCPort
conn, err := grpc.Dial(grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return sdkmath.Int{}, err
}
defer conn.Close()

queryClient := bankTypes.NewQueryClient(conn)
res, err := queryClient.Balance(ctx, params)

if err != nil {
return sdkmath.Int{}, err
}

return res.Balance.Amount, nil
}

// AllBalances fetches an account address's balance for all denoms it holds
func (c *CosmosChain) AllBalances(ctx context.Context, address string) (types.Coins, error) {
params := bankTypes.QueryAllBalancesRequest{Address: address}
Expand Down
21 changes: 21 additions & 0 deletions chain/cosmos/module_bank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cosmos

import (
"context"

sdkmath "cosmossdk.io/math"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

// GetBalance fetches the current balance for a specific account address and denom.
// Implements Chain interface
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (sdkmath.Int, error) {
return c.BankQueryBalance(ctx, address, denom)
}

// BankGetBalance is an alias for GetBalance
func (c *CosmosChain) BankQueryBalance(ctx context.Context, address string, denom string) (sdkmath.Int, error) {
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).Balance(ctx, &banktypes.QueryBalanceRequest{Address: address, Denom: denom})
return res.Balance.Amount, err
}
76 changes: 72 additions & 4 deletions examples/ibc/ics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"testing"
"time"

"cosmossdk.io/math"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcconntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/testreporter"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
Expand Down Expand Up @@ -63,7 +66,7 @@ func TestICS(t *testing.T) {

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
provider, consumer := chains[0], chains[1]
provider, consumer := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

// Relayer Factory
client, network := interchaintest.DockerSetup(t)
Expand Down Expand Up @@ -104,11 +107,76 @@ func TestICS(t *testing.T) {
})
require.NoError(t, err, "failed to build interchain")

err = testutil.WaitForBlocks(ctx, 10, provider, consumer)
require.NoError(t, err, "failed to wait for blocks")
// ------------------ ICS Setup ------------------

// Finish the ICS provider chain initialization.
// - Restarts the relayer to connect ics20-1 transfer channel
// - Delegates tokens to the provider to update consensus value
// - Flushes the IBC state to the consumer
err = provider.FinishICSProviderSetup(ctx, r, eRep, ibcPath)
require.NoError(t, err)

// ------------------ Test Begins ------------------

// Fund users
// NOTE: this has to be done after the provider delegation & IBC update to the consumer.
amt := math.NewInt(10_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", amt, consumer, provider)
consumerUser, providerUser := users[0], users[1]

t.Run("validate consumer action executed", func(t *testing.T) {
bal, err := consumer.BankQueryBalance(ctx, consumerUser.FormattedAddress(), consumer.Config().Denom)
require.NoError(t, err)
require.EqualValues(t, amt, bal)
})

t.Run("provider -> consumer IBC transfer", func(t *testing.T) {
providerChannelInfo, err := r.GetChannels(ctx, eRep, provider.Config().ChainID)
require.NoError(t, err)

channelID, err := getTransferChannel(providerChannelInfo)
require.NoError(t, err, providerChannelInfo)

consumerChannelInfo, err := r.GetChannels(ctx, eRep, consumer.Config().ChainID)
require.NoError(t, err)

consumerChannelID, err := getTransferChannel(consumerChannelInfo)
require.NoError(t, err, consumerChannelInfo)

dstAddress := consumerUser.FormattedAddress()
sendAmt := math.NewInt(7)
transfer := ibc.WalletAmount{
Address: dstAddress,
Denom: provider.Config().Denom,
Amount: sendAmt,
}

tx, err := provider.SendIBCTransfer(ctx, channelID, providerUser.KeyName(), transfer, ibc.TransferOptions{})
require.NoError(t, err)
require.NoError(t, tx.Validate())

require.NoError(t, r.Flush(ctx, eRep, ibcPath, channelID))

srcDenomTrace := transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom("transfer", consumerChannelID, provider.Config().Denom))
dstIbcDenom := srcDenomTrace.IBCDenom()

consumerBal, err := consumer.BankQueryBalance(ctx, consumerUser.FormattedAddress(), dstIbcDenom)
require.NoError(t, err)
require.EqualValues(t, sendAmt, consumerBal)
})
})
}
}
})
}
}

func getTransferChannel(channels []ibc.ChannelOutput) (string, error) {
for _, channel := range channels {
if channel.PortID == "transfer" && channel.State == ibcconntypes.OPEN.String() {
return channel.ChannelID, nil
}
}

return "", fmt.Errorf("no open transfer channel found")
}

0 comments on commit 9146757

Please sign in to comment.