mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-21 07:26:48 +00:00
* currency: Add method to derive pair * currency: Add method to lower entire charset but used the slice copy and returned that. This will change the original, just gotta see if this is an issue, but the slice usually goes out of scope anyway. * currency/pairs: add filter method * currency: add function to derive select currencies from currency pairs * currency/engine: slight adjustments * currency: fix linter issue also shift burden of proof to caller instead of repair, more performant. * currency: more linter * pairs: optimize; reduce allocs/op and B/op * currency: Add in function 'NewPairsFromString' for testing purposes * currency: don't suppress error * currency: stop panic on empty currency code * currency: Add helper method to match currencies between exchanges * currency: fixed my bad spelling * currency: Implement stable coin checks, refactored base code methods, optimized upper and lower case strings for currency code/pairs * currency: add pairs method to derive stable coins from internal list. * Currency: Cleanup, fix tests. * engine/exchanges/currency: fix whoops * Currency: force govet no copy on Item datatype * Currency: fix naughty linter issues * exchange: revert change * currency/config: fix config upgrade mistake * currency: re-implement currency sub-systems * *RetrieveConfigCurrencyPairs removed *CheckCurrencyConfigValues to only provide warnings, add additional support when, disable when support is lost or not available and set default values. *Drop Cryptocurrencies from configuration as this is not needed. *Drop REST Poll delay field as this was unused. *Update default values for currencyFileUpdateDuration & foreignExchangeUpdateDuration. *Allow Role to be marshalled for file type. *Refactor RunUpdater to verify and check config values and set default running foreign exchange provider. * currency: cleanup * currency: change match -> equal for comparison which is more of a standard and little easier to find * currency: address nits * currency: fix whoops * currency: Add some more pairs methods * currency: linter issues * currency: RM unused field * currency: rm verbose * currency: fix word * currency: gocritic * currency: fix another whoopsie * example_config: default to show log system name * Currency: Force all support packages to use Equal method for comparison as there is a small comparison bug when checking upper and lower casing, this has a more of a pronounced impact between exchanges and client instances of currency generation * currency: fix log name * ordermanager: fix potential panic * currency: small optim. * engine: display correct bool and force shutdown * currency: add function and fix regression * Change ConvertCurrency -> ConvertFiat to be more precise * ADD GetForeignExchangeRate to get specific exchange rate for fiat pair * Fix currency display and formatting regression and tied in with config.Currency fields * engine: fix tests * currency: return the amount when no conversion needs to take place * currency: reduce method name * currency: Address nits glorious nits * currency: fix linter * currency: addr nits * currency: check underlying role in test * gct: change to EMPTYCODE and EMPTYPAIR across codebase * currency: fix nits * currency: this fixes test race but this issue has not been resolved. Please see: https://trello.com/c/54eizOIo/143-currency-package-upgrades * currency: Add temp dir for testing * Update engine/engine.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * documentation: update and regen * currency: Address niterinos * currency: Add test case for config upgrade when falling over to exchange rate host as default from exchangeRates provider * currency: addr nits * currency: fix whoops Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
419 lines
9.3 KiB
Go
419 lines
9.3 KiB
Go
package ticker
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"math/rand"
|
|
"os"
|
|
"strconv"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
err := dispatch.Start(1, dispatch.DefaultJobsLimit)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
cpyMux = service.mux
|
|
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
var cpyMux *dispatch.Mux
|
|
|
|
func TestSubscribeTicker(t *testing.T) {
|
|
_, err := SubscribeTicker("", currency.EMPTYPAIR, asset.Item(""))
|
|
if err == nil {
|
|
t.Error("error cannot be nil")
|
|
}
|
|
|
|
p := currency.NewPair(currency.BTC, currency.USD)
|
|
|
|
// force error
|
|
service.mux = nil
|
|
err = ProcessTicker(&Price{
|
|
Pair: p,
|
|
ExchangeName: "subscribetest",
|
|
AssetType: asset.Spot})
|
|
if err == nil {
|
|
t.Error("error cannot be nil")
|
|
}
|
|
|
|
sillyP := p
|
|
sillyP.Base = currency.GALA_NEO
|
|
err = ProcessTicker(&Price{
|
|
Pair: sillyP,
|
|
ExchangeName: "subscribetest",
|
|
AssetType: asset.Spot})
|
|
if err == nil {
|
|
t.Error("error cannot be nil")
|
|
}
|
|
|
|
sillyP.Quote = currency.AAA
|
|
err = ProcessTicker(&Price{
|
|
Pair: sillyP,
|
|
ExchangeName: "subscribetest",
|
|
AssetType: asset.Spot})
|
|
if err == nil {
|
|
t.Error("error cannot be nil")
|
|
}
|
|
|
|
err = ProcessTicker(&Price{
|
|
Pair: sillyP,
|
|
ExchangeName: "subscribetest",
|
|
AssetType: "silly",
|
|
})
|
|
if err == nil {
|
|
t.Error("error cannot be nil")
|
|
}
|
|
// reinstate mux
|
|
service.mux = cpyMux
|
|
|
|
err = ProcessTicker(&Price{
|
|
Pair: p,
|
|
ExchangeName: "subscribetest",
|
|
AssetType: asset.Spot})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = SubscribeTicker("subscribetest", p, asset.Spot)
|
|
if err != nil {
|
|
t.Error("cannot subscribe to ticker", err)
|
|
}
|
|
}
|
|
|
|
func TestSubscribeToExchangeTickers(t *testing.T) {
|
|
_, err := SubscribeToExchangeTickers("")
|
|
if err == nil {
|
|
t.Error("error cannot be nil")
|
|
}
|
|
|
|
p := currency.NewPair(currency.BTC, currency.USD)
|
|
|
|
err = ProcessTicker(&Price{
|
|
Pair: p,
|
|
ExchangeName: "subscribeExchangeTest",
|
|
AssetType: asset.Spot})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
_, err = SubscribeToExchangeTickers("subscribeExchangeTest")
|
|
if err != nil {
|
|
t.Error("error cannot be nil", err)
|
|
}
|
|
}
|
|
|
|
func TestGetTicker(t *testing.T) {
|
|
newPair, err := currency.NewPairFromStrings("BTC", "USD")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
priceStruct := Price{
|
|
Pair: newPair,
|
|
Last: 1200,
|
|
High: 1298,
|
|
Low: 1148,
|
|
Bid: 1195,
|
|
Ask: 1220,
|
|
Volume: 5,
|
|
PriceATH: 1337,
|
|
ExchangeName: "bitfinex",
|
|
AssetType: asset.Spot,
|
|
}
|
|
|
|
err = ProcessTicker(&priceStruct)
|
|
if err != nil {
|
|
t.Fatal("ProcessTicker error", err)
|
|
}
|
|
|
|
tickerPrice, err := GetTicker("bitfinex", newPair, asset.Spot)
|
|
if err != nil {
|
|
t.Errorf("Ticker GetTicker init error: %s", err)
|
|
}
|
|
if !tickerPrice.Pair.Equal(newPair) {
|
|
t.Error("ticker tickerPrice.CurrencyPair value is incorrect")
|
|
}
|
|
|
|
_, err = GetTicker("blah", newPair, asset.Spot)
|
|
if err == nil {
|
|
t.Fatal("TestGetTicker returned nil error on invalid exchange")
|
|
}
|
|
|
|
newPair.Base = currency.ETH
|
|
_, err = GetTicker("bitfinex", newPair, asset.Spot)
|
|
if err == nil {
|
|
t.Fatal("TestGetTicker returned ticker for invalid first currency")
|
|
}
|
|
|
|
btcltcPair, err := currency.NewPairFromStrings("BTC", "LTC")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = GetTicker("bitfinex", btcltcPair, asset.Spot)
|
|
if err == nil {
|
|
t.Fatal("TestGetTicker returned ticker for invalid second currency")
|
|
}
|
|
|
|
priceStruct.PriceATH = 9001
|
|
priceStruct.Pair.Base = currency.ETH
|
|
priceStruct.AssetType = "futures_3m"
|
|
err = ProcessTicker(&priceStruct)
|
|
if err != nil {
|
|
t.Fatal("ProcessTicker error", err)
|
|
}
|
|
|
|
tickerPrice, err = GetTicker("bitfinex", newPair, "futures_3m")
|
|
if err != nil {
|
|
t.Errorf("Ticker GetTicker init error: %s", err)
|
|
}
|
|
|
|
if tickerPrice.PriceATH != 9001 {
|
|
t.Error("ticker tickerPrice.PriceATH value is incorrect")
|
|
}
|
|
|
|
_, err = GetTicker("bitfinex", newPair, "meowCats")
|
|
if err == nil {
|
|
t.Error("Ticker GetTicker error cannot be nil")
|
|
}
|
|
|
|
priceStruct.AssetType = "meowCats"
|
|
err = ProcessTicker(&priceStruct)
|
|
if err != nil {
|
|
t.Fatal("ProcessTicker error", err)
|
|
}
|
|
|
|
// process update again
|
|
err = ProcessTicker(&priceStruct)
|
|
if err != nil {
|
|
t.Fatal("ProcessTicker error", err)
|
|
}
|
|
}
|
|
|
|
func TestFindLast(t *testing.T) {
|
|
cp := currency.NewPair(currency.BTC, currency.XRP)
|
|
_, err := FindLast(cp, asset.Spot)
|
|
if !errors.Is(err, errTickerNotFound) {
|
|
t.Errorf("received: %v but expected: %v", err, errTickerNotFound)
|
|
}
|
|
|
|
err = service.update(&Price{Last: 0, ExchangeName: "testerinos", Pair: cp, AssetType: asset.Spot})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = FindLast(cp, asset.Spot)
|
|
if !errors.Is(err, errInvalidTicker) {
|
|
t.Errorf("received: %v but expected: %v", err, errInvalidTicker)
|
|
}
|
|
|
|
err = service.update(&Price{Last: 1337, ExchangeName: "testerinos", Pair: cp, AssetType: asset.Spot})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
last, err := FindLast(cp, asset.Spot)
|
|
if !errors.Is(err, nil) {
|
|
t.Errorf("received: %v but expected: %v", err, nil)
|
|
}
|
|
|
|
if last != 1337 {
|
|
t.Fatal("unexpected value")
|
|
}
|
|
}
|
|
|
|
func TestProcessTicker(t *testing.T) { // non-appending function to tickers
|
|
exchName := "bitstamp"
|
|
newPair, err := currency.NewPairFromStrings("BTC", "USD")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
priceStruct := Price{
|
|
Last: 1200,
|
|
High: 1298,
|
|
Low: 1148,
|
|
Bid: 1195,
|
|
Ask: 1220,
|
|
Volume: 5,
|
|
PriceATH: 1337,
|
|
}
|
|
|
|
err = ProcessTicker(&priceStruct)
|
|
if err == nil {
|
|
t.Fatal("empty exchange should throw an err")
|
|
}
|
|
|
|
priceStruct.ExchangeName = exchName
|
|
|
|
// test for empty pair
|
|
err = ProcessTicker(&priceStruct)
|
|
if err == nil {
|
|
t.Fatal("empty pair should throw an err")
|
|
}
|
|
|
|
// test for empty asset type
|
|
priceStruct.Pair = newPair
|
|
err = ProcessTicker(&priceStruct)
|
|
if err == nil {
|
|
t.Fatal("ProcessTicker error cannot be nil")
|
|
}
|
|
priceStruct.AssetType = asset.Spot
|
|
// now process a valid ticker
|
|
err = ProcessTicker(&priceStruct)
|
|
if err != nil {
|
|
t.Fatal("ProcessTicker error", err)
|
|
}
|
|
result, err := GetTicker(exchName, newPair, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal("TestProcessTicker failed to create and return a new ticker")
|
|
}
|
|
if !result.Pair.Equal(newPair) {
|
|
t.Fatal("TestProcessTicker pair mismatch")
|
|
}
|
|
|
|
// now test for processing a pair with a different quote currency
|
|
newPair, err = currency.NewPairFromStrings("BTC", "AUD")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
priceStruct.Pair = newPair
|
|
err = ProcessTicker(&priceStruct)
|
|
if err != nil {
|
|
t.Fatal("ProcessTicker error", err)
|
|
}
|
|
_, err = GetTicker(exchName, newPair, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal("TestProcessTicker failed to create and return a new ticker")
|
|
}
|
|
_, err = GetTicker(exchName, newPair, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal("TestProcessTicker failed to return an existing ticker")
|
|
}
|
|
|
|
// now test for processing a pair which has a different base currency
|
|
newPair, err = currency.NewPairFromStrings("LTC", "AUD")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
priceStruct.Pair = newPair
|
|
err = ProcessTicker(&priceStruct)
|
|
if err != nil {
|
|
t.Fatal("ProcessTicker error", err)
|
|
}
|
|
_, err = GetTicker(exchName, newPair, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal("TestProcessTicker failed to create and return a new ticker")
|
|
}
|
|
_, err = GetTicker(exchName, newPair, asset.Spot)
|
|
if err != nil {
|
|
t.Fatal("TestProcessTicker failed to return an existing ticker")
|
|
}
|
|
|
|
type quick struct {
|
|
Name string
|
|
P currency.Pair
|
|
TP Price
|
|
}
|
|
|
|
var testArray []quick
|
|
|
|
_ = rand.NewSource(time.Now().Unix())
|
|
|
|
var wg sync.WaitGroup
|
|
var sm sync.Mutex
|
|
|
|
var catastrophicFailure bool
|
|
for i := 0; i < 500; i++ {
|
|
if catastrophicFailure {
|
|
break
|
|
}
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
// nolint:gosec // no need to import crypo/rand for testing
|
|
newName := "Exchange" + strconv.FormatInt(rand.Int63(), 10)
|
|
newPairs, err := currency.NewPairFromStrings("BTC"+strconv.FormatInt(rand.Int63(), 10), // nolint:gosec // no need to import crypo/rand for testing
|
|
"USD"+strconv.FormatInt(rand.Int63(), 10)) // nolint:gosec // no need to import crypo/rand for testing
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
tp := Price{
|
|
Pair: newPairs,
|
|
Last: rand.Float64(), // nolint:gosec // no need to import crypo/rand for testing
|
|
ExchangeName: newName,
|
|
AssetType: asset.Spot,
|
|
}
|
|
|
|
sm.Lock()
|
|
err = ProcessTicker(&tp)
|
|
if err != nil {
|
|
t.Error(err)
|
|
catastrophicFailure = true
|
|
return
|
|
}
|
|
|
|
testArray = append(testArray, quick{Name: newName, P: newPairs, TP: tp})
|
|
sm.Unlock()
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
if catastrophicFailure {
|
|
t.Fatal("ProcessTicker error")
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
for _, test := range testArray {
|
|
wg.Add(1)
|
|
fatalErr := false
|
|
go func(test quick) {
|
|
result, err := GetTicker(test.Name, test.P, asset.Spot)
|
|
if err != nil {
|
|
fatalErr = true
|
|
return
|
|
}
|
|
|
|
if result.Last != test.TP.Last {
|
|
t.Error("TestProcessTicker failed bad values")
|
|
}
|
|
|
|
wg.Done()
|
|
}(test)
|
|
|
|
if fatalErr {
|
|
t.Fatal("TestProcessTicker failed to retrieve new ticker")
|
|
}
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestGetAssociation(t *testing.T) {
|
|
_, err := service.getAssociations("")
|
|
if !errors.Is(err, errExchangeNameIsEmpty) {
|
|
t.Errorf("received: %v but expected: %v", err, errExchangeNameIsEmpty)
|
|
}
|
|
|
|
service.mux = nil
|
|
|
|
_, err = service.getAssociations("getassociation")
|
|
if err == nil {
|
|
t.Error("error cannot be nil")
|
|
}
|
|
|
|
service.mux = cpyMux
|
|
}
|