Skip to content

Commit

Permalink
Proposed changes to PR #972:
Browse files Browse the repository at this point in the history
- avoid calculating current LUP twice in lender kick
- change _kick function to accept proposed LUP (for regular kick proposed LUP is current LUP, for lender kick proposed LUP is calculated based on additional debt)
- in both kick cases return current LUP in kick result
- reduce gas costs by saving a Fenwick traversal
- reduce contract size by removing LUP calculation within Pool
  • Loading branch information
grandizzy committed Dec 5, 2023
1 parent 9772b44 commit 0b3305d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/base/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;

// update pool interest rate state
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
_updateInterestState(poolState, result.lup);

// transfer from kicker to pool the difference to cover bond
if (result.amountToCoverBond != 0) _transferQuoteTokenFrom(msg.sender, result.amountToCoverBond);
Expand Down
38 changes: 20 additions & 18 deletions src/libraries/external/KickerActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ library KickerActions {

/**
* @notice See `IPoolKickerActions` for descriptions.
* @return The `KickResult` struct result of the kick action.
* @return kickResult_ The `KickResult` struct result of the kick action.
*/
function kick(
AuctionsState storage auctions_,
Expand All @@ -115,17 +115,20 @@ library KickerActions {
address borrowerAddress_,
uint256 limitIndex_
) external returns (
KickResult memory
KickResult memory kickResult_
) {
return _kick(
uint256 curLup = Deposits.getLup(deposits_, poolState_.debt);

kickResult_ = _kick(
auctions_,
deposits_,
loans_,
poolState_,
borrowerAddress_,
limitIndex_,
0
curLup // proposed LUP is the current pool LUP
);
// return current LUP in pool
kickResult_.lup = curLup;
}

/**
Expand All @@ -151,7 +154,8 @@ library KickerActions {
vars.bucketPrice = _priceAt(index_);

// revert if the bucket price is below current LUP
if (vars.bucketPrice < Deposits.getLup(deposits_, poolState_.debt)) revert PriceBelowLUP();
uint256 curLup = Deposits.getLup(deposits_, poolState_.debt);
if (vars.bucketPrice < curLup) revert PriceBelowLUP();

Bucket storage bucket = buckets_[index_];
Lender storage lender = bucket.lenders[msg.sender];
Expand All @@ -175,16 +179,21 @@ library KickerActions {
// revert if no entitled amount
if (vars.entitledAmount == 0) revert InsufficientLiquidity();

// add amount to remove to pool debt in order to calculate proposed LUP
// this simulates LUP movement with additional debt
uint256 proposedLup = Deposits.getLup(deposits_, poolState_.debt + vars.entitledAmount);

// kick top borrower
kickResult_ = _kick(
auctions_,
deposits_,
loans_,
poolState_,
Loans.getMax(loans_).borrower,
limitIndex_,
vars.entitledAmount
proposedLup
);
// return current LUP in pool
kickResult_.lup = curLup;
}

/*************************/
Expand Down Expand Up @@ -285,22 +294,20 @@ library KickerActions {
* @dev === Emit events ===
* @dev - `Kick`
* @param auctions_ Struct for pool auctions state.
* @param deposits_ Struct for pool deposits state.
* @param loans_ Struct for pool loans state.
* @param poolState_ Current state of the pool.
* @param borrowerAddress_ Address of the borrower to kick.
* @param limitIndex_ Index of the lower bound of `NP` tolerated when kicking the auction.
* @param additionalDebt_ Additional debt to be used when calculating proposed `LUP`.
* @param proposedLup_ Proposed `LUP` in pool.
* @return kickResult_ The `KickResult` struct result of the kick action.
*/
function _kick(
AuctionsState storage auctions_,
DepositsState storage deposits_,
LoansState storage loans_,
PoolState calldata poolState_,
address borrowerAddress_,
uint256 limitIndex_,
uint256 additionalDebt_
uint256 proposedLup_
) internal returns (
KickResult memory kickResult_
) {
Expand All @@ -313,18 +320,13 @@ library KickerActions {
kickResult_.t0KickedDebt = borrower.t0Debt;
kickResult_.collateralPreAction = borrower.collateral;

// add amount to remove to pool debt in order to calculate proposed LUP
// for regular kick this is the currrent LUP in pool
// for provisional kick this simulates LUP movement with additional debt
kickResult_.lup = Deposits.getLup(deposits_, poolState_.debt + additionalDebt_);

KickLocalVars memory vars;
vars.borrowerDebt = Maths.wmul(kickResult_.t0KickedDebt, poolState_.inflator);
vars.borrowerCollateral = kickResult_.collateralPreAction;
vars.borrowerNpTpRatio = borrower.npTpRatio;

// revert if kick on a collateralized borrower
if (_isCollateralized(vars.borrowerDebt, vars.borrowerCollateral, kickResult_.lup, poolState_.poolType)) {
if (_isCollateralized(vars.borrowerDebt, vars.borrowerCollateral, proposedLup_, poolState_.poolType)) {
revert BorrowerOk();
}

Expand Down

0 comments on commit 0b3305d

Please sign in to comment.