Skip to main content

Reposting an offer in the posthook

In the smart offer tutorial the offer was fully taken by the taker at the end.

In case an offer is partially taken, the maker may want to repost a new offer for the residual.

Repost in posthook

In the tutorials the posthook emitted an event. However, since reposting is such a common action, it is already implemented for the simple cases - if you invoke super like below, then the base implementation of __posthookSuccess__ will repost the residual.

OfferMakerTutorial.sol
loading...

When writing posthooks to repost residuals there are both caveats and points to be aware:

  • Use updateOffer instead of posting a new offer. The old offer is not alive and can be reused (and the memory is hot), this is cheaper than using _newOffer.
  • Use the helper methods __residualGives__ and __residualWants__ supplied to calculate the residual (see example below).
  • Beware that updates can fail, e.g., due to too low density.
  • Make sure to refer to the guidelines on Safe offer logic guidelines.
  • Note that the parameters to __posthookSuccess__ already point out the old offer. This can save storage since we do not have to store IDs of posted offers.
  • Beware of gas usage changes on different code paths. As an example, the gas requirements for the tutorial increases to 80,000 to be able to repost.

If you need to write a custom hook, for instance, for reposting multiple offers, then it can be a good idea to look at the base implementation below. A good exercise is to change the code above to emit the value returned from super and trigger the reposted and dust (see density) scenarios.

MangroveOffer.sol
loading...