Skip to main content

Forwarder

Forwarder​

Each offer posted via this contract are managed by their offer maker, not by this contract's admin. This class implements IForwarder, which contains specific Forwarder logic functions in additions to IOfferLogic interface.

GAS_APPROX​

uint256 GAS_APPROX

approx of amount of gas units required to complete __posthookFallback__ when evaluating penalty.

OwnerData​

data associated to each offer published on Mangrove by this contract.

OwnerData packs into one word.

Parameters​

NameTypeDescription
struct OwnerData {
address owner;
uint96 weiBalance;
}

ownerData​

mapping(bytes32 => mapping(uint256 => struct Forwarder.OwnerData)) ownerData

Owner data mapping.

mapping is olKeyHash -> offerId -> OwnerData 'ownerData[olKeyHash][offerId].owner == makerifmakeris offer owner ofofferIdin the(out, in)` offer list.

onlyOwner​

modifier onlyOwner(bytes32 olKeyHash, uint256 offerId)

modifier to enforce function caller to be offer owner

mgvOrOwner​

modifier mgvOrOwner(bytes32 olKeyHash, uint256 offerId)

modifier to enforce function caller to be offer owner or MGV (for use in the offer logic)

constructor​

constructor(contract IMangrove mgv, contract AbstractRouter router) internal

Forwarder constructor

Parameters​

NameTypeDescription
mgvcontract IMangrovethe deployed Mangrove contract on which this contract will post offers.
routercontract AbstractRouterthe router that this contract will use to pull/push liquidity from offer maker's reserve. This must not be NO_ROUTER.

setRouter​

function setRouter(contract AbstractRouter router) public virtual

offerOwners​

function offerOwners(bytes32 olKeyHash, uint256[] offerIds) public view returns (address[] offerOwners_)

view on offer owners.

if offerIds[i]==address(0) if and only if this offer has no owner.

Parameters​

NameTypeDescription
olKeyHashbytes32the hash of the offer list key.
offerIdsuint256[]an array of offer identifiers on the offer list.

Return Values​

NameTypeDescription
offerOwners_address[]

addOwner​

function addOwner(bytes32 olKeyHash, uint256 offerId, address owner, uint256 leftover) internal

grants managing (update/retract) rights on a particular offer.

Parameters​

NameTypeDescription
olKeyHashbytes32the hash of the offer list key.
offerIduint256the offer identifier in the offer list.
owneraddressthe address of the offer maker.
leftoveruint256the fraction of msg.value that is not locked in the offer provision due to rounding error (see _newOffer).

deriveGasprice​

function deriveGasprice(uint256 gasreq, uint256 provision, uint256 offerGasbase) internal pure returns (uint256 gasprice, uint256 leftover)

computes the maximum gasprice that can be covered by the amount of provision given in argument.

the returned gasprice is slightly lower than the real gasprice that the provision can cover because of the rounding error due to division

Parameters​

NameTypeDescription
gasrequint256the gas required by the offer
provisionuint256the amount of native token one wishes to use, to provision the offer on Mangrove.
offerGasbaseuint256Mangrove's offer_gasbase.

Return Values​

NameTypeDescription
gaspriceuint256the gas price that is covered by provision - leftover.
leftoveruint256the sub amount of provision that is not used to provision the offer.

ownerOf​

function ownerOf(bytes32 olKeyHash, uint256 offerId) public view returns (address owner)

view on an offer owner.

ownerOf(in,out,id) is equivalent to offerOwners(in, out, [id]) but more gas efficient.

Parameters​

NameTypeDescription
olKeyHashbytes32the hash of the offer list key.
offerIduint256the offer identifier on the offer list.

Return Values​

NameTypeDescription
owneraddressthe offer maker that can manage the offer.

deriveAndCheckGasprice​

function deriveAndCheckGasprice(struct IOfferLogic.OfferArgs args) internal view returns (uint256 gasprice, uint256 leftover)

Derives the gas price for the new offer and verifies it against the global configuration.

the returned gasprice is slightly lower than the real gasprice that the provision can cover because of the rounding error due to division

Parameters​

NameTypeDescription
argsstruct IOfferLogic.OfferArgsfunction's arguments in memory

Return Values​

NameTypeDescription
gaspriceuint256the gas price that is covered by provision - leftover.
leftoveruint256the sub amount of provision that is not used to provision the offer.

_newOffer​

function _newOffer(struct IOfferLogic.OfferArgs args, address owner) internal returns (uint256 offerId, bytes32 status)

Inserts a new offer on a Mangrove Offer List.

_If inside a hook, one should call _newOffer to create a new offer and not directly MGV.newOffer to make sure one is correctly dealing with:

  • offer ownership
  • offer provisions and gasprice_

Parameters​

NameTypeDescription
argsstruct IOfferLogic.OfferArgsfunction's arguments in memory
owneraddressthe address of the offer owner

Return Values​

