From 9baf46f1cb10746fce0e29bb31ac8783ca87e12f Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Fri, 22 Nov 2024 15:13:30 +0700 Subject: [PATCH] Assets: Fix LinearContracts support --- exchanges/asset/asset.go | 15 ++++-- exchanges/asset/asset_test.go | 86 ++++------------------------------- 2 files changed, 20 insertions(+), 81 deletions(-) diff --git a/exchanges/asset/asset.go b/exchanges/asset/asset.go index e229dffeab6..873da987cf2 100644 --- a/exchanges/asset/asset.go +++ b/exchanges/asset/asset.go @@ -20,10 +20,10 @@ type Item uint32 // Items stores a list of assets types type Items []Item -// Const vars for asset package +// Supported Assets const ( Empty Item = 0 - Spot Item = 1 << iota + Spot Item = 1 << (iota - 1) Margin CrossMargin MarginFunding @@ -41,9 +41,11 @@ const ( Options OptionCombo FutureCombo - LinearContract // Added to represent a USDT and USDC based linear derivatives(futures/perpetual) assets in Bybit V5 - All + LinearContract // A USDT or USDC base linear derivatives (futures/perpetual) + All // Must come immediately after all valid assets +) +const ( optionsFlag = OptionCombo | Options futuresFlag = PerpetualContract | PerpetualSwap | Futures | DeliveryFutures | UpsideProfitContract | DownsideProfitContract | CoinMarginedFutures | USDTMarginedFutures | USDCMarginedFutures | LinearContract | FutureCombo supportedFlag = Spot | Margin | CrossMargin | MarginFunding | Index | Binary | PerpetualContract | PerpetualSwap | Futures | DeliveryFutures | UpsideProfitContract | DownsideProfitContract | CoinMarginedFutures | USDTMarginedFutures | USDCMarginedFutures | Options | LinearContract | OptionCombo | FutureCombo @@ -67,6 +69,7 @@ const ( options = "options" optionCombo = "option_combo" futureCombo = "future_combo" + linearContract = "linearcontract" all = "all" ) @@ -118,6 +121,8 @@ func (a Item) String() string { return optionCombo case FutureCombo: return futureCombo + case LinearContract: + return linearContract case All: return all default: @@ -224,6 +229,8 @@ func New(input string) (Item, error) { return OptionCombo, nil case futureCombo: return FutureCombo, nil + case linearContract: + return LinearContract, nil case all: return All, nil default: diff --git a/exchanges/asset/asset_test.go b/exchanges/asset/asset_test.go index 2e8ba1a0577..73a1ae5a5e9 100644 --- a/exchanges/asset/asset_test.go +++ b/exchanges/asset/asset_test.go @@ -10,15 +10,10 @@ import ( func TestString(t *testing.T) { t.Parallel() - a := Spot - if a.String() != "spot" { - t.Fatal("TestString returned an unexpected result") - } - - a = 0 - if a.String() != "" { - t.Fatal("TestString returned an unexpected result") + for a := Item(1); a <= All; a = a << 1 { + assert.NotEmptyf(t, a.String(), "%s.String should return non-empty", a) } + assert.Empty(t, Empty.String(), "Empty.String should return empty") } func TestStrings(t *testing.T) { @@ -90,8 +85,9 @@ func TestNew(t *testing.T) { {Input: "Options", Expected: Options}, {Input: "Option", Expected: Options}, {Input: "Future", Error: ErrNotSupported}, - {Input: "future_combo", Expected: FutureCombo}, {Input: "option_combo", Expected: OptionCombo}, + {Input: "future_combo", Expected: FutureCombo}, + {Input: "linearContract", Expected: LinearContract}, } for _, tt := range cases { @@ -123,75 +119,11 @@ func TestSupported(t *testing.T) { func TestIsFutures(t *testing.T) { t.Parallel() - type scenario struct { - item Item - isFutures bool - } - scenarios := []scenario{ - { - item: Spot, - isFutures: false, - }, - { - item: Margin, - isFutures: false, - }, - { - item: MarginFunding, - isFutures: false, - }, - { - item: Index, - isFutures: false, - }, - { - item: Binary, - isFutures: false, - }, - { - item: PerpetualContract, - isFutures: true, - }, - { - item: PerpetualSwap, - isFutures: true, - }, - { - item: Futures, - isFutures: true, - }, - { - item: UpsideProfitContract, - isFutures: true, - }, - { - item: DownsideProfitContract, - isFutures: true, - }, - { - item: CoinMarginedFutures, - isFutures: true, - }, - { - item: USDTMarginedFutures, - isFutures: true, - }, - { - item: USDCMarginedFutures, - isFutures: true, - }, { - item: FutureCombo, - isFutures: true, - }, + for _, a := range []Item{Spot, Margin, MarginFunding, Index, Binary} { + assert.Falsef(t, a.IsFutures(), "%s should return correctly for IsFutures") } - for _, s := range scenarios { - testScenario := s - t.Run(testScenario.item.String(), func(t *testing.T) { - t.Parallel() - if testScenario.item.IsFutures() != testScenario.isFutures { - t.Errorf("expected %v isFutures to be %v", testScenario.item, testScenario.isFutures) - } - }) + for _, a := range []Item{PerpetualContract, PerpetualSwap, Futures, UpsideProfitContract, DownsideProfitContract, CoinMarginedFutures, USDTMarginedFutures, USDCMarginedFutures, FutureCombo} { + assert.Truef(t, a.IsFutures(), "%s should return correctly for IsFutures") } }