Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Libraries consistency: (#669)
Browse files Browse the repository at this point in the history
* Refactor LPs in funciton and events naming to LP

* Consistent naming: rename start reserve auction to kick reserve auction. add KickReserveAuction event

* Calculate LUP in single place Deposits.getLup(), remove duplicate functions to calculate LUP

* Split Auctions library in KickerActions, TakerActions and SettlerActions libraries

* Split LenderActions in LenderActions and LPOwnerActions external libs

* cleanup interfaces, restructure them based on actors and actions:
  - IPoolLPOwnerActions interface split from IPoolLenderActions
  - IPoolLiquidationActions and IPoolReserveAuctionActions interfaces restructured in IPoolSettlerActions, IPoolKickerActions, IPoolTakerActions to match functionality of external libs

* Deploy new libraries in brownie scripts

* Apply same style for all events. Emit BucketBankruptcy after storage bucket update

* Changes after review: rename from LPOwnerActions to LPActions, update comment

* Update kickMomp description

* More LPs to LP refactor - change function names, NAT specs and events / errors
  • Loading branch information
grandizzy authored Apr 13, 2023
1 parent a65bd98 commit b5d1cd1
Show file tree
Hide file tree
Showing 78 changed files with 2,977 additions and 2,696 deletions.
34 changes: 17 additions & 17 deletions docs/Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
reverts on:
- deposits locked RemoveDepositLockedByAuctionDebt()
- LenderActions.removeQuoteToken():
- no LPs NoClaim()
- no LP NoClaim()
- LUP lower than HTP LUPBelowHTP()
emit events:
- LenderActions.removeQuoteToken():
Expand All @@ -112,24 +112,24 @@
- PoolCommons.updateInterestRate():
- UpdateInterestRate

### transferLPs
### transferLP
external libraries call:
- LenderActions.transferLPs()
- LenderActions.transferLP()

write state:
- LenderActions.transferLPs():
- LenderActions.transferLP():
- delete allowance mapping
- increment new lender.lps accumulator and lender.depositTime state
- delete old lender from bucket -> lender mapping

reverts on:
- LenderActions.transferLPs():
- LenderActions.transferLP():
- invalid index InvalidIndex()
- no allowance NoAllowance()

emit events:
- LenderActions.transferLPs():
- TransferLPs
- LenderActions.transferLP():
- TransferLP

### kick
external libraries call:
Expand Down Expand Up @@ -232,25 +232,25 @@
emit events:
- BondWithdrawn

### startClaimableReserveAuction
### kickReserveAuction
external libraries call:
- Auctions.startClaimableReserveAuction()
- Auctions.kickReserveAuction()

write state:
- Auctions.startClaimableReserveAuction():
- Auctions.kickReserveAuction():
- update reserveAuction.unclaimed accumulator
- update reserveAuction.kicked timestamp state
- increment latestBurnEpoch counter
- update reserveAuction.latestBurnEventEpoch and burn event timestamp state

reverts on:
- 2 weeks not passed ReserveAuctionTooSoon()
- Auctions.startClaimableReserveAuction():
- Auctions.kickReserveAuction():
- no reserves to claim NoReserves()

emit events:
- Auctions.startClaimableReserveAuction():
- ReserveAuction
- Auctions.kickReserveAuction():
- KickReserveAuction


### takeReserves
Expand Down Expand Up @@ -488,7 +488,7 @@
- update values array state
- Buckets.addCollateral():
- increment bucket.collateral and bucket.lps accumulator
- addLenderLPs():
- addLenderLP():
- increment lender.lps accumulator and lender.depositTime state
- Auctions._settleAuction():
- _removeAuction():
Expand Down Expand Up @@ -594,7 +594,7 @@
- _prepareTake():
- update liquidation.alreadyTaken state
- _rewardBucketTake():
- Buckets.addLenderLPs:
- Buckets.addLenderLP:
- increment taker lender.lps accumulator and lender.depositTime state
- increment kicker lender.lps accumulator and lender.depositTime state
- update liquidation bond size accumulator
Expand Down Expand Up @@ -659,7 +659,7 @@
- LenderActions.addCollateral():
- Buckets.addCollateral():
- increment bucket.collateral and bucket.lps accumulator
- addLenderLPs():
- addLenderLP():
- increment lender.lps accumulator and lender.depositTime state
- _updateInterestState():
- PoolCommons.updateInterestRate():
Expand Down Expand Up @@ -700,7 +700,7 @@
reverts on:
- LenderActions.removeCollateral():
- not enough collateral InsufficientCollateral()
- insufficient LPs InsufficientLPs()
- insufficient LP InsufficientLP()

emit events:
- RemoveCollateral
Expand Down
5 changes: 4 additions & 1 deletion scripts/ajna_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ def main():
Deposits.deploy({"from": accounts[0]})
PoolCommons.deploy({"from": accounts[0]})
LenderActions.deploy({"from": accounts[0]})
LPActions.deploy({"from": accounts[0]})
BorrowerActions.deploy({"from": accounts[0]})
Auctions.deploy({"from": accounts[0]})
KickerActions.deploy({"from": accounts[0]}})
TakerActions.deploy({"from": accounts[0]}})
SettlerActions.deploy({"from": accounts[0]}})
erc20_pool_factory = ERC20PoolFactory.deploy(ajna_address, {"from": accounts[0]})
erc721_pool_factory = ERC721PoolFactory.deploy(ajna_address, {"from": accounts[0]})
pool_utils = PoolInfoUtils.deploy({"from": accounts[0]})
Expand Down
29 changes: 16 additions & 13 deletions src/ERC20Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { IERC20Taker } from './interfaces/pool/erc20/IERC20Taker.sol';

