Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
fix bot panicing when it cannot cast data from ccxtExchange, fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsaraf committed Oct 22, 2018
1 parent 1a62077 commit 0ccbc49
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions plugins/ccxtExchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,18 @@ func (c ccxtExchange) GetTickerPrice(pairs []model.TradingPair) (map[model.Tradi
return nil, fmt.Errorf("error while fetching ticker price for trading pair %s: %s", pairsMap[p], e)
}

askPrice, e := utils.CheckFetchFloat(tickerMap, "ask")
if e != nil {
return nil, fmt.Errorf("unable to correctly fetch value from tickerMap: %s", e)
}
bidPrice, e := utils.CheckFetchFloat(tickerMap, "bid")
if e != nil {
return nil, fmt.Errorf("unable to correctly fetch value from tickerMap: %s", e)
}

priceResult[p] = api.Ticker{
AskPrice: model.NumberFromFloat(tickerMap["ask"].(float64), c.precision),
BidPrice: model.NumberFromFloat(tickerMap["bid"].(float64), c.precision),
AskPrice: model.NumberFromFloat(askPrice, c.precision),
BidPrice: model.NumberFromFloat(bidPrice, c.precision),
}
}

Expand Down
15 changes: 15 additions & 0 deletions support/utils/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,18 @@ func AssetsEqual(baseAsset base.Asset, horizonAsset horizon.Asset) bool {
horizonAsset.Code == baseAsset.Code &&
horizonAsset.Issuer == baseAsset.Issuer
}

// CheckFetchFloat tries to fetch and then cast the value for the provided key
func CheckFetchFloat(m map[string]interface{}, key string) (float64, error) {
v, ok := m[key]
if !ok {
return 0.0, fmt.Errorf("'%s' field not in map", key)
}

f, ok := v.(float64)
if !ok {
return 0.0, fmt.Errorf("unable to cast '%s' field to float64, value: %v", key, v)
}

return f, nil
}

0 comments on commit 0ccbc49

Please sign in to comment.