Files
gocryptotrader/currency/forexprovider/forexprovider.go
Gareth Kirwan 88182ec414 Engine: Remove Default Forex Provider (#1395)
* Currency: Do not use a default forex provider

exchangerate.host now requires an API key.
Instead of finding a new Free (for now) default, this change simply
disables the currency exchange when nothing is enabled.

* SyncManager: Report ?.?? for an unknown forex amount

In a situation where we thought forex was available but we got an error,
this avoids showing 0.00 when there was actually an error.

* Currency: Tests for no default forex

* Currency: Use mock provider for tests

* Currency: Add API key to exchangerate.host

* Currency: Remove Exchangerate.host

Exchangerate.host was bought by apilayer, the old API deprecated, and
replaced with a proxy to the apilayer api.
We already have currencylayer support, so ther's no reason to keep exh.

Worth noting: New ERH keys actually work on currencylayer

* Currencies: Add test coverage for currency layer

* fixup! Currency: Tests for no default forex

Remove duplicate assignment

Fixes [review comment](https://github.com/thrasher-corp/gocryptotrader/pull/1395#discussion_r1395178513)

* fixup! Currency: Add API key to exchangerate.host

Remove unused ErrVar

Fixes [review
comment](https://github.com/thrasher-corp/gocryptotrader/pull/1395#discussion_r1396647418)

* fixup! Currency: Tests for no default forex

Fix spelling of override in test

Fixes [review comment](https://github.com/thrasher-corp/gocryptotrader/pull/1395#discussion_r1396701476)

* fixup! SyncManager: Report ?.?? for an unknown forex amount

Fix display of non-positive currency conversions.

Fixes [review comment](https://github.com/thrasher-corp/gocryptotrader/pull/1395/files#r1398527134)
2023-11-24 13:50:01 +11:00

97 lines
2.6 KiB
Go

// Package forexprovider utilises foreign exchange API services to manage
// relational FIAT currencies
package forexprovider
import (
"errors"
"fmt"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
currencyconverter "github.com/thrasher-corp/gocryptotrader/currency/forexprovider/currencyconverterapi"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/currencylayer"
exchangerates "github.com/thrasher-corp/gocryptotrader/currency/forexprovider/exchangeratesapi.io"
fixer "github.com/thrasher-corp/gocryptotrader/currency/forexprovider/fixer.io"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/openexchangerates"
)
var (
errUnhandledForeignExchangeProvider = errors.New("unhandled foreign exchange provider")
errNoPrimaryForexProviderEnabled = errors.New("no primary forex provider enabled")
)
// ForexProviders is a foreign exchange handler type
type ForexProviders struct {
base.FXHandler
}
// GetSupportedForexProviders returns a list of supported forex providers
func GetSupportedForexProviders() []string {
return []string{
"CurrencyConverter",
"CurrencyLayer",
"ExchangeRates",
"Fixer",
"OpenExchangeRates",
}
}
// SetProvider sets provider to the FX handler
func (f *ForexProviders) SetProvider(b base.IFXProvider) error {
currencies, err := b.GetSupportedCurrencies()
if err != nil {
return err
}
providerBase := base.Provider{
Provider: b,
SupportedCurrencies: currencies,
}
if b.IsPrimaryProvider() {
f.FXHandler = base.FXHandler{
Primary: providerBase,
}
return nil
}
f.FXHandler.Support = append(f.FXHandler.Support, providerBase)
return nil
}
// StartFXService starts the forex provider service and returns a pointer to it
func StartFXService(fxProviders []base.Settings) (*ForexProviders, error) {
handler := new(ForexProviders)
for i := range fxProviders {
var provider base.IFXProvider
switch fxProviders[i].Name {
case "CurrencyConverter":
provider = new(currencyconverter.CurrencyConverter)
case "CurrencyLayer":
provider = new(currencylayer.CurrencyLayer)
case "ExchangeRates":
provider = new(exchangerates.ExchangeRates)
case "Fixer":
provider = new(fixer.Fixer)
case "OpenExchangeRates":
provider = new(openexchangerates.OXR)
default:
return nil, fmt.Errorf("%s %w", fxProviders[i].Name,
errUnhandledForeignExchangeProvider)
}
err := provider.Setup(fxProviders[i])
if err != nil {
return nil, err
}
err = handler.SetProvider(provider)
if err != nil {
return nil, err
}
}
if handler.Primary.Provider == nil {
return nil, errNoPrimaryForexProviderEnabled
}
return handler, nil
}