ZenWebsocket.Examples.DeribitRpc (ZenWebsocket v0.1.4)

View Source

Shared Deribit JSON-RPC method definitions and request builders.

This module centralizes all Deribit RPC method definitions to avoid duplication across adapter examples.

Uses ZenWebsocket.JsonRpc.build_request/2 and returns the standard {:ok, map()} tuple for consistency with library conventions.

Summary

Functions

Builds an authentication request for Deribit API.

Builds a generic JSON-RPC request for any Deribit method.

Creates a buy order for the specified instrument.

Cancels an existing order by ID.

Retrieves available trading instruments for a currency.

Retrieves all open orders with optional filters.

Retrieves the order book for a specific instrument.

Creates a sell order for the specified instrument.

Sets the heartbeat interval for the WebSocket connection.

Subscribes to one or more channels for real-time data.

Builds a test request to verify the connection is alive.

Gets ticker data for a specific instrument.

Unsubscribes from one or more channels.

Functions

auth_request(client_id, client_secret)

@spec auth_request(String.t(), String.t()) :: {:ok, map()}

Builds an authentication request for Deribit API.

Parameters

  • client_id - Your Deribit API client ID
  • client_secret - Your Deribit API client secret

Returns

{:ok, request} tuple with JSON-RPC request map for authentication.

build_request(method, params \\ %{})

@spec build_request(String.t(), map()) :: {:ok, map()}

Builds a generic JSON-RPC request for any Deribit method.

Parameters

  • method - The RPC method name
  • params - Method parameters (default: %{})

Returns

{:ok, request} tuple with JSON-RPC request map.

buy(instrument, amount, opts \\ %{})

@spec buy(String.t(), number(), map()) :: {:ok, map()}

Creates a buy order for the specified instrument.

Parameters

  • instrument - Instrument name (e.g., "BTC-PERPETUAL")
  • amount - Order amount in contracts
  • opts - Additional order options (type, price, etc.)

Returns

{:ok, request} tuple with JSON-RPC request map for buy order.

cancel(order_id)

@spec cancel(String.t()) :: {:ok, map()}

Cancels an existing order by ID.

Parameters

  • order_id - The order ID to cancel

Returns

{:ok, request} tuple with JSON-RPC request map for order cancellation.

get_instruments(currency)

@spec get_instruments(String.t()) :: {:ok, map()}

Retrieves available trading instruments for a currency.

Parameters

  • currency - Currency code (e.g., "BTC", "ETH")

Returns

{:ok, request} tuple with JSON-RPC request map for retrieving instruments.

get_open_orders(opts \\ %{})

@spec get_open_orders(map()) :: {:ok, map()}

Retrieves all open orders with optional filters.

Parameters

  • opts - Optional filters (instrument, type, etc.)

Returns

{:ok, request} tuple with JSON-RPC request map for retrieving open orders.

get_order_book(instrument, depth \\ 10)

@spec get_order_book(String.t(), integer()) :: {:ok, map()}

Retrieves the order book for a specific instrument.

Parameters

  • instrument - Instrument name (e.g., "BTC-PERPETUAL")
  • depth - Order book depth (default: 10)

Returns

{:ok, request} tuple with JSON-RPC request map for order book data.

sell(instrument, amount, opts \\ %{})

@spec sell(String.t(), number(), map()) :: {:ok, map()}

Creates a sell order for the specified instrument.

Parameters

  • instrument - Instrument name (e.g., "BTC-PERPETUAL")
  • amount - Order amount in contracts
  • opts - Additional order options (type, price, etc.)

Returns

{:ok, request} tuple with JSON-RPC request map for sell order.

set_heartbeat(interval \\ 30)

@spec set_heartbeat(integer()) :: {:ok, map()}

Sets the heartbeat interval for the WebSocket connection.

Parameters

  • interval - Heartbeat interval in seconds (default: 30)

Returns

{:ok, request} tuple with JSON-RPC request map for setting heartbeat.

subscribe(channels)

@spec subscribe([String.t()]) :: {:ok, map()}

Subscribes to one or more channels for real-time data.

Parameters

  • channels - List of channel names to subscribe to

Example

subscribe(["book.BTC-PERPETUAL.raw", "ticker.ETH-PERPETUAL.raw"])

Returns

{:ok, request} tuple with JSON-RPC request map for subscription.

test_request()

@spec test_request() :: {:ok, map()}

Builds a test request to verify the connection is alive.

Returns

{:ok, request} tuple with JSON-RPC request map for connection testing.

ticker(instrument)

@spec ticker(String.t()) :: {:ok, map()}

Gets ticker data for a specific instrument.

Parameters

  • instrument - Instrument name (e.g., "BTC-PERPETUAL")

Returns

{:ok, request} tuple with JSON-RPC request map for ticker data.

unsubscribe(channels)

@spec unsubscribe([String.t()]) :: {:ok, map()}

Unsubscribes from one or more channels.

Parameters

  • channels - List of channel names to unsubscribe from

Returns

{:ok, request} tuple with JSON-RPC request map for unsubscription.