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

Commit 5392d3e

Browse files
committed
fix fill tracking OrderAction to correctly reflect direction of trade (#200), closes #193
uses /operations to get buyer and seller data which is missing in /trades endpoint
1 parent b08b6b7 commit 5392d3e

1 file changed

Lines changed: 48 additions & 13 deletions

File tree

plugins/sdex.go

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"math"
7+
"net/http"
78
"reflect"
89
"strconv"
910
"strings"
@@ -15,6 +16,7 @@ import (
1516
"github.com/stellar/go/clients/horizon"
1617
"github.com/stellar/kelp/api"
1718
"github.com/stellar/kelp/model"
19+
"github.com/stellar/kelp/support/networking"
1820
"github.com/stellar/kelp/support/utils"
1921
)
2022

@@ -562,9 +564,9 @@ func (sdex *SDEX) GetTradeHistory(pair model.TradingPair, maybeCursorStart inter
562564
}
563565
}
564566

565-
func (sdex *SDEX) getOrderAction(baseAsset horizon.Asset, quoteAsset horizon.Asset, trade horizon.Trade) *model.OrderAction {
567+
func (sdex *SDEX) getOrderAction(baseAsset horizon.Asset, quoteAsset horizon.Asset, trade horizon.Trade) (*model.OrderAction, error) {
566568
if trade.BaseAccount != sdex.TradingAccount && trade.CounterAccount != sdex.TradingAccount {
567-
return nil
569+
return nil, nil
568570
}
569571

570572
tradeBaseAsset := utils.Native
@@ -577,23 +579,53 @@ func (sdex *SDEX) getOrderAction(baseAsset horizon.Asset, quoteAsset horizon.Ass
577579
}
578580
sdexBaseAsset := utils.Asset2String(baseAsset)
579581
sdexQuoteAsset := utils.Asset2String(quoteAsset)
582+
if !(sdexBaseAsset == tradeBaseAsset && sdexQuoteAsset == tradeQuoteAsset) && !(sdexBaseAsset == tradeQuoteAsset && sdexQuoteAsset == tradeBaseAsset) {
583+
return nil, nil
584+
}
585+
586+
var output map[string]interface{}
587+
e := networking.JSONRequest(http.DefaultClient, "GET", trade.Links.Operation.Href, "", map[string]string{}, &output, "error")
588+
if e != nil {
589+
return nil, fmt.Errorf("could not get operation related to trade to fetch orderAction (URL=%s): %s", trade.Links.Operation.Href, e)
590+
}
591+
sourceAccount, ok := output["source_account"].(string)
592+
if !ok {
593+
return nil, fmt.Errorf("could not cast 'source_account' to a string in the operation json result: %s (type=%T)", output["source_account"], output["source_account"])
594+
}
595+
sourceSellingAsset := utils.Native
596+
sourceSellingAssetType, ok := output["selling_asset_type"].(string)
597+
if !ok {
598+
return nil, fmt.Errorf("could not cast 'selling_asset_type' to a string in the operation json result: %s (type=%T)", output["selling_asset_type"], output["selling_asset_type"])
599+
}
600+
if sourceSellingAssetType != utils.Native {
601+
sourceSellingAssetCode, ok := output["selling_asset_code"].(string)
602+
if !ok {
603+
return nil, fmt.Errorf("could not cast 'selling_asset_code' to a string in the operation json result: %s (type=%T)", output["selling_asset_code"], output["selling_asset_code"])
604+
}
605+
sourceSellingAssetIssuer, ok := output["selling_asset_issuer"].(string)
606+
if !ok {
607+
return nil, fmt.Errorf("could not cast 'selling_asset_issuer' to a string in the operation json result: %s (type=%T)", output["selling_asset_issuer"], output["selling_asset_issuer"])
608+
}
609+
sourceSellingAsset = sourceSellingAssetCode + ":" + sourceSellingAssetIssuer
610+
}
580611

581612
// compare the base and quote asset on the trade to what we are using as our base and quote
582613
// then compare whether it was the base or the quote that was the seller
583614
actionSell := model.OrderActionSell
584615
actionBuy := model.OrderActionBuy
585-
if sdexBaseAsset == tradeBaseAsset && sdexQuoteAsset == tradeQuoteAsset {
586-
if trade.BaseIsSeller {
587-
return &actionSell
588-
}
589-
return &actionBuy
590-
} else if sdexBaseAsset == tradeQuoteAsset && sdexQuoteAsset == tradeBaseAsset {
591-
if trade.BaseIsSeller {
592-
return &actionBuy
616+
if sourceAccount == sdex.TradingAccount {
617+
if sdexBaseAsset == sourceSellingAsset {
618+
return &actionSell, nil
619+
} else {
620+
return &actionBuy, nil
593621
}
594-
return &actionSell
622+
}
623+
624+
// else
625+
if sdexBaseAsset == sourceSellingAsset {
626+
return &actionBuy, nil
595627
} else {
596-
return nil
628+
return &actionSell, nil
597629
}
598630
}
599631

@@ -603,7 +635,10 @@ func (sdex *SDEX) tradesPage2TradeHistoryResult(baseAsset horizon.Asset, quoteAs
603635
trades := []model.Trade{}
604636

605637
for _, t := range tradesPage.Embedded.Records {
606-
orderAction := sdex.getOrderAction(baseAsset, quoteAsset, t)
638+
orderAction, e := sdex.getOrderAction(baseAsset, quoteAsset, t)
639+
if e != nil {
640+
return nil, false, fmt.Errorf("could not load orderAction: %s", e)
641+
}
607642
if orderAction == nil {
608643
// we have encountered a trade that is different from the base and quote asset for our trading account
609644
continue

0 commit comments

Comments
 (0)