Fill Limit Order
Last updated
Was this helpful?
Last updated
Was this helpful?
Takers are able to fill any signed Maker order within the KyberSwap Limit Order order books by executing the fill order on-chain. By utilizing KyberSwap Limit Order APIs, Takers gain access to slippage-free liquidity sources which are secured via on-chain settlement.
Please refer to for more detail on this design.
KyberSwap exposes 2 API options for Takers looking to fill orders on-chain:
In order to fill an order, Takers will first have to request an Operator signature via:
In addition to the above, Takers are also able to query active or open order(s) to aid with filtering orders to fill:
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.
We can use the /read-partner/api/v1/orders
to get the list of "active" or "open" orders by token pair:
For the purposes of this guide, we will be taking the first returned order to fill:
With our target orderId
, we can then request for the Operator signature by calling /read-partner/api/v1/orders/operator-signature
with the following parameters:
For each orderId
requested, the KyberSwap LO Service will return an operatorSignature
which will be required as part of the fill order transaction.
If there is insufficient spending allowance, we can then request for a higher allowance via the takerAsset
ERC20 token contract using our getTokenApproval()
helper function:
To get the encoded data, we will then need to format the /read-ks/api/v1/encode/fill-order-to
request body. Note the operatorSignature
that was returned in step 2:
With the fill order prepared, we can then request the encoded data via /read-ks/api/v1/encode/fill-order-to
:
This will return the fill order encoded data which will be used as the calldata when executing the transaction on-chain.
: Encode the fill order data to be sent on-chain. This API can be used to fill a single order.
: Encode the fill batch order data to be sent on-chain. This API can be used to fill multiple orders.
: Get the KyberSwap Operator to sign the target orders so that it can be filled.
: Returns orders for the queried token pair sorted by best rates in descending order.
To proceed with this guide, users must have created an Active or Open Limit Order. Please refer to the for instructions on how to achieve this programmatically.
Note that the returned orders will be sorted according to the in descending order. The makerAsset
and takerAsset
are defined in the file to enable convenient reuse across various operations.
Before executing the fill order, we will first need to ensure that the LO smart contract has sufficient . In this example, we will be filling half of the Maker order requested takingAmount
:
The Fill Batch Orders API requires the order of the orderIds
array to match their corresponding operatorSignatures
. Full code example can be found on .
To execute the transaction, we can use our to send the transaction with the required gas fees:
A transaction hash will be returned once the cancel order has been executed. You can copy this hash into a scanner (i.e. ) and see that your transaction has been successfully completed by the network.