2019-10-18 12:31:45 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2020-08-12 17:58:39 +00:00
|
|
|
"encoding"
|
2019-10-18 12:31:45 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FIL BigInt
|
|
|
|
|
|
|
|
func (f FIL) String() string {
|
2020-10-07 23:00:52 +00:00
|
|
|
return f.Unitless() + " FIL"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FIL) Unitless() string {
|
2020-06-30 13:18:01 +00:00
|
|
|
r := new(big.Rat).SetFrac(f.Int, big.NewInt(int64(build.FilecoinPrecision)))
|
2019-11-05 18:50:27 +00:00
|
|
|
if r.Sign() == 0 {
|
2020-10-08 00:01:01 +00:00
|
|
|
return "0"
|
2019-11-05 18:50:27 +00:00
|
|
|
}
|
2020-10-07 23:00:52 +00:00
|
|
|
return strings.TrimRight(strings.TrimRight(r.FloatString(18), "0"), ".")
|
2019-10-18 12:31:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 11:33:54 +00:00
|
|
|
var AttoFil = NewInt(1)
|
|
|
|
var FemtoFil = BigMul(AttoFil, NewInt(1000))
|
|
|
|
var PicoFil = BigMul(FemtoFil, NewInt(1000))
|
|
|
|
var NanoFil = BigMul(PicoFil, NewInt(1000))
|
|
|
|
|
2020-12-01 18:15:39 +00:00
|
|
|
var unitPrefixes = []string{"a", "f", "p", "n", "μ", "m"}
|
2020-12-01 17:56:43 +00:00
|
|
|
|
|
|
|
func (f FIL) Short() string {
|
2021-01-09 09:48:36 +00:00
|
|
|
n := BigInt(f).Abs()
|
2020-12-01 17:56:43 +00:00
|
|
|
|
|
|
|
dn := uint64(1)
|
|
|
|
var prefix string
|
2020-12-01 18:15:39 +00:00
|
|
|
for _, p := range unitPrefixes {
|
2020-12-01 17:56:43 +00:00
|
|
|
if n.LessThan(NewInt(dn * 1000)) {
|
2020-12-01 18:15:39 +00:00
|
|
|
prefix = p
|
2020-12-01 17:56:43 +00:00
|
|
|
break
|
|
|
|
}
|
2020-12-01 18:15:39 +00:00
|
|
|
dn *= 1000
|
2020-12-01 17:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:50:49 +00:00
|
|
|
func (f FIL) Nano() string {
|
|
|
|
r := new(big.Rat).SetFrac(f.Int, big.NewInt(int64(1e9)))
|
|
|
|
if r.Sign() == 0 {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimRight(strings.TrimRight(r.FloatString(9), "0"), ".") + " nFIL"
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:52:24 +00:00
|
|
|
func (f FIL) Format(s fmt.State, ch rune) {
|
|
|
|
switch ch {
|
|
|
|
case 's', 'v':
|
|
|
|
fmt.Fprint(s, f.String())
|
|
|
|
default:
|
|
|
|
f.Int.Format(s, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:58:39 +00:00
|
|
|
func (f FIL) MarshalText() (text []byte, err error) {
|
|
|
|
return []byte(f.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FIL) UnmarshalText(text []byte) error {
|
|
|
|
p, err := ParseFIL(string(text))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Int.Set(p.Int)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:31:45 +00:00
|
|
|
func ParseFIL(s string) (FIL, error) {
|
2021-01-09 09:48:36 +00:00
|
|
|
suffix := strings.TrimLeft(s, "-.1234567890")
|
2020-07-10 00:26:06 +00:00
|
|
|
s = s[:len(s)-len(suffix)]
|
2020-07-08 23:16:45 +00:00
|
|
|
var attofil bool
|
|
|
|
if suffix != "" {
|
|
|
|
norm := strings.ToLower(strings.TrimSpace(suffix))
|
|
|
|
switch norm {
|
|
|
|
case "", "fil":
|
|
|
|
case "attofil", "afil":
|
|
|
|
attofil = true
|
|
|
|
default:
|
|
|
|
return FIL{}, fmt.Errorf("unrecognized suffix: %q", suffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 01:12:32 +00:00
|
|
|
if len(s) > 50 {
|
|
|
|
return FIL{}, fmt.Errorf("string length too large: %d", len(s))
|
|
|
|
}
|
|
|
|
|
2022-11-25 21:19:20 +00:00
|
|
|
r, ok := new(big.Rat).SetString(s) //nolint:gosec
|
2019-10-18 12:31:45 +00:00
|
|
|
if !ok {
|
|
|
|
return FIL{}, fmt.Errorf("failed to parse %q as a decimal number", s)
|
|
|
|
}
|
|
|
|
|
2020-07-08 23:16:45 +00:00
|
|
|
if !attofil {
|
|
|
|
r = r.Mul(r, big.NewRat(int64(build.FilecoinPrecision), 1))
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:31:45 +00:00
|
|
|
if !r.IsInt() {
|
2020-07-08 23:16:45 +00:00
|
|
|
var pref string
|
|
|
|
if attofil {
|
|
|
|
pref = "atto"
|
|
|
|
}
|
|
|
|
return FIL{}, fmt.Errorf("invalid %sFIL value: %q", pref, s)
|
2019-10-18 12:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FIL{r.Num()}, nil
|
|
|
|
}
|
2020-08-12 17:58:39 +00:00
|
|
|
|
2020-10-15 00:46:47 +00:00
|
|
|
func MustParseFIL(s string) FIL {
|
|
|
|
n, err := ParseFIL(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:58:39 +00:00
|
|
|
var _ encoding.TextMarshaler = (*FIL)(nil)
|
|
|
|
var _ encoding.TextUnmarshaler = (*FIL)(nil)
|