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

Commit f6d31c2

Browse files
authored
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
1 parent a8dbcf4 commit f6d31c2

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

plugins/sdex.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,25 @@ func (sdex *SDEX) GetTradeHistory(pair model.TradingPair, maybeCursorStart inter
561561
Trades: trades,
562562
}, nil
563563
}
564+
565+
if strings.Contains(e.Error(), "Resource Missing") {
566+
eAsset := sdex.checkAssetExists(baseAsset)
567+
if eAsset != nil {
568+
return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (baseAssetError: %s)", e, eAsset)
569+
}
570+
571+
eAsset = sdex.checkAssetExists(quoteAsset)
572+
if eAsset != nil {
573+
return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (quoteAssetError: %s)", e, eAsset)
574+
}
575+
576+
log.Printf("received a Resource Missing error while fetching trades, treating as if no trades exist for this trading pair and continuing: %s", e)
577+
return &api.TradeHistoryResult{
578+
Cursor: cursorStart,
579+
Trades: trades,
580+
}, nil
581+
}
582+
564583
return nil, fmt.Errorf("error while fetching trades in SDEX (cursor=%s): %s", cursorStart, e)
565584
}
566585

@@ -782,7 +801,7 @@ func (sdex *SDEX) tradesPage2TradeHistoryResult(baseAsset hProtocol.Asset, quote
782801
func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) {
783802
baseAsset, quoteAsset, e := sdex.Assets()
784803
if e != nil {
785-
return nil, fmt.Errorf("error while convertig pair to base and quote asset: %s", e)
804+
return nil, fmt.Errorf("error while converting pair to base and quote asset: %s", e)
786805
}
787806

788807
tradeReq := horizonclient.TradeRequest{
@@ -798,6 +817,20 @@ func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) {
798817

799818
tradesPage, e := sdex.API.Trades(tradeReq)
800819
if e != nil {
820+
if strings.Contains(e.Error(), "Resource Missing") {
821+
eAsset := sdex.checkAssetExists(baseAsset)
822+
if eAsset != nil {
823+
return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (baseAssetError: %s)", e, eAsset)
824+
}
825+
826+
eAsset = sdex.checkAssetExists(quoteAsset)
827+
if eAsset != nil {
828+
return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s (quoteAssetError: %s)", e, eAsset)
829+
}
830+
831+
log.Printf("received a Resource Missing error while fetching trades, treating as if no trades exist for this trading pair and continuing: %s", e)
832+
return nil, nil
833+
}
801834
return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s", e)
802835
}
803836

@@ -810,6 +843,25 @@ func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) {
810843
return records[0].PT, nil
811844
}
812845

846+
func (sdex *SDEX) checkAssetExists(asset hProtocol.Asset) error {
847+
req := horizonclient.AssetRequest{
848+
ForAssetCode: asset.Code,
849+
ForAssetIssuer: asset.Issuer,
850+
Limit: uint(1),
851+
}
852+
853+
baseAssetPage, e := sdex.API.Assets(req)
854+
if e != nil {
855+
return fmt.Errorf("error fetching asset '%s:%s': %s", asset.Code, asset.Issuer, e)
856+
}
857+
858+
if len(baseAssetPage.Embedded.Records) == 0 {
859+
return fmt.Errorf("asset '%s:%s' did not exist", asset.Code, asset.Issuer)
860+
}
861+
862+
return nil
863+
}
864+
813865
// GetOrderBook gets the SDEX orderbook
814866
func (sdex *SDEX) GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error) {
815867
if pair != sdex.pair {

0 commit comments

Comments
 (0)