forked from rubblelabs/ripple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amount.go
267 lines (236 loc) · 5.65 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
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
262
263
264
265
266
267
package data
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
"github.com/ysv/ripple/crypto"
)
type Amount struct {
*Value
Currency Currency
Issuer Account
}
type ExchangeRate uint64
func newAmount(value *Value, currency Currency, issuer Account) *Amount {
return &Amount{
Value: value,
Currency: currency,
Issuer: issuer,
}
}
// Requires v to be in computer parsable form
func NewAmount(v interface{}) (*Amount, error) {
switch n := v.(type) {
case int64:
return &Amount{
Value: newValue(true, n < 0, abs(n), 0),
}, nil
case string:
var err error
amount := new(Amount)
parts := strings.Split(strings.TrimSpace(n), "/")
native := false
switch {
case len(parts) == 1:
native = true
case len(parts) > 1 && parts[1] == "XRP":
native = true
if !strings.Contains(parts[0], ".") {
parts[0] = parts[0] + "."
}
}
if amount.Value, err = NewValue(parts[0], native); err != nil {
return nil, err
}
if len(parts) > 1 {
if amount.Currency, err = NewCurrency(parts[1]); err != nil {
return nil, err
}
}
if len(parts) > 2 {
if issuer, err := crypto.NewRippleHash(parts[2]); err != nil {
return nil, err
} else {
copy(amount.Issuer[:], issuer.Payload())
}
}
return amount, nil
default:
return nil, fmt.Errorf("Bad type: %+v", v)
}
}
func (a Amount) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
err := a.Marshal(&buf)
return buf.Bytes(), err
}
func (a *Amount) UnmarshalBinary(b []byte) error {
buf := bytes.NewBuffer(b)
return a.Unmarshal(buf)
}
func (a Amount) Equals(b Amount) bool {
return a.Value.Equals(*b.Value) &&
a.Currency == b.Currency &&
a.Issuer == b.Issuer
}
// Returns true if the values are equal, but ignores the currency and issuer
func (a Amount) SameValue(b *Amount) bool {
return a.Value.Equals(*b.Value)
}
func (a Amount) Clone() *Amount {
return newAmount(a.Value.Clone(), a.Currency, a.Issuer)
}
// Returns a new Amount with the same currency and issuer, but a zero value
func (a Amount) ZeroClone() *Amount {
return newAmount(a.Value.ZeroClone(), a.Currency, a.Issuer)
}
func (a Amount) IsPositive() bool {
return !a.negative
}
func (a Amount) Negate() *Amount {
clone := a.Clone()
clone.negative = !clone.negative
return clone
}
func (a Amount) Abs() *Amount {
clone := a.Clone()
clone.negative = false
return clone
}
func (a Amount) Add(b *Amount) (*Amount, error) {
sum, err := a.Value.Add(*b.Value)
if err != nil {
return nil, err
}
return newAmount(sum, a.Currency, a.Issuer), nil
}
func (a Amount) Subtract(b *Amount) (*Amount, error) {
return a.Add(b.Negate())
}
func (a Amount) multiply(b *Amount) (*Amount, error) {
product, err := a.Value.Multiply(*b.Value)
if err != nil {
return nil, err
}
return newAmount(product, a.Currency, a.Issuer), nil
}
func (num Amount) divide(den *Amount) (*Amount, error) {
quotient, err := num.Value.Divide(*den.Value)
if err != nil {
return nil, err
}
return newAmount(quotient, num.Currency, num.Issuer), nil
}
func (a Amount) ApplyInterest() (*Amount, error) {
if a.Currency.Type() != CT_DEMURRAGE {
return &a, nil
}
rate := fmt.Sprintf("%f/%s/%s", a.Currency.Rate(Now().Uint32()), a.Currency.Machine(), a.Issuer.String())
factor, err := NewAmount(rate)
if err != nil {
return nil, err
}
return a.multiply(factor)
}
type amountFunc func(Amount, *Amount) (*Amount, error)
func applyInterestPair(a Amount, b *Amount, f amountFunc) (*Amount, error) {
v1, err := a.ApplyInterest()
if err != nil {
return nil, err
}
v2, err := b.ApplyInterest()
if err != nil {
return nil, err
}
return f(*v1, v2)
}
func (a Amount) Divide(b *Amount) (*Amount, error) {
return applyInterestPair(a, b, Amount.divide)
}
func (a Amount) Multiply(b *Amount) (*Amount, error) {
return applyInterestPair(a, b, Amount.multiply)
}
// Ratio returns the ratio between a and b.
// Returns a zero value when division is impossible
func (a Amount) Ratio(b Amount) *Value {
ratio, err := a.Value.Ratio(*b.Value)
switch {
case err == nil:
return ratio
case a.IsNative():
return &zeroNative
default:
return &zeroNonNative
}
}
func (a Amount) Bytes() []byte {
if a.IsNative() {
return a.Value.Bytes()
}
return append(a.Value.Bytes(), append(a.Currency.Bytes(), a.Issuer.Bytes()...)...)
}
// Amount in human parsable form
// with demurrage applied
func (a Amount) String() string {
factored, err := a.ApplyInterest()
if err != nil {
return err.Error()
}
switch {
case a.IsNative():
return factored.Value.String() + "/XRP"
case a.Issuer.IsZero():
return factored.Value.String() + "/" + a.Currency.String()
default:
return factored.Value.String() + "/" + a.Currency.String() + "/" + a.Issuer.String()
}
}
// Amount in computer parsable form
func (a Amount) Machine() string {
switch {
case a.IsNative():
return a.Value.String() + "/XRP"
case a.Issuer.IsZero():
return a.Value.String() + "/" + a.Currency.Machine()
default:
return a.Value.String() + "/" + a.Currency.Machine() + "/" + a.Issuer.String()
}
}
func (a Amount) Asset() *Asset {
switch {
case a.IsNative():
return &Asset{
Currency: "XRP",
}
default:
return &Asset{
Currency: a.Currency.String(),
Issuer: a.Issuer.String(),
}
}
}
func NewExchangeRate(a, b *Amount) (ExchangeRate, error) {
if b.IsZero() {
return 0, nil
}
rate, err := a.Divide(b)
if err != nil {
return 0, err
}
if rate.IsZero() {
return 0, nil
}
if rate.offset >= -100 || rate.offset <= 155 {
panic("Impossible Rate")
}
return ExchangeRate(uint64(rate.offset+100)<<54 | uint64(rate.num)), nil
}
func (e *ExchangeRate) Bytes() []byte {
if e == nil {
return nil
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(*e))
return b
}