add nicer printing and parsing of filecoin values
This commit is contained in:
parent
b877d41c09
commit
03d847b8a6
30
chain/types/fil.go
Normal file
30
chain/types/fil.go
Normal file
@ -0,0 +1,30 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/filecoin-project/lotus/build"
|
||||
)
|
||||
|
||||
type FIL BigInt
|
||||
|
||||
func (f FIL) String() string {
|
||||
r := big.NewRat(1, 1).SetFrac(f.Int, big.NewInt(build.FilecoinPrecision))
|
||||
return strings.TrimRight(r.FloatString(30), "0")
|
||||
}
|
||||
|
||||
func ParseFIL(s string) (FIL, error) {
|
||||
r, ok := big.NewRat(1, 1).SetString(s)
|
||||
if !ok {
|
||||
return FIL{}, fmt.Errorf("failed to parse %q as a decimal number", s)
|
||||
}
|
||||
|
||||
r = r.Mul(r, big.NewRat(build.FilecoinPrecision, 1))
|
||||
if !r.IsInt() {
|
||||
return FIL{}, fmt.Errorf("invalid FIL value: %q", s)
|
||||
}
|
||||
|
||||
return FIL{r.Num()}, nil
|
||||
}
|
@ -36,7 +36,7 @@ var sendCmd = &cli.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
val, err := types.BigFromString(cctx.Args().Get(1))
|
||||
val, err := types.ParseFIL(cctx.Args().Get(1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -61,7 +61,7 @@ var sendCmd = &cli.Command{
|
||||
msg := &types.Message{
|
||||
From: fromAddr,
|
||||
To: toAddr,
|
||||
Value: val,
|
||||
Value: types.BigInt(val),
|
||||
GasLimit: types.NewInt(1000),
|
||||
GasPrice: types.NewInt(0),
|
||||
}
|
||||
|
@ -197,7 +197,7 @@ var statePledgeCollateralCmd = &cli.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(coll.String())
|
||||
fmt.Println(types.FIL(coll).String())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ var walletBalance = &cli.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("%s\n", balance.String())
|
||||
fmt.Printf("%s\n", types.FIL(balance).String())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user