---
agentTools:
  projectIndex: https://x2y2.readme.io/llms.txt
---

# Cancel orders on chain

To cancel an order(s) on X2Y2, follow the steps below.

[block:api-header]
{
  "title": "Generate user signed message"
}
[/block]

Only order's maker could cancel the order. To verify the current user's address, two messages are necessary to be posted to X2Y2 server.

[block:code]
{
  "codes": [
    {
      "code": "// Generate a Keccak256 hash of any string\nconst signMessage = ethers.utils.keccak256('0x')\n\n// Ask user's wallet to sign the message\nconst sign = (await web3Provider.send('personal_sign', [\n    signMessage,\n    userAddress,\n  ])) as string",
      "language": "javascript",
      "name": "Sample"
    }
  ]
}
[/block]

[block:api-header]
{
  "title": "Get input for order cancel"
}
[/block]

Call the [Sign cancel order](https://x2y2.readme.io/reference/sign-cancel-order) to get the necessary input to be submitted via wallet.

[block:code]
{
  "codes": [
    {
      "code": "const cancelResponse = await post('https://api.x2y2.org/api/orders/cancel', JSON.stringify({\n    caller: userAddress,\n    op: 3,\n    items: [{\n        orderId: sellOrder.id\n    }],\n    sign_message: signMessage,\n    sign,\n}), {\n    headers: {\n        'Content-Type': 'application/json; charset=utf-8',\n        'X-API-KEY': X2Y2_API_KEY,\n    }\n}\n// Extract cancel input from cancelResponse…",
      "language": "javascript",
      "name": "Sample"
    }
  ]
}
[/block]

[block:api-header]
{
  "title": "Decode the input data"
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "const inputData = ethers.utils.defaultAbiCoder.decode(\n    [`tuple(bytes32[] itemHashes, uint256 deadline, uint8 v, bytes32 r, bytes32 s)`],\n    cancelData.input\n)[0]\n\nconst cancelInput = [inputData.itemHashes, inputData.deadline, inputData.v, inputData.r, inputData.s] as const",
      "language": "javascript"
    }
  ]
}
[/block]

[block:api-header]
{
  "title": "Submit the data to user's wallet"
}
[/block]

Submit the `cancelInput` decoded to buyer's wallet to finish the cancellation.

[block:code]
{
  "codes": [
    {
      "code": "const marketContract = '0x74312363e45DCaBA76c59ec49a7Aa8A65a67EeD3' // X2Y2's market address on Mainnet. Use 0x1891EcD5F7b1E751151d857265D6e6D08ae8989e for Goerli testnet\nconst abi = [] // JSON format of X2Y2's market contract ABI, available at `https://etherscan.io/address/0x6d7812d41a08bc2a910b562d8b56411964a4ed88#code`\nconst market = new ethers.Contract(marketContract, abi, web3Provider)\nconst tx = await market.cancel(cancelInput)",
      "language": "text"
    }
  ]
}
[/block]