-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
conversions.go
52 lines (43 loc) · 1.52 KB
/
conversions.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
package bsvrates
import (
"context"
"fmt"
"github.com/mrz1836/go-whatsonchain"
)
// GetConversion will get the satoshi amount for the given currency + amount provided.
// The first provider that succeeds is the conversion that is returned
func (c *Client) GetConversion(ctx context.Context, currency Currency, amount float64) (satoshis int64, 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 conversion value
for _, provider := range c.Providers() {
providerUsed = provider
switch provider {
case ProviderCoinPaprika:
var response *PriceConversionResponse
if response, err = c.CoinPaprika().GetPriceConversion(
ctx, USDCurrencyID, CoinPaprikaQuoteID, amount,
); err == nil && response != nil {
satoshis, err = response.GetSatoshi()
}
case ProviderWhatsOnChain:
var response *whatsonchain.ExchangeRate
if response, err = c.WhatsOnChain().GetExchangeRate(ctx); err == nil && response != nil {
satoshis, err = ConvertPriceToSatoshis(response.Rate, amount)
}
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 satoshi value? Otherwise, keep looping
if satoshis > 0 {
return
}
}
return
}
// todo: create a new method to get all three and then average the results