forked from strangelove-ventures/interchaintest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn_ibc_test.go
125 lines (104 loc) · 3.81 KB
/
learn_ibc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ibc_test
import (
"context"
"fmt"
"testing"
"time"
"cosmossdk.io/math"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/testreporter"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
// This test is meant to be used as a basic interchaintest tutorial.
// Code snippets are broken down in ./docs/upAndRunning.md
func TestLearn(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
t.Parallel()
ctx := context.Background()
// Chain Factory
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{Name: "gaia", Version: "v7.0.0", ChainConfig: ibc.ChainConfig{
GasPrices: "0.0uatom",
}},
{Name: "osmosis", Version: "v11.0.0"},
})
chains, err := cf.Chains(t.Name())
require.NoError(t, err)
gaia, osmosis := chains[0], chains[1]
// Relayer Factory
client, network := interchaintest.DockerSetup(t)
r := interchaintest.NewBuiltinRelayerFactory(ibc.CosmosRly, zaptest.NewLogger(t)).Build(
t, client, network)
// Prep Interchain
const ibcPath = "gaia-osmo-demo"
ic := interchaintest.NewInterchain().
AddChain(gaia).
AddChain(osmosis).
AddRelayer(r, "relayer").
AddLink(interchaintest.InterchainLink{
Chain1: gaia,
Chain2: osmosis,
Relayer: r,
Path: ibcPath,
})
// Log location
f, err := interchaintest.CreateLogFile(fmt.Sprintf("%d.json", time.Now().Unix()))
require.NoError(t, err)
// Reporter/logs
rep := testreporter.NewReporter(f)
eRep := rep.RelayerExecReporter(t)
// Build interchain
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: false},
),
)
// Create and Fund User Wallets
fundAmount := math.NewInt(10_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", fundAmount.Int64(), gaia, osmosis)
gaiaUser := users[0]
osmosisUser := users[1]
gaiaUserBalInitial, err := gaia.GetBalance(ctx, gaiaUser.FormattedAddress(), gaia.Config().Denom)
require.NoError(t, err)
require.True(t, gaiaUserBalInitial.Equal(fundAmount))
// Get Channel ID
gaiaChannelInfo, err := r.GetChannels(ctx, eRep, gaia.Config().ChainID)
require.NoError(t, err)
gaiaChannelID := gaiaChannelInfo[0].ChannelID
osmoChannelInfo, err := r.GetChannels(ctx, eRep, osmosis.Config().ChainID)
require.NoError(t, err)
osmoChannelID := osmoChannelInfo[0].ChannelID
// Send Transaction
amountToSend := math.NewInt(1_000_000)
dstAddress := osmosisUser.FormattedAddress()
transfer := ibc.WalletAmount{
Address: dstAddress,
Denom: gaia.Config().Denom,
Amount: amountToSend,
}
tx, err := gaia.SendIBCTransfer(ctx, gaiaChannelID, gaiaUser.KeyName(), transfer, ibc.TransferOptions{})
require.NoError(t, err)
require.NoError(t, tx.Validate())
// relay MsgRecvPacket to osmosis, then MsgAcknowledgement back to gaia
require.NoError(t, r.Flush(ctx, eRep, ibcPath, gaiaChannelID))
// test source wallet has decreased funds
expectedBal := gaiaUserBalInitial.Sub(amountToSend)
gaiaUserBalNew, err := gaia.GetBalance(ctx, gaiaUser.FormattedAddress(), gaia.Config().Denom)
require.NoError(t, err)
require.True(t, gaiaUserBalNew.Equal(expectedBal))
// Trace IBC Denom
srcDenomTrace := transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom("transfer", osmoChannelID, gaia.Config().Denom))
dstIbcDenom := srcDenomTrace.IBCDenom()
// Test destination wallet has increased funds
osmosUserBalNew, err := osmosis.GetBalance(ctx, osmosisUser.FormattedAddress(), dstIbcDenom)
require.NoError(t, err)
require.True(t, osmosUserBalNew.Equal(amountToSend))
}