When an offer fails, the caller has wasted some gas. To compensate the caller, Mangrove gives them a bounty in native tokens. Offers must provision enough ethers to maximize the chances that Mangrove can compensate the caller. In more details:
Every offer logic that posted an offer has a balance in ethers held by Mangrove. Funds can be freely added to or withdrawn from the balance.
Whenever the logic creates or updates an offer, its balance is adjusted so that enough native tokens are locked as the offer's provision.
If the offer is retracted that provision is credited back to the logic's account balance.
If the offer is executed and fails, part or all of the provision is sent as compensation, to the caller. We call that the bounty. The rest of the provision is credited back to the offer logic's account balance.
Balance funding & withdrawal
Funding an offer
There are three ways an offer logic can credit its balance on Mangrove. (1) The logic may either call the fund function, or (2) make a call to the fallback function with some value, or (3) pay on the fly when a new offer is posted.
functionfund(addressmaker)publicpayable;
// Offer Maker at address `maker` has been credited of `amount` weieventCredit(addressmaker, uintamount);
"mgv/dead"// Mangrove contract is no longer live
fund.sol
import"src/IMangrove.sol";//context IMangrove mgv;// Mangrove contract addressaddress maker_contract;// address of the maker contract one is willing to provision// funding maker_contractmgv.fund{value:0.1ether}(maker_contract);// if funding oneself one can use the overload:mgv.fund{value:0.1ether}();// which is equivalent to `msg.fund{value:0.1 ether}(address(this))// to avoid erreoneous transfer of native tokens to Mangrove, the fallback function will also credit `msg.sender`:(bool noRevert,)=address(mgv).call{value: amount}("");require(noRevert,"transfer failed");
fund.js
const{ethers}=require("ethers");//contextletMGV;// address of MangroveletMGV_abi;// Mangrove contract's abiletmaker_contract_address;// address of the Maker ContractconstMangrove=newethers.Contract(MGV,MGV_abi,ethers.provider );letoverrides={value:ethers.parseUnits("0.1",18) };// provisioning Mangrove on behalf of MakerContractawaitMangrove["fund(address)"](maker_contract_address,overrides);
Inputs
maker the offer logic's balance on Mangrove to credit
Do not use send or transfer to credit Mangrove
Upon receiving funds, Mangrove will credit the amount sent to maker (or msg.sender if the receive function was called). This involves writing to storage, which consumes more gas than the amount given by send and transfer.
Checking an account balance
Inputs
who The account of which you want to read the balance.
Outputs
balance The available balance of who.
Withdrawing
At any time, your available balance can be withdrawn. It may be less than what you deposited: your balance adjusts every time you create/update an offer.
Inputs
amount the amount of ethers (in wei) one wishes to withdraw from Mangrove's provisions.
Outputs
noRevert whether the ether transfer was successful.
Important points
The account credited will be msg.sender.
amount must be ≤ your available balance (available with balanceOf)
Balance adjustment when creating/updating offers
Whenever an offer is created or updated, Mangrove applies to following formula to get the offer's required provision in wei:
Mangrove will adjust the balance of the caller to ensure that provision wei are available as bounty if the offer fails. If the offer was already provisioned, the adjustment may be small, and the balance may actually increase -- for instance, if the gasprice dropped recently.
Incentivized book cleaning
Provisions are calculated so that, within reasonable gas estimates, taking a failing offer should be profitable for the taker.
Gas optimization
If you frequently update your offers, we recommend using a consistent, high gasprice argument, above the actual expected gas prices. Not changing gasprice when you call updateOffer will make the call cheaper (you save one SSTORE).
Provision and offer bounty
Applied bounty
Suppose an offer requires gofr gas units to execute. As explained above, Mangrove will require the logic posting the offer to provision β WEI. Suppo se the offer is executed during a Taker Order and fails after gusedgas units (gused<gofr). The portion of the bounty that will be transferred to the Offer Taker's account is G˙∗(g˙0/n+g˙1+gused) where G˙, g˙0, n and g˙1are respectively the global.gasprice, local.overhead_gasbase, the number of offers executed during the take order, and the local.offer_gasbase values at the time the offer is taken (which may differ from their values at the time the offer was posted, as a consequence of some parameter changes by the governance).