mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +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>
337 lines
8.6 KiB
Go
337 lines
8.6 KiB
Go
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
"sync/atomic"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/fill"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
// setupWebsocketRoutineManager creates a new websocket routine manager
|
|
func setupWebsocketRoutineManager(exchangeManager iExchangeManager, orderManager iOrderManager, syncer iCurrencyPairSyncer, cfg *currency.Config, verbose bool) (*websocketRoutineManager, error) {
|
|
if exchangeManager == nil {
|
|
return nil, errNilExchangeManager
|
|
}
|
|
if orderManager == nil {
|
|
return nil, errNilOrderManager
|
|
}
|
|
if syncer == nil {
|
|
return nil, errNilCurrencyPairSyncer
|
|
}
|
|
if cfg == nil {
|
|
return nil, errNilCurrencyConfig
|
|
}
|
|
if cfg.CurrencyPairFormat == nil && verbose {
|
|
return nil, errNilCurrencyPairFormat
|
|
}
|
|
return &websocketRoutineManager{
|
|
verbose: verbose,
|
|
exchangeManager: exchangeManager,
|
|
orderManager: orderManager,
|
|
syncer: syncer,
|
|
currencyConfig: cfg,
|
|
shutdown: make(chan struct{}),
|
|
}, nil
|
|
}
|
|
|
|
// Start runs the subsystem
|
|
func (m *websocketRoutineManager) Start() error {
|
|
if m == nil {
|
|
return fmt.Errorf("websocket routine manager %w", ErrNilSubsystem)
|
|
}
|
|
if !atomic.CompareAndSwapInt32(&m.started, 0, 1) {
|
|
return ErrSubSystemAlreadyStarted
|
|
}
|
|
m.shutdown = make(chan struct{})
|
|
m.websocketRoutine()
|
|
return nil
|
|
}
|
|
|
|
// IsRunning safely checks whether the subsystem is running
|
|
func (m *websocketRoutineManager) IsRunning() bool {
|
|
if m == nil {
|
|
return false
|
|
}
|
|
return atomic.LoadInt32(&m.started) == 1
|
|
}
|
|
|
|
// Stop attempts to shutdown the subsystem
|
|
func (m *websocketRoutineManager) Stop() error {
|
|
if m == nil {
|
|
return fmt.Errorf("websocket routine manager %w", ErrNilSubsystem)
|
|
}
|
|
if !atomic.CompareAndSwapInt32(&m.started, 1, 0) {
|
|
return fmt.Errorf("websocket routine manager %w", ErrSubSystemNotStarted)
|
|
}
|
|
close(m.shutdown)
|
|
m.wg.Wait()
|
|
return nil
|
|
}
|
|
|
|
// websocketRoutine Initial routine management system for websocket
|
|
func (m *websocketRoutineManager) websocketRoutine() {
|
|
if m.verbose {
|
|
log.Debugln(log.WebsocketMgr, "Connecting exchange websocket services...")
|
|
}
|
|
exchanges, err := m.exchangeManager.GetExchanges()
|
|
if err != nil {
|
|
log.Errorf(log.WebsocketMgr, "websocket routine manager cannot get exchanges: %v", err)
|
|
}
|
|
for i := range exchanges {
|
|
go func(i int) {
|
|
if exchanges[i].SupportsWebsocket() {
|
|
if m.verbose {
|
|
log.Debugf(log.WebsocketMgr,
|
|
"Exchange %s websocket support: Yes Enabled: %v",
|
|
exchanges[i].GetName(),
|
|
common.IsEnabled(exchanges[i].IsWebsocketEnabled()),
|
|
)
|
|
}
|
|
|
|
ws, err := exchanges[i].GetWebsocket()
|
|
if err != nil {
|
|
log.Errorf(
|
|
log.WebsocketMgr,
|
|
"Exchange %s GetWebsocket error: %s",
|
|
exchanges[i].GetName(),
|
|
err,
|
|
)
|
|
return
|
|
}
|
|
|
|
if ws.IsEnabled() {
|
|
err = ws.Connect()
|
|
if err != nil {
|
|
log.Errorf(log.WebsocketMgr, "%v", err)
|
|
}
|
|
go m.WebsocketDataReceiver(ws)
|
|
err = ws.FlushChannels()
|
|
if err != nil {
|
|
log.Errorf(log.WebsocketMgr, "Failed to subscribe: %v", err)
|
|
}
|
|
}
|
|
} else if m.verbose {
|
|
log.Debugf(log.WebsocketMgr,
|
|
"Exchange %s websocket support: No",
|
|
exchanges[i].GetName(),
|
|
)
|
|
}
|
|
}(i)
|
|
}
|
|
}
|
|
|
|
// WebsocketDataReceiver handles websocket data coming from a websocket feed
|
|
// associated with an exchange
|
|
func (m *websocketRoutineManager) WebsocketDataReceiver(ws *stream.Websocket) {
|
|
if m == nil || atomic.LoadInt32(&m.started) == 0 {
|
|
return
|
|
}
|
|
m.wg.Add(1)
|
|
defer m.wg.Done()
|
|
|
|
for {
|
|
select {
|
|
case <-m.shutdown:
|
|
return
|
|
case data := <-ws.ToRoutine:
|
|
err := m.WebsocketDataHandler(ws.GetName(), data)
|
|
if err != nil {
|
|
log.Error(log.WebsocketMgr, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// WebsocketDataHandler is a central point for exchange websocket implementations to send
|
|
// processed data. WebsocketDataHandler will then pass that to an appropriate handler
|
|
func (m *websocketRoutineManager) WebsocketDataHandler(exchName string, data interface{}) error {
|
|
if data == nil {
|
|
return fmt.Errorf("exchange %s nil data sent to websocket",
|
|
exchName)
|
|
}
|
|
|
|
switch d := data.(type) {
|
|
case string:
|
|
log.Info(log.WebsocketMgr, d)
|
|
case error:
|
|
return fmt.Errorf("exchange %s websocket error - %s", exchName, data)
|
|
case stream.FundingData:
|
|
if m.verbose {
|
|
log.Infof(log.WebsocketMgr, "%s websocket %s %s funding updated %+v",
|
|
exchName,
|
|
m.FormatCurrency(d.CurrencyPair),
|
|
d.AssetType,
|
|
d)
|
|
}
|
|
case *ticker.Price:
|
|
if m.syncer.IsRunning() {
|
|
err := m.syncer.Update(exchName,
|
|
d.Pair,
|
|
d.AssetType,
|
|
SyncItemTicker,
|
|
nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
err := ticker.ProcessTicker(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m.syncer.PrintTickerSummary(d, "websocket", err)
|
|
case stream.KlineData:
|
|
if m.verbose {
|
|
log.Infof(log.WebsocketMgr, "%s websocket %s %s kline updated %+v",
|
|
exchName,
|
|
m.FormatCurrency(d.Pair),
|
|
d.AssetType,
|
|
d)
|
|
}
|
|
case *orderbook.Base:
|
|
if m.syncer.IsRunning() {
|
|
err := m.syncer.Update(exchName,
|
|
d.Pair,
|
|
d.Asset,
|
|
SyncItemOrderbook,
|
|
nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
m.syncer.PrintOrderbookSummary(d, "websocket", nil)
|
|
case *order.Detail:
|
|
m.printOrderSummary(d)
|
|
if !m.orderManager.Exists(d) {
|
|
err := m.orderManager.Add(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
od, err := m.orderManager.GetByExchangeAndID(d.Exchange, d.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
od.UpdateOrderFromDetail(d)
|
|
|
|
err = m.orderManager.UpdateExistingOrder(od)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case *order.Modify:
|
|
m.printOrderChangeSummary(d)
|
|
od, err := m.orderManager.GetByExchangeAndID(d.Exchange, d.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
od.UpdateOrderFromModify(d)
|
|
err = m.orderManager.UpdateExistingOrder(od)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case order.ClassificationError:
|
|
return fmt.Errorf("%w %s", d.Err, d.Error())
|
|
case stream.UnhandledMessageWarning:
|
|
log.Warn(log.WebsocketMgr, d.Message)
|
|
case account.Change:
|
|
if m.verbose {
|
|
m.printAccountHoldingsChangeSummary(d)
|
|
}
|
|
case []trade.Data:
|
|
if m.verbose {
|
|
log.Infof(log.Trade, "%+v", d)
|
|
}
|
|
case []fill.Data:
|
|
if m.verbose {
|
|
log.Infof(log.Fill, "%+v", d)
|
|
}
|
|
default:
|
|
if m.verbose {
|
|
log.Warnf(log.WebsocketMgr,
|
|
"%s websocket Unknown type: %+v",
|
|
exchName,
|
|
d)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FormatCurrency is a method that formats and returns a currency pair
|
|
// based on the user currency display preferences
|
|
func (m *websocketRoutineManager) FormatCurrency(p currency.Pair) currency.Pair {
|
|
if m == nil || atomic.LoadInt32(&m.started) == 0 {
|
|
return p
|
|
}
|
|
return p.Format(m.currencyConfig.CurrencyPairFormat.Delimiter,
|
|
m.currencyConfig.CurrencyPairFormat.Uppercase)
|
|
}
|
|
|
|
// printOrderChangeSummary this function will be deprecated when a order manager
|
|
// update is done.
|
|
func (m *websocketRoutineManager) printOrderChangeSummary(o *order.Modify) {
|
|
if m == nil || atomic.LoadInt32(&m.started) == 0 || o == nil {
|
|
return
|
|
}
|
|
|
|
log.Debugf(log.WebsocketMgr,
|
|
"Order Change: %s %s %s %s %s %s OrderID:%s ClientOrderID:%s Price:%f Amount:%f Executed Amount:%f Remaining Amount:%f",
|
|
o.Exchange,
|
|
o.AssetType,
|
|
o.Pair,
|
|
o.Status,
|
|
o.Type,
|
|
o.Side,
|
|
o.ID,
|
|
o.ClientOrderID,
|
|
o.Price,
|
|
o.Amount,
|
|
o.ExecutedAmount,
|
|
o.RemainingAmount)
|
|
}
|
|
|
|
// printOrderSummary this function will be deprecated when a order manager
|
|
// update is done.
|
|
func (m *websocketRoutineManager) printOrderSummary(o *order.Detail) {
|
|
if m == nil || atomic.LoadInt32(&m.started) == 0 || o == nil {
|
|
return
|
|
}
|
|
log.Debugf(log.WebsocketMgr,
|
|
"New Order: %s %s %s %s %s %s OrderID:%s ClientOrderID:%s Price:%f Amount:%f Executed Amount:%f Remaining Amount:%f",
|
|
o.Exchange,
|
|
o.AssetType,
|
|
o.Pair,
|
|
o.Status,
|
|
o.Type,
|
|
o.Side,
|
|
o.ID,
|
|
o.ClientOrderID,
|
|
o.Price,
|
|
o.Amount,
|
|
o.ExecutedAmount,
|
|
o.RemainingAmount)
|
|
}
|
|
|
|
// printAccountHoldingsChangeSummary this function will be deprecated when a
|
|
// account holdings update is done.
|
|
func (m *websocketRoutineManager) printAccountHoldingsChangeSummary(o account.Change) {
|
|
if m == nil || atomic.LoadInt32(&m.started) == 0 {
|
|
return
|
|
}
|
|
log.Debugf(log.WebsocketMgr,
|
|
"Account Holdings Balance Changed: %s %s %s has changed balance by %f for account: %s",
|
|
o.Exchange,
|
|
o.Asset,
|
|
o.Currency,
|
|
o.Amount,
|
|
o.Account)
|
|
}
|