Fill Limit Order
Last updated
Last updated
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 Off-Chain Relay, On-Chain Settlement for more detail on this design.
KyberSwap exposes 2 API options for Takers looking to fill orders on-chain:
/read-ks/api/v1/encode/fill-order-to
: Encode the fill order data to be sent on-chain. This API can be used to fill a single order.
/read-ks/api/v1/encode/fill-batch-orders-to
: Encode the fill batch order data to be sent on-chain. This API can be used to fill multiple orders.
In order to fill an order, Takers will first have to request an Operator signature via:
/read-partner/api/v1/orders/operator-signature
: Get the KyberSwap Operator to sign the target orders so that it can be filled.
In addition to the above, Takers are also able to query active or open order(s) to aid with filtering orders to fill:
/read-partner/api/v1/orders
: Returns orders for the queried token pair sorted by best rates in descending order.
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-partner/api/v1/orders
to get the list of "active" or "open" orders by token pair:
Note that the returned orders will be sorted according to the best rates in descending order. The makerAsset
and takerAsset
are defined in the constants.ts
file to enable convenient reuse across various operations.
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.
Before executing the fill order, we will first need to ensure that the LO smart contract has sufficient allowance to spend the taker's ERC20 token. In this example, we will be filling half of the Maker order requested takingAmount
:
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:
Filling Batch Orders
For simplicity, the example below fills a single order using /read-ks/api/v1/encode/fill-order-to
. KyberSwap Limit Orders exposes another /read-ks/api/v1/encode/fill-batch-orders-to
API which enables Takers to get the encoded data to batch fill orders.
By filling multiple orders in a single on-chain tx, batch fill orders are more efficient. The only difference between the 2 APIs is the formatting of orderIds
and operatorSignatures
when preparing the requestBody for the respective API.
The Fill Batch Orders API requires the order of the orderIds
array to match their corresponding operatorSignatures
. Full code example can be found on postFillBatchOrders.ts.
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.
To execute the transaction, we can use our ethers.js signer instance 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. PolygonScan) and see that your transaction has been successfully completed by the network.