-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathamount.go
225 lines (189 loc) · 6.38 KB
/
amount.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
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
package crosschain
import (
"fmt"
"math"
"math/big"
"strings"
"github.com/shopspring/decimal"
)
const FLOAT_PRECISION = 6
// AmountBlockchain is a big integer amount as blockchain expects it for tx.
type AmountBlockchain big.Int
// AmountHumanReadable is a decimal amount as a human expects it for readability.
type AmountHumanReadable decimal.Decimal
func (amount AmountBlockchain) Bytes() []byte {
bigInt := big.Int(amount)
return bigInt.Bytes()
}
func (amount AmountBlockchain) String() string {
bigInt := big.Int(amount)
return bigInt.String()
}
// Int converts an AmountBlockchain into *bit.Int
func (amount AmountBlockchain) Int() *big.Int {
bigInt := big.Int(amount)
return &bigInt
}
func (amount AmountBlockchain) Sign() int {
bigInt := big.Int(amount)
return bigInt.Sign()
}
// Uint64 converts an AmountBlockchain into uint64
func (amount AmountBlockchain) Uint64() uint64 {
bigInt := big.Int(amount)
return bigInt.Uint64()
}
// UnmaskFloat64 converts an AmountBlockchain into float64 given the number of decimals
func (amount AmountBlockchain) 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 *AmountBlockchain) Cmp(other *AmountBlockchain) int {
return amount.Int().Cmp(other.Int())
}
// Use the underlying big.Int.Add()
func (amount *AmountBlockchain) Add(x *AmountBlockchain) AmountBlockchain {
sum := new(big.Int)
sum.Set((*big.Int)(amount))
return AmountBlockchain(*sum.Add(sum, x.Int()))
}
// Use the underlying big.Int.Sub()
func (amount *AmountBlockchain) Sub(x *AmountBlockchain) AmountBlockchain {
diff := new(big.Int)
diff.Set((*big.Int)(amount))
return AmountBlockchain(*diff.Sub(diff, x.Int()))
}
// Use the underlying big.Int.Mul()
func (amount *AmountBlockchain) Mul(x *AmountBlockchain) AmountBlockchain {
prod := new(big.Int)
prod.Set((*big.Int)(amount))
return AmountBlockchain(*prod.Mul(prod, x.Int()))
}
// Use the underlying big.Int.Div()
func (amount *AmountBlockchain) Div(x *AmountBlockchain) AmountBlockchain {
quot := new(big.Int)
quot.Set((*big.Int)(amount))
return AmountBlockchain(*quot.Div(quot, x.Int()))
}
func (amount *AmountBlockchain) Abs() AmountBlockchain {
abs := new(big.Int)
abs.Set((*big.Int)(amount))
return AmountBlockchain(*abs.Abs(abs))
}
var zero = big.NewInt(0)
func (amount *AmountBlockchain) IsZero() bool {
return amount.Int().Cmp(zero) == 0
}
func (amount *AmountBlockchain) ToHuman(decimals int32) AmountHumanReadable {
dec := decimal.NewFromBigInt(amount.Int(), -decimals)
return AmountHumanReadable(dec)
}
func (amount AmountBlockchain) ApplyGasPriceMultiplier(chain *ChainConfig) AmountBlockchain {
if chain.ChainGasMultiplier > 0.01 {
return MultiplyByFloat(amount, chain.ChainGasMultiplier)
}
// no multiplier configured, return same
return amount
}
func MultiplyByFloat(amount AmountBlockchain, multiplier float64) AmountBlockchain {
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) (AmountBlockchain, 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) AmountBlockchain {
bigInt := new(big.Int).SetUint64(u64)
return AmountBlockchain(*bigInt)
}
// NewAmountBlockchainToMaskFloat64 creates a new AmountBlockchain as a float64 times 10^FLOAT_PRECISION
func NewAmountBlockchainToMaskFloat64(f64 float64) AmountBlockchain {
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 AmountBlockchain(bigInt)
}
// NewAmountBlockchainFromStr creates a new AmountBlockchain from a string
func NewAmountBlockchainFromStr(str string) AmountBlockchain {
var ok bool
var bigInt *big.Int
bigInt, ok = new(big.Int).SetString(str, 0)
if !ok {
return NewAmountBlockchainFromUint64(0)
}
return AmountBlockchain(*bigInt)
}
// NewAmountHumanReadableFromStr creates a new AmountHumanReadable from a string
func NewAmountHumanReadableFromStr(str string) (AmountHumanReadable, error) {
decimal, err := decimal.NewFromString(str)
return AmountHumanReadable(decimal), err
}
// NewAmountHumanReadableFromFloat creates a new AmountHumanReadable from a float
func NewAmountHumanReadableFromFloat(float float64) AmountHumanReadable {
return AmountHumanReadable(decimal.NewFromFloat(float))
}
func (amount AmountHumanReadable) Decimal() decimal.Decimal {
return decimal.Decimal(amount)
}
func (amount AmountHumanReadable) ToBlockchain(decimals int32) AmountBlockchain {
factor := decimal.NewFromInt32(10).Pow(decimal.NewFromInt32(decimals))
raised := ((decimal.Decimal)(amount)).Mul(factor)
return AmountBlockchain(*raised.BigInt())
}
func (amount AmountHumanReadable) String() string {
return decimal.Decimal(amount).String()
}
func (amount AmountHumanReadable) Div(x AmountHumanReadable) AmountHumanReadable {
return AmountHumanReadable(decimal.Decimal(amount).Div(decimal.Decimal(x)))
}
func (b AmountHumanReadable) MarshalJSON() ([]byte, error) {
return []byte("\"" + b.String() + "\""), nil
}
func (b *AmountHumanReadable) 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 = AmountHumanReadable(decimal)
return nil
}
func (b AmountBlockchain) MarshalJSON() ([]byte, error) {
return []byte("\"" + b.String() + "\""), nil
}
func (b *AmountBlockchain) 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 = AmountBlockchain(z)
return nil
}