Skip to content

Commit

Permalink
test: add happy path for joinPool
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAustrian committed May 6, 2024
1 parent 20f064b commit 84e9c29
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"package.json": "sort-package-json"
},
"dependencies": {
"solmate": "github:transmissions11/solmate#a9e3ea2"
"solmate": "github:transmissions11/solmate#c892309"
},
"devDependencies": {
"@commitlint/cli": "19.3.0",
Expand Down
81 changes: 76 additions & 5 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

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

abstract contract Base is Test {
function setUp() public {}
import {BConst} from 'contracts/BConst.sol';
import {BPool} from 'contracts/BPool.sol';
import {IERC20} from 'contracts/BToken.sol';
import {Test, console} from 'forge-std/Test.sol';

Check warning on line 7 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';

abstract contract Base is Test, BConst {
using LibString for *;

BPool public bPool;
address[8] public tokens;

function setUp() public {
bPool = new BPool();

// Create fake tokens
for (uint256 i = 0; i < tokens.length; i++) {
tokens[i] = makeAddr(i.toString());
}
}

function _preparePool(
BPool _pool,
address[8] memory _tokens,
uint256[8] memory _balance,
uint256[8] memory _denorm
) internal {
// Create mocks
for (uint256 i = 0; i < _tokens.length; i++) {
vm.mockCall(_tokens[i], abi.encodeWithSelector(IERC20(_tokens[i]).transfer.selector), abi.encode(true));
vm.mockCall(_tokens[i], abi.encodeWithSelector(IERC20(_tokens[i]).transferFrom.selector), abi.encode(true));
}

// Set tokens
for (uint256 i = 0; i < _tokens.length; i++) {
vm.prank(_pool.getController());
_pool.bind(_tokens[i], _balance[i], _denorm[i]);
}

// Set public swap
vm.store(
address(_pool),
bytes32(uint256(6)),
bytes32(uint256(0x0000000000000000000000010000000000000000000000000000000000000000))
);
// Set finalize
vm.store(address(_pool), bytes32(uint256(8)), bytes32(uint256(1)));
// Set totalSupply
vm.store(address(_pool), bytes32(uint256(2)), bytes32(INIT_POOL_SUPPLY));
}

function _assumeHappyPath(

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

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

first return value does not have a name

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

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

second return value does not have a name
uint256[8] memory _balance,
uint256[8] memory _denorm
) internal view returns (uint256[8] memory, uint256[8] memory) {
for (uint256 i = 0; i < _balance.length; i++) {
_balance[i] = bound(_balance[i], MIN_BALANCE, MIN_BALANCE * 1000000); // TODO: found a better max
_denorm[i] = bound(_denorm[i], MIN_WEIGHT, MAX_WEIGHT / 8); // Div by the max number of tokens
}

return (_balance, _denorm);
}
}

contract BPool_Unit_Constructor is Base {
Expand Down Expand Up @@ -252,6 +310,19 @@ contract BPool_Unit_GetSpotPriceSansFee is Base {
}

contract BPool_Unit_JoinPool is Base {
function test_HappyPath(uint256 _poolAmountOut, uint256[8] memory _balance, uint256[8] memory _denorm) public {
vm.assume(_poolAmountOut > INIT_POOL_SUPPLY);
vm.assume(_poolAmountOut < type(uint256).max / BONE);

uint256[] memory maxAmountsIn = new uint256[](tokens.length);
for (uint256 i = 0; i < tokens.length; i++) { maxAmountsIn[i] = type(uint256).max; } // Using max possible amounts

(uint256[8] memory __balance, uint256[8] memory __denorm) = _assumeHappyPath(_balance, _denorm);
_preparePool(bPool, tokens, __balance, __denorm);

bPool.joinPool(_poolAmountOut, maxAmountsIn);
}

function test_Revert_NotFinalized() public view {}

function test_Revert_MathApprox() public view {}
Expand Down Expand Up @@ -463,7 +534,7 @@ contract BPool_Unit_ExitswapPoolAmountOut is Base {
function test_Revert_MaxOutRatio() public view {}

function test_Revert_MathApprox() public view {}

function test_Revert_LimitIn() public view {}

function test_Revert_Reentrancy() public view {}
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1713,9 +1713,9 @@ [email protected]:
optionalDependencies:
prettier "^2.8.3"

"solmate@github:transmissions11/solmate#a9e3ea2":
version "6.0.0"
resolved "https://codeload.github.com/transmissions11/solmate/tar.gz/a9e3ea26a2dc73bfa87f0cb189687d029028e0c5"
"solmate@github:transmissions11/solmate#c892309":
version "6.2.0"
resolved "https://codeload.github.com/transmissions11/solmate/tar.gz/c892309933b25c03d32b1b0d674df7ae292ba925"

sort-object-keys@^1.1.3:
version "1.1.3"
Expand Down

0 comments on commit 84e9c29

Please sign in to comment.