Skip to content

Commit

Permalink
Exchange: Poloniex fixes (thrasher-corp#537)
Browse files Browse the repository at this point in the history
* fix panic bug where item type was different intermittently

* fix nonce bug where it requires an actual timestamp for each consecutive call

* add in default case

* Added in type conversion check and check dataset length to continue
  • Loading branch information
shazbert authored Aug 14, 2020
1 parent 049f18e commit e242483
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
3 changes: 1 addition & 2 deletions exchanges/poloniex/poloniex.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ func (p *Poloniex) SendAuthenticatedHTTPRequest(method, endpoint string, values
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Key"] = p.API.Credentials.Key
values.Set("nonce", p.Requester.GetNonce(true).String())
values.Set("nonce", strconv.FormatInt(time.Now().UnixNano(), 10))
values.Set("command", endpoint)

hmac := crypto.GetHMAC(crypto.HashSHA512,
Expand All @@ -803,7 +803,6 @@ func (p *Poloniex) SendAuthenticatedHTTPRequest(method, endpoint string, values
Body: bytes.NewBufferString(values.Encode()),
Result: result,
AuthRequest: true,
NonceEnabled: true,
Verbose: p.Verbose,
HTTPDebugging: p.HTTPDebugging,
HTTPRecording: p.HTTPRecording,
Expand Down
25 changes: 21 additions & 4 deletions exchanges/poloniex/poloniex_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,28 @@ func (p *Poloniex) wsHandleData(respRaw []byte) error {
currencyPair := currencyIDMap[channelID]
var trade WsTrade
trade.Symbol = currencyIDMap[channelID]
dataL3 := dataL2.([]interface{})
trade.TradeID, err = strconv.ParseInt(dataL3[1].(string), 10, 64)
if err != nil {
return err
dataL3, ok := dataL2.([]interface{})
if !ok {
return errors.New("websocket trade update error: type conversion failure")
}

if len(dataL3) != 6 {
return errors.New("websocket trade update error: incorrect data returned")
}

// tradeID type intermittently changes
switch t := dataL3[1].(type) {
case string:
trade.TradeID, err = strconv.ParseInt(t, 10, 64)
if err != nil {
return err
}
case float64:
trade.TradeID = int64(t)
default:
return fmt.Errorf("unhandled type for websocket trade update: %v", t)
}

side := order.Buy
if dataL3[2].(float64) != 1 {
side = order.Sell
Expand Down

0 comments on commit e242483

Please sign in to comment.