Mangrove
Developper
Developper
  • Welcome
  • Protocol
    • Introduction
    • Technical References
      • Overview
      • Ticks, ratios, and prices
      • Offer-list
        • Views on offers
      • Market-order
        • Delegation
      • Creating & Updating offers
        • Maker contract
        • Offer provisions
        • Gas requirement
        • Public data structures
        • Executing offers
      • Cleaning offers
      • Governance-parameters
        • Global variables
        • Local variables
        • Data structures and views
      • Periphery Contracts
        • MgvReader
        • MgvOracle
      • Literate Source Code
    • Background
      • Taking available liquidity
      • Making liquidity available
      • Reneging on offers
  • Strat Lib
    • What is the Strat Library?
    • Getting-started
      • Set Up Your Local Environment
      • Post a Smart Offer
    • Guides
      • Unlocking liquidity
      • Reposting an offer in the posthook
      • Using last look to renege trades
      • Determining gas requirements
      • Creating a Direct contract
      • Deploying your contract
      • Testing a maker contract
      • Safe offer logic guidelines
      • Approvals
    • Technical references
      • Principal hooks
      • Liquidity routing
      • API preferences
        • Core
          • SRC
            • IMangrove
        • Strats
          • SRC
            • Strategies
              • MangroveOffer
              • MangroveOrder
              • Integrations
                • AaveV3Borrower
                • AaveV3BorrowerImplementation
                • AaveV3BorrowerStorage
                • AaveV3Lender
                • CompoundModule
              • Interfaces
                • IForwarder
                • ILiquidityProvider
                • IOfferLogic
                • IOrderLogic
              • Offer_forwarder
                • Abstract
                  • Forwarder
              • Offer_maker
                • Abstract
                  • Direct
                • Market_making
                  • Kandel
                    • AaveKandel
                    • AaveKandelSeeder
                    • KandelSeeder
                    • Abstract
                      • AbstractKandelSeeder
                      • CoreKandel
                      • DirectWithBidsAndAsksDistribution
                      • GeometricKandel
                      • HasIndexedBidsAndAsks
                      • KandelLib
                      • TradesBaseQuotePair
              • Routeurs
                • SimpleRouter
                • Abstract
                  • AbstractRouter
                • Integrations
                  • AavePooledRouter
                  • HasAaveBalanceMemoizer
              • Utils
                • AccessControlled
              • Vendor
                • AAVE
                  • V3
                    • Contracts
                      • Dependencies
                        • Oppenzeppelin
                          • Contracts
                            • IERC20
                      • Interfaces
                        • IAToken
                        • IAaveIncentivesController
                        • IAaveOracle
                        • ICreditDelegationToken
                        • IInitializableAToken
                        • IPool
                        • IPoolAddressesProvider
                        • IPriceOracleGetter
                        • IScaledBalanceToken
                      • Protocol
                        • Libraries
                          • Configurations
                            • ReserveConfiguration
                          • Helpers
                            • Errors
                          • Types
                            • DataTypes
                    • Periphery
                      • Contracts
                        • MISC
                          • Interfaces
                            • IEACAggregatorProxy
                        • Rewards
                          • Interfaces
                            • IRewardsController
                            • IRewardsDistributor
                            • ITransferStrategyBase
                          • Libraries
                            • RewardsDataTypes
                • Compound
                  • CarefulMath
                  • Exponential
                  • ExponentialNoError
                  • ICompound
    • Background
      • Building Blocks
        • MangroveOffer
        • Direct
        • Forwarder
  • Vaults
    • Understanding vaults
      • Oracles
    • Managing a vault (CLI)
      • Deploying an oracle
      • Creating a vault
      • Monitoring the vault
      • Setting the vault position
      • Setting the fee data
      • Rebalancing
      • Adding or removing liquidity
    • Custom interactions
      • Oracles
      • Vault Factory
      • Managing a vault
        • Setting the position
        • Rebalancing
        • Setting a manager
        • Setting fee
  • Keeper Bots
    • Keeper Bots
    • Guides
      • Using borrowed funds for cleaning
    • Backgroud
      • The role of cleaning bots in Mangrove
      • The role of gas price updater bots in Mangrove
  • Adresses
    • Deployment Addresses
  • Quick Links
    • Glossary
    • Website
    • Whitepaper
Powered by GitBook
On this page
  • Summary
  • Funding an offer
  • Inputs
  • Checking an account balance
  • Inputs
  • Outputs
  • Withdrawing
  • Inputs
  • Outputs
  • Provision calculation
  • Balance adjustment when creating/updating offers
  • Bounty calculation
  1. Protocol
  2. Technical References
  3. Creating & Updating offers

Offer provisions

PreviousMaker contractNextGas requirement

Last updated 1 month ago

Summary

