44 "context"
55 "crypto/ecdsa"
66 "encoding/json"
7+ "errors"
78 "fmt"
89 "io"
910 "math/big"
@@ -23,6 +24,7 @@ import (
2324 "github.com/iotaledger/wasp/v2/packages/testutil/testkey"
2425 "github.com/iotaledger/wasp/v2/packages/vm/gas"
2526 "github.com/iotaledger/wasp/v2/tools/cluster"
27+ "github.com/iotaledger/wasp/v2/tools/wasp-cli/format"
2628)
2729
2830func TestWaspAuth (t * testing.T ) {
@@ -137,10 +139,10 @@ func TestZeroGasFee(t *testing.T) {
137139 t .Run ("deposit directly to EVM" , func (t * testing.T ) {
138140 alternativeAddress := getAddressFromJSON (w .MustRun ("wallet" , "address" , "--address-index=1" , "--json" ))
139141 w .MustRun ("wallet" , "send-funds" , alternativeAddress , "base|1000000" )
140- outs := w .MustRun ("wallet" , "balance" , "--address-index=1" )
142+ w .MustRun ("wallet" , "balance" , "--address-index=1" )
141143 _ , eth := newEthereumAccount ()
142144 w .MustRun ("chain" , "deposit" , eth .String (), "base|1000000" , "--node=0" )
143- outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" )
145+ outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" , "--json" )
144146 checkL2Balance (t , outs , 1000000 )
145147 })
146148}
@@ -199,13 +201,27 @@ func checkL1BalanceJSON(t *testing.T, out []string, expected int) {
199201
200202func checkL2Balance (t * testing.T , out []string , expected int ) {
201203 t .Helper ()
202- r := regexp .MustCompile (`.*(?i:base)\s*(?i:tokens)?:*\s*(\d+).*` ).FindStringSubmatch (strings .Join (out , "" ))
203- if r == nil {
204- panic ("couldn't check balance" )
204+
205+ rawOutput := strings .Join (out , "\n " )
206+
207+ var balanceOutput format.ChainBalanceOutput
208+ err := json .Unmarshal ([]byte (rawOutput ), & balanceOutput )
209+ require .NoError (t , err , "Expected valid JSON output, got: %v" , rawOutput )
210+
211+ // Find the base token in the coins array
212+ var baseAmount int64
213+ found := false
214+ for _ , coin := range balanceOutput .Coins {
215+ if coin .Token == "base" {
216+ baseAmount , err = strconv .ParseInt (coin .Amount , 10 , 64 )
217+ require .NoError (t , err )
218+ found = true
219+ break
220+ }
205221 }
206- amount , err := strconv . Atoi ( r [ 1 ])
207- require .NoError (t , err )
208- require .EqualValues (t , expected , amount )
222+
223+ require .True (t , found , "base token not found in balance output" )
224+ require .EqualValues (t , expected , baseAmount , "Expected base token balance to be %d, got %d" , expected , baseAmount )
209225}
210226
211227// getAddressFromJSON extracts the address from JSON output
@@ -283,25 +299,25 @@ func TestWaspCLIDeposit(t *testing.T) {
283299 t .Run ("deposit directly to EVM" , func (t * testing.T ) {
284300 _ , eth := newEthereumAccount ()
285301 w .MustRun ("chain" , "deposit" , "base|1000000" , "--node=0" )
286- outs := w .MustRun ("chain" , "deposit" , eth .String (), "base|10000" , "--node=0" , "--print-receipt" )
287- outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" )
302+ w .MustRun ("chain" , "deposit" , eth .String (), "base|10000" , "--node=0" , "--print-receipt" )
303+ outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" , "--json" )
288304 checkL2Balance (t , outs , 10000 )
289305 })
290306
291307 t .Run ("deposit to own account, then to EVM" , func (t * testing.T ) {
292308 const depositAmount = int64 (1_000_000 )
293309 w .MustRun ("wallet" , "request-funds" , "--address-index=2" )
294- outs = w .MustRun ("chain" , "deposit" , "base|1000000" , "--address-index=2" , "--node=0" , "--print-receipt" )
310+ outs = w .MustRun ("chain" , "deposit" , "base|1000000" , "--address-index=2" , "--node=0" , "--print-receipt" , "--json" )
295311 l2GasFee := getL2GasFee (t , outs )
296- outs = w .MustRun ("chain" , "balance" , "--address-index=2" , "--node=0" )
312+ outs = w .MustRun ("chain" , "balance" , "--address-index=2" , "--node=0" , "--json" )
297313 checkL2Balance (t , outs , int (depositAmount - l2GasFee ))
298- outs := w .MustRun ("wallet" , "balance" , "--address-index=2" , "--json" )
314+ w .MustRun ("wallet" , "balance" , "--address-index=2" , "--json" )
299315 _ , eth := newEthereumAccount ()
300- outs = w .MustRun ("chain" , "deposit" , eth .String (), "base|1000000" , "--address-index=2" , "--node=0" , "--print-receipt" )
316+ outs = w .MustRun ("chain" , "deposit" , eth .String (), "base|1000000" , "--address-index=2" , "--node=0" , "--print-receipt" , "--json" )
301317 l2GasFee = getL2GasFee (t , outs )
302- outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" )
318+ outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" , "--json" )
303319 checkL2Balance (t , outs , 1000000 ) // receiver gets full amount
304- outs = w .MustRun ("chain" , "balance" , "--address-index=2" , "--node=0" )
320+ outs = w .MustRun ("chain" , "balance" , "--address-index=2" , "--node=0" , "--json" )
305321 expectedL2Balance := int (depositAmount - l2GasFee - int64 (minFee ))
306322 checkL2Balance (t , outs , expectedL2Balance )
307323 })
@@ -383,18 +399,28 @@ func TestWaspCLIDeposit(t *testing.T) {
383399}
384400
385401func getL2GasFee (t * testing.T , outs []string ) int64 {
386- var err error
387- var l2GasFee int64
388- re := regexp .MustCompile (`Gas fee charged:\s*(\d+)` )
389-
390- for _ , line := range outs {
391- matches := re .FindStringSubmatch (line )
392- if len (matches ) > 1 {
393- l2GasFee , err = strconv .ParseInt (matches [1 ], 10 , 64 )
394- require .NoError (t , err )
402+ t .Helper ()
403+
404+ rawOutput := strings .Join (outs , "\n " )
405+ decoder := json .NewDecoder (strings .NewReader (rawOutput ))
406+
407+ for {
408+ var receipt format.ChainReceiptOutput
409+ if err := decoder .Decode (& receipt ); err != nil {
410+ if errors .Is (err , io .EOF ) {
411+ break
412+ }
413+ require .FailNowf (t , "failed to decode CLI output" , "error: %v\n output:\n %s" , err , rawOutput )
414+ }
415+ if receipt .Type != "chain_receipt" {
416+ continue
395417 }
418+ l2GasFee , err := strconv .ParseInt (receipt .GasFeeCharged , 10 , 64 )
419+ require .NoError (t , err )
420+ return l2GasFee
396421 }
397- return l2GasFee
422+
423+ panic ("gas fee not found" )
398424}
399425
400426func findRequestIDInOutput (out []string ) string {
0 commit comments