NameTypeDescription
offerIduint256the identifier of the new offer on the offer list. Can be 0 if posting was rejected by Mangrove and args.noRevert is true.
statusbytes32the status of the new offer on Mangrove if the call has not reverted. It may be NEW_OFFER_SUCCESS or Mangrove's revert reason if args.noRevert was set to true. Forwarder logic does not manage user funds on Mangrove, as a consequence: An offer maker's redeemable provisions on Mangrove is just the sum Slocked(maker)S_locked(maker) of locked provision in all live offers it owns plus the sum Sfree(maker)S_free(maker) of weiBalance's in all dead offers it owns (see OwnerData.weiBalance). Notice βˆ‘iSfree(makeri)\sum_i S_free(maker_i) <= MGV.balanceOf(address(this)). Any fund of an offer maker on Mangrove that is either not locked on Mangrove or stored in the OwnerDatafree wei's is thus not recoverable by the offer maker (although it is admin recoverable). Therefore we need to make sure that allmsg.valueis either used to provision the offer atgaspriceor stored in the offer data underweiBalance. To do so, we do not let offer maker fix a gasprice. Rather we derive the gasprice based on msg.value. Because of rounding errors in deriveGaspricea small amount of WEIs will accumulate in mangrove's balance ofthiscontract We assign this leftover to the correspondingweiBalanceofOwnerData`.

UpdateOfferVars​

struct UpdateOfferVars {
uint256 leftover;
Global global;
Local local;
OfferDetail offerDetail;
}

_updateOffer​

function _updateOffer(struct IOfferLogic.OfferArgs args, uint256 offerId) internal returns (bytes32 reason)

Internal updateOffer, using arguments and variables on memory to avoid stack too deep.

Parameters​

NameTypeDescription
argsstruct IOfferLogic.OfferArgsA memory struct containing the offer parameters to update.
offerIduint256An unsigned integer representing the identifier of the offer to be updated.

Return Values​

NameTypeDescription
reasonbytes32Either REPOST_SUCCESS or Mangrove's revert reason if update was rejected by Mangrove and args.noRevert is true.

provisionOf​

function provisionOf(struct OLKey olKey, uint256 offerId) external view returns (uint256 provision)

computes the amount of native tokens that can be redeemed when deprovisioning a given offer.

Parameters​

NameTypeDescription
olKeystruct OLKeythe offer list key.
offerIduint256the identifier of the offer in the offer list

Return Values​

NameTypeDescription
provisionuint256the amount of native tokens that can be redeemed when deprovisioning the offer

_retractOffer​

function _retractOffer(struct OLKey olKey, uint256 offerId, bool deprovision) internal returns (uint256 freeWei)

Retracts an offer from an Offer List of Mangrove.

An offer that is retracted without deprovision is retracted from the offer list, but still has its provisions locked by Mangrove. Calling this function, with the deprovision flag, on an offer that is already retracted must be used to retrieve the locked provisions.

Parameters​

NameTypeDescription
olKeystruct OLKeythe offer list key.
offerIduint256the identifier of the offer in the offer list
deprovisionboolif set to true if offer owner wishes to redeem the offer's provision.

Return Values​

NameTypeDescription
freeWeiuint256the amount of native tokens (in WEI) that have been retrieved by retracting the offer.

put​

function __put__(uint256 amount, struct MgvLib.SingleOrder order) internal virtual returns (uint256)

Hook that implements where the inbound token, which are brought by the Offer Taker, should go during Taker Order's execution.

put received inbound tokens on offer maker's reserve during makerExecute if nothing is done at that stage then it could still be done during makerPosthook. However one would then need to pay attention to the following fact: if order.olKey.inbound_tkn is not pushed to reserve during makerExecute, in the posthook of this offer execution, the order.olKey.inbound_tkn balance of this contract would then contain the sum of all payments of offers managed by this that are in a better position in the offer list (because posthook is called in the call stack order). here we maintain an invariant that this balance is empty (both for order.olKey.inbound_tkn and order.olKey.outbound_tkn) at the end of makerExecute.

Parameters​

NameTypeDescription
amountuint256of inbound tokens that are on this contract's balance and still need to be deposited somewhere
orderstruct MgvLib.SingleOrderis a recall of the taker order that is at the origin of the current trade.

Return Values​

NameTypeDescription
[0]uint256

get​

function __get__(uint256 amount, struct MgvLib.SingleOrder order) internal virtual returns (uint256)

Hook that implements where the outbound token, which are promised to the taker, should be fetched from, during Taker Order's execution.

get outbound tokens from offer owner reserve

Parameters​

NameTypeDescription
amountuint256of outbound tokens that still needs to be brought to the balance of this contract when entering this function
orderstruct MgvLib.SingleOrderis a recall of the taker order that is at the origin of the current trade.

Return Values​

NameTypeDescription
[0]uint256

handleResidualProvision​

function __handleResidualProvision__(struct MgvLib.SingleOrder order) internal virtual

Hook that defines what needs to be done to the part of an offer provision that was added to the balance of this on Mangrove after an offer has failed.

if offer failed to execute, Mangrove retracts and deprovisions it after the posthook call. As a consequence if this hook is reached, this balance on Mangrove will increase, after the posthook, of some amount nn of native tokens. We evaluate here an underapproximation Β n~n in order to credit the offer maker in a pull based manner: failed offer owner can retrieve Β n~n by calling retractOffer on the failed offer. because Β n<n~n<n a small amount of WEIs will accumulate on the balance of this on Mangrove over time. Note that these WEIs are not burnt since they can be admin retrieved using withdrawFromMangrove.

Parameters​

NameTypeDescription
orderstruct MgvLib.SingleOrderis a recall of the taker order that failed

checkList​

function __checkList__(contract IERC20 token) internal view virtual

verifies that msg.sender is an allowed reserve id to trade tokens with this contract

Parameters​

NameTypeDescription
tokencontract IERC20a token that is traded by this contract