Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-building happy path for swapExactIn #17

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ src = 'src/interfaces/'

[fuzz]
runs = 1000
max_test_rejects = 20_000_000
fuzz_seed = 1
max_test_rejects = 1_000_000

[rpc_endpoints]
mainnet = "${MAINNET_RPC}"
Expand Down
51 changes: 20 additions & 31 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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';

Check warning on line 10 in test/unit/BPool.t.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "console" is unused
import {LibString} from 'solmate/utils/LibString.sol';
import {Utils} from 'test/unit/Utils.sol';

Expand Down Expand Up @@ -545,42 +545,31 @@
})
);

// TODO: Set swapFee

// Set public swap
_setPublicSwap(true);
// Set finalize
_setFinalize(true);
}

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);

// min
vm.assume(_fuzz.tokenInBalance >= MIN_BALANCE);
vm.assume(_fuzz.tokenOutBalance >= MIN_BALANCE);

// max - calcSpotPrice
vm.assume(_fuzz.tokenInBalance < type(uint256).max / _fuzz.tokenInDenorm);
vm.assume(_fuzz.tokenOutBalance < type(uint256).max / _fuzz.tokenOutDenorm);

// internal calculation for calcSpotPrice
uint _numer = bdiv(_fuzz.tokenInBalance, _fuzz.tokenInDenorm);
uint _denom = bdiv(_fuzz.tokenOutBalance, _fuzz.tokenOutDenorm);
uint _ratio = bdiv(_numer, _denom);
uint _scale = bdiv(BONE, bsub(BONE, MIN_FEE));
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));
function _assumeHappyPath(SwapExactAmountIn_FuzzScenario memory _fuzz) internal pure {
// safe bound assumptions
_fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT);
_fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, MIN_WEIGHT, MAX_WEIGHT);

// balance assumptions
vm.assume(_fuzz.tokenInBalance <= type(uint256).max - _fuzz.tokenAmountIn);
vm.assume(_fuzz.tokenInBalance <= type(uint256).max / BONE / MAX_IN_RATIO);

vm.assume(_fuzz.tokenAmountIn <= _fuzz.tokenInBalance * MAX_IN_RATIO / BONE); // bmul(tokenInBalance, MAX_IN_RATIO)

// no division by zero within calcSpotPrice
vm.assume(_fuzz.tokenOutBalance >= _fuzz.tokenOutDenorm);
vm.assume(_fuzz.tokenOutBalance < type(uint256).max / BONE);

// no division by zero within calcOutGivenIn
vm.assume(_fuzz.tokenAmountIn > BONE);
}

modifier happyPath(SwapExactAmountIn_FuzzScenario memory _fuzz) {
Expand All @@ -591,7 +580,7 @@

function test_HappyPath(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _maxPrice = type(uint256).max;
uint _minAmountOut = 0;
uint256 _minAmountOut = 0;
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, _minAmountOut, _maxPrice);
}

Expand Down
Loading