-
Notifications
You must be signed in to change notification settings - Fork 390
/
conversionrates.go
116 lines (94 loc) · 2.8 KB
/
conversionrates.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package coinpayments
import (
"context"
"encoding/json"
"net/url"
"strconv"
"time"
"github.com/shopspring/decimal"
"storj.io/storj/satellite/payments/monetary"
)
// cmdRates is API command for retrieving currency rate infos.
const cmdRates = "rates"
// ExchangeStatus defines if currency is exchangeable.
type ExchangeStatus string
const (
// ExchangeStatusOnline defines exchangeable currency.
ExchangeStatusOnline ExchangeStatus = "online"
// ExchangeStatusOffline defines currency that can not be convertible at the moment.
ExchangeStatusOffline ExchangeStatus = "offline"
)
// CurrencyRateInfo holds currency conversion info.
type CurrencyRateInfo struct {
IsFiat bool
RateBTC decimal.Decimal
TXFee decimal.Decimal
Status ExchangeStatus
LastUpdate time.Time
}
// UnmarshalJSON converts JSON string to currency rate info.
func (rateInfo *CurrencyRateInfo) UnmarshalJSON(b []byte) error {
var rateRaw struct {
IsFiat int `json:"is_fiat"`
RateBTC string `json:"rate_btc"`
TXFee string `json:"tx_fee"`
Status string `json:"status"`
LastUpdate string `json:"last_update"`
}
if err := json.Unmarshal(b, &rateRaw); err != nil {
return err
}
rateBTC, err := decimal.NewFromString(rateRaw.RateBTC)
if err != nil {
return err
}
txFee, err := decimal.NewFromString(rateRaw.TXFee)
if err != nil {
return err
}
lastUpdate, err := strconv.ParseInt(rateRaw.LastUpdate, 10, 64)
if err != nil {
return err
}
*rateInfo = CurrencyRateInfo{
IsFiat: rateRaw.IsFiat > 0,
RateBTC: rateBTC,
TXFee: txFee,
Status: ExchangeStatus(rateRaw.Status),
LastUpdate: time.Unix(lastUpdate, 0),
}
return nil
}
// CurrencyRateInfos maps currency to currency rate info.
type CurrencyRateInfos map[CurrencySymbol]CurrencyRateInfo
// ForCurrency allows lookup into a CurrencyRateInfos map by currency
// object, instead of by its coinpayments.net-specific symbol.
func (infos CurrencyRateInfos) ForCurrency(currency *monetary.Currency) (info CurrencyRateInfo, ok bool) {
coinpaymentsSymbol, ok := currencySymbols[currency]
if !ok {
return info, false
}
info, ok = infos[coinpaymentsSymbol]
return info, ok
}
// ConversionRates collection of API methods for retrieving currency
// conversion rates.
type ConversionRates struct {
client *Client
}
// Get returns USD rate for specified currency.
func (rates ConversionRates) Get(ctx context.Context) (CurrencyRateInfos, error) {
values := make(url.Values)
values.Set("short", "1")
rateInfos := make(CurrencyRateInfos)
res, err := rates.client.do(ctx, cmdRates, values)
if err != nil {
return nil, Error.Wrap(err)
}
if err = json.Unmarshal(res, &rateInfos); err != nil {
return nil, Error.Wrap(err)
}
return rateInfos, nil
}