Skip to content

Commit

Permalink
Fix fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Feb 24, 2023
1 parent 5f143f6 commit 71568b5
Showing 1 changed file with 42 additions and 73 deletions.
115 changes: 42 additions & 73 deletions tests/forge/ERC20Pool/ERC20PoolPrecision.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ contract ERC20PoolPrecisionTest is ERC20DSTestPlus {
assertEq(_quote.balanceOf(_borrower), 5_000 * _quotePrecision);
}

// FIXME: the scaled amounts are always 0
function _testDepositTwoActorSameBucket(
function testDepositTwoActorSameBucket(
uint8 collateralPrecisionDecimals_,
uint8 quotePrecisionDecimals_,
uint16 bucketId_,
Expand All @@ -453,17 +452,24 @@ contract ERC20PoolPrecisionTest is ERC20DSTestPlus {
// setup fuzzy bounds and initialize the pool
uint256 boundColPrecision = bound(uint256(collateralPrecisionDecimals_), 1, 18);
uint256 boundQuotePrecision = bound(uint256(quotePrecisionDecimals_), 1, 18);
uint256 bucketId = bound(uint256(bucketId_), 1, 7388);

init(boundColPrecision, boundQuotePrecision);

uint256 bucketId = bound(uint256(bucketId_), 1, 7388);

// ensure half of deposits are below the scale limit
uint256 maxColAmountBound = collateralAmount_ % 2 == 0 ? MAX_COLLATERAL : uint256(10) ** boundColPrecision;
uint256 maxQuoteAmountBound = quoteAmount_ % 2 == 0 ? MAX_DEPOSIT : uint256(10) ** boundQuotePrecision;
uint256 collateralAmount = bound(uint256(collateralAmount_), 0, maxColAmountBound);
uint256 quoteAmount = bound(uint256(quoteAmount_), 0, maxQuoteAmountBound);
init(boundColPrecision, boundQuotePrecision);
uint256 collateralAmount = bound(uint256(collateralAmount_), 1, maxColAmountBound);
uint256 quoteAmount = bound(uint256(quoteAmount_), 1, maxQuoteAmountBound);

uint256 quoteScale = _pool.quoteTokenScale();
uint256 quoteDust = _pool.quoteTokenDust();
if (quoteAmount < quoteDust) quoteAmount = quoteDust;

uint256 scaledQuoteAmount = (quoteAmount / 10 ** (18 - boundQuotePrecision)) * 10 ** (18 - boundQuotePrecision);
uint256 scaledColAmount = (collateralAmount / 10 ** (18 - boundColPrecision)) * 10 ** (18 - boundColPrecision);
uint256 colDustAmount = ERC20Pool(address(_pool)).bucketCollateralDust(bucketId);
uint256 colScale = ERC20Pool(address(_pool)).collateralScale();
uint256 colDustAmount = ERC20Pool(address(_pool)).bucketCollateralDust(bucketId);
if (collateralAmount < colDustAmount) collateralAmount = colDustAmount;

assertEq(ERC20Pool(address(_pool)).collateralScale(), 10 ** (18 - boundColPrecision));
assertEq(_pool.quoteTokenScale(), 10 ** (18 - boundQuotePrecision));
Expand All @@ -485,46 +491,24 @@ contract ERC20PoolPrecisionTest is ERC20DSTestPlus {
});

// addQuoteToken should add scaled quote token amount validate LP
_addInitialLiquidity({
from: _lender,
amount: quoteAmount,
index: bucketId
});

(uint256 lenderLpBalance, ) = _pool.lenderInfo(bucketId, _lender);
assertEq(lenderLpBalance, scaledQuoteAmount);
_addLiquidityNoEventCheck(_lender, quoteAmount, bucketId);

// deposit collateral and sanity check bidder LPs
uint256 bidderLpBalance;
if (collateralAmount != 0 && collateralAmount < colDustAmount) {
_assertAddCollateralDustRevert(_bidder, collateralAmount, bucketId);
} else {
_addCollateralWithoutCheckingLP(_bidder, collateralAmount, bucketId);
(bidderLpBalance, ) = _pool.lenderInfo(bucketId, _bidder);
if (collateralAmount == 0) {
assertEq(bidderLpBalance, 0);
} else {
assertGt(bidderLpBalance, 0);
}
}
_addCollateralWithoutCheckingLP(_bidder, collateralAmount, bucketId);

// check bucket quantities and LPs
uint256 curDeposit;
uint256 availableCollateral;
uint256 bucketLpBalance;
(, curDeposit, availableCollateral, bucketLpBalance,,) = _poolUtils.bucketInfo(address(_pool), bucketId);
if (bucketLpBalance == 0) {
assertEq(curDeposit, 0);
assertEq(availableCollateral, 0);
} else {
assertEq(curDeposit, scaledQuoteAmount);
assertEq(availableCollateral, collateralAmount >= colDustAmount ? scaledColAmount : 0);
}
(, uint256 curDeposit, uint256 availableCollateral, uint256 bucketLpBalance,,) = _poolUtils.bucketInfo(address(_pool), bucketId);
assertEq(curDeposit, _roundToScale(quoteAmount, quoteScale));
assertEq(availableCollateral, _roundToScale(collateralAmount, colScale));

(uint256 lenderLpBalance, ) = _pool.lenderInfo(bucketId, _lender);
assertEq(lenderLpBalance, _roundToScale(quoteAmount, quoteScale));
(uint256 bidderLpBalance, ) = _pool.lenderInfo(bucketId, _bidder);
assertGt(bidderLpBalance, 0);
assertEq(bucketLpBalance, lenderLpBalance + bidderLpBalance);
}

// FIXME: the scaled amounts are always 0
function _testDepositTwoLendersSameBucket(
function testDepositTwoLendersSameBucket(
uint8 collateralPrecisionDecimals_,
uint8 quotePrecisionDecimals_,
uint16 bucketId_,
Expand All @@ -534,17 +518,17 @@ contract ERC20PoolPrecisionTest is ERC20DSTestPlus {
// setup fuzzy bounds and initialize the pool
uint256 boundColPrecision = bound(uint256(collateralPrecisionDecimals_), 1, 18);
uint256 boundQuotePrecision = bound(uint256(quotePrecisionDecimals_), 1, 18);
uint256 bucketId = bound(uint256(bucketId_), 1, 7388);
// ensure half of deposits are below the scale limit
uint256 maxQuoteAmount1 = quoteAmount1_ % 2 == 0 ? MAX_DEPOSIT : uint256(10) ** boundQuotePrecision;
uint256 maxQuoteAmount2 = quoteAmount2_ % 2 == 0 ? MAX_DEPOSIT : uint256(10) ** boundQuotePrecision;
uint256 quoteAmount1 = bound(uint256(quoteAmount1_), 0, maxQuoteAmount1);
uint256 quoteAmount2 = bound(uint256(quoteAmount2_), 0, maxQuoteAmount2);

init(boundColPrecision, boundQuotePrecision);

// Scaled Quote Amount
uint256 scaledQuoteAmount1 = (quoteAmount1 / 10 ** (18 - boundQuotePrecision)) * 10 ** (18 - boundQuotePrecision);
uint256 scaledQuoteAmount2 = (quoteAmount2 / 10 ** (18 - boundQuotePrecision)) * 10 ** (18 - boundQuotePrecision);
uint256 bucketId = bound(uint256(bucketId_), 1, 7388);

// ensure half of deposits are below the scale limit
uint256 maxQuoteAmount1 = quoteAmount1_ % 2 == 0 ? MAX_DEPOSIT : 1e18;
uint256 maxQuoteAmount2 = quoteAmount2_ % 2 == 0 ? MAX_DEPOSIT : 1e18;

uint256 quoteAmount1 = bound(uint256(quoteAmount1_), 1e18, maxQuoteAmount1);
uint256 quoteAmount2 = bound(uint256(quoteAmount2_), 1e18, maxQuoteAmount2);

// mint and run approvals, ignoring amounts already init approved above
deal(address(_quote), _lender, quoteAmount1 * _quotePrecision);
Expand All @@ -556,35 +540,20 @@ contract ERC20PoolPrecisionTest is ERC20DSTestPlus {
_quote.approve(address(_pool), quoteAmount2 * _quotePrecision);

// addQuoteToken should add scaled quote token amount and LP
_addInitialLiquidity({
from: _lender,
amount: scaledQuoteAmount1,
index: bucketId
});

_addLiquidityNoEventCheck(_lender, quoteAmount1, bucketId);
(uint256 lpBalance1, ) = _pool.lenderInfo(bucketId, _lender);
assertGt(lpBalance1, 0);

// addQuoteToken should add scaled quote token amount and LP
vm.expectEmit(true, true, false, true);
emit AddQuoteToken(lender2, bucketId, scaledQuoteAmount2, scaledQuoteAmount2, MAX_PRICE);
_addLiquidityNoEventCheck(lender2, quoteAmount2, bucketId);
(uint256 lpBalance2, ) = _pool.lenderInfo(bucketId, lender2);
if (scaledQuoteAmount2 != 0) {
assertGt(lpBalance2, 0);
} else {
assertEq(lpBalance2, 0);
}
assertGt(lpBalance2, 0);

// check bucket
uint256 curDeposit;
uint256 bucketLPs;
(, curDeposit, , bucketLPs,,) = _poolUtils.bucketInfo(address(_pool), bucketId);
assertEq(curDeposit, scaledQuoteAmount1 + scaledQuoteAmount2);
if (curDeposit == 0) {
assertEq(bucketLPs, 0);
} else {
assertEq(bucketLPs, lpBalance1 + lpBalance2);
}
uint256 quoteScale = _pool.quoteTokenScale();
(, uint256 curDeposit, , uint256 bucketLPs,,) = _poolUtils.bucketInfo(address(_pool), bucketId);
assertEq(curDeposit, _roundToScale(quoteAmount1, quoteScale) + _roundToScale(quoteAmount2, quoteScale));
assertEq(bucketLPs, lpBalance1 + lpBalance2);
}

function testMoveQuoteToken(
Expand Down

0 comments on commit 71568b5

Please sign in to comment.