Gasless Cancel

Overview

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.

Sequence Diagram

KyberSwap exposes 2 APIs which Makers will need to call to gaslessly cancel their order:

In addition to the above, Makers are also able to query their active order(s) to aid with filtering orders to cancel:

TypeScript Example

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.

Step 1: Get Maker orders

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:

const targetPathConfig = {
    params: {
        chainId: ChainId.MATIC,
        maker: signerAddress,
        status: "active"
    }
};

getMakerOrders.ts

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.

Step 2: Get the target orderId to cancel

For our purposes, we will just take the first order which matches our makerAsset and takerAsset pair:

const orders = await getOrders();
const targetOrder = orders.filter(order => 
    order.maker.toLowerCase() == signerAddress.toLowerCase() &&
    order.makerAsset.toLowerCase() == makerAsset.address.toLowerCase() &&
    order.takerAsset.toLowerCase() == takerAsset.address.toLowerCase()
);
const targetOrderId = Number(targetOrder[0].id);

postCancelOrderUnsigned.ts

Step 3: Get the unsigned EIP712 Cancel Order message

To get the unsigned Cancel Order message, we will send the following request to the /write/api/v1/orders/cancel-sign endpoint:

const requestBody: CancelOrderUnsignedBody = {
    chainId: ChainId.MATIC.toString(),
    maker: signerAddress,
    orderIds: [targetOrderId]
};

postCancelOrderUnsigned.ts

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.

Step 4: Sign the EIP712 Cancel Order message

Based on the response above, we can then proceed to request the Maker's signature:

const signature = await signer.signTypedData(
    unsignedOrderReturnData.domain,
    { CancelOrder: unsignedOrderReturnData.types.CancelOrder },
    unsignedOrderReturnData.message
);

postCancelOrder.ts

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.

Step 5: Format the cancel order request body

With the maker-signed transaction, we can then append the signature to the request body to be sent to /write/api/v1/orders/cancel:

const requestBody: CancelOrderSignedBody = {
    ...unsignedOrderReqBody,
    signature: signature
};

postCancelOrder.ts

Step 6: Post the gasless cancel order

We are now ready to call the /write/api/v1/orders/cancel API with the formatted cancel order message:

const {data} = await axios.post(
    LimitOrderDomain+targetPath,
    requestBody,
    {headers: {Origin: 'https://kyberswap.com'}}
);

postCancelOrder.ts

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.

Last updated