This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
amount.go
231 lines (206 loc) · 6.31 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
226
227
228
229
230
231
// Copyright 2017-2018 The qitmeer developers
// Copyright 2015 The Decred developers
// Copyright 2013, 2014 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package types
import (
"errors"
"math"
"strconv"
)
// AmountUnit describes a method of converting an Amount to something
// other than the base unit of a coin. The value of the AmountUnit
// is the exponent component of the decadic multiple to convert from
// an amount in coins to an amount counted in atomic units.
type AmountUnit int
// These constants define various units used when describing a coin
// monetary amount.
const (
AmountMegaCoin AmountUnit = 6
AmountKiloCoin AmountUnit = 3
AmountCoin AmountUnit = 0
AmountMilliCoin AmountUnit = -3
AmountMicroCoin AmountUnit = -6
AmountAtom AmountUnit = -8
)
// String returns the unit as a string. For recognized units, the SI
// prefix is used, or "Atom" for the base unit. For all unrecognized
// units, "1eN MEER" is returned, where N is the AmountUnit.
func (u AmountUnit) String() string {
switch u {
case AmountMegaCoin:
return "M"
case AmountKiloCoin:
return "k"
case AmountCoin:
return ""
case AmountMilliCoin:
return "m"
case AmountMicroCoin:
return "μ"
case AmountAtom:
return "atom"
default:
return "1e" + strconv.FormatInt(int64(u), 10) + " "
}
}
// Amount represents the base coin monetary unit (colloquially referred
// to as an `Atom'). A single Amount is equal to 1e-8 of a coin.
// size is 10 bytes ( value -> 8 byte , coinId -> 2 byte)
type Amount struct {
Value int64
Id CoinID
}
func checkMaxAmount(x *Amount) error {
if x.Value > MaxAmount {
return errors.New("x+y, x exceeds max amount of " + x.Id.Name())
}
return nil
}
func checkSameCoinId(x, y CoinID) error {
if x != y {
err := errors.New("invalid amount add, unmatched " +
x.Name() + " with " + y.Name())
return err
}
return nil
}
func (z *Amount) Add(x, y *Amount) (*Amount, error) {
err := checkSameCoinId(z.Id, x.Id)
if err != nil {
return z, err
}
err = checkSameCoinId(x.Id, y.Id)
if err != nil {
return z, err
}
err = checkMaxAmount(x)
if err != nil {
return z, err
}
err = checkMaxAmount(y)
if err != nil {
return z, err
}
sum := x.Value + y.Value
if x.Value > 0 && y.Value > 0 && sum < 0 {
err := errors.New("add overflow")
return z, err
}
if x.Value < 0 && y.Value < 0 && sum > 0 {
err := errors.New("add overflow")
return z, err
}
z.Value = sum
return z, nil
}
// AmountGroup represents a group of multiple Amount,
type AmountGroup []Amount
// AmountMap represents a map of multiple Amount,
type AmountMap map[CoinID]int64
func (am AmountMap) Add(other AmountMap) {
if len(other) <= 0 {
return
}
for k, v := range am {
ov, ok := other[k]
if !ok {
continue
}
am[k] = v + ov
}
for k, v := range other {
_, ok := am[k]
if !ok {
am[k] = v
}
}
}
// round converts a floating point number, which may or may not be representable
// as an integer, to the Amount integer type by rounding to the nearest integer.
// This is performed by adding or subtracting 0.5 depending on the sign, and
// relying on integer truncation to round the value to the nearest Amount.
func round(f float64) int64 {
if f < 0 {
return int64(f - 0.5)
}
return int64(f + 0.5)
}
// NewAmount creates an Amount from a floating point value representing
// some value in the currency. NewAmount errors if f is NaN or +-Infinity,
// but does not check that the amount is within the total amount of coins
// producible as f may not refer to an amount at a single moment in time.
//
// NewAmount is for specifically for converting qitmeer to Atoms (atomic units).
// For creating a new Amount with an int64 value which denotes a quantity of
// Atoms, do a simple type conversion from type int64 to Amount.
func NewAmount(f float64) (*Amount, error) {
// The amount is only considered invalid if it cannot be represented
// as an integer type. This may happen if f is NaN or +-Infinity.
switch {
case math.IsNaN(f):
fallthrough
case math.IsInf(f, 1):
fallthrough
case math.IsInf(f, -1):
return &Amount{0, MEERID}, errors.New("invalid coin amount")
}
return &Amount{round(f * AtomsPerCoin), MEERID}, nil
}
func NewMeer(a uint64) (*Amount, error) {
amt := Amount{int64(a), MEERID}
err := checkMaxAmount(&amt)
if err != nil {
zero := Amount{0, MEERID}
return &zero, err
}
return &amt, nil
}
// ToUnit converts a monetary amount counted in coin base units to a
// floating point value representing an amount of coins.
func (a *Amount) ToUnit(u AmountUnit) float64 {
return float64(a.Value) / math.Pow10(int(u+8))
}
// ToCoin is the equivalent of calling ToUnit with AmountCoin.
func (a *Amount) ToCoin() float64 {
return a.ToUnit(AmountCoin)
}
// Format formats a monetary amount counted in coin base units as a
// string for a given unit. The conversion will succeed for any unit,
// however, known units will be formated with an appended label describing
// the units with SI notation, or "atom" for the base unit.
func (a *Amount) Format(u AmountUnit) string {
units := " " + u.String() + a.Id.Name()
return strconv.FormatFloat(a.ToUnit(u), 'f', -int(u+8), 64) + units
}
// String is the equivalent of calling Format with AmountCoin.
func (a *Amount) String() string {
return a.Format(AmountCoin)
}
// MulF64 multiplies an Amount by a floating point value. While this is not
// an operation that must typically be done by a full node or wallet, it is
// useful for services that build on top of qitmeer (for example, calculating
// a fee by multiplying by a percentage).
func (a *Amount) MulF64(f float64) *Amount {
return &Amount{round(float64(a.Value) * f), a.Id}
}
// AmountSorter implements sort.Interface to allow a slice of Amounts to
// be sorted.
type AmountSorter []Amount
// Len returns the number of Amounts in the slice. It is part of the
// sort.Interface implementation.
func (s AmountSorter) Len() int {
return len(s)
}
// Swap swaps the Amounts at the passed indices. It is part of the
// sort.Interface implementation.
func (s AmountSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less returns whether the Amount with index i should sort before the
// Amount with index j. It is part of the sort.Interface
// implementation.
func (s AmountSorter) Less(i, j int) bool {
return s[i].Value < s[j].Value
}