Create Limit Order
Last updated
Last updated
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.
KyberSwap exposes 2 APIs which Makers will need to call to create a new order:
/write/api/v1/orders/sign-message
: Get the EIP712 create order message to be signed. The response of this API will need to be signed with Sign Typed Data v4 before submitting create order request below.
/write/api/v1/orders
: Create new 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 making amount to aid with token approvals:
/read-ks/api/v1/orders/active-making-amount
: The aggregated making amount for the Maker given a specific token.
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.
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:
As part of the order configuration, note that the makingAmount
and takingAmount
are denominated in wei units and dependent on the decimals 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.ts
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 EIP712 Create Order message.
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 token. 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.
KyberSwap Limit Order Contract Addresses
Please refer to Limit Order Contract Addresses 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:
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.
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:
Once we have the necessary allowances in place, we can then go ahead and request the signature from the Maker:
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 format the request body according to the /write/api/v1/orders
specs:
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.
We are now ready to call the /write/api/v1/orders
API with the formatted create order message:
Note that the KyberSwap Limit Order domain is stored under constants.ts
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.