cancelOrder Function
Function Type: external
Function Signature: cancelOrder(address,MetadataCancelOrder,uint256,uint256,bool,bytes)
The cancelOrder function allows order creators to cancel their existing orders and reclaim their locked tokens. This function validates ownership, returns tokens to the original creator, and provides rewards to staker executors.
Key features:
- Order Cancellation: Removes existing orders from the marketplace
- Token Recovery: Returns locked tokens to the original order creator
- Ownership Validation: Ensures only order creators can cancel their orders
- Staker Rewards: Executors who are stakers receive MATE token rewards
- Market Cleanup: Updates market statistics after order removal
Parameters
| Field | Type | Description |
|---|---|---|
user | address | The address that created the original order and is requesting cancellation |
metadata | MetadataCancelOrder | Struct containing cancellation details and embedded signature |
_priorityFee_Evvm | uint256 | Priority fee for EVVM transaction processing (optional) |
_nonce_Evvm | uint256 | Nonce for EVVM payment transaction |
_priority_Evvm | bool | Priority flag for EVVM payment (sync/async) |
_signature_Evvm | bytes | EIP-191 signature for EVVM payment authorization |
_priorityFee_Evvm is paid using principal tokens
MetadataCancelOrder Structure
| Field | Type | Description |
|---|---|---|
nonce | uint256 | Unique nonce for P2P Swap replay protection |
tokenA | address | Token that was offered in the original order |
tokenB | address | Token that was requested in the original order |
orderId | uint256 | ID of the order to be cancelled |
signature | bytes | EIP-191 signature from user authorizing the cancellation |
Execution Methods
Fisher Execution
- User signs cancellation details and optional EVVM payment authorization
- Fisher captures the transaction and validates all signatures
- Fisher submits the transaction and receives staker rewards if eligible
- Order is cancelled and tokens are returned to the user
Direct Execution
- User or authorized service calls the function directly
- All signature validations are performed on-chain
- Order cancellation and token return happen immediately
- Staker benefits are distributed if executor qualifies
Workflow
-
P2P Signature Verification: Validates the embedded
metadata.signatureagainst theuseraddress and cancellation parameters usingSignatureUtils.verifyMessageSignedForCancelOrder. Reverts with"Invalid signature"on failure. -
Market Resolution: Finds the market for the token pair using
findMarket(metadata.tokenA, metadata.tokenB). -
Nonce Validation: Checks if the P2P nonce has been used before by consulting
nonceP2PSwap[user][metadata.nonce]. Reverts with"Invalid nonce"if the nonce was previously used. -
Order Ownership Verification: Validates that:
- The market exists (market != 0)
- The order exists and belongs to the requesting user
- Reverts with
"Invalid order"if validation fails
-
Priority Fee Processing: If
_priorityFee_Evvm > 0, processes the priority fee payment usingmakePaywith MATE tokens. -
Token Return: Returns the locked tokens to the order creator using
makeCaPay, transferringordersInsideMarket[market][metadata.orderId].amountAofmetadata.tokenAback touser. -
Order Removal: Marks the order as cancelled by setting the seller address to
address(0). -
Staker Reward Distribution: If the executor (
msg.sender) is a registered staker:- MATE Token Rewards: Grants MATE tokens to the executor:
- 3x base reward amount + priority fee if priority fee was provided
- 2x base reward amount if no priority fee
- MATE Token Rewards: Grants MATE tokens to the executor:
-
Market Update: Decrements the
ordersAvailablecounter for the market. -
Nonce Update: Marks the P2P nonce as used to prevent replay attacks.
Example
Scenario: User wants to cancel their order offering 100 USDC for 0.05 ETH
Parameters:
user:0x742c7b6b472c8f4bd58e6f9f6c82e8e6e7c82d8cmetadata.nonce:25metadata.tokenA:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48(USDC)metadata.tokenB:0x0000000000000000000000000000000000000000(ETH)metadata.orderId:3_priorityFee_Evvm:0(no priority fee)
Process:
- User signs the cancellation details with their private key
- Fisher or user submits the transaction
- Contract validates signature and order ownership
- 100 USDC is returned to the user's balance
- Order #3 is marked as cancelled in the USDC/ETH market
- Staker executor receives 2x MATE reward (no priority fee)
Market Impact:
- Market's
ordersAvailablecount decreases by 1 - Order slot becomes available for reuse
- Market statistics reflect the cancellation
Token Recovery:
- Original 100 USDC locked in the order is returned to user
- User can immediately use these tokens for other purposes
- No fees charged for order cancellation
For signature structure details, see Cancel Order Signature Structure
Want to create a new order?
Use makeOrder to create a new swap order with different parameters.
Looking to fulfill someone else's order?
Check dispatchOrder to execute existing orders in the marketplace.