Create Limit Order

Overview

KyberSwap Limit Order enables Makers to create orders without incurring gas. To achieve this, signed Maker orders are stored off-chain but settled by the Takers on-chain. By gaslessly signing the off-chain order, a Maker pre-commits to a limit order whose parameters can't be tampered with ensuring order predicatability and security. Please refer to Off-Chain Relay, On-Chain Settlement for a design overview.

Sequence diagram

KyberSwap exposes 2 APIs which Makers will need to call to create a new order:

In addition to the above, Makers are also able to query their active making amount to aid with token approvals:

TypeScript Example

circle-check

Step 1: Get the unsigned EIP712 Create Order message

To create a new limit order, the Maker must first specify the order parameters to be sent to the KyberSwap LO Service via the POST request body:

postCreateOrdersUnsigned.tsarrow-up-right

As part of the order configuration, note that the makingAmount and takingAmount are denominated in wei units and dependent on the decimalsarrow-up-right configured in the asset's ERC20 contract. The allowedSenders parameter is optional but have been included for the purposes of this demo so that only our controlled address can fill the created order.

The makerAsset and takerAsset are defined in the constants.tsarrow-up-right file to enable convenient reuse across various operations.

Full details for each parameter can be found via the Get Unsigned Create Order Message API specification. Upon posting the above request body to /write/api/v1/orders/sign-message, the KyberSwap LO Service will return an unsigned EIP712arrow-up-right Create Order message.

Step 2: Check Limit Order contract spending allowance

Before signing the order creation message, we will first need to ensure that the LO smart contract has sufficient allowance to spend the Maker's ERC20 tokenarrow-up-right. This ensures that when a Taker fills the order, the LO smart contract has the necessary approvals to transfer the specified makerAsset amount from the Maker's wallet.

circle-info

KyberSwap Limit Order Contract Addresses

Please refer to Limit Order Contract Addressesarrow-up-right for the exact addresses for KyberSwap Limit Order for each supported chain.

To get the Maker's current making amount for a specific token, KyberSwap exposes a /read-ks/api/v1/orders/active-making-amount API:

getMakerActiveAmount.tsarrow-up-right

Note that the new order making amount will have to be added to the current making amount. In other words, when creating a new order, the KyberSwap service checks that it has sufficient allowance across all open Maker orders.

postCreateOrder.tsarrow-up-right

If there is insufficient spending allowance, we can then request for a higher allowance via the makerAsset ERC20 token contract using our getTokenApproval() helper function:

approval.tsarrow-up-right

Step 3: Sign the EIP712 Create Order message

Once we have the necessary allowances in place, we can then go ahead and request the signature from the Maker:

postCreateOrder.tsarrow-up-right

circle-info

EIP712 Sign Typed Data

EIP712 arrow-up-rightexposes 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()arrow-up-right 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 V4arrow-up-right.

Step 4: Format the create order request body

With the maker-signed transaction, we can then format the request body according to the /write/api/v1/orders specs:

postCreateOrder.tsarrow-up-right

Note that apart from appending the signature to the existing request body from our earlier API call, we are also extracting the salt that was returned.

Step 5: Post the create order

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

postCreateOrder.tsarrow-up-right

Note that the KyberSwap Limit Order domain is stored under constants.tsarrow-up-right for easy retrieval across various API operations.

Once the KyberSwap LO service receives the creation order, a new off-chain order will be created in it's orderbook from which our network of Takers will be able to query. The created orderId will also be returned as part of the response values from the /write/api/v1/orders API.

Create order success response

Last updated

Was this helpful?