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
  • Gatekeeping​
  • Token amounts​
  • Which part of your Maker contract should not be in the offer logic?​
  • How to factor your offer logic between makerExecute and makerPosthook.​
  • Hooks that make your contract dev friendly​
  • Keepers as offer maintainers​
  1. Strat Lib
  2. Guides

Safe offer logic guidelines

PreviousTesting a maker contractNextApprovals

Last updated 1 month ago

Gatekeeping

Offer logic should only be called by Mangrove

External functions of an , makerExecute and makerPosthook, should only be callable if msg.sender is Mangrove. implemented with the strat library take care of this by construction.

Public functions that are called by offer logic

It is sometimes convenient to define public function that can also be called internally during offer logic's execution. If such function needs to be restricted to privileged access, one cannot simply use require(msg.sender == admin_address) as msg.sender will be Mangrove when the function is called internally by the offer logic. Using a guard of the form require(msg.sender == admin_address || msg.sender == mangrove_address) will work though (notice that internal call preserves msg.sender of makerExecute).

Token amounts

and tokens may have different decimals. Mangrove always use raw amounts, so offer do not have directly a price but a and a both expressed in raw token amounts.

Which part of your should not be in the ?

Functions in offer logic are automatically executed by Mangrove, who is then msg.sender. Beware that taker is not necessarily tx.origin, as a contract could be acting as a taker. Importantly, make sure that your logic is not exploitable by context manipulation, which could force your offer to fail.

How to factor your offer logic between and .

Must be in maker execute:

  • decision to renege on trade

  • every moves that are needed to bring liquidity promised to the taker.

The offer logic in makerExecute should be gas bounded since an out-of-gas exception will lead to Mangrove transferring your whole to the taker as a .

Must be in makerPosthook:

  • any logging of a revert reason raised during makerExecute. The (truncated to a bytes32) reason is passed to makerPosthook in the makerData field.

Should be in makerPosthook:

  • actions that are not gas bounded, such as posting or updating an offer on Mangrove.

  • in general, calls which may raise an exception that should not cause the current trade execution to fail.

  • offer logic that revert during makerPosthook do not abort trade and Mangrove will emit a PosthookFail log.

__activate__(IERC20 token) internal override {
    super.__activate__(token); // perform all pre defined approvals
    token.approve({spender: <lender>, owner: address(this), amount:type(uint).max});
}

cascading activation

The activate function's default behavior is to perform the maker contract's activations and then ask its router (if any) to perform its own.

cascading checkList

The checkList function's default behavior is to perform maker contract's checkList and then ask its router (if any) to perform its own.

any write action to the to which the currently executed offer belongs.

Hooks that make your contract dev friendly

Activate functions

The public function of all strat library based maker contracts, enables one to perform all maker contract centric approvals, for the provided token list, in a single transaction. If your implementation requires more approval from the maker contract you should add them by overriding the __activate__ hook. For instance assume your offer logic requires depositing asset on a lender's pool and the corresponding function requires an approval from the depositor, either you should approve the lender each time your logic deposits, or if you would rather approve once for all (with infinite approval), then you should add the approval to activate using:

The function follows the same pattern for custom (see router ).

Checklist functions

is a dev friendly view function that reverts whenever the tokens list contains an ERC20 token that msg.sender cannot yet use to trade using the maker contract, typically because it requires an approval from msg.sender. The revert reason should be documented or as self contained as possible. Following the activate pattern, this function can be augmented with additional checks, using the __checkList__ hook.

Router use a similar to verify that maker contract can source tokens via the router (see router ).

Keepers as offer maintainers

A failing offer should not be the consequence of a bug in your offer logic, but rather be a feature of it. Raising an exception during makerExecute is the proper way to cancel a trade if your logic deems it necessary. You should customize the for this, in order to fail early in the trade and save you money. For instance, if your logic relies on funds being on a lender, and a price oracle as a relative protection against arbitrage, you should check the oracle before redeeming the funds from the lender.

​
​
offer logic
Maker contracts
​
​
Outbound
inbound
wants
gives
Maker contract
offer logic
​
makerExecute
makerPosthook
​
provision
bounty
offer list
​
​
activate(IERC20[] calldata tokens)
activate(IERC20[] calldata tokens)
routers
activation
​
checkList(IERC20[] calldata tokens)
function
checklist
​
last look
hook