Developers
Clients
Composite Client

Composite Client

Getting Started

CompositeClient simplifies the transactions by transforming human readable parameters to chain-specific parameters.

For example, Placing an order with ValidatorClient requires parameters such as quantums and subticks, which are calculated based on ClobPair settings.

CompositeClient would take regular human readable parameters such as price and size, make the neccesory calculations and conversions, and call ValidatorClient with the correct quantums and subticks for you.

Installation

pnpm install @dydxprotocol/v4-client-js 

Initializing the Client

import { CompositeClient } from "@dydxprotocol/v4-client-js";
 
const client = await CompositeClient.connect(Network.testnet());

Creating a LocalWallet

import {
  BECH32_PREFIX,
  LocalWallet,
} from '@dydxprotocol/v4-client-js';
 
const mnemonic = 'YOUR MNEMONIC HERE';
const wallet = await LocalWallet.fromMnemonic(mnemonic, BECH32_PREFIX);

Placing Orders

Placing an Order

import {
  OrderExecution, OrderSide, OrderTimeInForce, OrderType,
} from '@dydxprotocol/v4-client-js';
const subaccount = new SubaccountClient(wallet, 0);
const clientId = 123; // set to a number, can be used by the client to identify the order
const market = "BTC-USD"; // perpertual market id
const type = OrderType.LIMIT; // order type
const side = OrderSide.BUY; // side of the order
const timeInForce = OrderTimeInForce.IOC; // UX TimeInForce
const execution = OrderExecution.DEFAULT;
const price = 30_000; // price of 30,000;
const size = 0.1; // subticks are calculated by the price of the order
const postOnly = false; // If true, order is post only
const reduceOnly = false; // if true, the order will only reduce the position size
const triggerPrice = null; // required for conditional orders
 
const tx = await client.placeOrder(
  subaccount,
  market,
  type,
  side,
  price,
  size,
  clientId,
  timeInForce,
  0,
  execution,
  postOnly,
  reduceOnly,
  triggerPrice
);

Replacing an Order

For more details on order replacements, see Replacing an Order in the validator client section.

Canceling an Order

/*
order is an Order object from the Indexer
*/
const tx = await client.cancelOrder(
  subaccount,
  order.clientId,
  order.orderFlags,
  order.clobPairId,
  order.goodTilBlock,
  order.goodTilBlockTime
);