mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-18 23:16:49 +00:00
* Added dispatch service * Added orderbook streaming capabilities * Assigned correct orderbook.base exchange name * Fixed Requested niterinos Add in cli orderbook QA tool to gctcli Add exchange orderbook streaming * Add ticker streaming support through dispatch package * Added in some more info on error returns for orderbook.go * fix linter issues * Fix some issues * Update * Fix requested * move dispatch out of exchanges folder to its own independant folder * Fix requested * change orderbook string to tickers * Limit orderbooks to 50 and made dispatch system more stateless in operation * lower cases for update/retrieve/sub exchange name * Adds in asset validation and lower case conversion on cli * Remove comment * Moved timer to a higher scope so its not constantly initialised just reset per instance and removed returning unused channel on error * Rm unused release function in dispatch.go Reset timer and bleed buffered timer chan if needed in dispatch.go Added in ticker.Stop() and timer.Stop() functions for worker routine return in dispatch.go Index aggregated bid and ask functions for orderbook.go Added in dummy slice for wsorderbook_test.go * Moved drain to above Reset so potential race would not occur in dispatch.go Fix various linter issues dispatch.go * Fix some issues * change to start/stop service, added in service state change via cli, updated logger * fix requested * Add worker amount init spawning * fix linter issues * Fix more linter issues * More fixes * Fix race issue on releasing pipe channel on a close after shutting down dispatcher system * Moved all types to dispatch_types.go && remove panic * Moved types into serperate file && improve test coverage * RM unnecessary select case for draining channel && fixed error string * Added orderbook_types file and improved code coverage * gofmt file * reinstated select cases on drain because I am silly * Remove error for drop worker * Added more test cases * not checking error issue fix * remove func causing race in test, this has required protection via an exported function * set Gemini websocket orderbook exchange name
208 lines
6.3 KiB
Go
208 lines
6.3 KiB
Go
package ticker
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gofrs/uuid"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
)
|
|
|
|
func init() {
|
|
service = new(Service)
|
|
service.Tickers = make(map[string]map[*currency.Item]map[*currency.Item]map[asset.Item]*Ticker)
|
|
service.Exchange = make(map[string]uuid.UUID)
|
|
service.mux = dispatch.GetNewMux()
|
|
}
|
|
|
|
// SubscribeTicker subcribes to a ticker and returns a communication channel to
|
|
// stream new ticker updates
|
|
func SubscribeTicker(exchange string, p currency.Pair, a asset.Item) (dispatch.Pipe, error) {
|
|
exchange = strings.ToLower(exchange)
|
|
service.RLock()
|
|
defer service.RUnlock()
|
|
|
|
tick, ok := service.Tickers[exchange][p.Base.Item][p.Quote.Item][a]
|
|
if !ok {
|
|
return dispatch.Pipe{}, fmt.Errorf("ticker item not found for %s %s %s",
|
|
exchange,
|
|
p,
|
|
a)
|
|
}
|
|
|
|
return service.mux.Subscribe(tick.Main)
|
|
}
|
|
|
|
// SubscribeToExchangeTickers subcribes to all tickers on an exchange
|
|
func SubscribeToExchangeTickers(exchange string) (dispatch.Pipe, error) {
|
|
exchange = strings.ToLower(exchange)
|
|
service.RLock()
|
|
defer service.RUnlock()
|
|
id, ok := service.Exchange[exchange]
|
|
if !ok {
|
|
return dispatch.Pipe{}, fmt.Errorf("%s exchange tickers not found",
|
|
exchange)
|
|
}
|
|
|
|
return service.mux.Subscribe(id)
|
|
}
|
|
|
|
// GetTicker checks and returns a requested ticker if it exists
|
|
func GetTicker(exchange string, p currency.Pair, tickerType asset.Item) (Price, error) {
|
|
exchange = strings.ToLower(exchange)
|
|
service.RLock()
|
|
defer service.RUnlock()
|
|
if service.Tickers[exchange] == nil {
|
|
return Price{}, fmt.Errorf("no tickers for %s exchange", exchange)
|
|
}
|
|
|
|
if service.Tickers[exchange][p.Base.Item] == nil {
|
|
return Price{}, fmt.Errorf("no tickers associated with base currency %s",
|
|
p.Base)
|
|
}
|
|
|
|
if service.Tickers[exchange][p.Base.Item][p.Quote.Item] == nil {
|
|
return Price{}, fmt.Errorf("no tickers associated with quote currency %s",
|
|
p.Quote)
|
|
}
|
|
|
|
if service.Tickers[exchange][p.Base.Item][p.Quote.Item][tickerType] == nil {
|
|
return Price{}, fmt.Errorf("no tickers associated with asset type %s",
|
|
tickerType)
|
|
}
|
|
|
|
return service.Tickers[exchange][p.Base.Item][p.Quote.Item][tickerType].Price, nil
|
|
}
|
|
|
|
// ProcessTicker processes incoming tickers, creating or updating the Tickers
|
|
// list
|
|
func ProcessTicker(exchangeName string, tickerNew *Price, assetType asset.Item) error {
|
|
if exchangeName == "" {
|
|
return fmt.Errorf(errExchangeNameUnset)
|
|
}
|
|
|
|
tickerNew.ExchangeName = strings.ToLower(exchangeName)
|
|
|
|
if tickerNew.Pair.IsEmpty() {
|
|
return fmt.Errorf("%s %s", exchangeName, errPairNotSet)
|
|
}
|
|
|
|
if assetType == "" {
|
|
return fmt.Errorf("%s %s %s", exchangeName,
|
|
tickerNew.Pair,
|
|
errAssetTypeNotSet)
|
|
}
|
|
|
|
tickerNew.AssetType = assetType
|
|
|
|
if tickerNew.LastUpdated.IsZero() {
|
|
tickerNew.LastUpdated = time.Now()
|
|
}
|
|
|
|
return service.Update(tickerNew)
|
|
}
|
|
|
|
// Update updates ticker price
|
|
func (s *Service) Update(p *Price) error {
|
|
var ids []uuid.UUID
|
|
|
|
s.Lock()
|
|
switch {
|
|
case s.Tickers[p.ExchangeName] == nil:
|
|
s.Tickers[p.ExchangeName] = make(map[*currency.Item]map[*currency.Item]map[asset.Item]*Ticker)
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item] = make(map[*currency.Item]map[asset.Item]*Ticker)
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] = make(map[asset.Item]*Ticker)
|
|
err := s.SetItemID(p)
|
|
if err != nil {
|
|
s.Unlock()
|
|
return err
|
|
}
|
|
|
|
case s.Tickers[p.ExchangeName][p.Pair.Base.Item] == nil:
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item] = make(map[*currency.Item]map[asset.Item]*Ticker)
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] = make(map[asset.Item]*Ticker)
|
|
err := s.SetItemID(p)
|
|
if err != nil {
|
|
s.Unlock()
|
|
return err
|
|
}
|
|
|
|
case s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] == nil:
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item] = make(map[asset.Item]*Ticker)
|
|
err := s.SetItemID(p)
|
|
if err != nil {
|
|
s.Unlock()
|
|
return err
|
|
}
|
|
|
|
case s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType] == nil:
|
|
err := s.SetItemID(p)
|
|
if err != nil {
|
|
s.Unlock()
|
|
return err
|
|
}
|
|
|
|
default:
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Last = p.Last
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].High = p.High
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Low = p.Low
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Bid = p.Bid
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Ask = p.Ask
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Volume = p.Volume
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].QuoteVolume = p.QuoteVolume
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].PriceATH = p.PriceATH
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Open = p.Open
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Close = p.Close
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].LastUpdated = p.LastUpdated
|
|
ids = s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Assoc
|
|
ids = append(ids, s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType].Main)
|
|
}
|
|
s.Unlock()
|
|
return s.mux.Publish(ids, p)
|
|
}
|
|
|
|
// SetItemID retrieves and sets dispatch mux publish IDs
|
|
func (s *Service) SetItemID(p *Price) error {
|
|
if p == nil {
|
|
return errors.New(errTickerPriceIsNil)
|
|
}
|
|
|
|
ids, err := s.GetAssociations(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
singleID, err := s.mux.GetID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.Tickers[p.ExchangeName][p.Pair.Base.Item][p.Pair.Quote.Item][p.AssetType] = &Ticker{Price: *p,
|
|
Main: singleID,
|
|
Assoc: ids}
|
|
return nil
|
|
}
|
|
|
|
// GetAssociations links a singular book with it's dispatch associations
|
|
func (s *Service) GetAssociations(p *Price) ([]uuid.UUID, error) {
|
|
if p == nil || *p == (Price{}) {
|
|
return nil, errors.New(errTickerPriceIsNil)
|
|
}
|
|
var ids []uuid.UUID
|
|
exchangeID, ok := s.Exchange[p.ExchangeName]
|
|
if !ok {
|
|
var err error
|
|
exchangeID, err = s.mux.GetID()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s.Exchange[p.ExchangeName] = exchangeID
|
|
}
|
|
|
|
ids = append(ids, exchangeID)
|
|
return ids, nil
|
|
}
|