Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into revert-on-zero-amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Feb 24, 2023
2 parents c276e77 + fd33630 commit 5f143f6
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs/Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
reverts on:
- deposits locked RemoveDepositLockedByAuctionDebt()
- LenderActions.moveQuoteToken():
- same index MoveToSamePrice()
- same index MoveToSameIndex()
- dust amount DustAmountNotExceeded()
- invalid index InvalidIndex()

Expand Down
2 changes: 1 addition & 1 deletion src/PoolInfoUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ contract PoolInfoUtils {

uint256 quoteTokenBalance = IERC20Token(pool.quoteTokenAddress()).balanceOf(ajnaPool_) * pool.quoteTokenScale();

(uint256 bondEscrowed, uint256 unclaimedReserve, uint256 auctionKickTime) = pool.reservesInfo();
(uint256 bondEscrowed, uint256 unclaimedReserve, uint256 auctionKickTime, ) = pool.reservesInfo();

// due to rounding issues, especially in Auction.settle, this can be slighly negative
if( poolDebt + quoteTokenBalance >= poolSize + bondEscrowed + unclaimedReserve) {
Expand Down
11 changes: 7 additions & 4 deletions src/base/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
uint256 neutralPrice,
address head,
address next,
address prev
address prev,
bool alreadyTaken
) {
Liquidation memory liquidation = auctions.liquidations[borrower_];
return (
Expand All @@ -623,7 +624,8 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
liquidation.neutralPrice,
auctions.head,
liquidation.next,
liquidation.prev
liquidation.prev,
liquidation.alreadyTaken
);
}

Expand Down Expand Up @@ -782,11 +784,12 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
}

