Skip to content

Commit

Permalink
(Exchange Interface) Convert Fetch & Update orderbook/ticker methods …
Browse files Browse the repository at this point in the history
…to return pointers (thrasher-corp#398)

* moved order and ticker fetching to return a pointer

* return nil instead of empty struct

* fixed incorrect nil

* general cleanup
  • Loading branch information
xtda authored and thrasher- committed Dec 17, 2019
1 parent 44aa1e3 commit 75ac5ee
Show file tree
Hide file tree
Showing 42 changed files with 320 additions and 348 deletions.
16 changes: 8 additions & 8 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ func ({{.Variable}} *{{.CapitalName}}) UpdateTradablePairs(forceUpdate bool) err


// UpdateTicker updates and returns the ticker for a currency pair
func ({{.Variable}} *{{.CapitalName}}) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
func ({{.Variable}} *{{.CapitalName}}) UpdateTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
// NOTE: EXAMPLE FOR GETTING TICKER PRICE
/*
var tickerPrice ticker.Price
tickerPrice := new(ticker.Price)
tick, err := {{.Variable}}.GetTicker(p.String())
if err != nil {
return tickerPrice, err
}
tickerPrice = ticker.Price{
tickerPrice = &ticker.Price{
High: tick.High,
Low: tick.Low,
Bid: tick.Bid,
Expand All @@ -216,7 +216,7 @@ func ({{.Variable}} *{{.CapitalName}}) UpdateTicker(p currency.Pair, assetType a
Close: tick.Close,
Pair: p,
}
err = ticker.ProcessTicker({{.Variable}}.Name, &tickerPrice, assetType)
err = ticker.ProcessTicker({{.Variable}}.Name, tickerPrice, assetType)
if err != nil {
return tickerPrice, err
}
Expand All @@ -225,7 +225,7 @@ func ({{.Variable}} *{{.CapitalName}}) UpdateTicker(p currency.Pair, assetType a
}

// FetchTicker returns the ticker for a currency pair
func ({{.Variable}} *{{.CapitalName}}) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
func ({{.Variable}} *{{.CapitalName}}) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerNew, err := ticker.GetTicker({{.Variable}}.Name, p, assetType)
if err != nil {
return {{.Variable}}.UpdateTicker(p, assetType)
Expand All @@ -234,7 +234,7 @@ func ({{.Variable}} *{{.CapitalName}}) FetchTicker(p currency.Pair, assetType as
}

// FetchOrderbook returns orderbook base on the currency pair
func ({{.Variable}} *{{.CapitalName}}) FetchOrderbook(currency currency.Pair, assetType asset.Item) (orderbook.Base, error) {
func ({{.Variable}} *{{.CapitalName}}) FetchOrderbook(currency currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get({{.Variable}}.Name, currency, assetType)
if err != nil {
return {{.Variable}}.UpdateOrderbook(currency, assetType)
Expand All @@ -243,8 +243,8 @@ func ({{.Variable}} *{{.CapitalName}}) FetchOrderbook(currency currency.Pair, as
}

// UpdateOrderbook updates and returns the orderbook for a currency pair
func ({{.Variable}} *{{.CapitalName}}) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
var orderBook orderbook.Base
func ({{.Variable}} *{{.CapitalName}}) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
orderBook := new(orderbook.Base)
// NOTE: UPDATE ORDERBOOK EXAMPLE
/*
orderbookNew, err := {{.Variable}}.GetOrderBook(exchange.FormatExchangeCurrency({{.Variable}}.Name, p).String(), 1000)
Expand Down
8 changes: 4 additions & 4 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
log.Printf("Executing wrappers for %v %v %v", base.GetName(), assetTypes[i], p)

if !authenticatedOnly {
var r1 ticker.Price
var r1 *ticker.Price
r1, err = e.FetchTicker(p, assetTypes[i])
msg = ""
if err != nil {
Expand All @@ -328,7 +328,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
Response: jsonifyInterface([]interface{}{r1}),
})

var r2 ticker.Price
var r2 *ticker.Price
r2, err = e.UpdateTicker(p, assetTypes[i])
msg = ""
if err != nil {
Expand All @@ -342,7 +342,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
Response: jsonifyInterface([]interface{}{r2}),
})

var r3 orderbook.Base
var r3 *orderbook.Base
r3, err = e.FetchOrderbook(p, assetTypes[i])
msg = ""
if err != nil {
Expand All @@ -356,7 +356,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
Response: jsonifyInterface([]interface{}{r3}),
})

var r4 orderbook.Base
var r4 *orderbook.Base
r4, err = e.UpdateOrderbook(p, assetTypes[i])
msg = ""
if err != nil {
Expand Down
36 changes: 5 additions & 31 deletions engine/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,40 +416,14 @@ func GetRelatableCurrencies(p currency.Pair, incOrig, incUSDT bool) currency.Pai

// GetSpecificOrderbook returns a specific orderbook given the currency,
// exchangeName and assetType
func GetSpecificOrderbook(p currency.Pair, exchangeName string, assetType asset.Item) (orderbook.Base, error) {
var specificOrderbook orderbook.Base
var err error
for x := range Bot.Exchanges {
if Bot.Exchanges[x] != nil {
if Bot.Exchanges[x].GetName() == exchangeName {
specificOrderbook, err = Bot.Exchanges[x].FetchOrderbook(
p,
assetType,
)
break
}
}
}
return specificOrderbook, err
func GetSpecificOrderbook(p currency.Pair, exchangeName string, assetType asset.Item) (*orderbook.Base, error) {
return GetExchangeByName(exchangeName).FetchOrderbook(p, assetType)
}

// GetSpecificTicker returns a specific ticker given the currency,
// exchangeName and assetType
func GetSpecificTicker(p currency.Pair, exchangeName string, assetType asset.Item) (ticker.Price, error) {
var specificTicker ticker.Price
var err error
for x := range Bot.Exchanges {
if Bot.Exchanges[x] != nil {
if Bot.Exchanges[x].GetName() == exchangeName {
specificTicker, err = Bot.Exchanges[x].FetchTicker(
p,
assetType,
)
break
}
}
}
return specificTicker, err
func GetSpecificTicker(p currency.Pair, exchangeName string, assetType asset.Item) (*ticker.Price, error) {
return GetExchangeByName(exchangeName).FetchTicker(p, assetType)
}

// GetCollatedExchangeAccountInfoByCoin collates individual exchange account
Expand Down Expand Up @@ -757,7 +731,7 @@ func GetAllActiveTickers() []EnabledExchangeCurrencies {
err)
continue
}
exchangeTicker.ExchangeValues = append(exchangeTicker.ExchangeValues, tp)
exchangeTicker.ExchangeValues = append(exchangeTicker.ExchangeValues, *tp)
}
tickerData = append(tickerData, exchangeTicker)
}
Expand Down
4 changes: 2 additions & 2 deletions engine/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func TestGetSpecificOrderbook(t *testing.T) {
t.Fatal("Unexpected result")
}

ob, err = GetSpecificOrderbook(currency.NewPairFromStrings("ETH", "LTC"),
_, err = GetSpecificOrderbook(currency.NewPairFromStrings("ETH", "LTC"),
"Bitstamp",
asset.Spot)
if err == nil {
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestGetSpecificTicker(t *testing.T) {
t.Fatal("Unexpected result")
}

tick, err = GetSpecificTicker(currency.NewPairFromStrings("ETH", "LTC"), "Bitstamp",
_, err = GetSpecificTicker(currency.NewPairFromStrings("ETH", "LTC"), "Bitstamp",
asset.Spot)
if err == nil {
t.Fatal("Unexpected result")
Expand Down
2 changes: 1 addition & 1 deletion engine/restful_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func GetAllActiveOrderbooks() []EnabledExchangeOrderbooks {
err)
continue
}
exchangeOB.ExchangeValues = append(exchangeOB.ExchangeValues, ob)
exchangeOB.ExchangeValues = append(exchangeOB.ExchangeValues, *ob)
}
orderbookData = append(orderbookData, exchangeOB)
}
Expand Down
6 changes: 3 additions & 3 deletions engine/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (e *ExchangeCurrencyPairSyncer) worker() {

if c.Ticker.IsUsingREST {
e.setProcessing(c.Exchange, c.Pair, c.AssetType, SyncItemTicker, true)
var result ticker.Price
var result *ticker.Price
var err error

if supportsRESTTickerBatching {
Expand Down Expand Up @@ -408,7 +408,7 @@ func (e *ExchangeCurrencyPairSyncer) worker() {
} else {
result, err = Bot.Exchanges[x].UpdateTicker(c.Pair, c.AssetType)
}
printTickerSummary(&result, c.Pair, c.AssetType, exchangeName, err)
printTickerSummary(result, c.Pair, c.AssetType, exchangeName, err)
if err == nil {
//nolint:gocritic Bot.CommsRelayer.StageTickerData(exchangeName, c.AssetType, result)
if Bot.Config.RemoteControl.WebsocketRPC.Enabled {
Expand Down Expand Up @@ -444,7 +444,7 @@ func (e *ExchangeCurrencyPairSyncer) worker() {

e.setProcessing(c.Exchange, c.Pair, c.AssetType, SyncItemOrderbook, true)
result, err := Bot.Exchanges[x].UpdateOrderbook(c.Pair, c.AssetType)
printOrderbookSummary(&result, c.Pair, c.AssetType, exchangeName, err)
printOrderbookSummary(result, c.Pair, c.AssetType, exchangeName, err)
if err == nil {
//nolint:gocritic Bot.CommsRelayer.StageOrderbookData(exchangeName, c.AssetType, result)
if Bot.Config.RemoteControl.WebsocketRPC.Enabled {
Expand Down
14 changes: 7 additions & 7 deletions exchanges/alphapoint/alphapoint_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func (a *Alphapoint) GetAccountInfo() (exchange.AccountInfo, error) {
}

// UpdateTicker updates and returns the ticker for a currency pair
func (a *Alphapoint) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
var tickerPrice ticker.Price
func (a *Alphapoint) UpdateTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerPrice := new(ticker.Price)
tick, err := a.GetTicker(p.String())
if err != nil {
return tickerPrice, err
Expand All @@ -128,7 +128,7 @@ func (a *Alphapoint) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker
tickerPrice.Volume = tick.Volume
tickerPrice.Last = tick.Last

err = ticker.ProcessTicker(a.Name, &tickerPrice, assetType)
err = ticker.ProcessTicker(a.Name, tickerPrice, assetType)
if err != nil {
return tickerPrice, err
}
Expand All @@ -137,7 +137,7 @@ func (a *Alphapoint) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker
}

// FetchTicker returns the ticker for a currency pair
func (a *Alphapoint) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
func (a *Alphapoint) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tick, err := ticker.GetTicker(a.Name, p, assetType)
if err != nil {
return a.UpdateTicker(p, assetType)
Expand All @@ -146,8 +146,8 @@ func (a *Alphapoint) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.
}

// UpdateOrderbook updates and returns the orderbook for a currency pair
func (a *Alphapoint) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
var orderBook orderbook.Base
func (a *Alphapoint) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
orderBook := new(orderbook.Base)
orderbookNew, err := a.GetOrderbook(p.String())
if err != nil {
return orderBook, err
Expand Down Expand Up @@ -180,7 +180,7 @@ func (a *Alphapoint) UpdateOrderbook(p currency.Pair, assetType asset.Item) (ord
}

// FetchOrderbook returns the orderbook for a currency pair
func (a *Alphapoint) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
func (a *Alphapoint) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(a.Name, p, assetType)
if err != nil {
return a.UpdateOrderbook(p, assetType)
Expand Down
16 changes: 8 additions & 8 deletions exchanges/anx/anx_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ func (a *ANX) FetchTradablePairs(asset asset.Item) ([]string, error) {
}

// UpdateTicker updates and returns the ticker for a currency pair
func (a *ANX) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
var tickerPrice ticker.Price
func (a *ANX) UpdateTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerPrice := new(ticker.Price)
tick, err := a.GetTicker(a.FormatExchangeCurrency(p, assetType).String())
if err != nil {
return tickerPrice, err
Expand All @@ -215,7 +215,7 @@ func (a *ANX) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price,
ask, _ := convert.FloatFromString(tick.Data.Sell.Value)
volume, _ := convert.FloatFromString(tick.Data.Volume.Value)

tickerPrice = ticker.Price{
tickerPrice = &ticker.Price{
Last: last,
High: high,
Low: low,
Expand All @@ -226,7 +226,7 @@ func (a *ANX) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price,
LastUpdated: time.Unix(0, tick.Data.UpdateTime),
}

err = ticker.ProcessTicker(a.Name, &tickerPrice, assetType)
err = ticker.ProcessTicker(a.Name, tickerPrice, assetType)
if err != nil {
return tickerPrice, err
}
Expand All @@ -235,7 +235,7 @@ func (a *ANX) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price,
}

// FetchTicker returns the ticker for a currency pair
func (a *ANX) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
func (a *ANX) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerNew, err := ticker.GetTicker(a.Name, p, assetType)
if err != nil {
return a.UpdateTicker(p, assetType)
Expand All @@ -244,7 +244,7 @@ func (a *ANX) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price,
}

// FetchOrderbook returns the orderbook for a currency pair
func (a *ANX) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
func (a *ANX) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(a.Name, p, assetType)
if err != nil {
return a.UpdateOrderbook(p, assetType)
Expand All @@ -253,8 +253,8 @@ func (a *ANX) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbook.B
}

// UpdateOrderbook updates and returns the orderbook for a currency pair
func (a *ANX) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
var orderBook orderbook.Base
func (a *ANX) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
orderBook := new(orderbook.Base)
orderbookNew, err := a.GetDepth(a.FormatExchangeCurrency(p, assetType).String())
if err != nil {
return orderBook, err
Expand Down
17 changes: 8 additions & 9 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,10 @@ func (b *Binance) UpdateTradablePairs(forceUpdate bool) error {
}

// UpdateTicker updates and returns the ticker for a currency pair
func (b *Binance) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
var tickerPrice ticker.Price
func (b *Binance) UpdateTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tick, err := b.GetTickers()
if err != nil {
return tickerPrice, err
return nil, err
}
pairs := b.GetEnabledPairs(assetType)
for i := range pairs {
Expand All @@ -270,7 +269,7 @@ func (b *Binance) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Pr
if tick[y].Symbol != pairFmt {
continue
}
tickerPrice := ticker.Price{
tickerPrice := &ticker.Price{
Last: tick[y].LastPrice,
High: tick[y].HighPrice,
Low: tick[y].LowPrice,
Expand All @@ -282,7 +281,7 @@ func (b *Binance) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Pr
Close: tick[y].PrevClosePrice,
Pair: pairs[i],
}
err = ticker.ProcessTicker(b.Name, &tickerPrice, assetType)
err = ticker.ProcessTicker(b.Name, tickerPrice, assetType)
if err != nil {
log.Error(log.Ticker, err)
}
Expand All @@ -292,7 +291,7 @@ func (b *Binance) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Pr
}

// FetchTicker returns the ticker for a currency pair
func (b *Binance) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
func (b *Binance) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerNew, err := ticker.GetTicker(b.Name, p, assetType)
if err != nil {
return b.UpdateTicker(p, assetType)
Expand All @@ -301,7 +300,7 @@ func (b *Binance) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Pri
}

// FetchOrderbook returns orderbook base on the currency pair
func (b *Binance) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
func (b *Binance) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(b.Name, p, assetType)
if err != nil {
return b.UpdateOrderbook(p, assetType)
Expand All @@ -310,8 +309,8 @@ func (b *Binance) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbo
}

// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Binance) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
var orderBook orderbook.Base
func (b *Binance) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
orderBook := new(orderbook.Base)
orderbookNew, err := b.GetOrderBook(OrderBookDataRequestParams{Symbol: b.FormatExchangeCurrency(p,
assetType).String(), Limit: 1000})
if err != nil {
Expand Down
Loading

0 comments on commit 75ac5ee

Please sign in to comment.