Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into feat/deployment-script
  • Loading branch information
wei3erHase committed Jul 15, 2024
2 parents c27b95b + d00b1eb commit d41d162
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 236 deletions.
1 change: 1 addition & 0 deletions .forge-snapshots/newBCoWFactory.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4889580
1 change: 1 addition & 0 deletions .forge-snapshots/newBCoWPool.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4033896
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ jobs:
- uses: actions/checkout@v3

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
uses: foundry-rs/foundry-toolchain@v1.2.0
with:
version: nightly

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: 'yarn'

- name: Install dependencies
run: yarn --frozen-lockfile --network-concurrency 1

- name: Precompile using 0.8.14 and via-ir=false
- name: Precompile contracts
run: yarn build

- name: Run tests
Expand All @@ -45,20 +45,20 @@ jobs:
- uses: actions/checkout@v3

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
uses: foundry-rs/foundry-toolchain@v1.2.0
with:
version: nightly

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: 'yarn'

- name: Install dependencies
run: yarn --frozen-lockfile --network-concurrency 1

- name: Precompile using 0.8.14 and via-ir=false
- name: Precompile contracts
run: yarn build

- name: Run tests
Expand All @@ -77,14 +77,14 @@ jobs:
- uses: wagoid/commitlint-github-action@v5

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
uses: foundry-rs/foundry-toolchain@v1.2.0
with:
version: nightly

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
cache: 'yarn'

- name: Install bulloak
Expand Down
29 changes: 25 additions & 4 deletions test/integration/DeploymentGas.t.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BCoWFactory} from 'contracts/BCoWFactory.sol';
import {BFactory} from 'contracts/BFactory.sol';

import {GasSnapshot} from 'forge-gas-snapshot/GasSnapshot.sol';
import {Test} from 'forge-std/Test.sol';

contract MockSolutionSettler {
address public vaultRelayer;
bytes32 public domainSeparator;
}

contract DeploymentIntegrationGasTest is Test, GasSnapshot {
BFactory public factory;
BFactory public bFactory;
BCoWFactory public bCowFactory;
address solutionSettler;
address deployer = makeAddr('deployer');

function setUp() public {
factory = new BFactory();
vm.startPrank(deployer);
bFactory = new BFactory();

solutionSettler = address(new MockSolutionSettler());
bCowFactory = new BCoWFactory(solutionSettler, bytes32('appData'));
}

function testFactoryDeployment() public {
snapStart('newBFactory');
new BFactory();
snapEnd();

snapStart('newBCoWFactory');
new BCoWFactory(solutionSettler, bytes32('appData'));
snapEnd();
}

function testDeployment() public {
function testPoolDeployment() public {
snapStart('newBPool');
factory.newBPool();
bFactory.newBPool();
snapEnd();

snapStart('newBCoWPool');
bCowFactory.newBPool();
snapEnd();
}
}
224 changes: 0 additions & 224 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -818,230 +818,6 @@ contract BPool_Unit_GetSpotPriceSansFee is BasePoolTest {
}
}

