Cancel Order

Cancel Order API

Cancel an existing open order by signing its hash.

Overview

Order cancellation requires:

  1. The hash of the order you want to cancel

  2. Signing the order hash with your private key

  3. Sending the signed cancellation via WebSocket

Cancel Request Parameters

Parameter
Type
Description

order_hash

string

The hash of the order to cancel

v

number

Signature recovery parameter

r

string

Signature r component

s

string

Signature s component

JavaScript/TypeScript Example

import Web3 from 'web3';

async function cancelOrder(privateKey: string, orderHash: string) {
    const web3 = new Web3();
    
    // Sign the order hash
    const signedData = web3.eth.accounts.sign(orderHash, privateKey);
    const signature = signedData.signature;

    // Parse signature into v, r, s
    const sig = signature.slice(2);
    const r = `0x${sig.slice(0, 64)}`;
    const s = `0x${sig.slice(64, 128)}`;
    let v = parseInt(`0x${sig.slice(128, 130)}`, 16);
    if (v < 27) v += 27;

    return {
        order_hash: orderHash,
        v: v,
        r: r,
        s: s
    };
}

// Send cancel request via WebSocket
function sendCancelOrder(ws: WebSocket, cancelRequest: object) {
    const message = JSON.stringify({
        op: 'cancel_order',
        data: cancelRequest
    });
    ws.send(message);
}

// Usage Example
async function main() {
    const privateKey = '0x...'; // Your private key
    const orderHash = '0x...';  // Hash of order to cancel
    
    const cancelRequest = await cancelOrder(privateKey, orderHash);

    const ws = new WebSocket('wss://api.dmex.app/ws');
    
    ws.onopen = () => {
        sendCancelOrder(ws, cancelRequest);
    };

    ws.onmessage = (event) => {
        const response = JSON.parse(event.data);
        console.log('Response:', response);
    };
}

Python Example

Notes

  • You can only cancel orders that you created (signature must match the original order creator)

  • The order must still be open (not fully filled or already cancelled)

  • To cancel and replace an order atomically, use the replace_hash parameter when creating a new order instead

Last updated