@@ -342,15 +342,55 @@ 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+ var token TokenInfo
362+ token , err = TokenInfoStringToStruct (tokenKey )
363+ if err != nil {
364+ return err
365+ }
366+
367+ if token .OriginNetwork .Uint64 () == uint64 (l2NetworkID ) {
368+ continue
369+ }
370+
371+ balanceEntries = append (balanceEntries , BalanceEntry {
372+ OriginNetwork : uint32 (token .OriginNetwork .Uint64 ()),
373+ OriginTokenAddress : token .OriginTokenAddress ,
374+ TotalSupply : balance .String (),
375+ })
376+ }
377+
378+ // Create the response structure
379+ response := struct {
380+ Root string `json:"root"`
381+ Balances []BalanceEntry `json:"balances"`
382+ }{
383+ Root : root .String (),
384+ Balances : balanceEntries ,
385+ }
386+
387+ // Marshal to JSON with proper formatting
388+ jsonOutput , err := json .MarshalIndent (response , "" , " " )
389+ if err != nil {
390+ return err
391+ }
392+
393+ fmt .Println (string (jsonOutput ))
354394 return nil
355395}
356396
@@ -371,7 +411,7 @@ func nullifierTree(args []string) error {
371411 return nil
372412}
373413
374- func nullifierAndBalanceTree (args [] string ) error {
414+ func nullifierAndBalanceTree () error {
375415 l2NetworkID := balanceTreeOptions .L2NetworkID
376416 bridgeAddress := common .HexToAddress (balanceTreeOptions .BridgeAddress )
377417
@@ -401,7 +441,7 @@ func nullifierAndBalanceTree(args []string) error {
401441 return err
402442 }
403443 log .Info ().Msgf ("Last LER count: %d" , ler_count )
404- balanceTreeRoot , err := computeBalanceTree (client , bridgeAddress , l2RawClaimsData , l2NetworkID , l2RawDepositsData )
444+ balanceTreeRoot , _ , err := computeBalanceTree (client , bridgeAddress , l2RawClaimsData , l2NetworkID , l2RawDepositsData )
405445 if err != nil {
406446 return err
407447 }
@@ -432,7 +472,7 @@ func computeNullifierTree(rawClaims []byte) (common.Hash, error) {
432472 var root common.Hash
433473 for scanner .Scan () {
434474 claim := new (ulxly.UlxlyClaimEvent )
435- err : = json .Unmarshal (scanner .Bytes (), claim )
475+ err = json .Unmarshal (scanner .Bytes (), claim )
436476 if err != nil {
437477 return common.Hash {}, err
438478 }
@@ -456,29 +496,29 @@ func computeNullifierTree(rawClaims []byte) (common.Hash, error) {
456496 return root , nil
457497}
458498
459- func computeBalanceTree (client * ethclient.Client , bridgeAddress common.Address , l2RawClaims []byte , l2NetworkID uint32 , l2RawDeposits []byte ) (common.Hash , error ) {
499+ func computeBalanceTree (client * ethclient.Client , bridgeAddress common.Address , l2RawClaims []byte , l2NetworkID uint32 , l2RawDeposits []byte ) (common.Hash , map [ string ] * big. Int , error ) {
460500 buf := bytes .NewBuffer (l2RawClaims )
461501 scanner := bufio .NewScanner (buf )
462502 scannerBuf := make ([]byte , 0 )
463503 scanner .Buffer (scannerBuf , 1024 * 1024 )
464504 bTree , err := NewBalanceTree ()
465505 if err != nil {
466- return common.Hash {}, err
506+ return common.Hash {}, nil , err
467507 }
468508 balances := make (map [string ]* big.Int )
469509 for scanner .Scan () {
470510 l2Claim := new (ulxly.UlxlyClaimEvent )
471- err : = json .Unmarshal (scanner .Bytes (), l2Claim )
511+ err = json .Unmarshal (scanner .Bytes (), l2Claim )
472512 if err != nil {
473- return common.Hash {}, err
513+ return common.Hash {}, nil , err
474514 }
475515 token := TokenInfo {
476516 OriginNetwork : big .NewInt (0 ).SetUint64 (uint64 (l2Claim .OriginNetwork )),
477517 OriginTokenAddress : l2Claim .OriginAddress ,
478518 }
479519 isMessage , err := checkClaimCalldata (client , bridgeAddress , l2Claim .Raw .TxHash )
480520 if err != nil {
481- return common.Hash {}, err
521+ return common.Hash {}, nil , err
482522 }
483523 if isMessage {
484524 token .OriginNetwork = big .NewInt (0 )
@@ -499,7 +539,7 @@ func computeBalanceTree(client *ethclient.Client, bridgeAddress common.Address,
499539 l2Deposit := new (ulxly.UlxlyBridgeEvent )
500540 err := json .Unmarshal (l2Scanner .Bytes (), l2Deposit )
501541 if err != nil {
502- return common.Hash {}, err
542+ return common.Hash {}, nil , err
503543 }
504544 token := TokenInfo {
505545 OriginNetwork : big .NewInt (0 ).SetUint64 (uint64 (l2Deposit .OriginNetwork )),
@@ -518,20 +558,20 @@ func computeBalanceTree(client *ethclient.Client, bridgeAddress common.Address,
518558 }
519559 token , err := TokenInfoStringToStruct (t )
520560 if err != nil {
521- return common.Hash {}, err
561+ return common.Hash {}, nil , err
522562 }
523563 if token .OriginNetwork .Uint64 () == uint64 (l2NetworkID ) {
524564 continue
525565 }
526566 root , err = bTree .UpdateBalanceTree (token , balance )
527567 if err != nil {
528- return common.Hash {}, err
568+ return common.Hash {}, nil , err
529569 }
530570 log .Info ().Msgf ("New balanceTree leaf. OriginNetwork: %s, TokenAddress: %s, Balance: %s, Root: %s" , token .OriginNetwork .String (), token .OriginTokenAddress .String (), balance .String (), root .String ())
531571 }
532572 log .Info ().Msgf ("Final balanceTree root: %s" , root .String ())
533573
534- return root , nil
574+ return root , balances , nil
535575}
536576
537577func rollupsExitRootProof (args []string ) error {
@@ -2409,7 +2449,7 @@ or if it's actually an intermediate hash.`,
24092449 Short : "Compute the balance tree and the nullifier tree given the deposits and claims." ,
24102450 Long : nullifierAndBalanceTreeUsage ,
24112451 RunE : func (cmd * cobra.Command , args []string ) error {
2412- return nullifierAndBalanceTree (args )
2452+ return nullifierAndBalanceTree ()
24132453 },
24142454 SilenceUsage : true ,
24152455 }
0 commit comments