Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exchanges: Add Deribit exchange support #1082

Merged
merged 211 commits into from
May 29, 2024
Merged

Conversation

samuael
Copy link
Collaborator

@samuael samuael commented Nov 11, 2022

PR Description

This pull request Included a code implementation for the Deribit exchange version 2; with REST and web-socket support. All the Websocket and Wrapper functions are tested. My code follows the style guidelines of other exchanges of GCT.
Fixed some errors with the help of golangci-lint. Endpoint methods that have no support from the GCT wrapper are also implemented for future use.

Fixes # (issue)

Type of change

Please delete options that are not relevant and add an x in [] as item is complete.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested

For testing the exchange with your own API Credentials you can use your own API key { } and passphrase fields in the unit test deribit_test.go file, or directly add credential information to the config file you will be using for running.

  • go test ./... -race
  • golangci-lint run
  • Test X

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and regenerated documentation via the documentation tool
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally and on Github Actions/AppVeyor with my changes
  • Any dependent changes have been merged and published in downstream modules

@gloriousCode gloriousCode requested a review from a team November 13, 2022 20:25
@gloriousCode gloriousCode added the review me This pull request is ready for review label Nov 13, 2022
Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! While I'm awaiting you to fix the OKx comments. I did a quick review here. Its very exciting having another exchange up for review!

I think you've gone too heavy handed on the API validation. By making the checks so strict, if Derebit ever changes anything, as you can see, you would need to update well over 100 functions. Some validation is fine for API requests, like instrument != "" or "price <= 0", but looking for specific candles intervals, or currencies is too much. Let the API fail the user. I understand that I haven't really emphasised this with the OKx reviews, but the things that you check there are less likely to change than something like a currency, or a candle interval

If you do have repeating validation checks that are specific, such as the time checks, please create validation functions. That way there is only one place where the validation goes and it can be easily updated and is repeated. As a result of this, I've needed to add many comments (not exhaustive) and you've created more work for yourself to address them. As you can see with the time start and end checks, they've also be inconsistently applied, having one function handle all instances reduces the amount of work you need to do

Also looks like there are some implementation issues to work out,see the lint logs.

exchanges/deribit/deribit_wrapper.go Show resolved Hide resolved
exchanges/deribit/deribit_wrapper.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_wrapper.go Show resolved Hide resolved
exchanges/deribit/deribit_wrapper.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_wrapper.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_websocket_eps.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_websocket_eps.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_websocket_eps.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_websocket_eps.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_websocket_eps.go Outdated Show resolved Hide resolved
@shazbert shazbert removed the review me This pull request is ready for review label Nov 13, 2022
Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the additional test coverage + other changes! Found these additional things:

