Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add increase p2p deltas logic #79

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/MorphoSetters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {PoolLib} from "./libraries/PoolLib.sol";
import {DataTypes} from "./libraries/aave/DataTypes.sol";
import {ReserveConfiguration} from "./libraries/aave/ReserveConfiguration.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

Expand All @@ -26,6 +27,9 @@ abstract contract MorphoSetters is IMorphoSetters, MorphoInternal {
using PoolLib for IPool;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

using Math for uint256;
using WadRayMath for uint256;

/// SETTERS ///

function initialize(
Expand Down Expand Up @@ -83,6 +87,40 @@ abstract contract MorphoSetters is IMorphoSetters, MorphoInternal {
emit Events.MarketCreated(underlying, reserveFactor, p2pIndexCursor);
}

function increaseP2PDeltas(address underlying, uint256 amount) external onlyOwner isMarketCreated(underlying) {
Types.Indexes256 memory indexes = _updateIndexes(underlying);

Types.Market storage market = _market[underlying];
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
uint256 poolSupplyIndex = indexes.poolSupplyIndex;
uint256 poolBorrowIndex = indexes.poolBorrowIndex;
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved

amount = Math.min(
amount,
Math.min(
market.deltas.p2pSupplyAmount.rayMul(indexes.p2pSupplyIndex).zeroFloorSub(
market.deltas.p2pSupplyDelta.rayMul(poolSupplyIndex)
),
market.deltas.p2pBorrowAmount.rayMul(indexes.p2pBorrowIndex).zeroFloorSub(
market.deltas.p2pBorrowDelta.rayMul(poolBorrowIndex)
)
)
);
if (amount == 0) revert Errors.AmountIsZero();

uint256 newP2PSupplyDelta = market.deltas.p2pSupplyDelta + amount.rayDiv(poolSupplyIndex);
uint256 newP2PBorrowDelta = market.deltas.p2pBorrowDelta + amount.rayDiv(poolBorrowIndex);

market.deltas.p2pSupplyDelta = newP2PSupplyDelta;
market.deltas.p2pBorrowDelta = newP2PBorrowDelta;
emit Events.P2PSupplyDeltaUpdated(underlying, newP2PSupplyDelta);
emit Events.P2PBorrowDeltaUpdated(underlying, newP2PBorrowDelta);

_pool.borrowFromPool(underlying, amount);
_pool.supplyToPool(underlying, amount);

emit Events.P2PDeltasIncreased(underlying, amount);
}

function setMaxSortedUsers(uint256 newMaxSortedUsers) external onlyOwner {
if (newMaxSortedUsers == 0) revert Errors.MaxSortedUsersCannotBeZero();
_maxSortedUsers = newMaxSortedUsers;
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface IMorphoSetters {
) external;

function createMarket(address underlying, uint16 reserveFactor, uint16 p2pIndexCursor) external;
function increaseP2PDeltas(address underlying, uint256 amount) external;

function setDefaultMaxLoops(Types.MaxLoops memory defaultMaxLoops) external;
function setEntryPositionsManager(address entryPositionsManager) external;
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ library Events {

event P2PSupplyDeltaUpdated(address indexed underlying, uint256 p2pSupplyDelta);

event P2PDeltasIncreased(address indexed _poolToken, uint256 _amount);

event MarketCreated(address indexed underlying, uint16 reserveFactor, uint16 p2pIndexCursor);

event MaxSortedUsersSet(uint256 maxSortedUsers);
Expand Down