/// @inheritdoc IPoolState
function reservesInfo() external view override returns (uint256, uint256, uint256) {
function reservesInfo() external view override returns (uint256, uint256, uint256, uint256) {
return (
auctions.totalBondEscrowed,
reserveAuction.unclaimed,
reserveAuction.kicked
reserveAuction.kicked,
reserveAuction.totalInterestEarned
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/pool/commons/IPoolErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ interface IPoolErrors {
error LUPGreaterThanTP();

/**
* @notice FromIndex_ and toIndex_ arguments to move are the same.
* @notice From index and to index arguments to move are the same.
*/
error MoveToSamePrice();
error MoveToSameIndex();

/**
* @notice Owner of the LP tokens must have approved the new owner prior to transfer.
Expand Down
8 changes: 6 additions & 2 deletions src/interfaces/pool/commons/IPoolState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface IPoolState {
* @return head Address of the head auction.
* @return next Address of the next auction in queue.
* @return prev Address of the prev auction in queue.
* @return alreadyTaken True if take has been called on auction
*/
function auctionInfo(address borrower)
external
Expand All @@ -32,7 +33,8 @@ interface IPoolState {
uint256 neutralPrice,
address head,
address next,
address prev
address prev,
bool alreadyTaken
);

/**
Expand Down Expand Up @@ -206,14 +208,16 @@ interface IPoolState {
* @return liquidationBondEscrowed Amount of liquidation bond across all liquidators.
* @return reserveAuctionUnclaimed Amount of claimable reserves which has not been taken in the Claimable Reserve Auction.
* @return reserveAuctionKicked Time a Claimable Reserve Auction was last kicked.
* @return totalInterestEarned Total interest earned by all lenders in the pool
*/
function reservesInfo()
external
view
returns (
uint256 liquidationBondEscrowed,
uint256 reserveAuctionUnclaimed,
uint256 reserveAuctionKicked
uint256 reserveAuctionKicked,
uint256 totalInterestEarned
);

/**
Expand Down
9 changes: 6 additions & 3 deletions src/libraries/external/LenderActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ library LenderActions {
error InsufficientLPs();
error InsufficientLiquidity();
error InsufficientCollateral();
error MoveToSamePrice();
error MoveToSameIndex();
error TransferorNotApproved();
error TransferToSameOwner();

Expand Down Expand Up @@ -203,7 +203,7 @@ library LenderActions {
* - decrement bucket.lps accumulator for from bucket
* - increment bucket.lps accumulator for to bucket
* @dev reverts on:
* - same index MoveToSamePrice()
* - same index MoveToSameIndex()
* - dust amount DustAmountNotExceeded()
* - invalid index InvalidIndex()
* @dev emit events:
Expand All @@ -219,7 +219,7 @@ library LenderActions {
if (params_.maxAmountToMove == 0)
revert InvalidAmount();
if (params_.fromIndex == params_.toIndex)
revert MoveToSamePrice();
revert MoveToSameIndex();
if (params_.maxAmountToMove != 0 && params_.maxAmountToMove < poolState_.quoteDustLimit)
revert DustAmountNotExceeded();
if (params_.toIndex == 0 || params_.toIndex > MAX_FENWICK_INDEX)
Expand Down Expand Up @@ -495,6 +495,9 @@ library LenderActions {
uint256 maxAmount_,
uint256 index_
) external returns (uint256, uint256) {
// revert if no amount to remove
if (maxAmount_ == 0) revert InvalidAmount();

return _removeMaxCollateral(
buckets_,
deposits_,
Expand Down
96 changes: 96 additions & 0 deletions tests/forge/ERC20Pool/ERC20PoolInputValidation.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.14;

import { ERC20HelperContract } from './ERC20DSTestPlus.sol';

import 'src/libraries/helpers/PoolHelper.sol';
import 'src/interfaces/pool/commons/IPoolErrors.sol';

import 'src/ERC20Pool.sol';

contract ERC20PoolBorrowTest is ERC20HelperContract {

function testValidateAddQuoteTokenInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
_pool.addQuoteToken(0, 1000, block.timestamp + 1);
// revert on zero index
vm.expectRevert(IPoolErrors.InvalidIndex.selector);
_pool.addQuoteToken(1000, 0, block.timestamp + 1);
// revert on index greater than max index
vm.expectRevert(IPoolErrors.InvalidIndex.selector);
_pool.addQuoteToken(1000, MAX_FENWICK_INDEX + 1, block.timestamp + 1);
}

function testValidateMoveQuoteTokenInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
_pool.moveQuoteToken(0, 1, 2, block.timestamp + 1);
// revert on move to same index
vm.expectRevert(IPoolErrors.MoveToSameIndex.selector);
_pool.moveQuoteToken(1000, 1, 1, block.timestamp + 1);
// revert on to zero index
vm.expectRevert(IPoolErrors.InvalidIndex.selector);
_pool.moveQuoteToken(1000, 1, 0, block.timestamp + 1);
// revert on to index greater than max index
vm.expectRevert(IPoolErrors.InvalidIndex.selector);
_pool.moveQuoteToken(1000, 1, MAX_FENWICK_INDEX + 1, block.timestamp + 1);
}

function testValidateRemoveQuoteTokenInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
_pool.removeQuoteToken(0, 1000);
// revert on zero index
vm.expectRevert(IPoolErrors.NoClaim.selector);
_pool.removeQuoteToken(1000, 0);
// revert on index greater than max index
vm.expectRevert(IPoolErrors.NoClaim.selector);
_pool.removeQuoteToken(1000, MAX_FENWICK_INDEX + 1);
}

function testValidateTakeReservesInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
_pool.takeReserves(0);
}

function testValidateDrawDebtInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
ERC20Pool(address(_pool)).drawDebt(address(this), 0, 0, 0);
}

function testValidateRepayDebtInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
ERC20Pool(address(_pool)).repayDebt(address(this), 0, 0, address(this), 1);
}

function testValidateAddCollateralInput() external tearDown {
// revert on zero index
vm.expectRevert(IPoolErrors.InvalidIndex.selector);
ERC20Pool(address(_pool)).addCollateral(1000 * 1e18, 0, block.timestamp + 1);
// revert on index greater than max index
vm.expectRevert(IPoolErrors.InvalidIndex.selector);
ERC20Pool(address(_pool)).addCollateral(1000 * 1e18, MAX_FENWICK_INDEX + 1, block.timestamp + 1);
}

function testValidateRemoveCollateralInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
ERC20Pool(address(_pool)).removeCollateral(0, 1000);
}

function testValidateSettleInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidBucketDepth.selector);
ERC20Pool(address(_pool)).settle(address(this), 0);
}

function testValidateTakeInput() external tearDown {
// revert on zero amount
vm.expectRevert(IPoolErrors.InvalidAmount.selector);
ERC20Pool(address(_pool)).take(address(this), 0, address(this), new bytes(0));
}
}
Loading

0 comments on commit 5f143f6

Please sign in to comment.