This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
/
balance.go
172 lines (140 loc) · 4 KB
/
balance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package nano
import (
"encoding/binary"
"errors"
"math/big"
"github.com/alexbakker/gonano/nano/internal/uint128"
"github.com/alexbakker/gonano/nano/internal/util"
"github.com/shopspring/decimal"
)
const (
// BalanceSize represents the size of a balance in bytes.
BalanceSize = 16
BalanceMaxPrecision = 33
)
type BalanceComp byte
const (
BalanceCompEqual BalanceComp = iota
BalanceCompBigger
BalanceCompSmaller
)
var (
units = map[string]decimal.Decimal{
"raw": decimal.New(1, 0),
"uxrb": decimal.New(1, 18),
"mxrb": decimal.New(1, 21),
"xrb": decimal.New(1, 24),
"kxrb": decimal.New(1, 27),
"Mxrb": decimal.New(1, 30),
"Gxrb": decimal.New(1, 33),
}
ZeroBalance = Balance(uint128.Uint128{})
ErrBadBalanceSize = errors.New("balances should be 16 bytes in size")
)
type Balance uint128.Uint128
// ParseBalance parses the given balance string.
func ParseBalance(s string, unit string) (Balance, error) {
d, err := decimal.NewFromString(s)
if err != nil {
return ZeroBalance, err
}
// zero is a special case
if d.Equals(decimal.Zero) {
return ZeroBalance, nil
}
d = d.Mul(units[unit])
c := d.Coefficient()
f := bigPow(10, int64(d.Exponent()))
i := c.Mul(c, f)
bytes := i.Bytes()
balanceBytes := make([]byte, BalanceSize)
copy(balanceBytes[len(balanceBytes)-len(bytes):], bytes)
var balance Balance
if err := balance.UnmarshalBinary(balanceBytes); err != nil {
return ZeroBalance, err
}
return balance, nil
}
func ParseBalanceInts(hi uint64, lo uint64) Balance {
return Balance(uint128.FromInts(hi, lo))
}
// Bytes returns the binary representation of this Balance with the given
// endianness.
func (b Balance) Bytes(order binary.ByteOrder) []byte {
bytes := uint128.Uint128(b).GetBytes()
switch order {
case binary.BigEndian:
return bytes
case binary.LittleEndian:
return util.ReverseBytes(bytes)
default:
panic("unsupported byte order")
}
}
// Equal reports whether this balance and the given balance are equal.
func (b Balance) Equal(b2 Balance) bool {
return uint128.Uint128(b).Equal(uint128.Uint128(b2))
}
func (b Balance) Add(n Balance) Balance {
return Balance(uint128.Uint128(b).Add(uint128.Uint128(n)))
}
func (b Balance) Sub(n Balance) Balance {
return Balance(uint128.Uint128(b).Sub(uint128.Uint128(n)))
}
func (b Balance) Compare(n Balance) BalanceComp {
res := uint128.Uint128(b).Compare(uint128.Uint128(n))
switch res {
case 1:
return BalanceCompBigger
case -1:
return BalanceCompSmaller
case 0:
return BalanceCompEqual
default:
panic("unexpected comparison result")
}
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (b Balance) MarshalBinary() ([]byte, error) {
return b.Bytes(binary.LittleEndian), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (b *Balance) UnmarshalBinary(data []byte) error {
if len(data) != BalanceSize {
return ErrBadBalanceSize
}
*b = Balance(uint128.FromBytes(data))
return nil
}
func (b Balance) BigInt() *big.Int {
i := big.NewInt(0)
i.SetBytes(b.Bytes(binary.BigEndian))
return i
}
// UnitString returns a decimal representation of this uint128 converted to the
// given unit.
func (b Balance) UnitString(unit string, precision int32) string {
d := decimal.NewFromBigInt(b.BigInt(), 0)
return d.DivRound(units[unit], BalanceMaxPrecision).Truncate(precision).String()
}
// String implements the fmt.Stringer interface. It returns the balance in Mxrb
// with maximum precision.
func (b Balance) String() string {
return b.UnitString("Mxrb", BalanceMaxPrecision)
}
func bigPow(base int64, exp int64) *big.Int {
return new(big.Int).Exp(big.NewInt(10), big.NewInt(exp), nil)
}
// MarshalText implements the encoding.TextMarshaler interface.
func (b Balance) MarshalText() ([]byte, error) {
return []byte(b.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (b *Balance) UnmarshalText(text []byte) error {
balance, err := ParseBalance(string(text), "Mxrb")
if err != nil {
return err
}
*b = balance
return nil
}