Skip to main content

Use Fill or Kill


This section will go through how to create a Fill or Kill (FOK) order using mangrove.js.

We assume you know how to connect to Mangrove. We are going to be buying 2000 USDC at a maximum avg. price of 1.3. It is a requirement for this script to have enough USDC. If you do not have enough USDC, you can use a testnet and mint some USDC. This can either be done by going to our dApp or by minting directly in the script (see the commented lines in the script about minting).


After having connected to Mangrove, we then have to make sure that we have the correct approvals for transferring our USDC tokens. When approving for transfers we have to determine what contract is going to make the actual transfers. If we were to use the normal buy function for a market, we would be using the Mangrove protocol, to make a standard market order. This would mean that we would have to approve Mangrove to make transfers of USDC on our behalf.

In this case we are not going to be using the Mangrove protocol directly. Instead we are going to be using a different contract, provided by the Mangrove strat lib - the MangroveOrder contract. This contract makes it possible to make a real FoK order and not just a normal market order. This means that we need to approve the MangroveOrder contract to handle all our USDC transfers. One way of doing this, would be to just call approve directly on the USDC token, with the MangroveOrder contract as spender. But because strategies made with the Mangrove strat lib, can be using a more complex way of dealing with transfers, we should not rely on calling approve directly on the token. Instead we will create an OfferLogic using the MangroveOrder contract. This will provide us with an 'approveToken' function. This function will handle all the necessary approvals in order to use a token with the contract.


Buying with MangroveOrder

We are now ready to buy some DAI using a FoK order. It is very simple to do. Using the same method as for a market order (buy), we just give it an extra parameter fillOrKill which we set to true. This way we use the MangroveOrder contract to buy.


Here we show logs asks for the market before buying and after buying, you will see that the first 3 offers were taken. Offers 1669, 3344 and 1157 were all taken. But when we look at the result of the Fill or Kill order, we see that we got a bounty. This means that one of the offers failed and we got a bounty for making the offer fail.

Asks before Fill or Kill order
> market.consoleAsks();
(index) │ id │ maker │ volume │ price │
Result of Fill or Kill order
  summary: {
got: 2000,
gave: 2006.934268,
partialFill: false,
bounty: 0.000426,
feePaid: 0
successes: [],
Asks after fill or kill order
> market.consoleAsks()
(index) │ id │ maker │ volume │ price │