PerpetualManagerFront.sol
PerpetualManager
is the contract handling all the Hedging Agents perpetuals and positions. There is one PerpetualManager
contract per collateral/stablecoin pair, meaning that every time a new collateral is deployed for a given stablecoin, a new PerpetualManager
contract should be deployed.margin
without having to close and open a new perpetual.PerpetualManager
has all the fees logic for Hedging Agents' perpetual futures: fees are computed when Hedging Agents go in the protocol and when they exit the protocol. It is important to note that there is no funding rate for Hedging Agents at Angle.ERC721Metadata
standard. They are non fungible, but they can be transferred from one address to another. It is possible to interact directly with the PerpetualManager
contract for the part that concerns this NFT logic: HAs can transfer their perpetuals from one address to another, approve other addresses to use their perpetuals or simply check the number of perpetuals they own.PerpetualManager
contract also works like a staking contract, that allows HAs to receive rewards depending on the amount of collateral cover.ReentrancyGuardUpgradeable
, IPerpetualManagerFront
, IPerpetualManager
, IStakingRewards
, IAccessControl
, IERC721Metadata
, IERC721
, IERC165
, PausableUpgradeable
, .PerpetualManager
contract in another contract (like a PoolManager
contract) will never change even though the code in the contract may change.rewardRate
, rewardsDuration
or rewardPerTokenStored
, the contract involves many other variables. For contract size reasons, some variables had to be stored as internal variables.entryRate
: Oracle value at the moment of perpetual creationentryTimestamp
: Timestamp at which the perpetual was createdmargin
: Amount initially brought in the perpetual (net from fees) + amount added - amount removed from it. This is the only element that can be modified in the perpetual after its creationcommittedAmount
: Amount hedged by the perpetual. This cannot be modified once the perpetual is createdtotalHedgeAmount
: Total amount of stablecoins that are insured (i.e. that could be redeemed against collateral thanks to HAs). When a HA opens a perpetual, it covers a fixed amount of stablecoins for the protocol, equal to the committed amount times the entry rate. totalHedgeAmount
is the sum of all these hedged amounts_perpetualIDcount
: Counter to generate a unique perpetual ID for each perpetual_stableMaster
: Address of the StableMaster
reference_token
: Interface for the underlying token accepted by this contractpoolManager
: PoolManager
handling the collateralrewardToken
: Interface for the token distributed as a staking rewardStableMaster
contract:_oracle
: Oracle to give the rate feed that is the price of the collateral with respect to the price of the stablecoin_feeManager:
Keeper address allowed to update the fees for this contracttargetHAHedge
: Target proportion of stablecoins issued using this collateral to insure with HAs. Above this proportion of coverage (or hedge), HAs cannot open new perpetuals. When keepers are forcing the cash out of some perpetuals, they are incentivized to bringing the hedge ratio to this proportionlimitHAHedge
: Limit proportion of stablecoins issued using this collateral that HAs can insure. Above this ratio forceClosePerpetuals
is activated and anyone can see its perpetual cashed outmaxLeverage
: Maximum leverage multiplier authorized for HAsmaintenanceMargin
: Maintenance Margin (in BASE_PARAMS
) for each perpetual. If the marginRatio
of a perpetual is below maintenanceMargin
: then the perpetual can be liquidatedlockTime
: Amount of time before a HA is allowed to withdraw funds from its perpetual (through cash out or remove)xHAFeesDeposit
: Thresholds for the ratio between to amount hedged and the amount to hedge. The bigger the ratio the bigger the fees will be because this means that the max amount to insure is soon to be reachedyHAFeesDeposit
: Deposit fees at threshold valuesxHAFeesWithdraw
: Thresholds for the hedge ratioyHAFeesDeposit
: Withdraw fees at threshold valueshaBonusMalusDeposit
: Extra parameter from the FeeManager
contract that is multiplied to the fees from above and that can be used to change deposit fees. It works as a bonus - malus fee, if haBonusMalusDeposit > BASE_PARAMS
, then the fee will be larger than haFeesDeposit
, if haBonusMalusDeposit < BASE_PARAMS
, fees will be smaller. This parameter, updated by keepers in the FeeManager
contract, could most likely depend on the collateral ratiohaBonusMalusWithdraw
: Extra parameter from the keeper that is multiplied to the fees from above and that can be used to change withdraw feeskeeperFeesLiquidationRatio
: Portion of the fees that go to keepers liquidating HA perpetualskeeperFeesLiquidationCap
: Cap on the fees that go to keepers liquidating a perpetualkeeperFeesClosingCap
: Cap on the fees that go to keepers cashing out perpetuals when too much collateral is hedged by HAs (hedge ratio above limitHAHedge
)xKeeperFeesClosing
: Thresholds on the values of the rate between the current hedged amount (totalHedgeAmount
) and the target hedged amount by HAs (targetCoveredAmount
) divided by 2. A value of 0.5
corresponds to a hedge ratio of 1
. Doing this allows to maintain an array with values of x
inferior to BASE_PARAMS
.yKeeperFeesClosing
: Values at thresholds of the proportions of the fees that should go to keepers cashing out perpetuals because there were too many HAs in the protocolperpetualData
: Mapping from a perpetual ID to the struct with the data of the perpetual (detailed above)_owners
: Mapping from perpetual ID to owner address_balances
: Mapping from owner address to perpetual owned count_operatorApprovals
, _perpetualApprovals
.POOLMANAGER_ROLE
: this role is admin of all roles, meaning changes in other roles will take place from the PoolManager
. This allows the manager contract corresponding to the contract to for instance revoke the GUARDIAN_ROLE
without having to call a specific function in this contractGUARDIAN_ROLE
: there is no GOVERNOR_ROLE
in this contract, the governor addresses are all guardianFEEMANAGER_ROLE
: some parameters need to be updated by the FeeManager
contract.STABLEMASTER_ROLE
: only the StableMaster
is allowed to change the reference to the Oracle contractbalanceOf()
, ownerOf()
, approve()
, transferFrom
, etc.msg.sender
should be the owner of the concerned perpetual or be approved for this perpetual.maxLeverage
parameter.block.timestamp
of perpetual creation as well as the oracle value at this time are also stored. It also computes the fees induced for creating the perpetual.owner
: Address of the future owner of the perpetualamountBrought
: Amount of collateral brought by the HAamountCommitted
: Amount of collateral hedged by the HAmaxOracleRate
: Maximum oracle value that the HA wants to see stored in the perpetual. This parameter serves as a protection against oracle manipulations for HAs opening perpetualsminNetMargin
: Minimum net margin that the HA is willing to see stored in the perpetualPerpetualManager
contract. The HA gets the current amount of her position depending on the entry oracle value and current oracle value. It is at the level of this function that HAs can exit the protocol.PoolManager
does not have enough collateral, the perpetual owner will be converted to a SLP.perpetualID
: ID of the perpetual to cash outto
: Address which will receive the proceeds from this perpetualminCashOutAmount
: Minimum net cash out amount that the HA is willing to get for cashing out the perpetual. It serves as a protection for HAs cashing out their perpetuals: it protects them both from fees that would have become too high and from a too big decrease in oracle value.margin
in a perpetual she controls for this stablecoin/collateral pair. Calling this function has the effect of decreasing the leverage multiple of this perpetual. If this perpetual is to be liquidated, the HA is not going to be able to add liquidity to it.perpetualID
: ID of the perpetual to which amount should be added to cashOutAmount
amount
: Amount to add to the perpetual's cashOutAmount
margin
in a perpetual she controls for this stablecoin/collateral pair. This increases the leverage multiple of this perpetual.perpetualID
: ID of the perpetual from which collateral should be removedamount
: Amount to remove from the perpetual's cashOutAmount
to
: Address which will receive the collateral removed from this perpetualperpetualIDs
: ID of the targeted perpetualsperpetualIDs
: ID of the targeted perpetualsperpetualID.
perpetualID
: ID of the perpetual which accumulated tokenscashOutAmount
of the perpetual owned by someone at a given oracle value. This function is used by the Collateral Settlement contract.perpetualID
: ID of the perpetualrate
: Oracle valuecashOutAmount
of the perpetualrewardsDuration
which is a protocol parameter. Only the reward distributor contract is allowed to call this function which starts a staking cycle.reward
: Amount of governance tokens to be distributed to HAsrewardsDistribution
associated to this contract. This function is part of the staking rewards interface and it is used to propagate a change of rewards distributor which handles all the staking at the scale of the contract.feeDeposit
: New deposit global feesfeeWithdraw
: New withdraw global feesgetReward
method as well as the functions allowing to create, modify or cash-out perpetuals. After calling this function, it is going to be impossible for HAs to interact with their perpetuals or claim their rewards on it._rewardsDuration
: Duration for the rewards for this contract_rewardsDistribution
: Address which will give the reward tokenssetLockTime
: Sets lockTime
that is the minimum amount of time HAs have to stay within the protocol. This parameter is used to prevent HAs from exiting after a certain amount of time, for instance if they have an insider information about the evolution of the price of a given collateral.setBoundsPerpetual
: Changes the maximum leverage authorized (commit/margin) and the maintenance margin under which perpetuals can be liquidated.setHAFees
: Sets xHAFees
that is the thresholds of values of the hedge ratio as well as yHAFees
that is the value of the deposit or withdraw fees at threshold. For deposit fees, the lower the x (that is the hedge ratio), the lower y should be. For withdraw fees, evolution should follow an opposite logic.setTargetAndLimitHAHedge
: Sets the target and limit proportions of collateral from users that can be insured by HAs. targetHAHedge
equal to BASE_PARAMS
means that all the collateral from users should be insured by HAs. targetHAHedge
equal to 0 means HA should not cover anything.setKeeperFeesRatio
: Sets the proportion of fees going to the keepers when liquidating a HA perpetual. This proportion should be inferior to BASE
setKeeperFeesCap
: Sets the maximum amounts going to the keepers when cashing out perpetuals because too much was hedged by HAs or when liquidating a perpetual.setKeeperFeesClosing
: Sets the x-array (ie thresholds) for FeeManager
when cashing out perpetuals and the y-array that is the value of the proportions of the fees going to keepers cashing out perpetuals. The x thresholds correspond to values of the hedge ratio divided by two.FeeManager
contract. This allows the PoolManager
contract to propagate changes to the PerpetualManager.
This is the only place where the _feeManager
can be changed, it is as if there was a FEEMANAGER_ROLE
for which PoolManager
was the admin.StableMaster
.