@@ -4,11 +4,15 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"math/big"
7
+ "regexp"
8
+ "strings"
7
9
"testing"
8
10
"time"
9
11
12
+ "github.com/icon-project/ibc-integration/test/chains"
10
13
"github.com/icon-project/ibc-integration/test/testsuite"
11
14
"github.com/strangelove-ventures/interchaintest/v7/ibc"
15
+ "github.com/strangelove-ventures/interchaintest/v7/testreporter"
12
16
"github.com/stretchr/testify/assert"
13
17
)
14
18
@@ -17,21 +21,146 @@ type HopchainTestSuite struct {
17
21
T * testing.T
18
22
}
19
23
24
+ type AddressBalance struct {
25
+ Address string
26
+ Balances []Balance `mapstructure:"balance"`
27
+ }
28
+
29
+ type Balance struct {
30
+ Denom string
31
+ Amount * big.Int
32
+ }
33
+
34
+ const IconChainName = "icon"
35
+ const CentauriChainName = "centauri"
36
+
37
+ func getBalance (balanceStdout string ) AddressBalance {
38
+ balanceSplit := strings .Split (balanceStdout , "\n " )
39
+ address := ""
40
+ var balances []Balance
41
+ for _ , balanceInfo := range balanceSplit {
42
+ if strings .HasPrefix (balanceInfo , "address" ) {
43
+ // Define regular expressions to extract address, ICX balance, and stake balance
44
+ addressRegex := regexp .MustCompile (`address\s*{([^}]*)}` )
45
+ balanceRegex := regexp .MustCompile (`balance\s(.*)` )
46
+
47
+ // Extract address
48
+ addressMatches := addressRegex .FindStringSubmatch (balanceInfo )
49
+ address = addressMatches [1 ]
50
+
51
+ // Extract ICX balance and stake balance
52
+ balanceMatches := balanceRegex .FindStringSubmatch (balanceInfo )
53
+ re := regexp .MustCompile (`(\d+)([a-zA-Z\/\-\d+]+)` )
54
+
55
+ // Find all matches in the input string
56
+ matches := re .FindAllStringSubmatch (balanceMatches [0 ], - 1 )
57
+
58
+ // Iterate over matches and populate the map
59
+ for _ , match := range matches {
60
+ amount := new (big.Int )
61
+ amount .SetString (match [1 ], 10 )
62
+ // currencyBalance[match[2]] = amount
63
+ balances = append (balances , Balance {
64
+ Denom : match [2 ],
65
+ Amount : amount ,
66
+ })
67
+ }
68
+
69
+ }
70
+ }
71
+ return AddressBalance {
72
+ Address : address ,
73
+ Balances : balances ,
74
+ }
75
+ }
76
+
77
+ func getLatestChannels (ctx context.Context , eRep * testreporter.RelayerExecReporter , chainID string , relayer ibc.Relayer ) (string , string , error ) {
78
+ channels , err := relayer .GetChannels (ctx , eRep , chainID )
79
+ if err != nil {
80
+ return "" , "" , err
81
+ }
82
+ latestChannel := channels [len (channels )- 1 ]
83
+ return latestChannel .Counterparty .ChannelID , latestChannel .ChannelID , nil
84
+ }
85
+
86
+ func getIconChain (chains ... chains.Chain ) chains.Chain {
87
+ for _ , chain := range chains {
88
+ if chain .(ibc.Chain ).Config ().Name == IconChainName {
89
+ return chain
90
+ }
91
+ }
92
+ return nil
93
+ }
94
+
95
+ func getCentauriChain (chains ... chains.Chain ) chains.Chain {
96
+ for _ , chain := range chains {
97
+ if chain .(ibc.Chain ).Config ().Name == CentauriChainName {
98
+ return chain
99
+ }
100
+ }
101
+ return nil
102
+ }
103
+
104
+ func getChainAddressBalance (ctx context.Context , eRep * testreporter.RelayerExecReporter , relayer ibc.Relayer , chain chains.Chain ) AddressBalance {
105
+ balanceCommands := []string {"rly" , "query" , "balance" , chain .(ibc.Chain ).Config ().ChainID }
106
+ balanceResult := relayer .Exec (ctx , eRep , balanceCommands , nil )
107
+ balanceInfo := getBalance (string (balanceResult .Stdout [:]))
108
+ return balanceInfo
109
+ }
110
+
111
+ func getChainBalance (balances []Balance , denom string ) Balance {
112
+ for _ , bal := range balances {
113
+ if bal .Denom == denom {
114
+ return bal
115
+ }
116
+ }
117
+ return Balance {}
118
+ }
119
+
20
120
func (h * HopchainTestSuite ) TestICS20 (relayer ibc.Relayer ) {
21
121
testcase := "ics20"
22
122
// portId := "transfer"
23
123
ctx := context .WithValue (context .TODO (), "testcase" , testcase )
24
124
chainA , chainB := h .GetChains ()
25
- h .T .Run ("send IBC Ftokens" , func (t * testing.T ) {
26
- receiver := "centauri1g5r2vmnp6lta9cpst4lzc4syy3kcj2ljte3tlh"
27
- txnhash , err := chainA .SendIBCTokenTransfer (ctx , "channel-0" , "transfer" , receiver , 100 )
28
- h .Require ().NoError (err , "error should be nil" )
29
- fmt .Println (txnhash )
30
- time .Sleep (5 * time .Second )
31
- // find balance in chainB
32
- balance , err := chainB .GetWalletBalance (ctx , receiver , "ibc/27029D876FCF5510CB000161B8CEEEB25B25C35309B3512ECD36D921385E8C52" )
125
+ eRep := h .GetRelayerExecReporter ()
126
+ portId := "transfer"
127
+ h .T .Run ("send IBC Ftokens for relay centauri-ibc" , func (t * testing.T ) {
128
+ iconChain := getIconChain (chainA , chainB )
129
+ centauriChain := getCentauriChain (chainA , chainB )
130
+ iconAccountBalance := getChainAddressBalance (ctx , eRep , relayer , iconChain )
131
+ cenauriAccountBalance := getChainAddressBalance (ctx , eRep , relayer , centauriChain )
132
+ centauriReceiver := cenauriAccountBalance .Address
133
+ iconReceiver := iconAccountBalance .Address
134
+ relaySourceChannel , relayDestinationChannel , err := getLatestChannels (ctx , eRep , centauriChain .(ibc.Chain ).Config ().ChainID , relayer )
135
+ denom := "transfer/" + relayDestinationChannel + "/icx"
33
136
h .Require ().NoError (err , "error should be nil" )
34
- fmt .Println ("The retrieved balance is" , balance )
35
- assert .True (t , balance .Cmp (big .NewInt (0 )) > 0 , "balance should be greater than 0" )
137
+ h .T .Run ("send IBC Ftokens from ibc to centauri " , func (t * testing.T ) {
138
+ _ , err := iconChain .SendIBCTokenTransfer (ctx , relaySourceChannel , relayDestinationChannel , portId , centauriReceiver , centauriChain .(ibc.Chain ).Config ().ChainID , 100 )
139
+ h .Require ().NoError (err , "error should be nil" )
140
+ // give some time for txn to settle
141
+ time .Sleep (10 * time .Second )
142
+ iconAccountBalance = getChainAddressBalance (ctx , eRep , relayer , iconChain )
143
+ cenauriAccountBalance = getChainAddressBalance (ctx , eRep , relayer , centauriChain )
144
+
145
+ valueTocheck := new (big.Int )
146
+ valueTocheck , _ = valueTocheck .SetString ("100000000000000000000" , 10 )
147
+
148
+ centauriChanBalance := getChainBalance (cenauriAccountBalance .Balances , denom )
149
+ assert .True (t , centauriChanBalance .Amount .Cmp (valueTocheck ) == 0 , "balance should be equal to 100000000000000000000" )
150
+ })
151
+
152
+ h .T .Run ("send IBC Ftokens from centauri to ibc " , func (t * testing.T ) {
153
+ // send back to icon
154
+ txnhash , err := centauriChain .SendIBCTokenTransfer (ctx , relayDestinationChannel , relayDestinationChannel , "transfer" , iconReceiver , centauriChain .(ibc.Chain ).Config ().ChainID , 50 )
155
+ h .Require ().NoError (err , "error should be nil" )
156
+ fmt .Println ("Txn hash is " , txnhash )
157
+ time .Sleep (10 * time .Second )
158
+ valueTocheck := new (big.Int )
159
+ valueTocheck , _ = valueTocheck .SetString ("50000000000000000000" , 10 )
160
+ iconAccountBalance = getChainAddressBalance (ctx , eRep , relayer , iconChain )
161
+ cenauriAccountBalance = getChainAddressBalance (ctx , eRep , relayer , centauriChain )
162
+ centauriChanBalance := getChainBalance (cenauriAccountBalance .Balances , denom )
163
+ assert .True (t , centauriChanBalance .Amount .Cmp (valueTocheck ) == 0 , "balance should be equal to 50000000000000000000" )
164
+ })
36
165
})
37
166
}
0 commit comments