@@ -4,30 +4,29 @@ import (
44 "context"
55 "fmt"
66 "os"
7- "os/exec"
87 "strings"
98 "time"
109
11- "github.com/pushchain/push-validator-cli/internal/config"
1210 "github.com/pushchain/push-validator-cli/internal/dashboard"
13- "github.com/pushchain/push-validator-cli/internal/validator"
1411)
1512
1613// handleBalance prints an account balance. It resolves the address from
1714// either a positional argument or KEY_NAME when --address/arg is omitted.
1815// When --output=json is set, it emits a structured object.
19- func handleBalance (cfg config. Config , args []string ) error {
16+ func handleBalance (d * Deps , args []string ) error {
2017 var addr string
2118 if len (args ) > 0 { addr = args [0 ] }
2219 if addr == "" {
2320 key := os .Getenv ("KEY_NAME" )
2421 if key == "" {
25- if flagOutput == "json" { getPrinter () .JSON (map [string ]any {"ok" : false , "error" : "address not provided; set KEY_NAME or pass --address" }) } else { fmt .Println ("usage: push-validator balance <address> (or set KEY_NAME)" ) }
22+ if flagOutput == "json" { d . Printer .JSON (map [string ]any {"ok" : false , "error" : "address not provided; set KEY_NAME or pass --address" }) } else { fmt .Println ("usage: push-validator balance <address> (or set KEY_NAME)" ) }
2623 return fmt .Errorf ("address not provided" )
2724 }
28- out , err := exec .Command (findPchaind (), "keys" , "show" , key , "-a" , "--keyring-backend" , cfg .KeyringBackend , "--home" , cfg .HomeDir ).Output ()
25+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
26+ out , err := d .Runner .Run (ctx , findPchaind (), "keys" , "show" , key , "-a" , "--keyring-backend" , d .Cfg .KeyringBackend , "--home" , d .Cfg .HomeDir )
27+ cancel ()
2928 if err != nil {
30- if flagOutput == "json" { getPrinter () .JSON (map [string ]any {"ok" : false , "error" : err .Error ()}) } else { fmt .Printf ("resolve address error: %v\n " , err ) }
29+ if flagOutput == "json" { d . Printer .JSON (map [string ]any {"ok" : false , "error" : err .Error ()}) } else { fmt .Printf ("resolve address error: %v\n " , err ) }
3130 return fmt .Errorf ("resolve address: %w" , err )
3231 }
3332 addr = strings .TrimSpace (string (out ))
@@ -36,23 +35,22 @@ func handleBalance(cfg config.Config, args []string) error {
3635 // Convert hex address (0x...) to bech32 if needed
3736 if strings .HasPrefix (addr , "0x" ) || strings .HasPrefix (addr , "0X" ) {
3837 convCtx , convCancel := context .WithTimeout (context .Background (), 10 * time .Second )
39- bech32Addr , convErr := hexToBech32Address (convCtx , addr )
38+ bech32Addr , convErr := hexToBech32Address (convCtx , addr , d . Runner )
4039 convCancel ()
4140 if convErr != nil {
42- if flagOutput == "json" { getPrinter (). JSON (map [string ]any {"ok" : false , "error" : convErr .Error (), "address" : addr }) } else { getPrinter () .Error (fmt .Sprintf ("address conversion error: %v" , convErr )) }
41+ if flagOutput == "json" { d . Printer . JSON (map [string ]any {"ok" : false , "error" : convErr .Error (), "address" : addr }) } else { d . Printer .Error (fmt .Sprintf ("address conversion error: %v" , convErr )) }
4342 return convErr
4443 }
4544 addr = bech32Addr
4645 }
4746
48- v := validator .NewWith (validator.Options {BinPath : findPchaind (), HomeDir : cfg .HomeDir , ChainID : cfg .ChainID , Keyring : cfg .KeyringBackend , GenesisDomain : cfg .GenesisDomain , Denom : cfg .Denom })
4947 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
5048 defer cancel ()
51- bal , err := v .Balance (ctx , addr )
49+ bal , err := d . Validator .Balance (ctx , addr )
5250 if err != nil {
53- if flagOutput == "json" { getPrinter (). JSON (map [string ]any {"ok" : false , "error" : err .Error (), "address" : addr }) } else { getPrinter () .Error (fmt .Sprintf ("balance error: %v" , err )) }
51+ if flagOutput == "json" { d . Printer . JSON (map [string ]any {"ok" : false , "error" : err .Error (), "address" : addr }) } else { d . Printer .Error (fmt .Sprintf ("balance error: %v" , err )) }
5452 return err
5553 }
56- if flagOutput == "json" { getPrinter (). JSON (map [string ]any {"ok" : true , "address" : addr , "balance" : bal , "denom" : cfg . Denom }) } else { getPrinter (). Info (fmt .Sprintf ("%s %s" , dashboard .FormatSmartNumber (bal ), cfg .Denom )) }
54+ if flagOutput == "json" { d . Printer . JSON (map [string ]any {"ok" : true , "address" : addr , "balance" : bal , "denom" : d . Cfg . Denom }) } else { d . Printer . Info (fmt .Sprintf ("%s %s" , dashboard .FormatSmartNumber (bal ), d . Cfg .Denom )) }
5755 return nil
5856}
0 commit comments