Gasless Cancel
Last updated
Last updated
In addition to gasless order creation, KyberSwap Limit Order Makers have the option to cancel their orders without incurring gas. As Taker order fulfilment requires both a Maker and KyberSwap Operator signature, Makers are able to cancel their orders gaslessly by instructing the KyberSwap Operator to cancel their orders.
Please refer to Gasless Cancellation for more details on this design.
KyberSwap exposes 2 APIs which Makers will need to call to gaslessly cancel their order:
/write/api/v1/orders/cancel-sign
: Get the EIP712 cancel order message to be signed. The response of this API will need to be signed with Sign Typed Data v4 before submitting cancel order request below.
/write/api/v1/orders/cancel
: Cancel order(s) by sending in order params which includes the signed EIP712 message.
In addition to the above, Makers are also able to query their active order(s) to aid with filtering orders to cancel:
/read-ks/api/v1/orders
: Get all orders created by a Maker address filtered by the order status.
Limit Order API Demo
The code snippets in the guide below have been extracted from our demo GitHub repo which showcases the full end-to-end Limit Order operations in a TypeScript environment.
Active/Open Orders
To proceed with this guide, users must have created an Active or Open Limit Order. Please refer to the Create Limit Order developer guide for instructions on how to achieve this programmatically.
We can use the /read-ks/api/v1/orders
to get the list of "active" or "open" Maker orders:
In this case, the above query will return all of the Maker active orders from which we can then filter for our target order to cancel.
For our purposes, we will just take the first order which matches our makerAsset
and takerAsset
pair:
To get the unsigned Cancel Order message, we will send the following request to the /write/api/v1/orders/cancel-sign
endpoint:
Note that you can specify more than a single orderId
to be cancelled by adding to the orderIds
array. Once submitted, the KyberSwap LO Service will return an unsigned EIP712 Cancel Order message.
Based on the response above, we can then proceed to request the Maker's signature:
EIP712 Sign Typed Data
EIP712 exposes human-readable data for user's to view prior to signing the transaction.
Our example assumes a pure Node.js implementation and therefore uses the ethers.js signTypedData()
function to sign the EIP712 message. Note that the type
object used is per the primaryType
that is returned by the KyberSwap LO Service.
For further information on how to implement this from the browser UI, you can also take reference from MetaMask's SignTypedData V4.
With the maker-signed transaction, we can then append the signature to the request body to be sent to /write/api/v1/orders/cancel
:
We are now ready to call the /write/api/v1/orders/cancel
API with the formatted cancel order message:
Note that we have included an Origin
header parameter as a means to authenticate the request.
Once the KyberSwap LO Service receives the gasless cancel order, the KyberSwap Operator will no longer sign the orderIds that were requested to be gaslessly cancelled. In rare cases where the KyberSwap Operator had recently signed the target orderId (i.e. when target order is close to the market price), Makers might need to wait up till a maximum of 5 minutes for the Operator signature to lapse before their order is cancelled.
Hard Cancel
For Makers who would like to immediately cancel their orders without waiting for the Operator signature to lapse, KyberSwap Limit Orders also offers a Hard Cancel option. By paying a small prioritization gas fee, Makers can cancel their orders immediately on-chain.
Please refer to Hard Cancel for more info.
The cancelled orderId
as well as any operator signature expiry timestamp will be returned as part of the response values from the /write/api/v1/orders/cancel
API.