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

Commit

Permalink
add testDrawRepayDebtFuzzy test for ERC20 and ERC721 Pools (#442)
Browse files Browse the repository at this point in the history
* add initial testDrawRepayDebtFuzzy test

* expand assertions

* update tests

* assert bucket after repay

* finalize testDrawRepayDebtFuzzy

* reduce stack size

* add erc721 collection drawrepay debt fuzzy test

* remove logs

* fix interest check for deposit above htp

* fix stack too deep

* reduce 721 borrow amounts to speed up tests

* add FuzzyHelperContracts which don't fork to speed up fuzz tests

* fix test

* update htp check for interest in erc20 pool

* clean up tests

* remove dead comment

* fix coverage; expand bounds of erc721

* remove solmate

* fix testLoad

Co-authored-by: Mike <mikehathaway@makerdao.com>
  • Loading branch information
MikeHathaway and Mike authored Jan 7, 2023
1 parent f01f459 commit 1893052
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ jobs:
- name: Verify coverage level
uses: zgosalvez/github-actions-report-lcov@v1
with:
coverage-files: ./lcov.info
minimum-coverage: 0 # Set coverage threshold.
coverage-files: ./lcov-filtered.info
minimum-coverage: 95 # Set coverage threshold.
# github-token: ${{ secrets.GITHUB_TOKEN }} # Adds a coverage summary comment to the PR.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "lib/solmate"]
path = lib/solmate
url = https://github.com/rari-capital/solmate
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/brockelmore/forge-std
Expand Down
1 change: 0 additions & 1 deletion lib/solmate
Submodule solmate deleted from 7f433c
10 changes: 5 additions & 5 deletions src/base/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -359,25 +359,25 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
/*****************************/

function _accruePoolInterest() internal returns (PoolState memory poolState_) {
// retrieve t0Debt amount from poolBalances struct
// retrieve t0Debt amount from poolBalances struct
uint256 t0Debt = poolBalances.t0Debt;

// initialize fields of poolState_ struct with initial values
// initialize fields of poolState_ struct with initial values
poolState_.collateral = poolBalances.pledgedCollateral;
poolState_.inflator = inflatorState.inflator;
poolState_.rate = interestState.interestRate;
poolState_.poolType = _getArgUint8(POOL_TYPE);
poolState_.quoteDustLimit = _getArgUint256(QUOTE_SCALE);

// check if t0Debt is not equal to 0, indicating that there is debt to be tracked for the pool
// check if t0Debt is not equal to 0, indicating that there is debt to be tracked for the pool
if (t0Debt != 0) {
// Calculate prior pool debt
poolState_.debt = Maths.wmul(t0Debt, poolState_.inflator);

// calculate elapsed time since inflator was last updated
// calculate elapsed time since inflator was last updated
uint256 elapsed = block.timestamp - inflatorState.inflatorUpdate;

// set isNewInterestAccrued field to true if elapsed time is not 0, indicating that new interest may have accrued
// set isNewInterestAccrued field to true if elapsed time is not 0, indicating that new interest may have accrued
poolState_.isNewInterestAccrued = elapsed != 0;

// if new interest may have accrued, call accrueInterest function and update inflator and debt fields of poolState_ struct
Expand Down
46 changes: 2 additions & 44 deletions tests/forge/AjnaRewards.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1290,48 +1290,6 @@ contract AjnaRewardsTest is DSTestPlus {
/*** FUZZ TESTING ***/
/********************/

function _randomIndex() internal view returns (uint256) {
// calculate a random index between 1 and 7388
return 1 + uint256(keccak256(abi.encodePacked(block.number, block.difficulty))) % 7387;
}

function _findHighestIndexPrice(uint256[] memory indexes) internal pure returns (uint256 highestIndex_) {
highestIndex_ = 7388;
// highest index corresponds to lowest price
for (uint256 i = 0; i < indexes.length; ++i) {
if (indexes[i] < highestIndex_) {
highestIndex_ = indexes[i];
}
}
}

function _findLowestIndexPrice(uint256[] memory indexes) internal pure returns (uint256 lowestIndex_) {
lowestIndex_ = 1;
// lowest index corresponds to highest price
for (uint256 i = 0; i < indexes.length; ++i) {
if (indexes[i] > lowestIndex_) {
lowestIndex_ = indexes[i];
}
}
}

// calculates a limit index leaving one index above the htp to accrue interest
function _findSecondLowestIndexPrice(uint256[] memory indexes) internal pure returns (uint256 secondLowestIndex_) {
secondLowestIndex_ = 1;
uint256 lowestIndex = secondLowestIndex_;

// lowest index corresponds to highest price
for (uint256 i = 0; i < indexes.length; ++i) {
if (indexes[i] > lowestIndex) {
secondLowestIndex_ = lowestIndex;
lowestIndex = indexes[i];
}
else if (indexes[i] > secondLowestIndex_) {
secondLowestIndex_ = indexes[i];
}
}
}

function _requiredCollateral(ERC20Pool pool_, uint256 borrowAmount, uint256 indexPrice) internal view returns (uint256 requiredCollateral_) {
// calculate the required collateral based upon the borrow amount and index price
(uint256 interestRate, ) = pool_.interestRateInfo();
Expand All @@ -1348,16 +1306,16 @@ contract AjnaRewardsTest is DSTestPlus {
uint256[] memory depositIndexes = new uint256[](indexes);
for (uint256 i = 0; i < indexes; ++i) {
depositIndexes[i] = _randomIndex();
vm.roll(block.number + 1); // advance block to ensure that the index price is different
}
MintAndMemorializeParams memory mintMemorializeParams = MintAndMemorializeParams({
indexes: depositIndexes,
minter: _minterOne,
mintAmount: mintAmount,
pool: _poolOne
});

uint256 tokenIdOne = _mintAndMemorializePositionNFT(mintMemorializeParams);

// stake NFT
_stakeToken(address(_poolOne), _minterOne, tokenIdOne);

// calculates a limit index leaving one index above the htp to accrue interest
Expand Down
44 changes: 43 additions & 1 deletion tests/forge/ERC20Pool/ERC20DSTestPlus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,48 @@ abstract contract ERC20HelperContract is ERC20DSTestPlus {
_collateral.approve(address(_pool), type(uint256).max);
vm.prank(operator_);
_quote.approve(address(_pool), type(uint256).max);
}

}

abstract contract ERC20FuzzyHelperContract is ERC20DSTestPlus {

using EnumerableSet for EnumerableSet.AddressSet;

uint256 public constant LARGEST_AMOUNT = type(uint256).max / 10**27;

uint internal _anonBorrowerCount = 0;
Token internal _collateral;
Token internal _quote;

ERC20PoolFactory internal _poolFactory;

constructor() {
_collateral = new Token("Collateral", "C");
_quote = new Token("Quote", "Q");
_poolFactory = new ERC20PoolFactory(_ajna);
_pool = ERC20Pool(_poolFactory.deployPool(address(_collateral), address(_quote), 0.05 * 10**18));
_poolUtils = new PoolInfoUtils();
_startTime = block.timestamp;
}

function _mintQuoteAndApproveTokens(address operator_, uint256 mintAmount_) internal {
deal(address(_quote), operator_, mintAmount_);

vm.prank(operator_);
_quote.approve(address(_pool), type(uint256).max);
vm.prank(operator_);
_collateral.approve(address(_pool), type(uint256).max);
}
}

function _mintCollateralAndApproveTokens(address operator_, uint256 mintAmount_) internal {
deal(address(_collateral), operator_, mintAmount_);

vm.prank(operator_);
_collateral.approve(address(_pool), type(uint256).max);
vm.prank(operator_);
_quote.approve(address(_pool), type(uint256).max);
}


}
Loading

0 comments on commit 1893052

Please sign in to comment.