Skip to content

smNRop/DeriSock

 
 

Repository files navigation

DeriSock Build Status NuGet Version NuGet Downloads

DeriSock is a client library that connects to the Deribit API via WebSocket.
All methods and subscriptions found on https://docs.deribit.com are supported.

Getting Started

To connect to the Deribit Network just instantiate a new instance of the DeribitClient class and call the Connect method to connect and the Disconnect method to disconnect.

var client = new DeribitClient(EndpointType.Testnet);
await client.Connect();

// do something

await client.Disconnect();

snippet source | anchor

The various methods are organized in categories (Authentication, Supporting, Market Data, ...) and scopes (Private, Public).

Example: Calling GetOrderBook from the Public scope.

var response = await client.Public.GetOrderBook(
                 new PublicGetOrderBookRequest
                 {
                   InstrumentName = "BTC-PERPETUAL"
                 });

if (response.Error is not null) {
  // Handle errors returned by the API
  return;
}

if (response.Data is null) {
  // Something unexpected happened. 'Data' should not be null if 'Error' is null
  return;
}

var bestBidPrice = response.Data.BestBidPrice;

snippet source | anchor

Example: Calling GetOrderBook from the MarketData category.

var response = await client.MarketData.PublicGetOrderBook(
                 new PublicGetOrderBookRequest
                 {
                   InstrumentName = "BTC-PERPETUAL"
                 });

if (response.Error is not null) {
  // Handle errors returned by the API
  return;
}

if (response.Data is null) {
  // Something unexpected happened. 'Data' should not be null if 'Error' is null
  return;
}

var bestBidPrice = response.Data.BestBidPrice;

snippet source | anchor

Authentication

The library supports authentication via credentials and signature

Authentication using Credentials

await client.Authentication.PublicLogin()
  .WithClientCredentials(
    "<client id",
    "<client secret>",
    "<optional state>",
    "<optional scope>");

snippet source | anchor

Authentication using Signature

await client.Authentication.PublicLogin()
  .WithClientSignature(
    "<client id",
    "<client secret>",
    "<optional state>",
    "<optional scope>");

snippet source | anchor

Logout

When authenticated, you can logout like this (this is the only synchroneous method):

client.Authentication.PrivateLogout();

snippet source | anchor

Note: The server will automatically close the connection when you logout

Subscriptions

The subscription system will choose public/subscribe or private/subscribe automatically. If the client is authenticated it will use private/subscribe, if the client is not authenticated it will use public/subscribe. This is also the reason why the subscription methods are not present in the Public or Private scopes.

// Subscribe to one or more channels. 
var subscriptionStream = await client.Subscriptions.SubscribeBookChanges(
                           new BookChangesChannel
                           {
                             InstrumentName = "BTC-PERPETUAL",
                             Interval = NotificationInterval2._100ms
                           },
                           new BookChangesChannel
                           {
                             InstrumentName = "ETH-PERPETUAL",
                             Interval = NotificationInterval2._100ms
                           });

// Create a CancellationTokenSource to be able to stop the stream
// (i.e. unsubscribe from the channel(s))
var cts = new CancellationTokenSource();

// An IAsyncEnumerable<T> stream that will listen to incoming notifications as long as
// cts.Cancel(); is not called.
await foreach (var notification in subscriptionStream.WithCancellation(cts.Token)) {
  //Here you can do something with the received information.
  var bookChangeId = notification.Data.ChangeId;
}

snippet source | anchor

About

Deribit WebSocket Client Library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%