From f6d31c2587c3bc7fb4a68a7e7ed6c1777021f001 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf <1028334+nikhilsaraf@users.noreply.github.com> Date: Sat, 15 Feb 2020 21:13:03 -0800 Subject: [PATCH] workaround empty trades error (#372) (closes #371) * 1 - workaround empty trades response error when fetching latest cursor value * 2 - workaround error in recurring trades call --- plugins/sdex.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/plugins/sdex.go b/plugins/sdex.go index 980661a2a..64b2c03d0 100644 --- a/plugins/sdex.go +++ b/plugins/sdex.go @@ -561,6 +561,25 @@ func (sdex *SDEX) GetTradeHistory(pair model.TradingPair, maybeCursorStart inter Trades: trades, }, nil } + + if strings.Contains(e.Error(), "Resource Missing") { + eAsset := sdex.checkAssetExists(baseAsset) + if eAsset != nil { + return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (baseAssetError: %s)", e, eAsset) + } + + eAsset = sdex.checkAssetExists(quoteAsset) + if eAsset != nil { + return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (quoteAssetError: %s)", e, eAsset) + } + + log.Printf("received a Resource Missing error while fetching trades, treating as if no trades exist for this trading pair and continuing: %s", e) + return &api.TradeHistoryResult{ + Cursor: cursorStart, + Trades: trades, + }, nil + } + return nil, fmt.Errorf("error while fetching trades in SDEX (cursor=%s): %s", cursorStart, e) } @@ -782,7 +801,7 @@ func (sdex *SDEX) tradesPage2TradeHistoryResult(baseAsset hProtocol.Asset, quote func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) { baseAsset, quoteAsset, e := sdex.Assets() if e != nil { - return nil, fmt.Errorf("error while convertig pair to base and quote asset: %s", e) + return nil, fmt.Errorf("error while converting pair to base and quote asset: %s", e) } tradeReq := horizonclient.TradeRequest{ @@ -798,6 +817,20 @@ func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) { tradesPage, e := sdex.API.Trades(tradeReq) if e != nil { + if strings.Contains(e.Error(), "Resource Missing") { + eAsset := sdex.checkAssetExists(baseAsset) + if eAsset != nil { + return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (baseAssetError: %s)", e, eAsset) + } + + eAsset = sdex.checkAssetExists(quoteAsset) + if eAsset != nil { + return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (quoteAssetError: %s)", e, eAsset) + } + + log.Printf("received a Resource Missing error while fetching trades, treating as if no trades exist for this trading pair and continuing: %s", e) + return nil, nil + } return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s", e) } @@ -810,6 +843,25 @@ func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) { return records[0].PT, nil } +func (sdex *SDEX) checkAssetExists(asset hProtocol.Asset) error { + req := horizonclient.AssetRequest{ + ForAssetCode: asset.Code, + ForAssetIssuer: asset.Issuer, + Limit: uint(1), + } + + baseAssetPage, e := sdex.API.Assets(req) + if e != nil { + return fmt.Errorf("error fetching asset '%s:%s': %s", asset.Code, asset.Issuer, e) + } + + if len(baseAssetPage.Embedded.Records) == 0 { + return fmt.Errorf("asset '%s:%s' did not exist", asset.Code, asset.Issuer) + } + + return nil +} + // GetOrderBook gets the SDEX orderbook func (sdex *SDEX) GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error) { if pair != sdex.pair {