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

use internal modifiers for constructor #78

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions contracts/BNBPartyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab
payable
override
nonReentrant
insufficientBNB
insufficientBNB(party.createTokenFee)
whenNotPaused
notZeroAddress(address(BNBPositionManager))
returns (IERC20 newToken)
Expand All @@ -58,7 +58,8 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab

/// @notice Handles token swaps for the liquidity pool
/// @param recipient The address of the entity making the exchange
function handleSwap(address recipient) external override onlyParty notZeroAddress(recipient) whenNotPaused {
function handleSwap(address recipient) external override notZeroAddress(recipient) whenNotPaused {
if (!isParty[msg.sender]) revert LPNotAtParty(); // Reverts if the LP is not part of a party
IUniswapV3Pool pool = IUniswapV3Pool(msg.sender);

uint256 WBNBBalance = WBNB.balanceOf(msg.sender);
Expand Down
4 changes: 2 additions & 2 deletions contracts/BNBPartyFee.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./BNBPartyModifiers.sol";
import "./BNBPartyState.sol";
import "./interfaces/IUniswapV3Pool.sol";

/// @title BNBPartyFee
/// @notice This abstract contract provides internal functions for calculating fees in the BNB Party system.
abstract contract BNBPartyFee is BNBPartyModifiers {
abstract contract BNBPartyFee is BNBPartyState {
/// @notice Internal function to retrieve the fee growth inside the position from the last observation
/// @param pool Address of the Uniswap V3 pool
/// @return feeGrowthInside0LastX128 Fee growth inside for token0 from the last observation
Expand Down
4 changes: 2 additions & 2 deletions contracts/BNBPartyManageable.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./BNBPartyModifiers.sol";
import "./BNBPartyFee.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";

/// @title BNBPartyManageable
/// @notice This abstract contract provides management functions for setting position managers, swap routers, and withdrawing fees in the BNBParty system.
abstract contract BNBPartyManageable is BNBPartyModifiers, Pausable {
abstract contract BNBPartyManageable is BNBPartyFee, Pausable {
/// @notice Sets the non-fungible position managers for BNB Party and Pancakeswap V3
/// @param _BNBPositionManager Address of the new BNB Party non-fungible position manager
/// @param _positionManager Address of the new Pancakeswap V3 non-fungible position manager
Expand Down
16 changes: 5 additions & 11 deletions contracts/BNBPartyModifiers.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./BNBPartyState.sol";
import "./interfaces/IBNBPartyFactory.sol";
import "@bnb-party/v3-periphery/contracts/interfaces/ISwapRouter.sol";

/// @title BNBPartyModifiers
/// @notice This abstract contract provides various modifiers used in the BNBParty system to enforce conditions on function calls.
abstract contract BNBPartyModifiers is BNBPartyState {
/// @notice Restricts function access to liquidity pools that are part of a party
/// @dev Reverts if the caller is not a registered party liquidity pool
modifier onlyParty() {
if (!isParty[msg.sender]) revert LPNotAtParty();
_;
}

abstract contract BNBPartyModifiers is IBNBPartyFactory{
/// @notice Ensures the provided address is not a zero address
/// @param _address Address to be checked
/// @dev Reverts if the address is zero
Expand All @@ -23,8 +17,8 @@ abstract contract BNBPartyModifiers is BNBPartyState {

/// @notice Ensures the amount of BNB sent is sufficient to cover the token creation fee
/// @dev Reverts if the sent BNB is less than the required fee
modifier insufficientBNB() {
if (msg.value < party.createTokenFee) revert InsufficientBNB();
modifier insufficientBNB(uint256 fee) {
if (msg.value < fee) revert InsufficientBNB();
_;
}

Expand Down
50 changes: 27 additions & 23 deletions contracts/BNBPartyState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@bnb-party/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "./interfaces/ISqrtPriceCalculator.sol";
import "./interfaces/INonfungiblePositionManager.sol";
import "./interfaces/IBNBPartyFactory.sol";
import "./interfaces/IWBNB.sol";
import "./BNBPartyModifiers.sol";

/// @title BNBPartyState
/// @notice This abstract contract handles the state variables and initial setup for the BNBParty system.
abstract contract BNBPartyState is IBNBPartyFactory, Ownable {
abstract contract BNBPartyState is BNBPartyModifiers, Ownable {
INonfungiblePositionManager public BNBPositionManager; // BNB Party position manager
INonfungiblePositionManager public positionManager; // Default Pancakeswap V3 position manager
ISwapRouter public BNBSwapRouter; // V3 swap router
Expand All @@ -19,7 +18,7 @@ abstract contract BNBPartyState is IBNBPartyFactory, Ownable {
mapping(address => uint256) public lpToTokenId; // Mapping from LiquidityPool to its NFT tokenId
mapping(address => address) public lpToCreator; // Mapping from LiquidityPool to its creator
mapping(address => bool) public isTokenOnPartyLP; // Mapping to track if a token is part of a party
uint256 constant FEE_GROWTH_GLOBAL_SCALE = 2**128;
uint256 constant FEE_GROWTH_GLOBAL_SCALE = 2 ** 128;

Party public party; // store party parameters

Expand All @@ -29,27 +28,32 @@ abstract contract BNBPartyState is IBNBPartyFactory, Ownable {
/// @notice Constructor to initialize the BNBPartyState contract
/// @param _party Struct containing party parameters
/// @param _WBNB Address of the Wrapped BNB token contract
constructor(Party memory _party, IWBNB _WBNB, ISqrtPriceCalculator _sqrtPriceCalculator) Ownable(_msgSender()) {
if (address(_WBNB) == address(0)) {
revert ZeroAddress(); // Reverts if the WBNB address is zero
}
if(address(_sqrtPriceCalculator) == address(0)) {
revert ZeroAddress(); // Reverts if the sqrt price calculator address is zero
}
if (_party.partyTarget == 0) {
revert ZeroAmount(); // Reverts if the party target is zero
}
if (_party.initialTokenAmount == 0) {
revert ZeroAmount(); // Reverts if the initial token amount is zero
}
if (_party.partyTarget <= (_party.bonusPartyCreator + _party.bonusTargetReach + _party.targetReachFee)) {
revert BonusGreaterThanTarget(); // Reverts if the party target is less than or equal to the sum of bonuses and fees
}
if (_party.sqrtPriceX96 == 0) {
revert ZeroAmount(); // Reverts if the sqrt price is zero
}
constructor(
Party memory _party,
IWBNB _WBNB,
ISqrtPriceCalculator _sqrtPriceCalculator
) Ownable(_msgSender()) {
_constructorValidation(_party, _WBNB, _sqrtPriceCalculator);
party = _party;
WBNB = _WBNB;
sqrtPriceCalculator = _sqrtPriceCalculator;
}

function _constructorValidation(
Party memory _party,
IWBNB _WBNB,
ISqrtPriceCalculator _sqrtPriceCalculator
)
private
pure
notZeroAddress(address(_WBNB))
notZeroAddress(address(_sqrtPriceCalculator))
notZeroAmount(_party.partyTarget)
notZeroAmount(_party.initialTokenAmount)
notZeroAmount(_party.sqrtPriceX96)
{
if (_party.partyTarget <= (_party.bonusPartyCreator + _party.bonusTargetReach + _party.targetReachFee)) {
revert BonusGreaterThanTarget(); // Reverts if the party target is less than or equal to the sum of bonuses and fees
}
}
}