-
Notifications
You must be signed in to change notification settings - Fork 28
/
exchange_rate.go
106 lines (81 loc) · 2.5 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
package frdrpc
import (
"fmt"
"sort"
"time"
"github.com/lightninglabs/faraday/fiat"
)
// granularityFromRPC gets a granularity enum value from a rpc request,
// defaulting getting the best granularity for the period being queried.
func granularityFromRPC(g 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 Granularity_UNKNOWN_GRANULARITY:
best, err := fiat.BestGranularity(duration)
if err != nil {
return nil, err
}
return &best, nil
case Granularity_MINUTE:
return &fiat.GranularityMinute, nil
case Granularity_FIVE_MINUTES:
return &fiat.Granularity5Minute, nil
case Granularity_FIFTEEN_MINUTES:
return &fiat.Granularity15Minute, nil
case Granularity_THIRTY_MINUTES:
return &fiat.Granularity30Minute, nil
case Granularity_HOUR:
return &fiat.GranularityHour, nil
case Granularity_SIX_HOURS:
return &fiat.Granularity6Hour, nil
case Granularity_TWELVE_HOURS:
return &fiat.Granularity12Hour, nil
case Granularity_DAY:
return &fiat.GranularityDay, nil
default:
return nil, fmt.Errorf("unknown granularity: %v", g)
}
}
func parseExchangeRateRequest(req *ExchangeRateRequest) ([]time.Time,
*fiat.Granularity, error) {
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]
granularity, err := granularityFromRPC(
req.Granularity, false, end.Sub(start),
)
if err != nil {
return nil, nil, err
}
return timestamps, granularity, nil
}
func exchangeRateResponse(prices map[time.Time]*fiat.USDPrice) *ExchangeRateResponse {
fiatVals := make([]*ExchangeRate, 0, len(prices))
for ts, price := range prices {
fiatVals = append(fiatVals, &ExchangeRate{
Timestamp: uint64(ts.Unix()),
BtcPrice: &BitcoinPrice{
Price: price.Price.String(),
PriceTimestamp: uint64(price.Timestamp.Unix()),
},
})
}
return &ExchangeRateResponse{
Rates: fiatVals,
}
}