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

test: ensure results are 0.1% from each other #187

Merged
merged 1 commit into from
Aug 1, 2024
Merged
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
23 changes: 10 additions & 13 deletions test/invariants/fuzz/BMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ contract FuzzBMath is EchidnaTest {
* @dev Reducing BPOW_PRECISION may allow broader range of values increasing the gas cost
*/
uint256 constant MAX_BALANCE = 1_000_000e18;
uint256 constant MIN_BALANCE = 1e18;
uint256 constant MIN_AMOUNT = 1e6;
uint256 constant MAX_TOLERANCE = 1e12;
uint256 constant MIN_BALANCE = 100e18;
uint256 constant MIN_AMOUNT = 1e12;
uint256 constant TOLERANCE_PRECISION = 1e18;
uint256 constant MAX_TOLERANCE = 1e18 + 1e15; //0.1%
Comment on lines +20 to +23
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what this whole thing means is 'ensure results are 0.1% from each other in every case where the amount to be traded is at most 8 orders of magnitude away from the pool's balance' which I think is pretty reasonable

used the same MIN_BALANCE as in bconst


constructor() {
bMath = new BMath();
Expand Down Expand Up @@ -53,11 +54,9 @@ contract FuzzBMath is EchidnaTest {
bMath.calcInGivenOut(tokenBalanceIn, tokenWeightIn, tokenBalanceOut, tokenWeightOut, calc_tokenAmountOut, swapFee);

assert(
calc_tokenAmountOut < MIN_AMOUNT // return amount too small
|| tokenAmountIn == calc_tokenAmountIn // exact match
|| tokenAmountIn > calc_tokenAmountIn // within tolerance
? (tokenAmountIn * 1e18 / calc_tokenAmountIn) - 1e18 <= MAX_TOLERANCE
: (calc_tokenAmountIn * 1e18 / tokenAmountIn) - 1e18 <= MAX_TOLERANCE
tokenAmountIn >= calc_tokenAmountIn
? (tokenAmountIn * TOLERANCE_PRECISION / calc_tokenAmountIn) <= MAX_TOLERANCE
: (calc_tokenAmountIn * TOLERANCE_PRECISION / tokenAmountIn) <= MAX_TOLERANCE
);
}

Expand All @@ -84,11 +83,9 @@ contract FuzzBMath is EchidnaTest {
bMath.calcOutGivenIn(tokenBalanceOut, tokenWeightOut, tokenBalanceIn, tokenWeightIn, calc_tokenAmountIn, swapFee);

assert(
calc_tokenAmountIn < MIN_AMOUNT // return amount too small
|| tokenAmountOut == calc_tokenAmountOut // exact match
|| tokenAmountOut > calc_tokenAmountOut // within tolerance
? (tokenAmountOut * 1e18 / calc_tokenAmountOut) - 1e18 <= MAX_TOLERANCE
: (calc_tokenAmountOut * 1e18 / tokenAmountOut) - 1e18 <= MAX_TOLERANCE
tokenAmountOut >= calc_tokenAmountOut
? (tokenAmountOut * TOLERANCE_PRECISION / calc_tokenAmountOut) <= MAX_TOLERANCE
: (calc_tokenAmountOut * TOLERANCE_PRECISION / tokenAmountOut) <= MAX_TOLERANCE
);
}
}
Loading