When an offer fails, the caller has wasted some gas. To compensate the caller, Mangrove gives them a in native tokens. Offers must enough native token to maximize the chances that Mangrove can compensate the caller. In more details:

  • Every maker contract that posted an offer has a balance in native token held by Mangrove. Funds can be freely added to or withdrawn from the balance.

  • Whenever the contract 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 can either stay on the offer or the logic can choose to have it credited back to the logic's account balance.

    • If the offer logic 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 maker contract's account balance.

Funding an offer

There are three ways a maker contract can credit its balance on Mangrove:

  1. the contract may either call the fund function,

  2. make a call to the fallback function with some value, or

  3. pay on the fly when a new offer is posted or updated.

function fund(address maker) public payable;
// Offer Maker at address `maker` has been credited of `amount` wei
event Credit(address maker, uint amount);
"mgv/dead" // Mangrove contract is no longer live
import {IMangrove} from "@mgv/src/IMangrove.sol";

//context 
// IMangrove mgv = IMangrove(payable(<address of Mangrove>));
IMangrove mgv = IMangrove(payable(mgv)); // Mangrove contract
address maker_contract; // address of the maker contract one is willing to provision

// funding maker_contract
mgv.fund{value: 0.1 ether}(maker_contract);

// if funding oneself one can use the overload:
mgv.fund{value: 0.1 ether}();
// 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");

Inputs

  • maker: the maker who will be credited with msg.value on Mangrove

Danger : 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

function balanceOf(address maker) external view returns (uint balance);

Inputs

  • maker is the account of which you want to read the balance.

Outputs

  • balance is the available balance of maker.

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/retract an offer.

function withdraw(uint amount) external returns (bool noRevert);
event Debit(address maker, uint amount);
// Trying to withdraw unavailable funds
"mgv/insufficientProvision"
import {IMangrove} from "@mgv/src/IMangrove.sol";

//context 
// IMangrove mgv = IMangrove(payable(<address of Mangrove>));
IMangrove mgv = IMangrove(payable(mgv)); // Mangrove contract

uint wei_balance = mgv.balanceOf(address(this));
require(mgv.withdraw(wei_balance), "Mangrove failed to transfer funds");

Inputs

  • amount the amount of native token (in wei) one wishes to withdraw from Mangrove's provisions.

Outputs

  • noRevert whether the transfer was successful.

Important points :

  • The account credited will be msg.sender.

Provision calculation

The provision is calculated with the following formula (in wei):

Balance adjustment when creating/updating offers

Whenever an offer is created or updated, Mangrove uses the Provision 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).

Bounty calculation

The bounty is paid to the taker as compensation for spent gas. It depends on how much gas the offer uses before failing. It is calculated with the following formula, based on the provision previously calculated:

Thus the bounty is capped at the offer's original provision.

amount must be ≤\leq≤ your available balance (available with balanceOf)

provision=max⁡(gaspricemgv,gaspriceofr)×(gasreq+gasbasemgv)×106\textrm{provision} = \max(\textrm{gasprice}_{\textrm{mgv}},\textrm{gasprice}_{\textrm{ofr}}) \times (\textrm{gasreq} + \textrm{gasbase}_{\textrm{mgv}}) \times 10^6provision=max(gaspricemgv​,gaspriceofr​)×(gasreq+gasbasemgv​)×106​

gaspricemgv\textrm{gasprice}_{\textrm{mgv}}gaspricemgv​ is the gasprice global governance parameter (in Mwei per gas unit)

gaspriceofr\textrm{gasprice}_{\textrm{ofr}}gaspriceofr​ is the gasprice argument of the function being called (newOffer or updateOffer) also in Mwei per gas unit.

gasreq\textrm{gasreq}gasreq is the gasreq amount of gas units required to execute the offer.

gasbasemgv\textrm{gasbase}_{\rm mgv}gasbasemgv​ is the offer_gasbase local governance parameter.

bounty=min⁡(offer.provision,(gasused+gasbasemgv)×gaspricemgv×106)\textrm{bounty} = \min(\textrm{offer.provision},(\textrm{gasused} + \textrm{gasbase}_{\textrm{mgv}}) \times \textrm{gasprice}_{\textrm{mgv}} \times 10^6)bounty=min(offer.provision,(gasused+gasbasemgv​)×gaspricemgv​×106)

offer.provision\textrm{offer.provision}offer.provision is the provision amount calculated when the offer was posted.

gasused\textrm{gasused}gasused is the gasused amount of gas units actually used when executing the offer.

gasbasemgv\textrm{gasbase}_{\textrm{mgv}}gasbasemgv​ is the offer_gasbase local governance parameter.

gaspricemgv\textrm{gasprice}_{\textrm{mgv}}gaspricemgv​ is Mangrove's global gasprice at the time of offer execution.

bounty
provision