Skip to content

Commit

Permalink
add ibc_transfer_cosmos_test
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 committed May 3, 2024
1 parent aa7c957 commit 06d19bb
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
196 changes: 196 additions & 0 deletions tests/interchaintest/ibc_transfer_cosmos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package interchaintest

import (
"context"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/relayer"
"testing"

"cosmossdk.io/math"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
interchaintestrelayer "github.com/strangelove-ventures/interchaintest/v8/relayer"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
)

// TestJunoGaiaIBCTransfer spins up a Juno and Gaia network, initializes an IBC connection between them,
// and sends an ICS20 token transfer from Juno->Gaia and then back from Gaia->Juno.
func TestJunoGaiaIBCTransfer(t *testing.T) {
if testing.Short() {
t.Skip()
}

t.Parallel()

// Create chain factory with Juno and Gaia
numVals := 1
numFullNodes := 1

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: "Centauri",
ChainConfig: CentauriConfig,
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
{
Name: "gaia",
Version: "v9.1.0",
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
})

const (
path = "ibc-path"
)

// Get chains from the chain factory
chains, err := cf.Chains(t.Name())
require.NoError(t, err)

client, network := interchaintest.DockerSetup(t)

juno, gaia := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

relayerType, relayerName := ibc.CosmosRly, "relay"

// Get a relayer instance
rf := interchaintest.NewBuiltinRelayerFactory(
relayerType,
zaptest.NewLogger(t),
relayer.DockerImage(&DefaultRelayer),
interchaintestrelayer.StartupFlags("--processor", "events", "--block-history", "100"),
)

r := rf.Build(t, client, network)

ic := interchaintest.NewInterchain().
AddChain(juno).
AddChain(gaia).
AddRelayer(r, relayerName).
AddLink(interchaintest.InterchainLink{
Chain1: juno,
Chain2: gaia,
Relayer: r,
Path: path,
})

ctx := context.Background()

rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)

require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: false,
}))
t.Cleanup(func() {
_ = ic.Close()
})

// Create some user accounts on both chains
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), genesisWalletAmount, juno, gaia)

// Wait a few blocks for relayer to start and for user accounts to be created
err = testutil.WaitForBlocks(ctx, 5, juno, gaia)
require.NoError(t, err)

// Get our Bech32 encoded user addresses
junoUser, gaiaUser := users[0], users[1]

junoUserAddr := junoUser.FormattedAddress()
gaiaUserAddr := gaiaUser.FormattedAddress()

// Get original account balances
junoOrigBal, err := juno.GetBalance(ctx, junoUserAddr, juno.Config().Denom)
require.NoError(t, err)
require.Equal(t, genesisWalletAmount, junoOrigBal)

gaiaOrigBal, err := gaia.GetBalance(ctx, gaiaUserAddr, gaia.Config().Denom)
require.NoError(t, err)
require.Equal(t, genesisWalletAmount, gaiaOrigBal)

// Compose an IBC transfer and send from Juno -> Gaia
var transferAmount = math.NewInt(1_000)
transfer := ibc.WalletAmount{
Address: gaiaUserAddr,
Denom: juno.Config().Denom,
Amount: transferAmount,
}

channel, err := ibc.GetTransferChannel(ctx, r, eRep, juno.Config().ChainID, gaia.Config().ChainID)
require.NoError(t, err)

junoHeight, err := juno.Height(ctx)
require.NoError(t, err)

transferTx, err := juno.SendIBCTransfer(ctx, channel.ChannelID, junoUserAddr, transfer, ibc.TransferOptions{})
require.NoError(t, err)

err = r.StartRelayer(ctx, eRep, path)
require.NoError(t, err)

t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
t.Logf("an error occurred while stopping the relayer: %s", err)
}
},
)

// Poll for the ack to know the transfer was successful
_, err = testutil.PollForAck(ctx, juno, junoHeight, junoHeight+50, transferTx.Packet)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 10, juno)
require.NoError(t, err)

// Get the IBC denom for ujuno on Gaia
junoTokenDenom := transfertypes.GetPrefixedDenom(channel.Counterparty.PortID, channel.Counterparty.ChannelID, juno.Config().Denom)
junoIBCDenom := transfertypes.ParseDenomTrace(junoTokenDenom).IBCDenom()

// Assert that the funds are no longer present in user acc on Juno and are in the user acc on Gaia
junoUpdateBal, err := juno.GetBalance(ctx, junoUserAddr, juno.Config().Denom)
require.NoError(t, err)
require.Equal(t, junoOrigBal.Sub(transferAmount), junoUpdateBal)

gaiaUpdateBal, err := gaia.GetBalance(ctx, gaiaUserAddr, junoIBCDenom)
require.NoError(t, err)
require.Equal(t, transferAmount, gaiaUpdateBal)

// Compose an IBC transfer and send from Gaia -> Juno
transfer = ibc.WalletAmount{
Address: junoUserAddr,
Denom: junoIBCDenom,
Amount: transferAmount,
}

gaiaHeight, err := gaia.Height(ctx)
require.NoError(t, err)

transferTx, err = gaia.SendIBCTransfer(ctx, channel.Counterparty.ChannelID, gaiaUserAddr, transfer, ibc.TransferOptions{})
require.NoError(t, err)

// Poll for the ack to know the transfer was successful
_, err = testutil.PollForAck(ctx, gaia, gaiaHeight, gaiaHeight+25, transferTx.Packet)
require.NoError(t, err)

// Assert that the funds are now back on Juno and not on Gaia
junoUpdateBal, err = juno.GetBalance(ctx, junoUserAddr, juno.Config().Denom)
require.NoError(t, err)
require.Equal(t, junoOrigBal, junoUpdateBal)

gaiaUpdateBal, err = gaia.GetBalance(ctx, gaiaUserAddr, junoIBCDenom)
require.NoError(t, err)
require.Equal(t, true, gaiaUpdateBal.IsZero())
}
3 changes: 3 additions & 0 deletions tests/interchaintest/setup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package interchaintest

import (
sdkmath "cosmossdk.io/math"
"os"

"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand Down Expand Up @@ -35,6 +36,8 @@ var (
ConfigFileOverrides: nil,
}

genesisWalletAmount = sdkmath.NewInt(10_000_000)

DefaultRelayer = ibc.DockerImage{
Repository: "ghcr.io/cosmos/relayer",
Version: "main",
Expand Down

0 comments on commit 06d19bb

Please sign in to comment.