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

Commit

Permalink
fix fill tracking OrderAction to correctly reflect direction of trade (
Browse files Browse the repository at this point in the history
…#200), closes #193

uses /operations to get buyer and seller data which is missing in /trades endpoint
  • Loading branch information
nikhilsaraf authored Jul 18, 2019
1 parent 4d8d066 commit 8ec0f64
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions plugins/sdex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"math"
"net/http"
"reflect"
"strconv"
"strings"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/stellar/go/clients/horizon"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/support/networking"
"github.com/stellar/kelp/support/utils"
)

Expand Down Expand Up @@ -562,9 +564,9 @@ func (sdex *SDEX) GetTradeHistory(pair model.TradingPair, maybeCursorStart inter
}
}

func (sdex *SDEX) getOrderAction(baseAsset horizon.Asset, quoteAsset horizon.Asset, trade horizon.Trade) *model.OrderAction {
func (sdex *SDEX) getOrderAction(baseAsset horizon.Asset, quoteAsset horizon.Asset, trade horizon.Trade) (*model.OrderAction, error) {
if trade.BaseAccount != sdex.TradingAccount && trade.CounterAccount != sdex.TradingAccount {
return nil
return nil, nil
}

tradeBaseAsset := utils.Native
Expand All @@ -577,23 +579,53 @@ func (sdex *SDEX) getOrderAction(baseAsset horizon.Asset, quoteAsset horizon.Ass
}
sdexBaseAsset := utils.Asset2String(baseAsset)
sdexQuoteAsset := utils.Asset2String(quoteAsset)
if !(sdexBaseAsset == tradeBaseAsset && sdexQuoteAsset == tradeQuoteAsset) && !(sdexBaseAsset == tradeQuoteAsset && sdexQuoteAsset == tradeBaseAsset) {
return nil, nil
}

var output map[string]interface{}
e := networking.JSONRequest(http.DefaultClient, "GET", trade.Links.Operation.Href, "", map[string]string{}, &output, "error")
if e != nil {
return nil, fmt.Errorf("could not get operation related to trade to fetch orderAction (URL=%s): %s", trade.Links.Operation.Href, e)
}
sourceAccount, ok := output["source_account"].(string)
if !ok {
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"])
}
sourceSellingAsset := utils.Native
sourceSellingAssetType, ok := output["selling_asset_type"].(string)
if !ok {
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"])
}
if sourceSellingAssetType != utils.Native {
sourceSellingAssetCode, ok := output["selling_asset_code"].(string)
if !ok {
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"])
}
sourceSellingAssetIssuer, ok := output["selling_asset_issuer"].(string)
if !ok {
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"])
}
sourceSellingAsset = sourceSellingAssetCode + ":" + sourceSellingAssetIssuer
}

// compare the base and quote asset on the trade to what we are using as our base and quote
// then compare whether it was the base or the quote that was the seller
actionSell := model.OrderActionSell
actionBuy := model.OrderActionBuy
if sdexBaseAsset == tradeBaseAsset && sdexQuoteAsset == tradeQuoteAsset {
if trade.BaseIsSeller {
return &actionSell
}
return &actionBuy
} else if sdexBaseAsset == tradeQuoteAsset && sdexQuoteAsset == tradeBaseAsset {
if trade.BaseIsSeller {
return &actionBuy
if sourceAccount == sdex.TradingAccount {
if sdexBaseAsset == sourceSellingAsset {
return &actionSell, nil
} else {
return &actionBuy, nil
}
return &actionSell
}

// else
if sdexBaseAsset == sourceSellingAsset {
return &actionBuy, nil
} else {
return nil
return &actionSell, nil
}
}

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

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

0 comments on commit 8ec0f64

Please sign in to comment.