mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-06 07:26:47 +00:00
Merge branch 'master' into engine
This commit is contained in:
@@ -11,11 +11,13 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// Const values for the ticker package
|
||||
// const values for the ticker package
|
||||
const (
|
||||
ErrTickerForExchangeNotFound = "ticker for exchange does not exist"
|
||||
ErrPrimaryCurrencyNotFound = "error primary currency for ticker not found"
|
||||
ErrSecondaryCurrencyNotFound = "error secondary currency for ticker not found"
|
||||
errExchangeTickerNotFound = "ticker for exchange does not exist"
|
||||
errPairNotSet = "ticker currency pair not set"
|
||||
errAssetTypeNotSet = "ticker asset type not set"
|
||||
errBaseCurrencyNotFound = "ticker base currency not found"
|
||||
errQuoteCurrencyNotFound = "ticker quote currency not found"
|
||||
)
|
||||
|
||||
// Vars for the ticker package
|
||||
@@ -74,12 +76,12 @@ func GetTicker(exchange string, p currency.Pair, tickerType asset.Item) (Price,
|
||||
return Price{}, err
|
||||
}
|
||||
|
||||
if !FirstCurrencyExists(exchange, p.Base) {
|
||||
return Price{}, errors.New(ErrPrimaryCurrencyNotFound)
|
||||
if !BaseCurrencyExists(exchange, p.Base) {
|
||||
return Price{}, errors.New(errBaseCurrencyNotFound)
|
||||
}
|
||||
|
||||
if !SecondCurrencyExists(exchange, p) {
|
||||
return Price{}, errors.New(ErrSecondaryCurrencyNotFound)
|
||||
if !QuoteCurrencyExists(exchange, p) {
|
||||
return Price{}, errors.New(errQuoteCurrencyNotFound)
|
||||
}
|
||||
|
||||
return ticker.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType.String()], nil
|
||||
@@ -94,12 +96,12 @@ func GetTickerByExchange(exchange string) (*Ticker, error) {
|
||||
return &Tickers[x], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New(ErrTickerForExchangeNotFound)
|
||||
return nil, errors.New(errExchangeTickerNotFound)
|
||||
}
|
||||
|
||||
// FirstCurrencyExists checks to see if the first currency of the Price map
|
||||
// BaseCurrencyExists checks to see if the base currency of the ticker map
|
||||
// exists
|
||||
func FirstCurrencyExists(exchange string, currency currency.Code) bool {
|
||||
func BaseCurrencyExists(exchange string, currency currency.Code) bool {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, y := range Tickers {
|
||||
@@ -112,9 +114,9 @@ func FirstCurrencyExists(exchange string, currency currency.Code) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SecondCurrencyExists checks to see if the second currency of the Price map
|
||||
// QuoteCurrencyExists checks to see if the quote currency of the ticker map
|
||||
// exists
|
||||
func SecondCurrencyExists(exchange string, p currency.Pair) bool {
|
||||
func QuoteCurrencyExists(exchange string, p currency.Pair) bool {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, y := range Tickers {
|
||||
@@ -147,23 +149,29 @@ func CreateNewTicker(exchangeName string, tickerNew *Price, tickerType asset.Ite
|
||||
|
||||
// ProcessTicker processes incoming tickers, creating or updating the Tickers
|
||||
// list
|
||||
func ProcessTicker(exchangeName string, tickerNew *Price, tickerType asset.Item) error {
|
||||
if tickerNew.Pair.String() == "" {
|
||||
return errors.New("")
|
||||
func ProcessTicker(exchangeName string, tickerNew *Price, assetType asset.Item) error {
|
||||
if tickerNew.Pair.IsEmpty() {
|
||||
return errors.New(errPairNotSet)
|
||||
}
|
||||
|
||||
tickerNew.LastUpdated = time.Now()
|
||||
if assetType == "" {
|
||||
return errors.New(errAssetTypeNotSet)
|
||||
}
|
||||
|
||||
if tickerNew.LastUpdated.IsZero() {
|
||||
tickerNew.LastUpdated = time.Now()
|
||||
}
|
||||
|
||||
ticker, err := GetTickerByExchange(exchangeName)
|
||||
if err != nil {
|
||||
CreateNewTicker(exchangeName, tickerNew, tickerType)
|
||||
CreateNewTicker(exchangeName, tickerNew, assetType)
|
||||
return nil
|
||||
}
|
||||
|
||||
if FirstCurrencyExists(exchangeName, tickerNew.Pair.Base) {
|
||||
if BaseCurrencyExists(exchangeName, tickerNew.Pair.Base) {
|
||||
m.Lock()
|
||||
a := make(map[string]Price)
|
||||
a[tickerType.String()] = *tickerNew
|
||||
a[assetType.String()] = *tickerNew
|
||||
ticker.Price[tickerNew.Pair.Base.Upper().String()][tickerNew.Pair.Quote.Upper().String()] = a
|
||||
m.Unlock()
|
||||
return nil
|
||||
@@ -172,7 +180,7 @@ func ProcessTicker(exchangeName string, tickerNew *Price, tickerType asset.Item)
|
||||
m.Lock()
|
||||
a := make(map[string]map[string]Price)
|
||||
b := make(map[string]Price)
|
||||
b[tickerType.String()] = *tickerNew
|
||||
b[assetType.String()] = *tickerNew
|
||||
a[tickerNew.Pair.Quote.Upper().String()] = b
|
||||
ticker.Price[tickerNew.Pair.Base.Upper().String()] = a
|
||||
m.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user