This guide explains how to programmatically create orders on the DMEX exchange.
Overview
Orders are created by:
Building the order parameters
Generating a keccak256 hash of the order
Signing the hash with your private key
Sending the signed order via WebSocket
Order Parameters
Parameter
Type
Description
amount
string
Order size (will be multiplied by 1e8)
price
string
Order price (will be multiplied by 1e8)
side
boolean
true = buy/long, false = sell/short
leverage
string
Leverage multiplier (e.g., "10" for 10x)
base_token
address
The margin token contract address
asset
string
Trading pair symbol (e.g., "BTC", "ETH")
closing_order
boolean
true if this order closes an existing position
stop
boolean
true if this is a stop order
stop_price
string
Trigger price for stop orders (multiplied by 1e8)
is_market
boolean
true for market orders
post_only
boolean
true to ensure order is maker only
replace_hash
string
Hash of order to replace, or zero hash
JavaScript/TypeScript Example
Python Example
Order Hash Structure
The order hash is generated using keccak256 with ABI-encoded parameters:
Order Types
Limit Order
Market Order
Stop-Limit Order
Stop-Market Order
Closing Positions
To close an existing position, set closing_order: true. The side should be opposite to your current position:
To close a long position: side: false (sell)
To close a short position: side: true (buy)
Replacing Orders
To replace an existing order, pass the hash of the order you want to cancel in replace_hash. This atomically cancels the old order and creates the new one.
Error Handling
Common validation errors:
Amount must be greater than zero
Invalid signature - Ensure the hash is signed correctly
Insufficient margin - Not enough balance for the order
Invalid leverage - Leverage outside allowed range for the asset