Skip to content

Commit

Permalink
manually merged from private-contracts/immutable-kicks
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Oct 12, 2023
1 parent a255759 commit 087216b
Show file tree
Hide file tree
Showing 80 changed files with 4,405 additions and 4,951 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ node_modules
.env
.env.prod
.idea/
.vscode/*.log
.vscode/
__pycache__/
reports/
.gas-snapshot
Expand Down
20 changes: 0 additions & 20 deletions docs/Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,19 +386,12 @@
- update scaling array state
- increment reserveAuction.totalInterestEarned accumulator
- BorrowerActions.drawDebt():
- SettlerActions._settleAuction():
- _removeAuction():
- decrement kicker locked accumulator, increment kicker claimable accumumlator
- decrement auctions count accumulator
- decrement auctions.totalBondEscrowed accumulator
- update auction queue state
- Loans.update():
- _upsert():
- insert or update loan in loans array
- remove():
- remove loan from loans array
- update borrower in address => borrower mapping
- decrement poolBalances.t0DebtInAuction accumulator
- increment poolBalances.pledgedCollateral accumulator
- increment poolBalances.t0Debt accumulator
- _updateInterestState():
Expand All @@ -416,8 +409,6 @@

emit events:
- BorrowerActions.drawDebt():
- SettlerActions._settleAuction():
- AuctionNFTSettle or AuctionSettle
- DrawDebt
- PoolCommons.updateInterestRate():
- UpdateInterestRate
Expand All @@ -435,20 +426,13 @@
- update scaling array state
- increment reserveAuction.totalInterestEarned accumulator
- BorrowerActions.repayDebt():
- SettlerActions._settleAuction():
- _removeAuction():
- decrement kicker locked accumulator, increment kicker claimable accumumlator
- decrement auctions count accumulator
- decrement auctions.totalBondEscrowed accumulator
- update auction queue state
- Loans.update():
- _upsert():
- insert or update loan in loans array
- remove():
- remove loan from loans array
- update borrower in address => borrower mapping
- decrement poolBalances.t0Debt accumulator
- decrement poolBalances.t0DebtInAuction accumulator
- decrement poolBalances.pledgedCollateral accumulator
- _updateInterestState():
- PoolCommons.updateInterestRate():
Expand All @@ -465,8 +449,6 @@

emit events:
- BorrowerActions.repayDebt():
- SettlerActions._settleAuction:
- AuctionNFTSettle or AuctionSettle
- RepayDebt
- PoolCommons.updateInterestRate():
- UpdateInterestRate
Expand Down Expand Up @@ -567,7 +549,6 @@
- insufficient collateral InsufficientCollateral()
- _prepareTake():
- loan is not in auction NoAuction()
- in 1 hour cool down period TakeNotPastCooldown()
- _takeLoan():
- borrower debt less than pool min debt AmountLTMinDebt()

Expand Down Expand Up @@ -630,7 +611,6 @@
- insufficient collateral InsufficientCollateral()
- _prepareTake():
- loan is not in auction NoAuction()
- in 1 hour cool down period TakeNotPastCooldown()
- _takeLoan():
- borrower debt less than pool min debt AmountLTMinDebt()

Expand Down
468 changes: 234 additions & 234 deletions docs/drawio/bucketTake.drawio

Large diffs are not rendered by default.

454 changes: 210 additions & 244 deletions docs/drawio/drawDebt.drawio

Large diffs are not rendered by default.

468 changes: 217 additions & 251 deletions docs/drawio/repayDebt.drawio

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions docs/drawio/take.drawio

Large diffs are not rendered by default.

52 changes: 16 additions & 36 deletions src/ERC20Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
/**
* @inheritdoc IERC20PoolBorrowerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0DebtInAuction` accumulator
* @dev - increment `poolBalances.pledgedCollateral` accumulator
* @dev - increment `poolBalances.t0Debt` accumulator
* @dev - update `t0Debt2ToCollateral` ratio only if loan not in auction, debt and collateral pre action are considered 0 if auction settled
* @dev - update `t0Debt2ToCollateral` ratio
* @dev === Emit events ===
* @dev - `DrawDebt`
*/
Expand All @@ -143,7 +142,6 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {

DrawDebtResult memory result = BorrowerActions.drawDebt(
auctions,
buckets,
deposits,
loans,
poolState,
Expand All @@ -161,21 +159,13 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
poolState.t0Debt = result.t0PoolDebt;
poolState.collateral = result.poolCollateral;

// update t0 debt in auction in memory pool state struct and pool balances state
if (result.t0DebtInAuctionChange != 0) {
poolState.t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
}

// adjust t0Debt2ToCollateral ratio if loan not in auction
if (!result.inAuction) {
_updateT0Debt2ToCollateral(
result.settledAuction ? 0 : result.debtPreAction, // debt pre settle (for loan in auction) not taken into account
result.debtPostAction,
result.settledAuction ? 0 : result.collateralPreAction, // collateral pre settle (for loan in auction) not taken into account
result.collateralPostAction
);
}
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
result.debtPostAction,
result.collateralPreAction,
result.collateralPostAction
);

// update pool interest rate state
_updateInterestState(poolState, result.newLup);
Expand All @@ -201,9 +191,8 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
* @inheritdoc IERC20PoolBorrowerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0Debt accumulator`
* @dev - decrement `poolBalances.t0DebtInAuction accumulator`
* @dev - decrement `poolBalances.pledgedCollateral accumulator`
* @dev - update `t0Debt2ToCollateral` ratio only if loan not in auction, debt and collateral pre action are considered 0 if auction settled
* @dev - update `t0Debt2ToCollateral` ratio
* @dev === Emit events ===
* @dev - `RepayDebt`
*/
Expand All @@ -223,7 +212,6 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {

RepayDebtResult memory result = BorrowerActions.repayDebt(
auctions,
buckets,
deposits,
loans,
poolState,
Expand All @@ -240,21 +228,13 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
poolState.t0Debt = result.t0PoolDebt;
poolState.collateral = result.poolCollateral;

// update t0 debt in auction in memory pool state struct and pool balances state
if (result.t0DebtInAuctionChange != 0) {
poolState.t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
}

// adjust t0Debt2ToCollateral ratio if loan not in auction
if (!result.inAuction) {
_updateT0Debt2ToCollateral(
result.settledAuction ? 0 : result.debtPreAction, // debt pre settle (for loan in auction) not taken into account
result.debtPostAction,
result.settledAuction ? 0 : result.collateralPreAction, // collateral pre settle (for loan in auction) not taken into account
result.collateralPostAction
);
}
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
result.debtPostAction,
result.collateralPreAction,
result.collateralPostAction
);

// update pool interest rate state
_updateInterestState(poolState, result.newLup);
Expand Down
50 changes: 14 additions & 36 deletions src/ERC721Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {

DrawDebtResult memory result = BorrowerActions.drawDebt(
auctions,
buckets,
deposits,
loans,
poolState,
Expand All @@ -173,21 +172,13 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
poolState.t0Debt = result.t0PoolDebt;
poolState.collateral = result.poolCollateral;

// update t0 debt in auction in memory pool state struct and pool balances state
if (result.t0DebtInAuctionChange != 0) {
poolState.t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
}

// adjust t0Debt2ToCollateral ratio if loan not in auction
if (!result.inAuction) {
_updateT0Debt2ToCollateral(
result.settledAuction ? 0 : result.debtPreAction, // debt pre settle (for loan in auction) not taken into account
result.debtPostAction,
result.settledAuction ? 0 : result.collateralPreAction, // collateral pre settle (for loan in auction) not taken into account
result.collateralPostAction
);
}
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
result.debtPostAction,
result.collateralPreAction,
result.collateralPostAction
);

// update pool interest rate state
_updateInterestState(poolState, result.newLup);
Expand All @@ -200,8 +191,6 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
_transferFromSenderToPool(borrowerTokenIds[borrowerAddress_], tokenIdsToPledge_);
}

if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);

// move borrowed amount from pool to sender
if (amountToBorrow_ != 0) {
// update pool balances t0 debt state
Expand Down Expand Up @@ -238,7 +227,6 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {

RepayDebtResult memory result = BorrowerActions.repayDebt(
auctions,
buckets,
deposits,
loans,
poolState,
Expand All @@ -255,23 +243,13 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
poolState.t0Debt = result.t0PoolDebt;
poolState.collateral = result.poolCollateral;

// update t0 debt in auction in memory pool state struct and pool balances state
if (result.t0DebtInAuctionChange != 0) {
poolState.t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
}

if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);

// adjust t0Debt2ToCollateral ratio if loan not in auction
if (!result.inAuction) {
_updateT0Debt2ToCollateral(
result.settledAuction ? 0 : result.debtPreAction, // debt pre settle (for loan in auction) not taken into account
result.debtPostAction,
result.settledAuction ? 0 : result.collateralPreAction, // collateral pre settle (for loan in auction) not taken into account
result.collateralPostAction
);
}
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
result.debtPostAction,
result.collateralPreAction,
result.collateralPostAction
);

// update pool interest rate state
_updateInterestState(poolState, result.newLup);
Expand Down
33 changes: 9 additions & 24 deletions src/PoolInfoUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

pragma solidity 0.8.18;

import { Math } from '@openzeppelin/contracts/utils/math/Math.sol';

import { IPool, IERC20Token } from './interfaces/pool/IPool.sol';

import {
Expand Down Expand Up @@ -56,16 +58,16 @@ contract PoolInfoUtils {
)
{
IPool pool = IPool(ajnaPool_);
uint256 kickMomp;
( , , , kickTime_, kickMomp, neutralPrice_, , , , ) = pool.auctionInfo(borrower_);
uint256 referencePrice;
( , , , kickTime_, referencePrice, neutralPrice_, , , ) = pool.auctionInfo(borrower_);
if (kickTime_ != 0) {
(debtToCover_, collateral_, ) = this.borrowerInfo(ajnaPool_, borrower_);

(uint256 poolDebt,,,) = pool.debtInfo();
uint256 lup_ = _priceAt(pool.depositIndex(poolDebt));
isCollateralized_ = _isCollateralized(debtToCover_, collateral_, lup_, pool.poolType());

price_ = _auctionPrice(kickMomp, neutralPrice_, kickTime_);
price_ = _auctionPrice(referencePrice, kickTime_);
}
}

Expand Down Expand Up @@ -98,7 +100,10 @@ contract PoolInfoUtils {
uint256 pendingInflator = PoolCommons.pendingInflator(inflator, lastInflatorUpdate, interestRate);

uint256 t0Debt;
(t0Debt, collateral_, t0Np_) = pool.borrowerInfo(borrower_);
uint256 npTpRatio;
(t0Debt, collateral_, npTpRatio) = pool.borrowerInfo(borrower_);

t0Np_ = collateral_ == 0 ? 0 : Math.mulDiv(t0Debt, npTpRatio, collateral_);

debt_ = Maths.ceilWmul(t0Debt, pendingInflator);
}
Expand Down Expand Up @@ -407,26 +412,6 @@ contract PoolInfoUtils {
(, htp_, ) = IPool(ajnaPool_).loansInfo();
}

/**
* @notice Returns current `MOMP` for a given pool.
*/
function momp(
address ajnaPool_
) external view returns (uint256) {
IPool pool = IPool(ajnaPool_);

( , , uint256 noOfLoans) = pool.loansInfo();
noOfLoans += pool.totalAuctionsInPool();
if (noOfLoans == 0) {
// if there are no borrowers, return the HPB
return _priceAt(pool.depositIndex(1));
} else {
// otherwise, calculate the MOMP
(uint256 debt, , , ) = pool.debtInfo();
return _priceAt(pool.depositIndex(Maths.wdiv(debt, noOfLoans * 1e18)));
}
}

/**
* @notice Calculates origination fee rate for a pool.
* @notice Calculated as greater of the current annualized interest rate divided by `52` (one week of interest) or `5` bps.
Expand Down
Loading

0 comments on commit 087216b

Please sign in to comment.