Skip to content

Commit

Permalink
fix: issues on merging dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Jul 26, 2024
1 parent 1eedf2f commit 810e129
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crytic-export/combined_solc.json

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions test/integration/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ abstract contract BPoolIntegrationTest is Test, GasSnapshot {
uint256 public constant DAI_AMOUNT = HUNDRED_UNITS;
uint256 public constant WETH_AMOUNT_INVERSE = ONE_TENTH_UNIT;

// settings
uint256 constant SWAP_FEE = 0.999999e18; // 99.9999%

// swap amounts OUT
// NOTE: amounts OUT are hardcoded from test result
uint256 public constant WETH_OUT_AMOUNT = 94_049_266_814_811_022; // 0.094 ETH
Expand All @@ -73,8 +76,8 @@ abstract contract BPoolIntegrationTest is Test, GasSnapshot {
deal(address(weth), lp, 2 * WETH_LP_AMOUNT);
deal(address(wbtc), lp, 2 * WBTC_LP_AMOUNT);

deal(address(dai), swapper.addr, 2 * DAI_AMOUNT);
deal(address(weth), swapperInverse.addr, 2 * WETH_AMOUNT_INVERSE);
deal(address(dai), swapper.addr, type(uint192).max);
deal(address(weth), swapperInverse.addr, type(uint192).max);

deal(address(dai), joiner.addr, 2 * DAI_LP_AMOUNT);
deal(address(weth), joiner.addr, 2 * WETH_LP_AMOUNT);
Expand All @@ -96,6 +99,10 @@ abstract contract BPoolIntegrationTest is Test, GasSnapshot {
whitnessPool.bind(address(weth), WETH_LP_AMOUNT, WETH_WEIGHT);
whitnessPool.bind(address(wbtc), WBTC_LP_AMOUNT, WBTC_WEIGHT);

// set swap fee
pool.setSwapFee(SWAP_FEE);
whitnessPool.setSwapFee(SWAP_FEE);

// finalize
pool.finalize();
whitnessPool.finalize();
Expand Down
2 changes: 1 addition & 1 deletion test/invariants/PROPERTIES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| Properties | Type | Id | Halmos | Echidna |
| ------------------------------------------------------------------------------------------- | ------------------- | --- | ------ | ------- |
| BFactory should always be able to deploy new pools | Unit | 1 | [x] | [x] |
| BFactory's blab should always be modifiable by the current blabs | Unit | 2 | [x] | [x] |
| BFactory's blab should always be modifiable by the current BDao | Unit | 2 | [x] | [x] |
| BFactory should always be able to transfer the BToken to the blab, if called by it | Unit | 3 | [x] | [x] |
| the amount received can never be less than min amount out | Unit | 4 | :( | [x] |
| the amount spent can never be greater than max amount in | Unit | 5 | :( | [x] |
Expand Down
24 changes: 13 additions & 11 deletions test/invariants/fuzz/BMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract FuzzBMath is EchidnaTest {
uint256 MIN_FEE;
uint256 MAX_FEE;

function setUp() public {
constructor() {
bmath = new BMath();

MIN_WEIGHT = bmath.MIN_WEIGHT();
Expand Down Expand Up @@ -112,7 +112,7 @@ contract FuzzBMath is EchidnaTest {
// );
// emit Log('calc_poolAmountOut', calc_poolAmountOut);

// assert(tokenAmountOut == calc_poolAmountOut);
// assert(tokenAmountOut >= calc_poolAmountOut);
// }

// // calcPoolOutGivenSingleIn should be inverse of calcSingleInGivenPoolOut
Expand All @@ -139,11 +139,11 @@ contract FuzzBMath is EchidnaTest {
// );
// emit Log('calc_tokenAmountIn', calc_tokenAmountIn);

// assert(tokenAmountIn == calc_tokenAmountIn);
// assert(tokenAmountIn <= calc_tokenAmountIn);
// }

// calcPoolOutGivenSingleIn * calcSingleOutGivenPoolIn should be equal to calcOutGivenIn
function testIndirectSwaps_CalcOutGivenIn(
function fuzz_testIndirectSwaps_CalcOutGivenIn(
uint256 tokenBalanceIn,
uint256 tokenWeightIn,
uint256 tokenBalanceOut,
Expand All @@ -153,11 +153,12 @@ contract FuzzBMath is EchidnaTest {
uint256 totalWeight,
uint256 swapFee
) public {
tokenWeightIn = clamp(tokenWeightIn, MIN_WEIGHT, MAX_WEIGHT);
tokenWeightOut = clamp(tokenWeightOut, MIN_WEIGHT, MAX_WEIGHT);
tokenWeightIn = clamp(tokenWeightIn, MIN_WEIGHT, MAX_WEIGHT - MIN_WEIGHT);
tokenWeightOut = clamp(tokenWeightOut, MIN_WEIGHT, MAX_TOTAL_WEIGHT - tokenWeightIn);
totalWeight = clamp(totalWeight, tokenWeightIn + tokenWeightOut, MAX_TOTAL_WEIGHT);
tokenBalanceIn = clamp(tokenBalanceIn, BONE, type(uint256).max);
tokenBalanceOut = clamp(tokenBalanceOut, BONE, type(uint256).max);
tokenAmountIn = clamp(tokenAmountIn, BONE, type(uint256).max);
poolSupply = clamp(poolSupply, 100 * BONE, type(uint256).max);
swapFee = clamp(swapFee, MIN_FEE, MAX_FEE);

Expand All @@ -183,12 +184,12 @@ contract FuzzBMath is EchidnaTest {
emit Log('calc_inv_tokenAmountOut', calc_inv_tokenAmountOut);

assert(
calc_tokenAmountOut * 1_000_001 / 1_000_000 >= calc_inv_tokenAmountOut // direct path should be greater or equal to indirect path
calc_tokenAmountOut >= calc_inv_tokenAmountOut // direct path should be greater or equal to indirect path
);
}

// calcPoolInGivenSingleOut * calcSingleInGivenPoolOut should be equal to calcInGivenOut
function testIndirectSwaps_CalcInGivenOut(
function fuzz_testIndirectSwaps_CalcInGivenOut(
uint256 tokenBalanceIn,
uint256 tokenWeightIn,
uint256 tokenBalanceOut,
Expand All @@ -198,12 +199,13 @@ contract FuzzBMath is EchidnaTest {
uint256 totalWeight,
uint256 swapFee
) public {
tokenWeightIn = clamp(tokenWeightIn, MIN_WEIGHT, MAX_WEIGHT);
tokenWeightOut = clamp(tokenWeightOut, MIN_WEIGHT, MAX_WEIGHT);
tokenWeightIn = clamp(tokenWeightIn, MIN_WEIGHT, MAX_WEIGHT - MIN_WEIGHT);
tokenWeightOut = clamp(tokenWeightOut, MIN_WEIGHT, MAX_TOTAL_WEIGHT - tokenWeightIn);
totalWeight = clamp(totalWeight, tokenWeightIn + tokenWeightOut, MAX_TOTAL_WEIGHT);
tokenBalanceIn = clamp(tokenBalanceIn, BONE, type(uint256).max);
tokenBalanceOut = clamp(tokenBalanceOut, BONE, type(uint256).max);
poolSupply = clamp(poolSupply, 100 * BONE, type(uint256).max);
tokenAmountOut = clamp(tokenAmountOut, BONE, type(uint256).max);
swapFee = clamp(swapFee, MIN_FEE, MAX_FEE);

emit Log('tokenWeightIn', tokenWeightIn);
Expand All @@ -228,7 +230,7 @@ contract FuzzBMath is EchidnaTest {
emit Log('calc_inv_poolAmountIn', calc_inv_poolAmountIn);

assert(
calc_tokenAmountIn <= calc_inv_poolAmountIn * 1_000_001 / 1_000_000 // direct path should be lesser or equal to indirect path
calc_tokenAmountIn <= calc_inv_poolAmountIn // direct path should be lesser or equal to indirect path
);
}
}
2 changes: 1 addition & 1 deletion test/invariants/fuzz/BMath.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
testMode: assertion
corpusDir: test/invariants/fuzz/corpuses/BMath/
coverageFormats: ["html","lcov"]
allContracts: true
allContracts: false
testLimit: 50000
8 changes: 4 additions & 4 deletions test/invariants/fuzz/Protocol.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ contract FuzzProtocol is EchidnaTest {
}

/// @custom:property-id 2
/// @custom:property BFactory's blab should always be modifiable by the current blabs
/// @custom:property BFactory's blab should always be modifiable by the current BDao
function fuzz_blabAlwaysModByBLab() public AgentOrDeployer {
// Precondition
address _currentBLab = factory.getBLabs();
address _currentBLab = factory.getBDao();

hevm.prank(currentCaller);

// Action
try factory.setBLabs(address(123)) {
try factory.setBDao(address(123)) {
// Postcondition
assert(_currentBLab == currentCaller);
} catch {
Expand All @@ -146,7 +146,7 @@ contract FuzzProtocol is EchidnaTest {
/// @custom:property BFactory should always be able to transfer the BToken to the blab, if called by it
function fuzz_alwaysCollect() public AgentOrDeployer {
// Precondition
address _currentBLab = factory.getBLabs();
address _currentBLab = factory.getBDao();

if (address(pool) == address(0)) {
return;
Expand Down
8 changes: 4 additions & 4 deletions test/invariants/symbolic/Protocol.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ contract HalmosBalancer is HalmosTest {
}

/// @custom:property-id 2
/// @custom:property BFactory's blab should always be modifiable by the current blabs
/// @custom:property BFactory's blab should always be modifiable by the current BDao
function check_blabAlwaysModByBLab() public {
// Precondition
address _currentBLab = factory.getBLabs();
address _currentBLab = factory.getBDao();

vm.prank(currentCaller);

// Action
try factory.setBLabs(address(123)) {
try factory.setBDao(address(123)) {
// Postcondition
assert(_currentBLab == currentCaller);
} catch {
Expand All @@ -95,7 +95,7 @@ contract HalmosBalancer is HalmosTest {
/// @custom:property BFactory should always be able to transfer the BToken to the blab, if called by it
function check_alwaysCollect() public {
// Precondition
address _currentBLab = factory.getBLabs();
address _currentBLab = factory.getBDao();

vm.prank(currentCaller);

Expand Down

0 comments on commit 810e129

Please sign in to comment.