@@ -342,15 +342,54 @@ func balanceTree() error {
342342 if err != nil {
343343 return err
344344 }
345- root , err := computeBalanceTree (client , bridgeAddress , l2RawClaimsData , l2NetworkID , l2RawDepositsData )
345+ root , balances , err := computeBalanceTree (client , bridgeAddress , l2RawClaimsData , l2NetworkID , l2RawDepositsData )
346346 if err != nil {
347347 return err
348348 }
349- fmt .Printf (`
350- {
351- "root": "%s"
352- }
353- ` , root .String ())
349+ type BalanceEntry struct {
350+ OriginNetwork uint32 `json:"originNetwork"`
351+ OriginTokenAddress common.Address `json:"originTokenAddress"`
352+ TotalSupply string `json:"totalSupply"`
353+ }
354+
355+ var balanceEntries []BalanceEntry
356+ for tokenKey , balance := range balances {
357+ if balance .Cmp (big .NewInt (0 )) == 0 {
358+ continue
359+ }
360+
361+ token , err := TokenInfoStringToStruct (tokenKey )
362+ if err != nil {
363+ return err
364+ }
365+
366+ if token .OriginNetwork .Uint64 () == uint64 (l2NetworkID ) {
367+ continue
368+ }
369+
370+ balanceEntries = append (balanceEntries , BalanceEntry {
371+ OriginNetwork : uint32 (token .OriginNetwork .Uint64 ()),
372+ OriginTokenAddress : token .OriginTokenAddress ,
373+ TotalSupply : balance .String (),
374+ })
375+ }
376+
377+ // Create the response structure
378+ response := struct {
379+ Root string `json:"root"`
380+ Balances []BalanceEntry `json:"balances"`
381+ }{
382+ Root : root .String (),
383+ Balances : balanceEntries ,
384+ }
385+
386+ // Marshal to JSON with proper formatting
387+ jsonOutput , err := json .MarshalIndent (response , "" , " " )
388+ if err != nil {
389+ return err
390+ }
391+
392+ fmt .Println (string (jsonOutput ))
354393 return nil
355394}
356395
@@ -401,7 +440,7 @@ func nullifierAndBalanceTree(args []string) error {
401440 return err
402441 }
403442 log .Info ().Msgf ("Last LER count: %d" , ler_count )
404- balanceTreeRoot , err := computeBalanceTree (client , bridgeAddress , l2RawClaimsData , l2NetworkID , l2RawDepositsData )
443+ balanceTreeRoot , _ , err := computeBalanceTree (client , bridgeAddress , l2RawClaimsData , l2NetworkID , l2RawDepositsData )
405444 if err != nil {
406445 return err
407446 }
@@ -456,29 +495,29 @@ func computeNullifierTree(rawClaims []byte) (common.Hash, error) {
456495 return root , nil
457496}
458497
459- func computeBalanceTree (client * ethclient.Client , bridgeAddress common.Address , l2RawClaims []byte , l2NetworkID uint32 , l2RawDeposits []byte ) (common.Hash , error ) {
498+ func computeBalanceTree (client * ethclient.Client , bridgeAddress common.Address , l2RawClaims []byte , l2NetworkID uint32 , l2RawDeposits []byte ) (common.Hash , map [ string ] * big. Int , error ) {
460499 buf := bytes .NewBuffer (l2RawClaims )
461500 scanner := bufio .NewScanner (buf )
462501 scannerBuf := make ([]byte , 0 )
463502 scanner .Buffer (scannerBuf , 1024 * 1024 )
464503 bTree , err := NewBalanceTree ()
465504 if err != nil {
466- return common.Hash {}, err
505+ return common.Hash {}, nil , err
467506 }
468507 balances := make (map [string ]* big.Int )
469508 for scanner .Scan () {
470509 l2Claim := new (ulxly.UlxlyClaimEvent )
471510 err := json .Unmarshal (scanner .Bytes (), l2Claim )
472511 if err != nil {
473- return common.Hash {}, err
512+ return common.Hash {}, nil , err
474513 }
475514 token := TokenInfo {
476515 OriginNetwork : big .NewInt (0 ).SetUint64 (uint64 (l2Claim .OriginNetwork )),
477516 OriginTokenAddress : l2Claim .OriginAddress ,
478517 }
479518 isMessage , err := checkClaimCalldata (client , bridgeAddress , l2Claim .Raw .TxHash )
480519 if err != nil {
481- return common.Hash {}, err
520+ return common.Hash {}, nil , err
482521 }
483522 if isMessage {
484523 token .OriginNetwork = big .NewInt (0 )
@@ -499,7 +538,7 @@ func computeBalanceTree(client *ethclient.Client, bridgeAddress common.Address,
499538 l2Deposit := new (ulxly.UlxlyBridgeEvent )
500539 err := json .Unmarshal (l2Scanner .Bytes (), l2Deposit )
501540 if err != nil {
502- return common.Hash {}, err
541+ return common.Hash {}, nil , err
503542 }
504543 token := TokenInfo {
505544 OriginNetwork : big .NewInt (0 ).SetUint64 (uint64 (l2Deposit .OriginNetwork )),
@@ -518,20 +557,20 @@ func computeBalanceTree(client *ethclient.Client, bridgeAddress common.Address,
518557 }
519558 token , err := TokenInfoStringToStruct (t )
520559 if err != nil {
521- return common.Hash {}, err
560+ return common.Hash {}, nil , err
522561 }
523562 if token .OriginNetwork .Uint64 () == uint64 (l2NetworkID ) {
524563 continue
525564 }
526565 root , err = bTree .UpdateBalanceTree (token , balance )
527566 if err != nil {
528- return common.Hash {}, err
567+ return common.Hash {}, nil , err
529568 }
530569 log .Info ().Msgf ("New balanceTree leaf. OriginNetwork: %s, TokenAddress: %s, Balance: %s, Root: %s" , token .OriginNetwork .String (), token .OriginTokenAddress .String (), balance .String (), root .String ())
531570 }
532571 log .Info ().Msgf ("Final balanceTree root: %s" , root .String ())
533572
534- return root , nil
573+ return root , balances , nil
535574}
536575
537576func rollupsExitRootProof (args []string ) error {
0 commit comments