-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamount.go
More file actions
261 lines (218 loc) · 6.46 KB
/
amount.go
File metadata and controls
261 lines (218 loc) · 6.46 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package offchain
import (
"encoding/json"
"fmt"
"math"
"math/big"
"strconv"
"strings"
"github.com/shopspring/decimal"
"gopkg.in/yaml.v3"
)
const FLOAT_PRECISION = 6
// Balance is a big integer amount as blockchain expects it for tx.
type Balance big.Int
// Amount is a decimal amount as a human expects it for readability.
type Amount decimal.Decimal
func (amount Balance) Bytes() []byte {
bigInt := big.Int(amount)
return bigInt.Bytes()
}
func (amount Balance) String() string {
bigInt := big.Int(amount)
return bigInt.String()
}
// Int converts an AmountBlockchain into *bit.Int
func (amount Balance) Int() *big.Int {
bigInt := big.Int(amount)
return &bigInt
}
func (amount Balance) Sign() int {
bigInt := big.Int(amount)
return bigInt.Sign()
}
// Uint64 converts an AmountBlockchain into uint64
func (amount Balance) Uint64() uint64 {
bigInt := big.Int(amount)
return bigInt.Uint64()
}
// UnmaskFloat64 converts an AmountBlockchain into float64 given the number of decimals
func (amount Balance) UnmaskFloat64() float64 {
bigInt := big.Int(amount)
bigFloat := new(big.Float).SetInt(&bigInt)
exponent := new(big.Float).SetFloat64(math.Pow10(FLOAT_PRECISION))
bigFloat = bigFloat.Quo(bigFloat, exponent)
f64, _ := bigFloat.Float64()
return f64
}
// Use the underlying big.Int.Cmp()
func (amount *Balance) Cmp(other *Balance) int {
return amount.Int().Cmp(other.Int())
}
// Use the underlying big.Int.Add()
func (amount *Balance) Add(x *Balance) Balance {
sum := new(big.Int)
sum.Set((*big.Int)(amount))
return Balance(*sum.Add(sum, x.Int()))
}
// Use the underlying big.Int.Sub()
func (amount *Balance) Sub(x *Balance) Balance {
diff := new(big.Int)
diff.Set((*big.Int)(amount))
return Balance(*diff.Sub(diff, x.Int()))
}
// Use the underlying big.Int.Mul()
func (amount *Balance) Mul(x *Balance) Balance {
prod := new(big.Int)
prod.Set((*big.Int)(amount))
return Balance(*prod.Mul(prod, x.Int()))
}
// Use the underlying big.Int.Div()
func (amount *Balance) Div(x *Balance) Balance {
quot := new(big.Int)
quot.Set((*big.Int)(amount))
return Balance(*quot.Div(quot, x.Int()))
}
func (amount *Balance) Abs() Balance {
abs := new(big.Int)
abs.Set((*big.Int)(amount))
return Balance(*abs.Abs(abs))
}
var zero = big.NewInt(0)
func (amount *Balance) IsZero() bool {
return amount.Int().Cmp(zero) == 0
}
func (amount *Balance) ToHuman(decimals int32) Amount {
dec := decimal.NewFromBigInt(amount.Int(), -decimals)
return Amount(dec)
}
func MultiplyByFloat(amount Balance, multiplier float64) Balance {
if amount.Uint64() == 0 {
return amount
}
// We are computing (100000 * multiplier * amount) / 100000
precision := uint64(1000000)
multBig := NewAmountBlockchainFromUint64(uint64(float64(precision) * multiplier))
divBig := NewAmountBlockchainFromUint64(precision)
product := multBig.Mul(&amount)
result := product.Div(&divBig)
return result
}
// NewAmountBlockchainFromUint64 creates a new AmountBlockchain from a uint64
func NewAmountBlockchainFromInt64(i64 int64) (Balance, bool) {
if i64 < 0 {
return NewAmountBlockchainFromUint64(0), false
}
return NewAmountBlockchainFromUint64(uint64(i64)), true
}
// NewAmountBlockchainFromUint64 creates a new AmountBlockchain from a uint64
func NewAmountBlockchainFromUint64(u64 uint64) Balance {
bigInt := new(big.Int).SetUint64(u64)
return Balance(*bigInt)
}
// NewAmountBlockchainToMaskFloat64 creates a new AmountBlockchain as a float64 times 10^FLOAT_PRECISION
func NewAmountBlockchainToMaskFloat64(f64 float64) Balance {
bigFloat := new(big.Float).SetFloat64(f64)
exponent := new(big.Float).SetFloat64(math.Pow10(FLOAT_PRECISION))
bigFloat = bigFloat.Mul(bigFloat, exponent)
var bigInt big.Int
bigFloat.Int(&bigInt)
return Balance(bigInt)
}
// NewAmountBlockchainFromStr creates a new AmountBlockchain from a string
func NewAmountBlockchainFromStr(str string) Balance {
var ok bool
var bigInt *big.Int
bigInt, ok = new(big.Int).SetString(str, 0)
if !ok {
return NewAmountBlockchainFromUint64(0)
}
return Balance(*bigInt)
}
// NewAmountFromString creates a new AmountHumanReadable from a string
func NewAmountFromString(str string) (Amount, error) {
decimal, err := decimal.NewFromString(str)
return Amount(decimal), err
}
// NewAmountHumanReadableFromFloat creates a new AmountHumanReadable from a float
func NewAmountHumanReadableFromFloat(float float64) Amount {
return Amount(decimal.NewFromFloat(float))
}
func (amount Amount) Decimal() decimal.Decimal {
return decimal.Decimal(amount)
}
func (amount Amount) ToBlockchain(decimals int32) Balance {
factor := decimal.NewFromInt32(10).Pow(decimal.NewFromInt32(decimals))
raised := ((decimal.Decimal)(amount)).Mul(factor)
return Balance(*raised.BigInt())
}
func (amount Amount) String() string {
return decimal.Decimal(amount).String()
}
func (amount Amount) Div(x Amount) Amount {
return Amount(decimal.Decimal(amount).Div(decimal.Decimal(x)))
}
var _ json.Marshaler = Amount{}
var _ json.Unmarshaler = &Amount{}
var _ yaml.Unmarshaler = &Amount{}
var _ yaml.Marshaler = Amount{}
var _ yaml.IsZeroer = Amount{}
func (b Amount) MarshalYAML() (interface{}, error) {
return b.String(), nil
}
func (b Amount) IsZero() bool {
return decimal.Decimal(b).IsZero()
}
func (b *Amount) UnmarshalYAML(node *yaml.Node) error {
value := node.Value
value = strings.TrimSpace(value)
value = strings.TrimPrefix(value, "\"")
value = strings.TrimSuffix(value, "\"")
dec, err := decimal.NewFromString(value)
if err != nil {
return fmt.Errorf("invalid decimal amount: %v", err)
}
*b = Amount(dec)
return nil
}
func (b Amount) MarshalJSON() ([]byte, error) {
return []byte("\"" + b.String() + "\""), nil
}
func (b *Amount) UnmarshalJSON(p []byte) error {
if string(p) == "null" {
return nil
}
str := strings.Trim(string(p), "\"")
decimal, err := decimal.NewFromString(str)
if err != nil {
return err
}
*b = Amount(decimal)
return nil
}
var _ json.Marshaler = Balance{}
var _ json.Unmarshaler = &Balance{}
func (b Balance) MarshalJSON() ([]byte, error) {
return []byte("\"" + b.String() + "\""), nil
}
func (b *Balance) UnmarshalJSON(p []byte) error {
if string(p) == "null" {
return nil
}
str := strings.Trim(string(p), "\"")
var z big.Int
_, ok := z.SetString(str, 0)
if !ok {
return fmt.Errorf("not a valid big integer: %s", p)
}
*b = Balance(z)
return nil
}
type StringOrInt string
func (s StringOrInt) AsString() string {
return string(s)
}
func (s StringOrInt) AsInt() (uint64, bool) {
asInt, err := strconv.ParseUint(string(s), 0, 64)
return asInt, err == nil
}