Print basefee in miner info

This commit is contained in:
Łukasz Magiera 2020-12-01 18:56:43 +01:00
parent 82b5cb89cd
commit 00e5ace818
3 changed files with 96 additions and 1 deletions

View File

@ -23,6 +23,28 @@ func (f FIL) Unitless() string {
return strings.TrimRight(strings.TrimRight(r.FloatString(18), "0"), ".")
}
var unitPrefixes = []string{"a", "f", "p", "n", "μ", "m", ""}
func (f FIL) Short() string {
n := BigInt(f)
dn := uint64(1)
var prefix string
for _, prefix = range unitPrefixes {
if n.LessThan(NewInt(dn * 1000)) {
break
}
dn = dn * 1000
}
r := new(big.Rat).SetFrac(f.Int, big.NewInt(int64(dn)))
if r.Sign() == 0 {
return "0"
}
return strings.TrimRight(strings.TrimRight(r.FloatString(3), "0"), ".") + " " + prefix + "FIL"
}
func (f FIL) Format(s fmt.State, ch rune) {
switch ch {
case 's', 'v':

58
chain/types/fil_test.go Normal file
View File

@ -0,0 +1,58 @@
package types
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestFilShort(t *testing.T) {
for _, s := range []struct {
fil string
expect string
}{
{fil: "1", expect: "1 FIL"},
{fil: "1.1", expect: "1.1 FIL"},
{fil: "0.1", expect: "100 mFIL"},
{fil: "0.01", expect: "10 mFIL"},
{fil: "0.001", expect: "1 mFIL"},
{fil: "0.0001", expect: "100 μFIL"},
{fil: "0.00001", expect: "10 μFIL"},
{fil: "0.000001", expect: "1 μFIL"},
{fil: "0.0000001", expect: "100 nFIL"},
{fil: "0.00000001", expect: "10 nFIL"},
{fil: "0.000000001", expect: "1 nFIL"},
{fil: "0.0000000001", expect: "100 pFIL"},
{fil: "0.00000000001", expect: "10 pFIL"},
{fil: "0.000000000001", expect: "1 pFIL"},
{fil: "0.0000000000001", expect: "100 fFIL"},
{fil: "0.00000000000001", expect: "10 fFIL"},
{fil: "0.000000000000001", expect: "1 fFIL"},
{fil: "0.0000000000000001", expect: "100 aFIL"},
{fil: "0.00000000000000001", expect: "10 aFIL"},
{fil: "0.000000000000000001", expect: "1 aFIL"},
{fil: "0.0000012", expect: "1.2 μFIL"},
{fil: "0.00000123", expect: "1.23 μFIL"},
{fil: "0.000001234", expect: "1.234 μFIL"},
{fil: "0.0000012344", expect: "1.234 μFIL"},
{fil: "0.00000123444", expect: "1.234 μFIL"},
{fil: "0.0002212", expect: "221.2 μFIL"},
{fil: "0.00022123", expect: "221.23 μFIL"},
{fil: "0.000221234", expect: "221.234 μFIL"},
{fil: "0.0002212344", expect: "221.234 μFIL"},
{fil: "0.00022123444", expect: "221.234 μFIL"},
} {
t.Run(s.fil, func(t *testing.T) {
f, err := ParseFIL(s.fil)
require.NoError(t, err)
require.Equal(t, s.expect, f.Short())
})
}
}

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"github.com/filecoin-project/go-state-types/big"
"sort"
"time"
@ -59,7 +60,7 @@ func infoCmdAct(cctx *cli.Context) error {
ctx := lcli.ReqContext(cctx)
fmt.Print("Full node: ")
fmt.Print("Chain: ")
head, err := api.ChainHead(ctx)
if err != nil {
@ -75,6 +76,20 @@ func infoCmdAct(cctx *cli.Context) error {
fmt.Printf("[%s]", color.RedString("sync behind! (%s behind)", time.Now().Sub(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second)))
}
basefee := head.MinTicketBlock().ParentBaseFee
gasCol := []color.Attribute{color.FgBlue}
switch {
case basefee.GreaterThan(big.NewInt(7000_000_000)): // 7 nFIL
gasCol = []color.Attribute{color.BgRed, color.FgBlack}
case basefee.GreaterThan(big.NewInt(3000_000_000)): // 3 nFIL
gasCol = []color.Attribute{color.FgRed}
case basefee.GreaterThan(big.NewInt(750_000_000)): // 750 uFIL
gasCol = []color.Attribute{color.FgYellow}
case basefee.GreaterThan(big.NewInt(100_000_000)): // 100 uFIL
gasCol = []color.Attribute{color.FgGreen}
}
fmt.Printf("[basefee %s]", color.New(gasCol...).Sprint(types.FIL(basefee).Short()))
fmt.Println()
maddr, err := getActorAddress(ctx, nodeApi, cctx.String("actor"))