deribit: Update FetchTradablePairs to not need a currency code (#1791)

* deribit: Update FetchTradablePairs to not need a currency code

* gk: nits

* t.Context() usage in tests

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2025-04-14 11:22:51 +10:00
committed by GitHub
parent cad7586e98
commit b91d11e735
4 changed files with 378 additions and 389 deletions

View File

@@ -356,21 +356,19 @@ func (d *Deribit) GetInstrument(ctx context.Context, instrument string) (*Instru
}
// GetInstruments gets data for all available instruments
func (d *Deribit) GetInstruments(ctx context.Context, ccy currency.Code, kind string, expired bool) ([]InstrumentData, error) {
if ccy.IsEmpty() {
return nil, currency.ErrCurrencyCodeEmpty
}
func (d *Deribit) GetInstruments(ctx context.Context, ccy currency.Code, kind string, expired bool) ([]*InstrumentData, error) {
params := url.Values{}
params.Set("currency", ccy.String())
if !ccy.IsEmpty() {
params.Set("currency", ccy.String())
}
if kind != "" {
params.Set("kind", kind)
}
if expired {
params.Set("expired", "true")
}
var resp []InstrumentData
return resp, d.SendHTTPRequest(ctx, exchange.RestFutures, nonMatchingEPL,
common.EncodeURLValues(getInstruments, params), &resp)
var resp []*InstrumentData
return resp, d.SendHTTPRequest(ctx, exchange.RestFutures, nonMatchingEPL, common.EncodeURLValues(getInstruments, params), &resp)
}
// GetLastSettlementsByCurrency gets last settlement data by currency

File diff suppressed because it is too large Load Diff

View File

@@ -200,30 +200,22 @@ func (d *Deribit) FetchTradablePairs(ctx context.Context, assetType asset.Item)
if !d.SupportsAsset(assetType) {
return nil, fmt.Errorf("%s: %w - %v", d.Name, asset.ErrNotSupported, assetType)
}
var resp currency.Pairs
for _, x := range baseCurrencies {
var instrumentsData []InstrumentData
var err error
if d.Websocket.IsConnected() {
instrumentsData, err = d.WSRetrieveInstrumentsData(currency.NewCode(x), d.GetAssetKind(assetType), false)
} else {
instrumentsData, err = d.GetInstruments(ctx, currency.NewCode(x), d.GetAssetKind(assetType), false)
instruments, err := d.GetInstruments(ctx, currency.EMPTYCODE, d.GetAssetKind(assetType), false)
if err != nil {
return nil, err
}
resp := make(currency.Pairs, 0, len(instruments))
for _, inst := range instruments {
if !inst.IsActive {
continue
}
cp, err := currency.NewPairFromString(inst.InstrumentName)
if err != nil {
return nil, err
}
for y := range instrumentsData {
if !instrumentsData[y].IsActive {
continue
}
var cp currency.Pair
cp, err = currency.NewPairFromString(instrumentsData[y].InstrumentName)
if err != nil {
return nil, err
}
resp = resp.Add(cp)
}
resp = resp.Add(cp)
}
return resp, nil
}
@@ -1155,7 +1147,7 @@ func (d *Deribit) GetFuturesContractDetails(ctx context.Context, item asset.Item
}
resp := []futures.Contract{}
for _, ccy := range baseCurrencies {
var marketSummary []InstrumentData
var marketSummary []*InstrumentData
var err error
if d.Websocket.IsConnected() {
marketSummary, err = d.WSRetrieveInstrumentsData(currency.NewCode(ccy), d.GetAssetKind(item), false)
@@ -1165,17 +1157,16 @@ func (d *Deribit) GetFuturesContractDetails(ctx context.Context, item asset.Item
if err != nil {
return nil, err
}
for i := range marketSummary {
if marketSummary[i].Kind != "future" && marketSummary[i].Kind != "future_combo" {
for _, inst := range marketSummary {
if inst.Kind != "future" && inst.Kind != "future_combo" {
continue
}
var cp currency.Pair
cp, err = currency.NewPairFromString(marketSummary[i].InstrumentName)
cp, err := currency.NewPairFromString(inst.InstrumentName)
if err != nil {
return nil, err
}
var ct futures.ContractType
switch marketSummary[i].SettlementPeriod {
switch inst.SettlementPeriod {
case "day":
ct = futures.Daily
case "week":
@@ -1186,7 +1177,7 @@ func (d *Deribit) GetFuturesContractDetails(ctx context.Context, item asset.Item
ct = futures.Perpetual
}
var contractSettlementType futures.ContractSettlementType
if marketSummary[i].InstrumentType == "reversed" {
if inst.InstrumentType == "reversed" {
contractSettlementType = futures.Inverse
} else {
contractSettlementType = futures.Linear
@@ -1194,16 +1185,16 @@ func (d *Deribit) GetFuturesContractDetails(ctx context.Context, item asset.Item
resp = append(resp, futures.Contract{
Exchange: d.Name,
Name: cp,
Underlying: currency.NewPair(currency.NewCode(marketSummary[i].BaseCurrency), currency.NewCode(marketSummary[i].QuoteCurrency)),
Underlying: currency.NewPair(currency.NewCode(inst.BaseCurrency), currency.NewCode(inst.QuoteCurrency)),
Asset: item,
SettlementCurrencies: []currency.Code{currency.NewCode(marketSummary[i].SettlementCurrency)},
StartDate: marketSummary[i].CreationTimestamp.Time(),
EndDate: marketSummary[i].ExpirationTimestamp.Time(),
SettlementCurrencies: []currency.Code{currency.NewCode(inst.SettlementCurrency)},
StartDate: inst.CreationTimestamp.Time(),
EndDate: inst.ExpirationTimestamp.Time(),
Type: ct,
SettlementType: contractSettlementType,
IsActive: marketSummary[i].IsActive,
MaxLeverage: marketSummary[i].MaxLeverage,
Multiplier: marketSummary[i].ContractSize,
IsActive: inst.IsActive,
MaxLeverage: inst.MaxLeverage,
Multiplier: inst.ContractSize,
})
}
}
@@ -1216,7 +1207,7 @@ func (d *Deribit) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item)
return fmt.Errorf("%s: %w - %v", d.Name, asset.ErrNotSupported, a)
}
for _, x := range baseCurrencies {
var instrumentsData []InstrumentData
var instrumentsData []*InstrumentData
var err error
if d.Websocket.IsConnected() {
instrumentsData, err = d.WSRetrieveInstrumentsData(currency.NewCode(x), d.GetAssetKind(a), false)
@@ -1230,17 +1221,17 @@ func (d *Deribit) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item)
}
limits := make([]order.MinMaxLevel, len(instrumentsData))
for x := range instrumentsData {
for x, inst := range instrumentsData {
var pair currency.Pair
pair, err = currency.NewPairFromString(instrumentsData[x].InstrumentName)
pair, err = currency.NewPairFromString(inst.InstrumentName)
if err != nil {
return err
}
limits[x] = order.MinMaxLevel{
Pair: pair,
Asset: a,
PriceStepIncrementSize: instrumentsData[x].TickSize,
MinimumBaseAmount: instrumentsData[x].MinimumTradeAmount,
PriceStepIncrementSize: inst.TickSize,
MinimumBaseAmount: inst.MinimumTradeAmount,
}
}
err = d.LoadLimits(limits)

View File

@@ -232,7 +232,7 @@ func (d *Deribit) WSRetrieveInstrumentData(instrument string) (*InstrumentData,
}
// WSRetrieveInstrumentsData gets data for all available instruments
func (d *Deribit) WSRetrieveInstrumentsData(ccy currency.Code, kind string, expired bool) ([]InstrumentData, error) {
func (d *Deribit) WSRetrieveInstrumentsData(ccy currency.Code, kind string, expired bool) ([]*InstrumentData, error) {
if ccy.IsEmpty() {
return nil, currency.ErrCurrencyCodeEmpty
}
@@ -245,7 +245,7 @@ func (d *Deribit) WSRetrieveInstrumentsData(ccy currency.Code, kind string, expi
Expired: expired,
Kind: kind,
}
var resp []InstrumentData
var resp []*InstrumentData
return resp, d.SendWSRequest(nonMatchingEPL, getInstruments, input, &resp, false)
}