From 6ee26a7da1589182e6788b6c81d43b8a2d2ede43 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 6 Mar 2025 15:17:05 +1100 Subject: [PATCH] exchanges/margin: Fix marshalling issue (#1812) * Fixes issue with marshalling margin types * Update exchanges/order/order_test.go Co-authored-by: Gareth Kirwan * Update exchanges/margin/margin.go Co-authored-by: Gareth Kirwan * Update exchanges/order/order_test.go Co-authored-by: Gareth Kirwan * Update test name --------- Co-authored-by: Gareth Kirwan --- exchanges/margin/margin.go | 8 ++++++-- exchanges/margin/margin_test.go | 27 ++++++++++++++++++++++++++- exchanges/order/order_test.go | 21 +++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/exchanges/margin/margin.go b/exchanges/margin/margin.go index ce6386e7..d8d5007f 100644 --- a/exchanges/margin/margin.go +++ b/exchanges/margin/margin.go @@ -23,6 +23,11 @@ func (t *Type) UnmarshalJSON(d []byte) error { return err } +// MarshalJSON conforms type to the json.Marshaler interface +func (t Type) MarshalJSON() ([]byte, error) { + return json.Marshal(t.String()) +} + // String returns the string representation of the margin type in lowercase // the absence of a lower func should hopefully highlight that String is lower func (t Type) String() string { @@ -37,10 +42,9 @@ func (t Type) String() string { return spotIsolatedStr case NoMargin: return cashStr - case Unknown: + default: return unknownStr } - return "" } // Upper returns the upper case string representation of the margin type diff --git a/exchanges/margin/margin_test.go b/exchanges/margin/margin_test.go index 32fbe561..f0401b5f 100644 --- a/exchanges/margin/margin_test.go +++ b/exchanges/margin/margin_test.go @@ -1,6 +1,7 @@ package margin import ( + "fmt" "strings" "testing" @@ -33,6 +34,7 @@ func TestUnmarshalJSON(t *testing.T) { "spotIsolated": {`{"margin":"spot_isolated"}`, SpotIsolated, nil}, "invalid": {`{"margin":"hello moto"}`, Unknown, ErrInvalidMarginType}, "unset": {`{"margin":""}`, Unset, nil}, + "": {`{"margin":""}`, Unset, nil}, } { t.Run(name, func(t *testing.T) { t.Parallel() @@ -46,6 +48,28 @@ func TestUnmarshalJSON(t *testing.T) { } } +func TestMarshalJSON(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + in Type + want string + }{ + {Isolated, fmt.Sprintf(`%q`, isolatedStr)}, + {Multi, fmt.Sprintf(`%q`, multiStr)}, + {NoMargin, fmt.Sprintf(`%q`, cashStr)}, + {SpotIsolated, fmt.Sprintf(`%q`, spotIsolatedStr)}, + {Type(uint8(123)), fmt.Sprintf(`%q`, unknownStr)}, + {Unset, fmt.Sprintf(`%q`, unsetStr)}, + } { + t.Run(tc.want, func(t *testing.T) { + t.Parallel() + resp, err := json.Marshal(tc.in) + require.NoError(t, err) + assert.Equal(t, tc.want, string(resp)) + }) + } +} + func TestString(t *testing.T) { t.Parallel() assert.Equal(t, unknownStr, Unknown.String()) @@ -54,7 +78,8 @@ func TestString(t *testing.T) { assert.Equal(t, unsetStr, Unset.String()) assert.Equal(t, spotIsolatedStr, SpotIsolated.String()) assert.Equal(t, cashStr, NoMargin.String()) - assert.Equal(t, "", Type(30).String()) + assert.Equal(t, unknownStr, Type(30).String()) + assert.Equal(t, "", Unset.String()) } func TestUpper(t *testing.T) { diff --git a/exchanges/order/order_test.go b/exchanges/order/order_test.go index d78a61ef..27e4b1a5 100644 --- a/exchanges/order/order_test.go +++ b/exchanges/order/order_test.go @@ -16,6 +16,7 @@ import ( "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/encoding/json" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" + "github.com/thrasher-corp/gocryptotrader/exchanges/margin" "github.com/thrasher-corp/gocryptotrader/exchanges/protocol" "github.com/thrasher-corp/gocryptotrader/exchanges/validate" ) @@ -2192,3 +2193,23 @@ func TestTrackingModeString(t *testing.T) { require.Equal(t, v, k.String()) } } + +func TestMarshalOrder(t *testing.T) { + t.Parallel() + btx := currency.NewBTCUSDT() + btx.Delimiter = "-" + orderSubmit := Submit{ + Exchange: "test", + Pair: btx, + AssetType: asset.Spot, + MarginType: margin.Multi, + Side: Buy, + Type: Market, + Amount: 1, + Price: 1000, + } + j, err := json.Marshal(orderSubmit) + require.NoError(t, err, "json.Marshal must not error") + exp := []byte(`{"Exchange":"test","Type":4,"Side":"BUY","Pair":"BTC-USDT","AssetType":"spot","ImmediateOrCancel":false,"FillOrKill":false,"PostOnly":false,"ReduceOnly":false,"Leverage":0,"Price":1000,"Amount":1,"QuoteAmount":0,"TriggerPrice":0,"TriggerPriceType":0,"ClientID":"","ClientOrderID":"","AutoBorrow":false,"MarginType":"multi","RetrieveFees":false,"RetrieveFeeDelay":0,"RiskManagementModes":{"Mode":"","TakeProfit":{"Enabled":false,"TriggerPriceType":0,"Price":0,"LimitPrice":0,"OrderType":0},"StopLoss":{"Enabled":false,"TriggerPriceType":0,"Price":0,"LimitPrice":0,"OrderType":0},"StopEntry":{"Enabled":false,"TriggerPriceType":0,"Price":0,"LimitPrice":0,"OrderType":0}},"Hidden":false,"Iceberg":false,"TrackingMode":0,"TrackingValue":0}`) + assert.Equal(t, exp, j) +}