-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rates.go
53 lines (44 loc) · 1.46 KB
/
rates.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
/*
Package bsvrates brings multiple providers into one place to obtain the current BSV exchange rate
*/
package bsvrates
import (
"context"
"fmt"
"github.com/mrz1836/go-whatsonchain"
)
// GetRate will get a BSV->Currency rate from the list of providers.
// The first provider that succeeds is the rate that is returned
func (c *Client) GetRate(ctx context.Context, currency Currency) (rate float64, providerUsed Provider, err error) {
// Check if currency is accepted across all providers
if !currency.IsAccepted() {
err = fmt.Errorf("currency [%s] is not accepted by all providers at this time", currency.Name())
return
}
// Loop providers and get a rate
for _, provider := range c.Providers() {
providerUsed = provider
switch provider {
case ProviderCoinPaprika:
var response *TickerResponse
if response, err = c.CoinPaprika().GetMarketPrice(ctx, CoinPaprikaQuoteID); err == nil && response != nil {
rate = response.Quotes.USD.Price
}
case ProviderWhatsOnChain:
var response *whatsonchain.ExchangeRate
if response, err = c.WhatsOnChain().GetExchangeRate(ctx); err == nil && response != nil {
rate = response.Rate
}
case providerLast:
err = fmt.Errorf("provider unknown")
return
}
// todo: log the error for sanity in case the user want's to see the failure?
// Did we get a rate? Otherwise, keep looping
if rate > 0 {
return
}
}
return
}
// todo: create a new method to get all three and then average the results