Skip to main content

Use Fill or Kill

Intro

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).

Approvals

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.

packages/mangrove.js/examples/how-tos/fill-or-kill.js
loading...

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.

packages/mangrove.js/examples/how-tos/fill-or-kill.js
loading...

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 │
├─────────┼──────┼──────────────────────────────────────────────┼────────────────────┼────────────────────────┤
01669'0x4326Ab97823d7509C1f0CB3bF68151081B26c970'1376.62734385504151.00346478817687987934
13344'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'1622.8363734073791.00346894837019272508
21157'0x4326Ab97823d7509C1f0CB3bF68151081B26c970'1163.37093081162541.0034723140155791771
34214'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'1227.05620381714381.003482667028260286
4930'0x4326Ab97823d7509C1f0CB3bF68151081B26c970'1486.7357925923641.00348572922872847571
53837'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'1458.65235284146431.00348711133850858816
62721'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'629.97485275438231.00348954285398244855
72668'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'1504.3077404879911.00349288205504048327
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 │
├─────────┼──────┼──────────────────────────────────────────────┼────────────────────┼────────────────────────┤
04214'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'1227.05620381714381.003482667028260286
1930'0x4326Ab97823d7509C1f0CB3bF68151081B26c970'1486.7357925923641.00348572922872847571
23837'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'1458.65235284146431.00348711133850858816
32721'0x2CB51201CD176CcEa67a9c0B64391aE34e50C058'629.97485275438231.00348954285398244855