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 USDT. If you do not have enough USDT, you can use a testnet and mint some USDT. 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 USDT 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 USDT 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 USDT transfers. For this guide, we will just call approve directly on the USDT token, with the MangroveOrder contract as spender.

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

Buying with MangroveOrder​

We are now ready to buy some USDC 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.

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 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 0 β”‚ 1669 β”‚ '0x4326Ab97823d7509C1f0CB3bF68151081B26c970' β”‚ 1376.6273438550415 β”‚ 1.00346478817687987934 β”‚
β”‚ 1 β”‚ 3344 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 1622.836373407379 β”‚ 1.00346894837019272508 β”‚
β”‚ 2 β”‚ 1157 β”‚ '0x4326Ab97823d7509C1f0CB3bF68151081B26c970' β”‚ 1163.3709308116254 β”‚ 1.0034723140155791771 β”‚
β”‚ 3 β”‚ 4214 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 1227.0562038171438 β”‚ 1.003482667028260286 β”‚
β”‚ 4 β”‚ 930 β”‚ '0x4326Ab97823d7509C1f0CB3bF68151081B26c970' β”‚ 1486.735792592364 β”‚ 1.00348572922872847571 β”‚
β”‚ 5 β”‚ 3837 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 1458.6523528414643 β”‚ 1.00348711133850858816 β”‚
β”‚ 6 β”‚ 2721 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 629.9748527543823 β”‚ 1.00348954285398244855 β”‚
β”‚ 7 β”‚ 2668 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 1504.307740487991 β”‚ 1.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 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 0 β”‚ 4214 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 1227.0562038171438 β”‚ 1.003482667028260286 β”‚
β”‚ 1 β”‚ 930 β”‚ '0x4326Ab97823d7509C1f0CB3bF68151081B26c970' β”‚ 1486.735792592364 β”‚ 1.00348572922872847571 β”‚
β”‚ 2 β”‚ 3837 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 1458.6523528414643 β”‚ 1.00348711133850858816 β”‚
β”‚ 3 β”‚ 2721 β”‚ '0x2CB51201CD176CcEa67a9c0B64391aE34e50C058' β”‚ 629.9748527543823 β”‚ 1.00348954285398244855 β”‚