diff --git a/bfa_client/src/client/bfa_client.go b/bfa_client/src/client/bfa_client.go index dfa5bab2ebc46eca0e5890a36c2bfecbfa9f3dd1..cc1cfe3f91e32031bb59006147649fba2b86c07b 100644 --- a/bfa_client/src/client/bfa_client.go +++ b/bfa_client/src/client/bfa_client.go @@ -31,6 +31,31 @@ var ( flags = flag.NewFlagSet("", flag.ExitOnError) description string otherArgs string + wei = new(big.Float).SetFloat64(1) + kilowei = new(big.Float).SetFloat64(1e3) + megawei = new(big.Float).SetFloat64(1e6) + gigawei = new(big.Float).SetFloat64(1e9) + microether = new(big.Float).SetFloat64(1e12) + milliether = new(big.Float).SetFloat64(1e15) + ether = new(big.Float).SetFloat64(1e18) + + units = map[string]*big.Float{ + "wei": wei, + "kwei": kilowei, + "kilowei": kilowei, + "babbage": kilowei, + "mwei": megawei, + "megawei": megawei, + "lovelace": megawei, + "gwei": gigawei, + "gigawei": gigawei, + "shannon": gigawei, + "microether": microether, + "szabo": microether, + "milliether": milliether, + "finney": milliether, + "ether": ether, + } ) func setFlags() { @@ -144,6 +169,25 @@ func parseFormatString(s string) (format string) { return } +func printSealers(sealers []string) { + sort.Slice(sealers, func(i, j int) bool { return sealers[i] < sealers[j] }) + if json { + util.PrintJson(sealers) + } else { + for _, sealer := range sealers { + fmt.Println(sealer) + } + } +} + +func weiTo(wei *big.Float, unit string) (value *big.Float, ok bool) { + divisor, ok := units[unit] + if ok { + value = wei.Quo(wei, divisor) + } + return +} + func sealers() { var ( blockNumber int64 @@ -154,6 +198,7 @@ func sealers() { header bool format string formatStr string + unit string ) description = "Presenta la lista de selladores. Opcionalmente presenta información sobre los selladores." setFlags() @@ -165,28 +210,27 @@ func sealers() { flags.BoolVar(&header, "header", false, "Muestra un encabezado en cada columna.") flags.StringVar(&format, "format", "", "Formato del timestamp. Ignorado en formato json. Opciones: 'unix', 'rfc3339', 'long' ('YYYY-MM-DD hh:mm:ss'), 'short' ('hh:mm:ss') o un formato especÃfico. Ejemplo 'DD/MM/YY hh.mm.ss'. También se admite el formato del paquete 'time' de go.") flags.StringVar(&formatStr, "output-format", "", "Formato de la salida. Ignorado en formato json. Es una string en la que se reemplazan las ocurrencias de {sealer}, {last-block}, {timestamp}, {difficulty} y {balance} por sus respectivos valores.") + flags.StringVar(&unit, "unit", "wei", "Unidades en la que se expresa el balance. Posibles valores: wei, Kwei, kilowei, Mwei, megawei, Gwei, gigawei, microether, milliether, ether, babbage, lovelace, shannon, szabo, finney.") parseFlags(false) if blockNumber == 0 { - util.Error("El bloque génesis no tiene firmantes") + util.Error("El bloque génesis no tiene firmantes\n") + } + unit = strings.ToLower(unit) + if _, ok := units[unit]; !ok { + util.Error("Unidad '%v' desconocida\n", unit) } url = updateURL(url) node, err := bfa.Dial(url) util.Check(err) defer node.Close() blockNumber = node.BlockNumberInRange(blockNumber) - extended := lastBlock || timestamp || difficulty || balance - if !extended && formatStr == "" { - sealers := node.SealersAtBlock(blockNumber) - sort.Slice(sealers, func(i, j int) bool { return sealers[i] < sealers[j] }) - if json { - util.PrintJson(sealers) - } else { - for _, sealer := range sealers { - fmt.Println(sealer) - } - } + extended := lastBlock || timestamp || difficulty || balance || len(formatStr) > 0 + + if !extended { + printSealers(node.SealersAtBlock(blockNumber)) return } + sealers := node.SealersStatus(blockNumber) if json { util.PrintJson(sealers) @@ -241,7 +285,9 @@ func sealers() { formatStr += " %[7]*[8]v" balanceLen = 0 for _, s := range sealers { - balanceLen = util.Max(balanceLen, int64(len(s.Balance.String()))) + f := new(big.Float).SetInt(s.Balance) + b, _ := weiTo(f, unit) + balanceLen = util.Max(balanceLen, int64(len(fmt.Sprintf("%v", b)))) } } if header { @@ -256,13 +302,16 @@ func sealers() { formatStr += "\n" for _, sealer := range list { var formatedTimestamp interface{} + s := sealers[sealer] t := int64(s.Time) formatedTimestamp = t - if timestamp && len(format) > 0 && t > 0 { + if len(format) > 0 && t > 0 { formatedTimestamp = time.Unix(t, 0).Format(format) } - fmt.Printf(formatStr, sealer, lastBlockLen, s.LastBlock, timestampLen, formatedTimestamp, s.Difficulty, balanceLen, s.Balance) + b := new(big.Float).SetInt(s.Balance) + convertedBalance, _ := weiTo(b, unit) + fmt.Printf(formatStr, sealer, lastBlockLen, s.LastBlock, timestampLen, formatedTimestamp, s.Difficulty, balanceLen, convertedBalance) } }