Perpetual-futures DEX. dYdX v4 is a sovereign Cosmos SDK / CometBFT app-chain (the dYdX Chain) with a fully on-chain orderbook and matching engine — a complete rewrite from the v3 StarkEx L2.
- 01perpetual futures with on-chain orderbook
- 02professional / market-maker UX
- 03high-throughput order placement
- 04Cosmos-native trading apps
- pnpm add @dydxprotocol/v4-client-js
| Variable | Scope | Description |
|---|---|---|
| DYDX_MNEMONIC | Server | BIP-39 mnemonic for the dYdX Chain account used to sign Cosmos transactions. Server-side / secure-enclave only — never expose to the browser. |
| NEXT_PUBLIC_DYDX_NETWORK | Client | Network selector: `mainnet` or `testnet`. Drives RPC, indexer, and chain-id selection in the client. |
| DYDX_INDEXER_URL | Client | Optional override for the dYdX Indexer REST/WebSocket endpoint (defaults bake in mainnet/testnet URLs). |
Use `@dydxprotocol/v4-client-js` against the dYdX Chain. Initialize: `const network = Network.mainnet(); const client = await CompositeClient.connect(network);` then load a wallet with `LocalWallet.fromMnemonic(DYDX_MNEMONIC, BECH32_PREFIX)` and a `SubaccountClient(wallet, 0)`. Place an order with `client.placeOrder(subaccount, marketId, type, side, price, size, clientId, timeInForce, goodTilTimeInSeconds, execution, postOnly, reduceOnly)`. Read positions/markets/fills via the Indexer client (`IndexerClient.markets.getPerpetualMarkets()`, `client.indexerClient.account.getSubaccount(...)`). dYdX v4 is a Cosmos chain — orders are signed Cosmos messages, not Ethereum txs.
- ⚑dYdX **v4 is Cosmos**, not Ethereum — the v3 StarkEx L2 is deprecated. Do not use `@dydxprotocol/v3-client` or expect EIP-712 signing; v4 uses Cosmos SDK secp256k1 signatures over `MsgPlaceOrder`.
- ⚑Funds live in **subaccounts** (numbered child accounts of a single Cosmos address) — every order, transfer, and position read must reference a `subaccountNumber`; mixing them up shows phantom-empty balances.
- ⚑USDC on dYdX Chain is bridged via Noble — depositing requires an IBC transfer (Noble → dYdX Chain), not a direct ERC20 send. The keplr/cosmos wallet flow is fundamentally different from a standard EVM deposit.
- ⚑Order types and time-in-force are constrained: `IOC`, `FOK`, `POST_ONLY`, `GTT` (good-til-time, max ~28 days). `GTC` is not supported — use `GTT` with a long expiry.
- ⚑Stateful orders (long-lived limit orders) have a per-block placement rate limit and require an explicit `goodTilBlockTime` / `goodTilBlock` — short-term orders use `goodTilBlock` and expire within ~20 blocks.
- ⚑Slippage on market orders is bounded by the `price` parameter (worst acceptable price) — there is no separate slippage field; setting price too tight yields silent rejections by the matching engine.
- ⚑The Indexer is a separate read-only service from the chain — query positions/fills/orderbook from the Indexer (REST + WS), submit orders to the validator RPC; expect ~block-time staleness on Indexer reads.