import {
IPoolLenderActions,
IPoolLiquidationActions
IPoolKickerActions,
IPoolTakerActions,
IPoolSettlerActions
} from './interfaces/pool/IPool.sol';
import {
IERC3156FlashBorrower,
Expand Down Expand Up @@ -50,12 +52,13 @@ import { Maths } from './libraries/internal/Maths.sol';

import { BorrowerActions } from './libraries/external/BorrowerActions.sol';
import { LenderActions } from './libraries/external/LenderActions.sol';
import { Auctions } from './libraries/external/Auctions.sol';
import { SettlerActions } from './libraries/external/SettlerActions.sol';
import { TakerActions } from './libraries/external/TakerActions.sol';

/**
* @title ERC20 Pool contract
* @notice Entrypoint of ERC20 Pool actions for pool actors:
* - Lenders: add, remove and move quote tokens; transfer LPs
* - Lenders: add, remove and move quote tokens; transfer LP
* - Borrowers: draw and repay debt
* - Traders: add, remove and move quote tokens; add and remove collateral
* - Kickers: kick undercollateralized loans; settle auctions; claim bond rewards
Expand All @@ -64,7 +67,7 @@ import { Auctions } from './libraries/external/Auctions.sol';
* - Flash borrowers: initiate flash loans on quote tokens and collateral
* @dev Contract is FlashloanablePool with flash loan logic.
* @dev Contract is base Pool with logic to handle ERC20 collateral.
* @dev Calls logic from external PoolCommons, LenderActions, BorrowerActions and Auctions libraries.
* @dev Calls logic from external PoolCommons, LenderActions, BorrowerActions and auction actions libraries.
*/
contract ERC20Pool is FlashloanablePool, IERC20Pool {
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -286,7 +289,7 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
emit AddCollateral(msg.sender, index_, amountToAdd_, bucketLPs_);

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

// move required collateral from sender to pool
_transferCollateralFrom(msg.sender, amountToAdd_);
Expand Down Expand Up @@ -318,7 +321,7 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
emit RemoveCollateral(msg.sender, index_, collateralAmount_, lpAmount_);

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

// move collateral from pool to lender
_transferCollateral(msg.sender, collateralAmount_);
Expand All @@ -329,7 +332,7 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
/*******************************/

/**
* @inheritdoc IPoolLiquidationActions
* @inheritdoc IPoolSettlerActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
Expand All @@ -341,7 +344,7 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();

SettleResult memory result = Auctions.settlePoolDebt(
SettleResult memory result = SettlerActions.settlePoolDebt(
auctions,
buckets,
deposits,
Expand Down Expand Up @@ -372,11 +375,11 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
poolState.debt -= Maths.wmul(result.t0DebtSettled, poolState.inflator);
poolState.t0Debt -= result.t0DebtSettled;
poolState.collateral -= result.collateralSettled;
_updateInterestState(poolState, _lup(poolState.debt));
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
}

/**
* @inheritdoc IPoolLiquidationActions
* @inheritdoc IPoolTakerActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
Expand All @@ -395,7 +398,7 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
// round requested collateral to an amount which can actually be transferred
collateral_ = _roundToScale(collateral_, collateralDust);

TakeResult memory result = Auctions.take(
TakeResult memory result = TakerActions.take(
auctions,
buckets,
deposits,
Expand Down Expand Up @@ -445,7 +448,7 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {
}

/**
* @inheritdoc IPoolLiquidationActions
* @inheritdoc IPoolTakerActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
Expand All @@ -459,7 +462,7 @@ contract ERC20Pool is FlashloanablePool, IERC20Pool {

PoolState memory poolState = _accruePoolInterest();

BucketTakeResult memory result = Auctions.bucketTake(
BucketTakeResult memory result = TakerActions.bucketTake(
auctions,
buckets,
deposits,
Expand Down
31 changes: 17 additions & 14 deletions src/ERC721Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
IERC721Token,
IPoolErrors,
IPoolLenderActions,
IPoolLiquidationActions
IPoolKickerActions,
IPoolTakerActions,
IPoolSettlerActions
} from './interfaces/pool/IPool.sol';
import {
BucketTakeResult,
Expand Down Expand Up @@ -38,14 +40,15 @@ import { Maths } from './libraries/internal/Maths.sol';
import { Deposits } from './libraries/internal/Deposits.sol';
import { Loans } from './libraries/internal/Loans.sol';

import { Auctions } from './libraries/external/Auctions.sol';
import { LenderActions } from './libraries/external/LenderActions.sol';
import { BorrowerActions } from './libraries/external/BorrowerActions.sol';
import { SettlerActions } from './libraries/external/SettlerActions.sol';
import { TakerActions } from './libraries/external/TakerActions.sol';

/**
* @title ERC721 Pool contract
* @notice Entrypoint of ERC721 Pool actions for pool actors:
* - Lenders: add, remove and move quote tokens; transfer LPs
* - Lenders: add, remove and move quote tokens; transfer LP
* - Borrowers: draw and repay debt
* - Traders: add, remove and move quote tokens; add and remove collateral
* - Kickers: auction undercollateralized loans; settle auctions; claim bond rewards
Expand All @@ -54,7 +57,7 @@ import { BorrowerActions } from './libraries/external/BorrowerActions.sol';
* - Flash borrowers: initiate flash loans on ERC20 quote tokens
* @dev Contract is FlashloanablePool with flash loan logic.
* @dev Contract is base Pool with logic to handle ERC721 collateral.
* @dev Calls logic from external PoolCommons, LenderActions, BorrowerActions and Auctions libraries.
* @dev Calls logic from external PoolCommons, LenderActions, BorrowerActions and auction actions libraries.
*/
contract ERC721Pool is FlashloanablePool, IERC721Pool {

Expand Down Expand Up @@ -286,7 +289,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
emit AddCollateralNFT(msg.sender, index_, tokenIdsToAdd_, bucketLPs_);

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

// move required collateral from sender to pool
_transferFromSenderToPool(bucketTokenIds, tokenIdsToAdd_);
Expand Down Expand Up @@ -323,7 +326,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
emit MergeOrRemoveCollateralNFT(msg.sender, collateralMerged_, bucketLPs_);

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

if (collateralMerged_ == collateralAmount) {
// Total collateral in buckets meets the requested removal amount, noOfNFTsToRemove_
Expand Down Expand Up @@ -358,7 +361,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
emit RemoveCollateral(msg.sender, index_, noOfNFTsToRemove_, lpAmount_);

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

_transferFromPoolToAddress(msg.sender, bucketTokenIds, noOfNFTsToRemove_);
}
Expand All @@ -368,7 +371,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
/*******************************/

/**
* @inheritdoc IPoolLiquidationActions
* @inheritdoc IPoolSettlerActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
Expand All @@ -386,7 +389,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
bucketDepth: maxDepth_
});

SettleResult memory result = Auctions.settlePoolDebt(
SettleResult memory result = SettlerActions.settlePoolDebt(
auctions,
buckets,
deposits,
Expand Down Expand Up @@ -415,11 +418,11 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
poolState.debt -= Maths.wmul(result.t0DebtSettled, poolState.inflator);
poolState.t0Debt -= result.t0DebtSettled;
poolState.collateral -= result.collateralSettled;
_updateInterestState(poolState, _lup(poolState.debt));
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
}

/**
* @inheritdoc IPoolLiquidationActions
* @inheritdoc IPoolTakerActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
Expand All @@ -433,7 +436,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();

TakeResult memory result = Auctions.take(
TakeResult memory result = TakerActions.take(
auctions,
buckets,
deposits,
Expand Down Expand Up @@ -497,7 +500,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
}

/**
* @inheritdoc IPoolLiquidationActions
* @inheritdoc IPoolTakerActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
Expand All @@ -511,7 +514,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {

PoolState memory poolState = _accruePoolInterest();

BucketTakeResult memory result = Auctions.bucketTake(
BucketTakeResult memory result = TakerActions.bucketTake(
auctions,
buckets,
deposits,
Expand Down
Loading

0 comments on commit b5d1cd1

Please sign in to comment.