-
Notifications
You must be signed in to change notification settings - Fork 15
/
coin.go
228 lines (188 loc) · 4.71 KB
/
coin.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
/*
____ _ _
/ __ \ | | | |
| | | |_ __ ___| | ___ __| | __ _ ___ _ __
| | | | '_ \ / _ \ |/ _ \/ _` |/ _` |/ _ \ '__|
| |__| | | | | __/ | __/ (_| | (_| | __/ |
\____/|_| |_|\___|_|\___|\__,_|\__, |\___|_|
__/ |
|___/
Copyright 2017 - 2019 OneLedger
*/
package balance
import (
"fmt"
"math/big"
"runtime/debug"
)
/*
Coin starts here
*/
// Coin is the basic amount, specified in integers, at the smallest increment (i.e. a satoshi, not a bitcoin)
type Coin struct {
Currency Currency `json:"currency"`
Amount *Amount `json:"amount,string"`
}
// See if the coin is one of a list of currencies
func (coin Coin) IsCurrency(currencies ...string) bool {
if coin.Amount == nil {
return false
}
found := false
for _, currency := range currencies {
if coin.Currency.Name == currency {
found = true
break
}
}
return found
}
// LessThan, for coins...
func (coin Coin) LessThanCoin(value Coin) bool {
if coin.Amount == nil || value.Amount == nil {
coin.Amount = NewAmount(0)
}
if coin.Currency.Chain != value.Currency.Chain {
logger.Fatal("Compare two different coin", coin, value)
}
if coin.Amount.BigInt().Cmp(value.Amount.BigInt()) < 0 {
return true
}
return false
}
// LessThanEqual, for coins...
func (coin Coin) LessThanEqualCoin(value Coin) bool {
if coin.Amount == nil || value.Amount == nil {
coin.Amount = NewAmount(0)
}
if coin.Currency.Chain != value.Currency.Chain {
logger.Fatal("Compare two different coin", coin, value)
}
//logger.Dump("LessThanEqualCoin", value, coin)
if coin.Amount.BigInt().Cmp(value.Amount.BigInt()) <= 0 {
return true
}
return false
}
// IsValid coin or is it broken
func (coin Coin) IsValid() bool {
switch {
case coin.Amount == nil:
return false
case coin.Currency.Name == "":
return false
default:
return coin.Amount.BigInt().Cmp(big.NewInt(0)) >= 0
}
}
// Equals another coin
func (coin Coin) Equals(value Coin) bool {
if coin.Amount == nil {
coin.Amount = NewAmount(0)
}
if coin.Currency.Chain != value.Currency.Chain {
return false
}
if coin.Amount.BigInt().Cmp(value.Amount.BigInt()) == 0 {
return true
}
return false
}
// Minus two coins
func (coin Coin) Minus(value Coin) (Coin, error) {
if coin.Amount == nil {
coin.Amount = NewAmount(0)
}
if coin.Currency.Name != value.Currency.Name {
logger.Fatal("Mismatching currencies", coin, value)
}
base := NewAmount(0)
result := Coin{
Currency: coin.Currency,
Amount: (*Amount)(base.BigInt().Sub(coin.Amount.BigInt(), value.Amount.BigInt())),
}
if result.Amount.BigInt().Cmp(big.NewInt(0)) == -1 {
return result, ErrInsufficientBalance
}
return result, nil
}
// Plus two coins
func (coin Coin) Plus(value Coin) Coin {
if coin.Amount == nil {
debug.PrintStack()
logger.Fatal("Invalid Coin", "coin", coin)
}
if coin.Currency.Name != value.Currency.Name {
logger.Fatal("Mismatching currencies", coin, value)
}
base := big.NewInt(0)
result := Coin{
Currency: coin.Currency,
Amount: (*Amount)(base.Add(coin.Amount.BigInt(), value.Amount.BigInt())),
}
return result
}
func (coin Coin) Divide(value int) Coin {
return coin.DivideInt64(int64(value))
}
func (coin Coin) DivideInt64(value int64) Coin {
if coin.Amount == nil {
coin.Amount = NewAmount(0)
}
base := big.NewInt(0)
divisor := big.NewInt(value)
result := Coin{
Currency: coin.Currency,
Amount: (*Amount)(base.Div(coin.Amount.BigInt(), divisor)),
}
return result
}
// Multiply one coin by another
func (coin Coin) MultiplyInt(value int) Coin {
return coin.MultiplyInt64(int64(value))
}
func (coin Coin) MultiplyInt64(value int64) Coin {
if coin.Amount == nil {
coin.Amount = NewAmount(0)
return coin
}
multiplier := big.NewInt(value)
base := big.NewInt(0)
result := Coin{
Currency: coin.Currency,
Amount: (*Amount)(base.Mul(coin.Amount.BigInt(), multiplier)),
}
return result
}
// Turn a coin into a readable, floating point string with the currency
func (coin Coin) String() string {
return fmt.Sprintf("%s %s", coin.Humanize(), coin.Currency.Name)
}
func (coin Coin) Humanize() string {
return PrintDecimal(coin.Amount.BigInt(), int(coin.Currency.Decimal))
}
func PrintDecimal(i *big.Int, decimal int) string {
str := i.String()
l := len(str)
if l <= decimal {
padZero := ""
for i := 0; i < decimal-l; i++ {
padZero += "0"
}
return removeTrailingZero("0." + padZero + str)
}
return removeTrailingZero(str[:l-decimal] + "." + str[l-decimal:])
}
func removeTrailingZero(str string) string {
l := len(str)
if l == 0 {
return ""
}
if str[l-1] == '.' {
return str[:l-1]
}
if str[l-1] == '0' {
return removeTrailingZero(str[:l-1])
}
return str
}