diff --git a/exchanges/deribit/deribit.go b/exchanges/deribit/deribit.go index f4787df6be4..4bbe7c31fdb 100644 --- a/exchanges/deribit/deribit.go +++ b/exchanges/deribit/deribit.go @@ -7,7 +7,6 @@ import ( "fmt" "net/http" "net/url" - "regexp" "strconv" "strings" "time" @@ -28,12 +27,6 @@ type Deribit struct { exchange.Base } -var ( - // optionRegex compiles optionDecimalRegex at startup and is used to help set - // option currency lower-case d eg MATIC-USDC-3JUN24-0d64-P - optionRegex *regexp.Regexp -) - const ( deribitAPIVersion = "/api/v2" tradeBaseURL = "https://www.deribit.com/" @@ -43,8 +36,7 @@ const ( tradeFuturesCombo = "futures-spreads/" tradeOptionsCombo = "combos/" - perpString = "PERPETUAL" - optionDecimalRegex = `\d+(D)\d+` + perpString = "PERPETUAL" // Public endpoints // Market Data @@ -2837,20 +2829,19 @@ func (d *Deribit) formatFuturesTradablePair(pair currency.Pair) string { return instrumentID } -// optionPairToString to format and return an Options currency pairs with the following format: MATIC_USDC-6APR24-0d98-P -// it has both uppercase or lowercase characters, which we can not achieve with the Upper=true or Upper=false +// optionPairToString formats an options pair as: SYMBOL-EXPIRE-STRIKE-TYPE +// SYMBOL may be a currency (BTC) or a pair (XRP_USDC) +// EXPIRE is DDMMMYY +// STRIKE make include a d for decimal point in linear options +// TYPE is Call or Put func (d *Deribit) optionPairToString(pair currency.Pair) string { - subCodes := strings.Split(pair.Quote.String(), currency.DashDelimiter) initialDelimiter := currency.DashDelimiter - if subCodes[0] == "USDC" { + q := pair.Quote.String() + if q[:4] == "USDC" && len(q) > 11 { // Linear option initialDelimiter = currency.UnderscoreDelimiter + // Replace a capital D with d for decimal place in Strike price + // Char 11 is either the hyphen before Strike price or first digit + q = q[:11] + strings.Replace(q[11:], "D", "d", -1) } - for i := range subCodes { - if match := optionRegex.MatchString(subCodes[i]); match { - subCodes[i] = strings.ToLower(subCodes[i]) - break - } - } - - return pair.Base.String() + initialDelimiter + strings.Join(subCodes, currency.DashDelimiter) + return pair.Base.String() + initialDelimiter + q } diff --git a/exchanges/deribit/deribit_test.go b/exchanges/deribit/deribit_test.go index 50bfea11b8e..a926c3d4621 100644 --- a/exchanges/deribit/deribit_test.go +++ b/exchanges/deribit/deribit_test.go @@ -3751,18 +3751,15 @@ func TestFormatFuturesTradablePair(t *testing.T) { func TestOptionPairToString(t *testing.T) { t.Parallel() - optionsList := map[currency.Pair]string{ + for pair, exp := range map[currency.Pair]string{ {Delimiter: currency.DashDelimiter, Base: currency.BTC, Quote: currency.NewCode("30MAY24-61000-C")}: "BTC-30MAY24-61000-C", {Delimiter: currency.DashDelimiter, Base: currency.ETH, Quote: currency.NewCode("1JUN24-3200-P")}: "ETH-1JUN24-3200-P", {Delimiter: currency.DashDelimiter, Base: currency.SOL, Quote: currency.NewCode("USDC-31MAY24-162-P")}: "SOL_USDC-31MAY24-162-P", {Delimiter: currency.DashDelimiter, Base: currency.MATIC, Quote: currency.NewCode("USDC-6APR24-0d98-P")}: "MATIC_USDC-6APR24-0d98-P", - } - for pair, instrumentID := range optionsList { - t.Run(instrumentID, func(t *testing.T) { - t.Parallel() - instrument := d.optionPairToString(pair) - require.Equal(t, instrumentID, instrument) - }) + {Delimiter: currency.DashDelimiter, Base: currency.MATIC, Quote: currency.NewCode("USDC-8JUN24-0D99-P")}: "MATIC_USDC-8JUN24-0d99-P", + {Delimiter: currency.DashDelimiter, Base: currency.MATIC, Quote: currency.NewCode("USDC-6DEC29-0D87-C")}: "MATIC_USDC-6DEC29-0d87-C", + } { + assert.Equal(t, exp, d.optionPairToString(pair), "optionPairToString must return correctly") } } diff --git a/exchanges/deribit/deribit_wrapper.go b/exchanges/deribit/deribit_wrapper.go index 56acd0eb070..0e62d5fbf0e 100644 --- a/exchanges/deribit/deribit_wrapper.go +++ b/exchanges/deribit/deribit_wrapper.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "regexp" "sort" "strconv" "strings" @@ -188,9 +187,6 @@ func (d *Deribit) Setup(exch *config.Exchange) error { return err } - // setup option decimal regex at startup to make constant checks more efficient - optionRegex = regexp.MustCompile(optionDecimalRegex) - return d.Websocket.SetupNewConnection(stream.ConnectionSetup{ URL: d.Websocket.GetWebsocketURL(), ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,