-
Notifications
You must be signed in to change notification settings - Fork 29
/
exchange_rate.go
187 lines (146 loc) · 4.45 KB
/
exchange_rate.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
package frdrpcserver
import (
"errors"
"fmt"
"sort"
"time"
"github.com/lightninglabs/faraday/fiat"
"github.com/lightninglabs/faraday/frdrpc"
)
func priceCfgFromRPC(rpcBackend frdrpc.FiatBackend,
rpcGranularity frdrpc.Granularity, disable bool, start, end time.Time,
prices []*frdrpc.BitcoinPrice) (*fiat.PriceSourceConfig, error) {
backend, err := fiatBackendFromRPC(rpcBackend)
if err != nil {
return nil, err
}
if len(prices) > 0 && backend != fiat.CustomPriceBackend {
return nil, errors.New(
"custom price points provided but custom fiat " +
"backend not set",
)
}
var (
granularity *fiat.Granularity
pricePoints []*fiat.Price
)
// Get additional values for backends that require additional
// information.
switch backend {
case fiat.CoinCapPriceBackend:
granularity, err = granularityFromRPC(
rpcGranularity, disable, end.Sub(start),
)
case fiat.CustomPriceBackend:
pricePoints, err = pricePointsFromRPC(prices)
if err != nil {
return nil, err
}
err = validateCustomPricePoints(pricePoints, start)
}
if err != nil {
return nil, err
}
return &fiat.PriceSourceConfig{
Backend: backend,
Granularity: granularity,
PricePoints: pricePoints,
}, nil
}
// granularityFromRPC gets a granularity enum value from a rpc request,
// defaulting getting the best granularity for the period being queried.
func granularityFromRPC(g frdrpc.Granularity, disableFiat bool,
duration time.Duration) (*fiat.Granularity, error) {
// If we do not need fiat prices, we can return nil granularity.
if disableFiat {
return nil, nil
}
switch g {
// If granularity is not set, allow it to default to the best
// granularity that we can get for the query period.
case frdrpc.Granularity_UNKNOWN_GRANULARITY:
best, err := fiat.BestGranularity(duration)
if err != nil {
return nil, err
}
return &best, nil
case frdrpc.Granularity_MINUTE:
return &fiat.GranularityMinute, nil
case frdrpc.Granularity_FIVE_MINUTES:
return &fiat.Granularity5Minute, nil
case frdrpc.Granularity_FIFTEEN_MINUTES:
return &fiat.Granularity15Minute, nil
case frdrpc.Granularity_THIRTY_MINUTES:
return &fiat.Granularity30Minute, nil
case frdrpc.Granularity_HOUR:
return &fiat.GranularityHour, nil
case frdrpc.Granularity_SIX_HOURS:
return &fiat.Granularity6Hour, nil
case frdrpc.Granularity_TWELVE_HOURS:
return &fiat.Granularity12Hour, nil
case frdrpc.Granularity_DAY:
return &fiat.GranularityDay, nil
default:
return nil, fmt.Errorf("unknown granularity: %v", g)
}
}
func fiatBackendFromRPC(backend frdrpc.FiatBackend) (fiat.PriceBackend, error) {
switch backend {
case frdrpc.FiatBackend_UNKNOWN_FIATBACKEND:
return fiat.UnknownPriceBackend, nil
case frdrpc.FiatBackend_COINCAP:
return fiat.CoinCapPriceBackend, nil
case frdrpc.FiatBackend_COINDESK:
return fiat.CoinDeskPriceBackend, nil
case frdrpc.FiatBackend_CUSTOM:
return fiat.CustomPriceBackend, nil
case frdrpc.FiatBackend_COINGECKO:
return fiat.CoinGeckoPriceBackend, nil
default:
return fiat.UnknownPriceBackend,
fmt.Errorf("unknown fiat backend: %v", backend)
}
}
func parseExchangeRateRequest(req *frdrpc.ExchangeRateRequest) ([]time.Time,
*fiat.PriceSourceConfig, error) {
if len(req.Timestamps) == 0 {
return nil, nil, errors.New("at least one timestamp required")
}
timestamps := make([]time.Time, len(req.Timestamps))
for i, timestamp := range req.Timestamps {
timestamps[i] = time.Unix(int64(timestamp), 0)
}
// Sort timestamps in ascending order so that we can get the duration
// we're querying over.
sort.SliceStable(timestamps, func(i, j int) bool {
return timestamps[i].Before(timestamps[j])
})
// Get our start and end times, these may be the same if we have a
// single timestamp.
start, end := timestamps[0], timestamps[len(timestamps)-1]
cfg, err := priceCfgFromRPC(
req.FiatBackend, req.Granularity, false, start, end,
req.CustomPrices,
)
if err != nil {
return nil, nil, err
}
return timestamps, cfg, nil
}
func exchangeRateResponse(
prices map[time.Time]*fiat.Price) *frdrpc.ExchangeRateResponse {
fiatVals := make([]*frdrpc.ExchangeRate, 0, len(prices))
for ts, price := range prices {
fiatVals = append(fiatVals, &frdrpc.ExchangeRate{
Timestamp: uint64(ts.Unix()),
BtcPrice: &frdrpc.BitcoinPrice{
Price: price.Price.String(),
PriceTimestamp: uint64(price.Timestamp.Unix()),
Currency: price.Currency,
},
})
}
return &frdrpc.ExchangeRateResponse{
Rates: fiatVals,
}
}