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 23, 2024
1 parent 1fab9c7 commit 6f955c5
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 // Derivatives with a linear Base (e.g. USDT or USDC)
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 <<= 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) {
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", a)
}
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", a)
}
}

Expand Down

0 comments on commit 6f955c5

Please sign in to comment.