PositionsManager
Inherits: IPositionsManager, MatchingEngine
Author: Morpho Labs.
Main Logic of Morpho Protocol, implementation of the 5 main functionalities: supply, borrow, withdraw, repay and liquidate.
Functions
supplyLogic
LOGIC ///
Implements supply logic.
function supplyLogic(address _poolToken, address _from, address _onBehalf, uint256 _amount, uint256 _maxGasForMatching)
external;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token the user wants to interact with. |
_from | address | The address of the account sending funds. |
_onBehalf | address | The address of the account whose positions will be updated. |
_amount | uint256 | The amount of token (in underlying). |
_maxGasForMatching | uint256 | The maximum amount of gas to consume within a matching engine loop. |
borrowLogic
Peer-to-peer supply /// Pool supply ///
Implements borrow logic.
function borrowLogic(address _poolToken, uint256 _amount, uint256 _maxGasForMatching) external;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market the user wants to interact with. |
_amount | uint256 | The amount of token (in underlying). |
_maxGasForMatching | uint256 | The maximum amount of gas to consume within a matching engine loop. |
withdrawLogic
Peer-to-peer borrow /// Pool borrow ///
Implements withdraw logic with security checks.
function withdrawLogic(
address _poolToken,
uint256 _amount,
address _supplier,
address _receiver,
uint256 _maxGasForMatching
) external;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market the user wants to interact with. |
_amount | uint256 | The amount of token (in underlying). |
_supplier | address | The address of the supplier. |
_receiver | address | The address of the user who will receive the tokens. |
_maxGasForMatching | uint256 | The maximum amount of gas to consume within a matching engine loop. |
repayLogic
Implements repay logic with security checks.
function repayLogic(
address _poolToken,
address _repayer,
address _onBehalf,
uint256 _amount,
uint256 _maxGasForMatching
) external;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market the user wants to interact with. |
_repayer | address | The address of the account repaying the debt. |
_onBehalf | address | The address of the account whose debt is repaid. |
_amount | uint256 | The amount of token (in underlying). |
_maxGasForMatching | uint256 | The maximum amount of gas to consume within a matching engine loop. |
liquidateLogic
Liquidates a position.
function liquidateLogic(address _poolTokenBorrowed, address _poolTokenCollateral, address _borrower, uint256 _amount)
external;
Parameters
Name | Type | Description |
---|---|---|
_poolTokenBorrowed | address | The address of the pool token the liquidator wants to repay. |
_poolTokenCollateral | address | The address of the collateral pool token the liquidator wants to seize. |
_borrower | address | The address of the borrower to liquidate. |
_amount | uint256 | The amount of token (in underlying) to repay. |
increaseP2PDeltasLogic
Implements increaseP2PDeltas logic.
The current Morpho supply on the pool might not be enough to borrow _amount
before resupplying it.
In this case, consider calling this function multiple times.
function increaseP2PDeltasLogic(address _poolToken, uint256 _amount) external isMarketCreated(_poolToken);
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market on which to increase deltas. |
_amount | uint256 | The maximum amount to add to the deltas (in underlying). |
_unsafeWithdrawLogic
INTERNAL ///
Implements withdraw logic without security checks.
function _unsafeWithdrawLogic(
address _poolToken,
uint256 _amount,
address _supplier,
address _receiver,
uint256 _maxGasForMatching
) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market the user wants to interact with. |
_amount | uint256 | The amount of token (in underlying). |
_supplier | address | The address of the supplier. |
_receiver | address | The address of the user who will receive the tokens. |
_maxGasForMatching | uint256 | The maximum amount of gas to consume within a matching engine loop. |
_unsafeRepayLogic
Pool withdraw /// Transfer withdraw /// Breaking withdraw ///
Implements repay logic without security checks.
function _unsafeRepayLogic(
address _poolToken,
address _repayer,
address _onBehalf,
uint256 _amount,
uint256 _maxGasForMatching
) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market the user wants to interact with. |
_repayer | address | The address of the account repaying the debt. |
_onBehalf | address | The address of the account whose debt is repaid. |
_amount | uint256 | The amount of token (in underlying). |
_maxGasForMatching | uint256 | The maximum amount of gas to consume within a matching engine loop. |
_supplyToPool
Pool repay /// Transfer repay /// Breaking repay ///
Supplies underlying tokens to Compound.
function _supplyToPool(address _poolToken, ERC20 _underlyingToken, uint256 _amount) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token. |
_underlyingToken | ERC20 | The underlying token of the market to supply to. |
_amount | uint256 | The amount of token (in underlying). |
_withdrawFromPool
Withdraws underlying tokens from Compound.
function _withdrawFromPool(address _poolToken, uint256 _amount) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token. |
_amount | uint256 | The amount of token (in underlying). |
_borrowFromPool
Borrows underlying tokens from Compound.
function _borrowFromPool(address _poolToken, uint256 _amount) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token. |
_amount | uint256 | The amount of token (in underlying). |
_repayToPool
Repays underlying tokens to Compound.
function _repayToPool(address _poolToken, ERC20 _underlyingToken, uint256 _amount) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token. |
_underlyingToken | ERC20 | The underlying token of the market to repay to. |
_amount | uint256 | The amount of token (in underlying). |
_enterMarketIfNeeded
Enters the user into the market if not already there.
function _enterMarketIfNeeded(address _poolToken, address _user) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market to check. |
_user | address | The address of the user to update. |
_leaveMarketIfNeeded
Removes the user from the market if its balances are null.
function _leaveMarketIfNeeded(address _poolToken, address _user) internal;
Parameters
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the market to check. |
_user | address | The address of the user to update. |
_liquidationAllowed
Returns whether a given user is liquidatable and the applicable close factor, given the deprecated status of the borrowed market.
function _liquidationAllowed(address _user, bool _isDeprecated)
internal
view
returns (bool liquidationAllowed, uint256 closeFactor);
Parameters
Name | Type | Description |
---|---|---|
_user | address | The user to check. |
_isDeprecated | bool | Whether the borrowed market is deprecated or not. |
Returns
Name | Type | Description |
---|---|---|
liquidationAllowed | bool | Whether the liquidation is allowed or not. |
closeFactor | uint256 | The close factor to apply. |
Events
Supplied
EVENTS ///
Emitted when a supply happens.
event Supplied(
address indexed _supplier,
address indexed _onBehalf,
address indexed _poolToken,
uint256 _amount,
uint256 _balanceOnPool,
uint256 _balanceInP2P
);
Borrowed
Emitted when a borrow happens.
event Borrowed(
address indexed _borrower,
address indexed _poolToken,
uint256 _amount,
uint256 _balanceOnPool,
uint256 _balanceInP2P
);
Withdrawn
Emitted when a withdrawal happens.
event Withdrawn(
address indexed _supplier,
address indexed _receiver,
address indexed _poolToken,
uint256 _amount,
uint256 _balanceOnPool,
uint256 _balanceInP2P
);
Repaid
Emitted when a repayment happens.
event Repaid(
address indexed _repayer,
address indexed _onBehalf,
address indexed _poolToken,
uint256 _amount,
uint256 _balanceOnPool,
uint256 _balanceInP2P
);
Liquidated
Emitted when a liquidation happens.
event Liquidated(
address _liquidator,
address indexed _liquidated,
address indexed _poolTokenBorrowed,
uint256 _amountRepaid,
address indexed _poolTokenCollateral,
uint256 _amountSeized
);
P2PDeltasIncreased
Emitted when the peer-to-peer deltas are increased by the governance.
event P2PDeltasIncreased(address indexed _poolToken, uint256 _amount);
P2PBorrowDeltaUpdated
Emitted when the borrow peer-to-peer delta is updated.
event P2PBorrowDeltaUpdated(address indexed _poolToken, uint256 _p2pBorrowDelta);
P2PSupplyDeltaUpdated
Emitted when the supply peer-to-peer delta is updated.
event P2PSupplyDeltaUpdated(address indexed _poolToken, uint256 _p2pSupplyDelta);
P2PAmountsUpdated
Emitted when the supply and borrow peer-to-peer amounts are updated.
event P2PAmountsUpdated(address indexed _poolToken, uint256 _p2pSupplyAmount, uint256 _p2pBorrowAmount);
Errors
AmountAboveWhatAllowedToRepay
ERRORS ///
Thrown when the amount repaid during the liquidation is above what is allowed to be repaid.
error AmountAboveWhatAllowedToRepay();
BorrowOnCompoundFailed
Thrown when the borrow on Compound failed and throws back the Compound error code.
error BorrowOnCompoundFailed(uint256 errorCode);
RedeemOnCompoundFailed
Thrown when the redeem on Compound failed and throws back the Compound error code.
error RedeemOnCompoundFailed(uint256 errorCode);
RepayOnCompoundFailed
Thrown when the repay on Compound failed and throws back the Compound error code.
error RepayOnCompoundFailed(uint256 errorCode);
MintOnCompoundFailed
Thrown when the mint on Compound failed and throws back the Compound error code.
error MintOnCompoundFailed(uint256 errorCode);
UserNotMemberOfMarket
Thrown when user is not a member of the market.
error UserNotMemberOfMarket();
UnauthorisedWithdraw
Thrown when the user does not have enough remaining collateral to withdraw.
error UnauthorisedWithdraw();
UnauthorisedLiquidate
Thrown when the positions of the user is not liquidatable.
error UnauthorisedLiquidate();
UnauthorisedBorrow
Thrown when the user does not have enough collateral for the borrow.
error UnauthorisedBorrow();
WithdrawTooSmall
Thrown when the amount desired for a withdrawal is too small.
error WithdrawTooSmall();
AddressIsZero
Thrown when the address is zero.
error AddressIsZero();
AmountIsZero
Thrown when the amount is equal to 0.
error AmountIsZero();
SameBlockBorrowRepay
Thrown when a user tries to repay its debt after borrowing in the same block.
error SameBlockBorrowRepay();
SupplyIsPaused
Thrown when someone tries to supply but the supply is paused.
error SupplyIsPaused();
BorrowIsPaused
Thrown when someone tries to borrow but the borrow is paused.
error BorrowIsPaused();
WithdrawIsPaused
Thrown when someone tries to withdraw but the withdraw is paused.
error WithdrawIsPaused();
RepayIsPaused
Thrown when someone tries to repay but the repay is paused.
error RepayIsPaused();
LiquidateCollateralIsPaused
Thrown when someone tries to liquidate but the liquidation with this asset as collateral is paused.
error LiquidateCollateralIsPaused();
LiquidateBorrowIsPaused
Thrown when someone tries to liquidate but the liquidation with this asset as debt is paused.
error LiquidateBorrowIsPaused();
Structs
SupplyVars
STRUCTS ///
struct SupplyVars {
uint256 remainingToSupply;
uint256 poolBorrowIndex;
uint256 toRepay;
}
WithdrawVars
struct WithdrawVars {
uint256 remainingGasForMatching;
uint256 remainingToWithdraw;
uint256 poolSupplyIndex;
uint256 p2pSupplyIndex;
uint256 toWithdraw;
ERC20 underlyingToken;
}
RepayVars
struct RepayVars {
uint256 remainingGasForMatching;
uint256 remainingToRepay;
uint256 maxToRepayOnPool;
uint256 poolBorrowIndex;
uint256 p2pSupplyIndex;
uint256 p2pBorrowIndex;
uint256 borrowedOnPool;
uint256 feeToRepay;
uint256 toRepay;
}
LiquidateVars
struct LiquidateVars {
uint256 collateralPrice;
uint256 borrowBalance;
uint256 supplyBalance;
uint256 borrowedPrice;
uint256 amountToSeize;
uint256 closeFactor;
bool liquidationAllowed;
}