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,37 +299,27 @@ 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 ) {
292- t . Fatal ( "gas on L2 has problem" )
293- w .MustRun ("chain " , "deposit " , "base|1000000" , "--node=0 " )
294- outs = w .MustRun ("chain" , "balance " , "-- node=0" )
295- // The default wallet (address-index=0) is the chain admin and payout address.
296- // Gas fees are charged from the sender then paid to the payout account.
297- // Since sender == payout for admin, net effect is zero; balance remains 1,000,000.
298- checkL2Balance ( t , outs , 1000000 )
308+ const depositAmount = int64 ( 1_000_000 )
309+ w .MustRun ("wallet " , "request-funds " , "--address-index=2 " )
310+ outs = w .MustRun ("chain" , "deposit " , "base|1000000" , "--address-index=2" , "-- node=0" , "--print-receipt" , "--json " )
311+ l2GasFee := getL2GasFee ( t , outs )
312+ outs = w . MustRun ( "chain" , "balance" , "--address-index=2" , "--node=0" , "--json" )
313+ checkL2Balance ( t , outs , int ( depositAmount - l2GasFee ))
314+ w . MustRun ( "wallet" , "balance" , "--address-index=2" , "--json" )
299315 _ , eth := newEthereumAccount ()
300- outs = w .MustRun ("chain" , "deposit" , eth .String (), "base|1000000" , "--node=0" , "--print-receipt" )
301-
302- re := regexp .MustCompile (`Gas fee charged:\s*(\d+)` )
303- var l2GasFee int64
304- for _ , line := range outs {
305- matches := re .FindStringSubmatch (line )
306- if len (matches ) > 1 {
307- l2GasFee , err = strconv .ParseInt (matches [1 ], 10 , 64 )
308- require .NoError (t , err )
309- }
310- }
311- outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" )
316+ outs = w .MustRun ("chain" , "deposit" , eth .String (), "base|1000000" , "--address-index=2" , "--node=0" , "--print-receipt" , "--json" )
317+ l2GasFee = getL2GasFee (t , outs )
318+ outs = w .MustRun ("chain" , "balance" , eth .String (), "--node=0" , "--json" )
312319 checkL2Balance (t , outs , 1000000 ) // receiver gets full amount
313- outs = w .MustRun ("chain" , "balance" , "--node=0" )
314- fmt .Println ("l2GasFee: " , l2GasFee )
315- fmt .Println ("minFee: " , minFee )
316- checkL2Balance (t , outs , 1000000 )
320+ outs = w .MustRun ("chain" , "balance" , "--address-index=2" , "--node=0" , "--json" )
321+ expectedL2Balance := int (depositAmount - l2GasFee - int64 (minFee ))
322+ checkL2Balance (t , outs , expectedL2Balance )
317323 })
318324
319325 // t.Run("mint and deposit native tokens to an ethereum account", func(t *testing.T) {
@@ -392,6 +398,31 @@ func TestWaspCLIDeposit(t *testing.T) {
392398 // })
393399}
394400
401+ func getL2GasFee (t * testing.T , outs []string ) int64 {
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
417+ }
418+ l2GasFee , err := strconv .ParseInt (receipt .GasFeeCharged , 10 , 64 )
419+ require .NoError (t , err )
420+ return l2GasFee
421+ }
422+
423+ panic ("gas fee not found" )
424+ }
425+
395426func findRequestIDInOutput (out []string ) string {
396427 for _ , line := range out {
397428 m := regexp .MustCompile (`Request ID:\s*(0x[a-f0-9]+)` ).FindStringSubmatch (line )
0 commit comments