Clients
Socket Client

Socket Client

Getting Started

Installation

pnpm install @dydxprotocol/v4-client-js

Initializing the Client

/**
// For the deployment by DYDX token holders, use below:
 
import { IndexerConfig, ValidatorConfig } from "@dydxprotocol/v4-client-js";
 
const NETWORK: Network = new Network(
  'mainnet',
  new IndexerConfig(
    'https://indexer.dydx.trade',
    'wss://indexer.dydx.trade',
  ),
  new ValidatorConfig(
    'https://dydx-ops-rpc.kingnodes.com', // or other node URL
    'dydx-mainnet-1',
    {
      CHAINTOKEN_DENOM: 'adydx',
      CHAINTOKEN_DECIMALS: 18,
      USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
      USDC_GAS_DENOM: 'uusdc',
      USDC_DECIMALS: 6,
    },
  ),
);
*/
const NETWORK = Network.testnet();
 
const mySocket = new SocketClient(
    NETWORK.indexerConfig,
    () => {
      console.log('socket opened');
    },
    () => {
      console.log('socket closed');
    },
    (message) => {
      console.log(message);
    },
  );
  mySocket.connect();

Subscription

Markets Channel

// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToMarkets();
// unsubscribe
mySocket.unsubscribeFromMarkets()

Response and Channel Data: See Indexer Socket for Initial Response and Channel Update

Trades Channel

// ticker is the market ticker, such as "BTC-USD"
// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToTrades(ticker);
// unsubscribe
mySocket.unsubscribeFromTrades(ticker)

Response and Channel Data: See Indexer Socket for Initial Response and Channel Update

Orderbook Channel

// ticker is the market ticker, such as "BTC-USD"
// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToOrderbook(ticker);
// unsubscribe
mySocket.unsubscribeFromOrderbook(ticker)

Response and Channel Data: See Indexer Socket for Initial Response and Channel Update

Candles Channel

// ticker is the market ticker, such as "BTC-USD"; resolution is the candles resolution
// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToCandles(ticker, resolution);
// unsubscribe
mySocket.unsubscribeFromCandles(ticker, resolution)

Response and Channel Data: See Indexer Socket for Initial Response and Channel Update

Subaccount Channel

// address is the wallet address on dYdX Chain, subaccount_number is the subaccount number
// subscribe
// updates are not batched, received in channel_data
mySocket.subscribeToSubaccount(ticker, resolution);
// unsubscribe
mySocket.unsubscribeFromSubaccount(ticker, resolution)

Response and Channel Data: See Indexer Socket for Initial Response and Channel Update