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.
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.
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.
> 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 │
summary: {
got: 2000,
gave: 2006.934268,
partialFill: false,
bounty: 0.000426,
feePaid: 0
},
successes: [],
> 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 │