exchanges/deribit/deribit.go Show resolved Hide resolved
exchanges/deribit/deribit.go Show resolved Hide resolved
exchanges/deribit/deribit.go Show resolved Hide resolved
exchanges/deribit/deribit.go Show resolved Hide resolved
exchanges/deribit/deribit.go Show resolved Hide resolved
exchanges/deribit/deribit.go Show resolved Hide resolved
exchanges/deribit/deribit.go Show resolved Hide resolved
exchanges/deribit/deribit_test.go Show resolved Hide resolved
t.Run(testScenario.item.String(), func(t *testing.T) {
t.Parallel()
if testScenario.item.IsOptions() != testScenario.isOptions {
t.Errorf("expected %v isFutures to be %v", testScenario.item, testScenario.isOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Errorf("expected %v isFutures to be %v", testScenario.item, testScenario.isOptions)
t.Errorf("expected %v isOptions to be %v", testScenario.item, testScenario.isOptions)

CONTRIBUTORS Outdated
TaltaM | https://github.com/TaltaM
dackroyd | https://github.com/dackroyd
cranktakular | https://github.com/cranktakular
khcchiu | https://github.com/khcchiu
yangrq1018 | https://github.com/yangrq1018
woshidama323 | https://github.com/woshidama323
crackcomm | https://github.com/crackcomm
azhang | https://github.com/azhang
mshogin | https://github.com/mshogin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mshogin, herenow, soxipy, azhang are duplicated

Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basic things on new changes

exchanges/deribit/deribit.go Show resolved Hide resolved
@@ -2393,6 +2393,9 @@ func (d *Deribit) ExecuteBlockTrade(ctx context.Context, timestampMS time.Time,
if len(trades) == 0 {
return nil, errNoArgumentPassed
}
if timestampMS.IsZero() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VerifiyBlockTrade is called below so there are a double up on these checks, can probably remove them and also target coverage for trade data.

@@ -2192,7 +2192,7 @@ func (d *Deribit) WSExecuteBlockTrade(timestampMS time.Time, nonce, role, symbol
if timestampMS.IsZero() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same doubling up affect as the other function as this calls WSVerifyBlockTrade

Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all those changes!

exchanges/deribit/deribit_websocket.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_websocket.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_test.go Show resolved Hide resolved
exchanges/deribit/deribit_test.go Outdated Show resolved Hide resolved
exchanges/deribit/deribit_test.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes ACK. Thanks sir.

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes!

@shazbert shazbert removed the gcrc GloriousCode Review Complete label May 23, 2024
Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK!

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last batch of nits, I swear 😆

CONTRIBUTORS Show resolved Hide resolved
errInvalidAPIKeyID = errors.New("invalid api key id")
errMaxScopeIsRequired = errors.New("max scope is required")
errTradeModeIsRequired = errors.New("self trading mode is required")
errInvalidSubaccountPassword = errors.New("subaccount password can not be empty")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused

exchanges/deribit/deribit_types.go Outdated Show resolved Hide resolved
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = d.DisableAssetWebsocketSupport(asset.Options)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err = d.DisableAssetWebsocketSupport(asset.Options)
for _, assetType := range []asset.Item{asset.Options, asset.OptionCombo, asset.FutureCombo} {
if err = d.DisableAssetWebsocketSupport(assetType); err != nil {
log.Errorln(log.ExchangeSys, err)
}
}

if err != nil {
log.Errorln(log.ExchangeSys, err)
}
if err = d.StoreAssetPairFormat(asset.Futures, currency.PairStore{RequestFormat: requestFmt, ConfigFormat: configFmt}); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err = d.StoreAssetPairFormat(asset.Futures, currency.PairStore{RequestFormat: requestFmt, ConfigFormat: configFmt}); err != nil {
for _, assetType := range []asset.Item{asset.Futures, asset.Options, asset.OptionCombo, asset.FutureCombo} {
if err = d.StoreAssetPairFormat(assetType, currency.PairStore{RequestFormat: requestFmt, ConfigFormat: configFmt}); err != nil {
log.Errorln(log.ExchangeSys, err)
}
}

)

func TestMain(m *testing.M) {
d.SetDefaults()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can take advtange of the internal test exchange Setup helper, like:

d = new(Deribit)
if err := testexch.Setup(d); err != nil {
 	log.Fatal(err)
 }
 

To rid a lot of the TestMain boilerplate


var (
d = &Deribit{}
futuresTradablePair, optionsTradablePair, optionComboTradablePair, futureComboTradablePair, spotTradablePair currency.Pair
Copy link
Collaborator

@thrasher- thrasher- May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can hardcode spotTradablePair to currency.NewPairWithDelimiter("BTC", "USDC", "_") and futuresTradablePair to currency.NewPairWithDelimiter("BTC", "PERPETUAL", "-") thus eliminating the need to use NewPairFromString in instantiateTradablePairs

exchanges/deribit/deribit_test.go Show resolved Hide resolved
Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on the cleanup! One thing I found on recent changes that could be a problem.

exchanges/deribit/deribit.go Show resolved Hide resolved
@shazbert
Copy link
Collaborator

also can you add these lines to fix codespell reference here. Your PR will be merged first.

Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes ACK. Thanks.

Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK! Thanks for the test updates

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes, some further ones I've found

}
}
case instrumentStateChannel,
rawUsersOrdersKindCurrencyChannel:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case statement contains string formatting directives

}
case incrementalTickerChannel,
quoteChannel,
rawUserOrdersChannel:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string contains string format directives

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These strings differentiate channels selected for subscription and are not used in handling the incoming stream data messages.

@thrasher- thrasher- merged commit aeb4a87 into thrasher-corp:master May 29, 2024
5 of 11 checks passed
@samuael samuael deleted the gderibit branch June 22, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority review me This pull request is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants