Skip to content

Commit

Permalink
test: bound denorms
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAustrian committed May 14, 2024
1 parent 42aeb4a commit 20dfb97
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ src = 'src/interfaces/'

[fuzz]
runs = 1000
max_test_rejects = 20_000_000
max_test_rejects = 1000000

[rpc_endpoints]
mainnet = "${MAINNET_RPC}"
Expand Down
3 changes: 1 addition & 2 deletions src/contracts/BPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ contract BPool is BBronze, BToken, BMath {
spotPriceAfter = calcSpotPrice(inRecord.balance, inRecord.denorm, outRecord.balance, outRecord.denorm, _swapFee);
require(spotPriceAfter >= spotPriceBefore, 'ERR_MATH_APPROX');
require(spotPriceAfter <= maxPrice, 'ERR_LIMIT_PRICE');
// TODO: re-enable when fuzzing is complete
// require(spotPriceBefore <= bdiv(tokenAmountIn, tokenAmountOut), 'ERR_MATH_APPROX');
require(spotPriceBefore <= bdiv(tokenAmountIn, tokenAmountOut), 'ERR_MATH_APPROX');

emit LOG_SWAP(msg.sender, tokenIn, tokenOut, tokenAmountIn, tokenAmountOut);

Expand Down
20 changes: 9 additions & 11 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {MockBPool} from 'test/smock/MockBPool.sol';
import {BConst} from 'contracts/BConst.sol';
import {BMath} from 'contracts/BMath.sol';
import {IERC20} from 'contracts/BToken.sol';
import {Test, console} from 'forge-std/Test.sol';
import {Test} from 'forge-std/Test.sol';
import {LibString} from 'solmate/utils/LibString.sol';
import {Utils} from 'test/unit/Utils.sol';

Expand Down Expand Up @@ -552,11 +552,9 @@ contract BPool_Unit_SwapExactAmountIn is BasePoolTest, BMath {
}

function _assumeHappyPath(SwapExactAmountIn_FuzzScenario memory _fuzz) internal view {
// weights
vm.assume(_fuzz.tokenInDenorm >= MIN_WEIGHT);
vm.assume(_fuzz.tokenInDenorm <= MAX_WEIGHT);
vm.assume(_fuzz.tokenOutDenorm >= MIN_WEIGHT);
vm.assume(_fuzz.tokenOutDenorm <= MAX_WEIGHT);
// safe bound assumptions
_fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT);
_fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, MIN_WEIGHT, MAX_WEIGHT);

// min
vm.assume(_fuzz.tokenInBalance >= MIN_BALANCE);
Expand All @@ -574,13 +572,13 @@ contract BPool_Unit_SwapExactAmountIn is BasePoolTest, BMath {
vm.assume(_ratio < type(uint256).max / _scale);

// MAX_IN_RATIO
// vm.assume(_fuzz.tokenInBalance < type(uint256).max / MAX_IN_RATIO);
vm.assume(_fuzz.tokenAmountIn <= bmul(_fuzz.tokenInBalance, MAX_IN_RATIO));

// uint _spotPriceBefore = calcSpotPrice(_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, MIN_FEE);
// uint _tokenAmountOut = calcOutGivenIn(_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.tokenAmountIn, MIN_FEE);
// vm.assume(_tokenAmountOut > 0);
// vm.assume(_spotPriceBefore <= bdiv(_fuzz.tokenAmountIn, _tokenAmountOut));
// L338 BPool.sol
uint _spotPriceBefore = calcSpotPrice(_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, MIN_FEE);
uint _tokenAmountOut = calcOutGivenIn(_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.tokenAmountIn, MIN_FEE);
vm.assume(_tokenAmountOut > BONE);
vm.assume(bmul(_spotPriceBefore, _tokenAmountOut) <= _fuzz.tokenAmountIn);
}

modifier happyPath(SwapExactAmountIn_FuzzScenario memory _fuzz) {
Expand Down

0 comments on commit 20dfb97

Please sign in to comment.