contract BPool_Unit_SwapExactAmountIn is SwapExactAmountInUtils {
function test_Revert_NotBoundTokenIn(
SwapExactAmountIn_FuzzScenario memory _fuzz,
address _tokenIn
) public happyPath(_fuzz) {
assumeNotForgeAddress(_tokenIn);
vm.assume(_tokenIn != tokenIn);
vm.assume(_tokenIn != tokenOut);

vm.expectRevert(IBPool.BPool_TokenNotBound.selector);
bPool.swapExactAmountIn(_tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Revert_NotBoundTokenOut(
SwapExactAmountIn_FuzzScenario memory _fuzz,
address _tokenOut
) public happyPath(_fuzz) {
assumeNotForgeAddress(_tokenOut);
vm.assume(_tokenOut != tokenIn);
vm.assume(_tokenOut != tokenOut);

vm.expectRevert(IBPool.BPool_TokenNotBound.selector);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, _tokenOut, 0, type(uint256).max);
}

function test_Revert_NotFinalized(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
_setFinalize(false);

vm.expectRevert(IBPool.BPool_PoolNotFinalized.selector);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Revert_TokenAmountInAboveMaxIn(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _tokenAmountIn = bmul(_fuzz.tokenInBalance, MAX_IN_RATIO) + 1;

vm.expectRevert(IBPool.BPool_TokenAmountInAboveMaxRatio.selector);
bPool.swapExactAmountIn(tokenIn, _tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Revert_SpotPriceAboveMaxPrice(
SwapExactAmountIn_FuzzScenario memory _fuzz,
uint256 _maxPrice
) public happyPath(_fuzz) {
uint256 _spotPriceBefore = calcSpotPrice(
_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.swapFee
);
vm.assume(_spotPriceBefore > 0);
_maxPrice = bound(_maxPrice, 0, _spotPriceBefore - 1);

vm.expectRevert(IBPool.BPool_SpotPriceAboveMaxPrice.selector);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, _maxPrice);
}

function test_Revert_TokenAmountOutBelowMinOut(
SwapExactAmountIn_FuzzScenario memory _fuzz,
uint256 _minAmountOut
) public happyPath(_fuzz) {
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.tokenAmountIn,
_fuzz.swapFee
);
_minAmountOut = bound(_minAmountOut, _tokenAmountOut + 1, type(uint256).max);

vm.expectRevert(IBPool.BPool_TokenAmountOutBelowMinOut.selector);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, _minAmountOut, type(uint256).max);
}

function test_Revert_Reentrancy(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
_expectRevertByReentrancy();
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Revert_SpotPriceAfterBelowSpotPriceBefore() public {
vm.skip(true);
// TODO: this revert might be unreachable. Find a way to test it or remove the revert in the code.
}

function test_Revert_SpotPriceAfterAboveMaxPrice(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.tokenAmountIn,
_fuzz.swapFee
);
uint256 _spotPriceBefore = calcSpotPrice(
_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.swapFee
);
uint256 _spotPriceAfter = calcSpotPrice(
_fuzz.tokenInBalance + _fuzz.tokenAmountIn,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance - _tokenAmountOut,
_fuzz.tokenOutDenorm,
_fuzz.swapFee
);
vm.assume(_spotPriceAfter > _spotPriceBefore);

vm.expectRevert(IBPool.BPool_SpotPriceAboveMaxPrice.selector);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, _spotPriceBefore);
}

function test_Revert_SpotPriceBeforeAboveTokenRatio(SwapExactAmountIn_FuzzScenario memory _fuzz) public {
// Replicating _assumeHappyPath, but removing irrelevant assumptions and conditioning the revert
_fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT);
_fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, MIN_WEIGHT, MAX_WEIGHT);
_fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE);
_fuzz.tokenInBalance = bound(_fuzz.tokenInBalance, MIN_BALANCE, type(uint256).max / _fuzz.tokenInDenorm);
_fuzz.tokenOutBalance = bound(_fuzz.tokenOutBalance, MIN_BALANCE, type(uint256).max / _fuzz.tokenOutDenorm);
vm.assume(_fuzz.tokenAmountIn < type(uint256).max - _fuzz.tokenInBalance);
vm.assume(_fuzz.tokenInBalance + _fuzz.tokenAmountIn < type(uint256).max / _fuzz.tokenInDenorm);
_assumeCalcSpotPrice(
_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.swapFee
);
vm.assume(_fuzz.tokenAmountIn <= bmul(_fuzz.tokenInBalance, MAX_IN_RATIO));
uint256 _spotPriceBefore = calcSpotPrice(
_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.swapFee
);
_assumeCalcOutGivenIn(_fuzz.tokenInBalance, _fuzz.tokenAmountIn, _fuzz.swapFee);
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.tokenAmountIn,
_fuzz.swapFee
);
vm.assume(_tokenAmountOut > BONE);
_assumeCalcSpotPrice(
_fuzz.tokenInBalance + _fuzz.tokenAmountIn,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance - _tokenAmountOut,
_fuzz.tokenOutDenorm,
_fuzz.swapFee
);
vm.assume(_spotPriceBefore > bdiv(_fuzz.tokenAmountIn, _tokenAmountOut));

_setValues(_fuzz);

vm.expectRevert(IBPool.BPool_SpotPriceBeforeAboveTokenRatio.selector);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Emit_LogSwap(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.tokenAmountIn,
_fuzz.swapFee
);

vm.expectEmit();
emit IBPool.LOG_SWAP(address(this), tokenIn, tokenOut, _fuzz.tokenAmountIn, _tokenAmountOut);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Set_ReentrancyLock(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
_expectSetReentrancyLock();
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Pull_TokenAmountIn(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
vm.expectCall(
address(tokenIn),
abi.encodeWithSelector(IERC20.transferFrom.selector, address(this), address(bPool), _fuzz.tokenAmountIn)
);
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Push_TokenAmountOut(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.tokenAmountIn,
_fuzz.swapFee
);

vm.expectCall(address(tokenOut), abi.encodeWithSelector(IERC20.transfer.selector, address(this), _tokenAmountOut));
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}

function test_Returns_AmountAndPrice(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _expectedTokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.tokenAmountIn,
_fuzz.swapFee
);
uint256 _expectedSpotPriceAfter = calcSpotPrice(
_fuzz.tokenInBalance + _fuzz.tokenAmountIn,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance - _expectedTokenAmountOut,
_fuzz.tokenOutDenorm,
_fuzz.swapFee
);

(uint256 _tokenAmountOut, uint256 _spotPriceAfter) =
bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);

assertEq(_tokenAmountOut, _expectedTokenAmountOut);
assertEq(_spotPriceAfter, _expectedSpotPriceAfter);
}

function test_Emit_LogCall(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
vm.expectEmit();
bytes memory _data = abi.encodeWithSelector(
BPool.swapExactAmountIn.selector, tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max
);
emit IBPool.LOG_CALL(BPool.swapExactAmountIn.selector, address(this), _data);

bPool.swapExactAmountIn(tokenIn, _fuzz.tokenAmountIn, tokenOut, 0, type(uint256).max);
}
}

contract BPool_Unit_SwapExactAmountOut is BasePoolTest {
address tokenIn;
address tokenOut;
Expand Down
Loading

0 comments on commit d41d162

Please sign in to comment.