Skip to content

Commit

Permalink
Assets: Fix LinearContracts support
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 22, 2024
1 parent 1fab9c7 commit 9baf46f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 81 deletions.
15 changes: 11 additions & 4 deletions exchanges/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -67,6 +69,7 @@ const (
options = "options"
optionCombo = "option_combo"
futureCombo = "future_combo"
linearContract = "linearcontract"
all = "all"
)

Expand Down Expand Up @@ -118,6 +121,8 @@ func (a Item) String() string {
return optionCombo
case FutureCombo:
return futureCombo
case LinearContract:
return linearContract
case All:
return all
default:
Expand Down Expand Up @@ -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:
Expand Down
86 changes: 9 additions & 77 deletions exchanges/asset/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 13 in exchanges/asset/asset_test.go

View workflow job for this annotation

GitHub Actions / lint

assignOp: replace `a = a << 1` with `a <<= 1` (gocritic)
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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")

Check failure on line 123 in exchanges/asset/asset_test.go

View workflow job for this annotation

GitHub Actions / lint

formatter: assert.Falsef format %s reads arg #1, but call has 0 args (testifylint)
}
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")

Check failure on line 126 in exchanges/asset/asset_test.go

View workflow job for this annotation

GitHub Actions / lint

formatter: assert.Truef format %s reads arg #1, but call has 0 args (testifylint)
}
}

Expand Down

0 comments on commit 9baf46f

Please sign in to comment.