Deribit Websocket library for .NET.
- Supports Api V2.0.0
- Supports WebSocket Api
- Does not support rest or FIX api
- Namespaces
using Deribit.S4KTNET.Core;
using Deribit.S4KTNET.Core.Authentication;
using Deribit.S4KTNET.Core.SubscriptionManagement;
using Deribit.S4KTNET.Core.SessionManagement;
using Deribit.S4KTNET.Core.Trading;
- Configure
var deribitconfig = new DeribitConfig
{
Environment = DeribitEnvironment.Live,
// ConnectOnConstruction // automatically connect
// EnableJsonRpcTracing // troubleshooting jsonrpc
// NoRefreshAuthToken // do not automatically refresh authentication token
// NoRespondHeartbeats // do not automatically response to hearbeats,
};
- Construct client object
var deribit = new DeribitService(deribitconfig);
- Connect
await deribit.Connect();
- Test connection
await deribit.Supporting.Test(new TestRequest());
- Enable heartbeats
deribit.SessionManagement.SetHeartbeat(new SetHeartbeatRequest()
{
interval = 10, // seconds
});
- Subscribe to data streams
// subscribe to BTC perp quotes
await deribit.SubscriptionManagement.SubscribePublic(new SubscribeRequest()
{
channels = new string[]
{
DeribitSubscriptions.quote(DeribitInstruments.Perpetual.BTCPERPETUAL),
},
});
// listen to stream
deribit.SubscriptionManagement.QuoteStream.Subscribe(quoteObserver);
- Authenticate
deribit.Authentication.Auth(new AuthRequest()
{
grant_type = GrantType.client_credentials,
client_id = "<yourapikey>",
client_secret = "<yourapisecret>",
});
- Submit orders
await deribit.Trading.buy(new BuySellRequest()
{
instrument_name = DeribitInstruments.Perpetual.BTCPERPETUAL,
type = OrderType.limit,
amount = 10,
price = 1234,
});
- Disconnect
deribit.Dispose();
See the sample client Deribit.S4KTNET.Sample
.
Api | Method | Covered | Notes |
---|---|---|---|
Authentication | * | ✗ | unsupported |
SessionManagement | * | ✗ | unsupported |
Supporting | * | ✗ | unsupported |
Subscriptions | * | ✗ | unsupported |
AccountManagement | * | ✗ | unsupported |
Trading | * | ✗ | unsupported |
MarketData | * | ✗ | unsupported |
Wallet | * | ✗ | unsupported |
Notifications | * | ✗ | unsupported |
Private methods require authentication. The library attempts to support all authentication flows available.
SecureString
is not supported, as the credentials are exposed in memory through the websocket libraries anyway.
This flow is not recommended.
// form request
var request = new AuthRequest()
{
grant_type = GrantType.password,
username = "<yourusername>",
password = "<yourpassword>",
}
// execute request
deribit.Authentication.Auth(request);
This is the recommended method.
// form request
var request = new AuthRequest()
{
grant_type = GrantType.client_credentials,
client_id = "<yourapikey>",
client_secret = "<yourapisecret>",
}
// execute request
deribit.Authentication.Auth(request);
This flow is not computing the correct signature, for reasons unknown. PR fix welcome.
This is the most secure option available. Client signatures are computed as per documentation.
// form request
var request = new AuthRequest()
{
grant_type = GrantType.client_credentials,
client_id = "<yourapikey>",
client_secret = "yourapisecret>",
};
// sign the request
request = request.Sign();
// execute request
deribit.Authentication.Auth(request);
Token refresh is handled by the library. If a valid refresh token is available, the library will periodically refresh the auth token every 15m. Note that Deribit seems to grant auth tokens with extended lifetimes (several months) which I do not understand; this functionality may not be required.
If required, you can refresh manually:
var request = new AuthRequest()
{
grant_type = GrantType.refresh_token,
refresh_token = "<refresh_token>",
}
You can disable auto refresh through the config object
new DeribitConfig()
{
NoRefreshAuthToken = true,
}
The client automatically responds to heartbeat requests. But you must enable heartbeats yourself:
deribit.SessionManagement.SetHeartbeat(new SetHeartbeatRequest()
{
interval = 10, // 10 seconds
})
Rx observables are synchronous by default. That means your observer code is called on the same thread the message is received on the websocket feed. Be sure to unblock the thread as soon as possible.
Rx supports customizable stream synchronization:
This library was written with extensive defensive programming through validation checks, at the expense of cpu/memory performance. If you need the extra performance you can remove this validation. I can help with that.
Take appropriate security measures to protect yourself from malicious code:
- Do not trust the binaries. Compile from source.
- Do not store plaintext api secrets in source control.