-
Notifications
You must be signed in to change notification settings - Fork 241
/
currency.go
151 lines (113 loc) · 3.47 KB
/
currency.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
package currency
import (
"errors"
"math"
"strings"
iso4217 "github.com/ladydascalie/currency"
"github.com/status-im/status-go/services/wallet/market"
"github.com/status-im/status-go/services/wallet/token"
)
const decimalsCalculationCurrency = "USD"
const lowerTokenResolutionInUsd = 0.1
const higherTokenResolutionInUsd = 0.01
type Format struct {
Symbol string `json:"symbol"`
DisplayDecimals uint `json:"displayDecimals"`
StripTrailingZeroes bool `json:"stripTrailingZeroes"`
}
type FormatPerSymbol = map[string]Format
type Currency struct {
marketManager *market.Manager
}
func NewCurrency(marketManager *market.Manager) *Currency {
return &Currency{
marketManager: marketManager,
}
}
func IsCurrencyFiat(symbol string) bool {
return iso4217.Valid(strings.ToUpper(symbol))
}
func GetAllFiatCurrencySymbols() []string {
return iso4217.ValidCodes
}
func calculateFiatDisplayDecimals(symbol string) (uint, error) {
currency, err := iso4217.Get(strings.ToUpper(symbol))
if err != nil {
return 0, err
}
return uint(currency.MinorUnits()), nil
}
func calculateFiatCurrencyFormat(symbol string) (*Format, error) {
displayDecimals, err := calculateFiatDisplayDecimals(symbol)
if err != nil {
return nil, err
}
format := &Format{
Symbol: symbol,
DisplayDecimals: displayDecimals,
StripTrailingZeroes: false,
}
return format, nil
}
func calculateTokenDisplayDecimals(price float64) uint {
var displayDecimals float64 = 0.0
if price > 0 {
lowerDecimalsBound := math.Max(0.0, math.Log10(price)-math.Log10(lowerTokenResolutionInUsd))
upperDecimalsBound := math.Max(0.0, math.Log10(price)-math.Log10(higherTokenResolutionInUsd))
// Use as few decimals as needed to ensure lower precision
displayDecimals = math.Ceil(lowerDecimalsBound)
if displayDecimals+1.0 <= upperDecimalsBound {
// If allowed by upper bound, ensure resolution changes as soon as currency hits multiple of 10
displayDecimals += 1.0
}
}
return uint(displayDecimals)
}
func (cm *Currency) calculateTokenCurrencyFormat(symbol string, price float64) (*Format, error) {
pegSymbol := token.GetTokenPegSymbol(symbol)
if pegSymbol != "" {
var currencyFormat, err = calculateFiatCurrencyFormat(pegSymbol)
if err != nil {
return nil, err
}
currencyFormat.Symbol = symbol
return currencyFormat, nil
}
currencyFormat := &Format{
Symbol: symbol,
DisplayDecimals: calculateTokenDisplayDecimals(price),
StripTrailingZeroes: true,
}
return currencyFormat, nil
}
func GetFiatCurrencyFormats(symbols []string) (FormatPerSymbol, error) {
formats := make(FormatPerSymbol)
for _, symbol := range symbols {
format, err := calculateFiatCurrencyFormat(symbol)
if err != nil {
return nil, err
}
formats[symbol] = *format
}
return formats, nil
}
func (cm *Currency) FetchTokenCurrencyFormats(symbols []string) (FormatPerSymbol, error) {
formats := make(FormatPerSymbol)
// Get latest cached price, fetch only if not available
prices, err := cm.marketManager.GetOrFetchPrices(symbols, []string{decimalsCalculationCurrency}, math.MaxInt64)
if err != nil {
return nil, err
}
for _, symbol := range symbols {
priceData, ok := prices[symbol][decimalsCalculationCurrency]
if !ok {
return nil, errors.New("Could not get price for: " + symbol)
}
format, err := cm.calculateTokenCurrencyFormat(symbol, priceData.Price)
if err != nil {
return nil, err
}
formats[symbol] = *format
}
return formats, nil
}