Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into feat/helper-poc
  • Loading branch information
wei3erHase committed Jul 9, 2024
2 parents 68e8368 + bbc79ca commit eadc1e0
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/newBFactory.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4130633
4130621
2 changes: 1 addition & 1 deletion script/DeployBCoWFactory.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract DeployBCoWFactory is Script, Params {

vm.startBroadcast();
BCoWFactory bCoWFactory = new BCoWFactory(params.settlement, params.appData);
bCoWFactory.setBLabs(params.bLabs);
bCoWFactory.setBDao(params.bDao);
vm.stopBroadcast();
}
}
2 changes: 1 addition & 1 deletion script/DeployBFactory.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract DeployBFactory is Script, Params {

vm.startBroadcast();
BFactory bFactory = new BFactory();
bFactory.setBLabs(params.bLabs);
bFactory.setBDao(params.bDao);
vm.stopBroadcast();
}
}
4 changes: 2 additions & 2 deletions script/Params.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity 0.8.25;

contract Params {
struct BFactoryDeploymentParams {
address bLabs;
address bDao;
}

struct BCoWFactoryDeploymentParams {
address bLabs;
address bDao;
address settlement;
bytes32 appData;
}
Expand Down
28 changes: 14 additions & 14 deletions src/contracts/BFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {IBPool} from 'interfaces/IBPool.sol';
contract BFactory is IBFactory {
/// @dev Mapping indicating whether the address is a BPool.
mapping(address => bool) internal _isBPool;
/// @dev bLabs address.
address internal _bLabs;
/// @dev bDao address.
address internal _bDao;

constructor() {
_bLabs = msg.sender;
_bDao = msg.sender;
}

/// @inheritdoc IBFactory
Expand All @@ -29,25 +29,25 @@ contract BFactory is IBFactory {
}

/// @inheritdoc IBFactory
function setBLabs(address bLabs) external {
if (bLabs == address(0)) {
function setBDao(address bDao) external {
if (bDao == address(0)) {
revert BFactory_AddressZero();
}

if (msg.sender != _bLabs) {
revert BFactory_NotBLabs();
if (msg.sender != _bDao) {
revert BFactory_NotBDao();
}
emit LOG_BLABS(msg.sender, bLabs);
_bLabs = bLabs;
emit LOG_BDAO(msg.sender, bDao);
_bDao = bDao;
}

/// @inheritdoc IBFactory
function collect(IBPool bPool) external {
if (msg.sender != _bLabs) {
revert BFactory_NotBLabs();
if (msg.sender != _bDao) {
revert BFactory_NotBDao();
}
uint256 collected = bPool.balanceOf(address(this));
SafeERC20.safeTransfer(bPool, _bLabs, collected);
SafeERC20.safeTransfer(bPool, _bDao, collected);
}

/// @inheritdoc IBFactory
Expand All @@ -56,8 +56,8 @@ contract BFactory is IBFactory {
}

/// @inheritdoc IBFactory
function getBLabs() external view returns (address) {
return _bLabs;
function getBDao() external view returns (address) {
return _bDao;
}

/**
Expand Down
26 changes: 13 additions & 13 deletions src/interfaces/IBFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ interface IBFactory {
event LOG_NEW_POOL(address indexed caller, address indexed bPool);

/**
* @notice Emitted when setting the BLabs address
* @param caller The caller of the set BLabs function
* @param bLabs The address of the new BLabs
* @notice Emitted when setting the BDao address
* @param caller The caller of the set BDao function
* @param bDao The address of the new BDao
*/
event LOG_BLABS(address indexed caller, address indexed bLabs);
event LOG_BDAO(address indexed caller, address indexed bDao);

/**
* @notice Thrown when setting a variable to address zero
*/
error BFactory_AddressZero();

/**
* @notice Thrown when caller is not BLabs address
* @notice Thrown when caller is not BDao address
*/
error BFactory_NotBLabs();
error BFactory_NotBDao();

/**
* @notice Creates a new BPool, assigning the caller as the pool controller
Expand All @@ -35,13 +35,13 @@ interface IBFactory {
function newBPool() external returns (IBPool bPool);

/**
* @notice Sets the BLabs address in the factory
* @param bLabs The new BLabs address
* @notice Sets the BDao address in the factory
* @param bDao The new BDao address
*/
function setBLabs(address bLabs) external;
function setBDao(address bDao) external;

/**
* @notice Collects the fees of a pool and transfers it to BLabs address
* @notice Collects the fees of a pool and transfers it to BDao address
* @param bPool The address of the pool to collect fees from
*/
function collect(IBPool bPool) external;
Expand All @@ -54,8 +54,8 @@ interface IBFactory {
function isBPool(address bPool) external view returns (bool isBPool);

/**
* @notice Gets the BLabs address
* @return bLabs The address of the BLabs
* @notice Gets the BDao address
* @return bDao The address of the BDao
*/
function getBLabs() external view returns (address bLabs);
function getBDao() external view returns (address bDao);
}
16 changes: 8 additions & 8 deletions test/smock/MockBFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ contract MockBFactory is BFactory, Test {
return _isBPool[_key0];
}

function set__bLabs(address __bLabs) public {
_bLabs = __bLabs;
function set__bDao(address __bDao) public {
_bDao = __bDao;
}

function call__bLabs() public view returns (address) {
return _bLabs;
function call__bDao() public view returns (address) {
return _bDao;
}

constructor() BFactory() {}
Expand All @@ -27,8 +27,8 @@ contract MockBFactory is BFactory, Test {
vm.mockCall(address(this), abi.encodeWithSignature('newBPool()'), abi.encode(bPool));
}

function mock_call_setBLabs(address bLabs) public {
vm.mockCall(address(this), abi.encodeWithSignature('setBLabs(address)', bLabs), abi.encode());
function mock_call_setBDao(address bDao) public {
vm.mockCall(address(this), abi.encodeWithSignature('setBDao(address)', bDao), abi.encode());
}

function mock_call_collect(IBPool bPool) public {
Expand All @@ -39,8 +39,8 @@ contract MockBFactory is BFactory, Test {
vm.mockCall(address(this), abi.encodeWithSignature('isBPool(address)', bPool), abi.encode(_returnParam0));
}

function mock_call_getBLabs(address _returnParam0) public {
vm.mockCall(address(this), abi.encodeWithSignature('getBLabs()'), abi.encode(_returnParam0));
function mock_call_getBDao(address _returnParam0) public {
vm.mockCall(address(this), abi.encodeWithSignature('getBDao()'), abi.encode(_returnParam0));
}

function mock_call__newBPool(IBPool bPool) public {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/BCoWFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ contract BCoWFactoryTest is Test {
);
}

function test_ConstructorWhenCalled(address _blabs, address _newSettler, bytes32 _appData) external {
vm.prank(_blabs);
function test_ConstructorWhenCalled(address _bDao, address _newSettler, bytes32 _appData) external {
vm.prank(_bDao);
MockBCoWFactory _newFactory = new MockBCoWFactory(_newSettler, _appData);
// it should set solution settler
assertEq(_newFactory.SOLUTION_SETTLER(), _newSettler);
// it should set app data
assertEq(_newFactory.APP_DATA(), _appData);
// it should set blabs
assertEq(_newFactory.getBLabs(), _blabs);
// it should set BDao
assertEq(_newFactory.getBDao(), _bDao);
}

function test__newBPoolWhenCalled() external {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/BCoWFactory.tree
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BCoWFactoryTest::constructor
└── when called
├── it should set solution settler
├── it should set app data
└── it should set blabs
└── it should set BDao

BCoWFactoryTest::_newBPool
└── when called
Expand Down
47 changes: 22 additions & 25 deletions test/unit/BFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ contract BFactoryTest is Test {
factory = new MockBFactory();
}

function test_ConstructorWhenCalled(address _blabs) external {
vm.prank(_blabs);
function test_ConstructorWhenCalled(address _bDao) external {
vm.prank(_bDao);
MockBFactory newFactory = new MockBFactory();
// it should set BLabs
assertEq(newFactory.getBLabs(), _blabs);
// it should set BDao
assertEq(newFactory.getBDao(), _bDao);
}

function test_NewBPoolWhenCalled(address _deployer, address _newBPool) external {
Expand Down Expand Up @@ -60,71 +60,68 @@ contract BFactoryTest is Test {
assertEq(_newBPool.code, _expectedCode);
}

function test_SetBLabsRevertWhen_TheSenderIsNotTheCurrentBLabs(address _caller) external {
function test_SetBDaoRevertWhen_TheSenderIsNotTheCurrentBDao(address _caller) external {
vm.assume(_caller != factoryDeployer);

// it should revert
vm.expectRevert(IBFactory.BFactory_NotBLabs.selector);
vm.expectRevert(IBFactory.BFactory_NotBDao.selector);

vm.prank(_caller);
factory.setBLabs(makeAddr('newBLabs'));
factory.setBDao(makeAddr('newBDao'));
}

modifier whenTheSenderIsTheCurrentBLabs() {
modifier whenTheSenderIsTheCurrentBDao() {
vm.startPrank(factoryDeployer);
_;
}

function test_SetBLabsRevertWhen_TheAddressIsZero() external whenTheSenderIsTheCurrentBLabs {
function test_SetBDaoRevertWhen_TheAddressIsZero() external whenTheSenderIsTheCurrentBDao {
// it should revert
vm.expectRevert(IBFactory.BFactory_AddressZero.selector);

factory.setBLabs(address(0));
factory.setBDao(address(0));
}

function test_SetBLabsWhenTheAddressIsNotZero(address _newBLabs) external whenTheSenderIsTheCurrentBLabs {
vm.assume(_newBLabs != address(0));
function test_SetBDaoWhenTheAddressIsNotZero(address _newBDao) external whenTheSenderIsTheCurrentBDao {
vm.assume(_newBDao != address(0));

// it should emit a BLabsSet event
// it should emit a BDaoSet event
vm.expectEmit(address(factory));
emit IBFactory.LOG_BLABS(factoryDeployer, _newBLabs);
emit IBFactory.LOG_BDAO(factoryDeployer, _newBDao);

factory.setBLabs(_newBLabs);
factory.setBDao(_newBDao);

// it should set the new bLabs address
assertEq(factory.getBLabs(), _newBLabs);
// it should set the new bDao address
assertEq(factory.getBDao(), _newBDao);
}

function test_CollectRevertWhen_TheSenderIsNotTheCurrentBLabs(address _caller) external {
function test_CollectRevertWhen_TheSenderIsNotTheCurrentBDao(address _caller) external {
vm.assume(_caller != factoryDeployer);

// it should revert
vm.expectRevert(IBFactory.BFactory_NotBLabs.selector);
vm.expectRevert(IBFactory.BFactory_NotBDao.selector);

vm.prank(_caller);
factory.collect(IBPool(makeAddr('pool')));
}

function test_CollectWhenTheSenderIsTheCurrentBLabs(uint256 _factoryBTBalance)
external
whenTheSenderIsTheCurrentBLabs
{
function test_CollectWhenTheSenderIsTheCurrentBDao(uint256 _factoryBTBalance) external whenTheSenderIsTheCurrentBDao {
address _mockPool = makeAddr('pool');
vm.mockCall(_mockPool, abi.encodeCall(IERC20.balanceOf, address(factory)), abi.encode(_factoryBTBalance));
vm.mockCall(_mockPool, abi.encodeCall(IERC20.transfer, (factoryDeployer, _factoryBTBalance)), abi.encode(true));

// it should get the pool's btoken balance of the factory
vm.expectCall(_mockPool, abi.encodeCall(IERC20.balanceOf, address(factory)));

// it should transfer the btoken balance of the factory to BLabs
// it should transfer the btoken balance of the factory to BDao
vm.expectCall(_mockPool, abi.encodeCall(IERC20.transfer, (factoryDeployer, _factoryBTBalance)));

factory.collect(IBPool(_mockPool));
}

function test_CollectRevertWhen_TheBtokenTransferFails(uint256 _factoryBTBalance)
external
whenTheSenderIsTheCurrentBLabs
whenTheSenderIsTheCurrentBDao
{
address _mockPool = makeAddr('pool');
vm.mockCall(_mockPool, abi.encodeCall(IERC20.balanceOf, address(factory)), abi.encode(_factoryBTBalance));
Expand Down
18 changes: 9 additions & 9 deletions test/unit/BFactory.tree
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BFactoryTest::constructor
└── when called
└── it should set the deployer as BLabs
└── it should set the deployer as BDao

BFactoryTest::newBPool
└── when called
Expand All @@ -14,22 +14,22 @@ BFactoryTest::_newBPool
└── when called
└── it should deploy a new BPool

BFactoryTest::setBLabs
├── when the sender is not the current BLabs
BFactoryTest::setBDao
├── when the sender is not the current BDao
│ └── it should revert
└── when the sender is the current BLabs
└── when the sender is the current BDao
├── when the address is zero
│ └── it should revert
└── when the address is not zero
├── it should set the new BLabs address
└── it should emit a BLabsSet event
├── it should set the new BDao address
└── it should emit a BDaoSet event


BFactoryTest::collect
├── when the sender is not the current BLabs
├── when the sender is not the current BDao
│ └── it should revert
└── when the sender is the current BLabs
└── when the sender is the current BDao
├── it should get the pool's btoken balance of the factory
├── it should transfer the btoken balance of the factory to BLabs
├── it should transfer the btoken balance of the factory to BDao
└── when the btoken transfer fails
└── it should revert

0 comments on commit eadc1e0

Please sign in to comment.