Skip to content

Commit 7083866

Browse files
Merge branch 'master' into issue-68
2 parents 6691aa4 + 95c7bd5 commit 7083866

7 files changed

+80
-82
lines changed

contracts/BNBPartyFactory.sol

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab
3737
payable
3838
override
3939
nonReentrant
40-
insufficientBNB
40+
insufficientBNB(party.createTokenFee)
4141
whenNotPaused
4242
notZeroAddress(address(BNBPositionManager))
4343
returns (IERC20 newToken)
@@ -49,7 +49,6 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab
4949
lpToCreator[liquidityPool] = msg.sender; // Set the creator of the liquidity pool
5050
lpToTokenId[liquidityPool] = tokenId; // Set the token ID of the liquidity pool
5151
isParty[liquidityPool] = true; // Mark the liquidity pool as a party pool
52-
isTokenOnPartyLP[address(newToken)] = true; // Mark the token as part of the party LP
5352
if (msg.value > party.createTokenFee) {
5453
_executeSwap(address(newToken));
5554
}
@@ -58,7 +57,8 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab
5857

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

6464
uint256 WBNBBalance = WBNB.balanceOf(msg.sender);

contracts/BNBPartyFee.sol

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.0;
33

4-
import "./BNBPartyModifiers.sol";
4+
import "./BNBPartyState.sol";
55
import "./interfaces/IUniswapV3Pool.sol";
66

77
/// @title BNBPartyFee
88
/// @notice This abstract contract provides internal functions for calculating fees in the BNB Party system.
9-
abstract contract BNBPartyFee is BNBPartyModifiers {
9+
abstract contract BNBPartyFee is BNBPartyState {
1010
/// @notice Internal function to retrieve the fee growth inside the position from the last observation
1111
/// @param pool Address of the Uniswap V3 pool
1212
/// @return feeGrowthInside0LastX128 Fee growth inside for token0 from the last observation
@@ -126,4 +126,38 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
126126
adjustedTicks = ticks;
127127
}
128128
}
129+
130+
/// @notice Internal function to withdraw LP fees from specified liquidity pools
131+
/// @param liquidityPools Array of liquidity pool addresses from which fees will be withdrawn
132+
/// @param manager The non-fungible position manager used to collect fees
133+
/// @dev Reverts if the liquidity pools array is empty
134+
function _withdrawLPFees(
135+
address[] calldata liquidityPools,
136+
INonfungiblePositionManager manager
137+
) internal {
138+
if (liquidityPools.length == 0) {
139+
revert ZeroLength();
140+
}
141+
for (uint256 i = 0; i < liquidityPools.length; ++i) {
142+
_collectFee(liquidityPools[i], manager); // Collects fees from each specified liquidity pool
143+
}
144+
}
145+
146+
/// @notice Internal function to collect LP fees from a specific liquidity pool
147+
/// @param liquidityPool Address of the liquidity pool from which fees will be collected
148+
/// @param manager The non-fungible position manager used to collect fees
149+
/// @dev Reverts if the provided liquidity pool address is zero
150+
function _collectFee(
151+
address liquidityPool,
152+
INonfungiblePositionManager manager
153+
) internal notZeroAddress(liquidityPool) {
154+
manager.collect(
155+
INonfungiblePositionManager.CollectParams({
156+
tokenId: lpToTokenId[liquidityPool],
157+
recipient: msg.sender,
158+
amount0Max: type(uint128).max,
159+
amount1Max: type(uint128).max
160+
})
161+
); // Collects fees from the specified liquidity pool
162+
}
129163
}

contracts/BNBPartyLiquidity.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract contract BNBPartyLiquidity is BNBPartyLiquidityHelper {
2525

2626
if (token0 == address(WBNB)) {
2727
amount0 -= unwrapAmount; // Deduct unwrap amount from token0 if it is WBNB
28-
isTokenOnPartyLP[token1] = false;
28+
isTokenTargetReached[token1] = true;
2929
newSqrtPriceX96 = sqrtPriceCalculator.getNextSqrtPriceFromAmount0RoundingUp(
3030
sqrtPriceX96,
3131
liquidity,
@@ -34,7 +34,7 @@ abstract contract BNBPartyLiquidity is BNBPartyLiquidityHelper {
3434
);
3535
} else {
3636
amount1 -= unwrapAmount; // Deduct unwrap amount from token1 if it is WBNB
37-
isTokenOnPartyLP[token0] = false;
37+
isTokenTargetReached[token0] = true;
3838
newSqrtPriceX96 = sqrtPriceCalculator.getNextSqrtPriceFromAmount1RoundingDown(
3939
sqrtPriceX96,
4040
liquidity,

contracts/BNBPartyManageable.sol

+2-36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.0;
33

4-
import "./BNBPartyModifiers.sol";
4+
import "./BNBPartyFee.sol";
55
import "@openzeppelin/contracts/utils/Pausable.sol";
66

77
/// @title BNBPartyManageable
88
/// @notice This abstract contract provides management functions for setting position managers, swap routers, and withdrawing fees in the BNBParty system.
9-
abstract contract BNBPartyManageable is BNBPartyModifiers, Pausable {
9+
abstract contract BNBPartyManageable is BNBPartyFee, Pausable {
1010
/// @notice Sets the non-fungible position managers for BNB Party and Pancakeswap V3
1111
/// @param _BNBPositionManager Address of the new BNB Party non-fungible position manager
1212
/// @param _positionManager Address of the new Pancakeswap V3 non-fungible position manager
@@ -69,38 +69,4 @@ abstract contract BNBPartyManageable is BNBPartyModifiers, Pausable {
6969
function unpause() external onlyOwner {
7070
_unpause();
7171
}
72-
73-
/// @notice Internal function to withdraw LP fees from specified liquidity pools
74-
/// @param liquidityPools Array of liquidity pool addresses from which fees will be withdrawn
75-
/// @param manager The non-fungible position manager used to collect fees
76-
/// @dev Reverts if the liquidity pools array is empty
77-
function _withdrawLPFees(
78-
address[] calldata liquidityPools,
79-
INonfungiblePositionManager manager
80-
) internal {
81-
if (liquidityPools.length == 0) {
82-
revert ZeroLength();
83-
}
84-
for (uint256 i = 0; i < liquidityPools.length; ++i) {
85-
_collectFee(liquidityPools[i], manager); // Collects fees from each specified liquidity pool
86-
}
87-
}
88-
89-
/// @notice Internal function to collect LP fees from a specific liquidity pool
90-
/// @param liquidityPool Address of the liquidity pool from which fees will be collected
91-
/// @param manager The non-fungible position manager used to collect fees
92-
/// @dev Reverts if the provided liquidity pool address is zero
93-
function _collectFee(
94-
address liquidityPool,
95-
INonfungiblePositionManager manager
96-
) internal notZeroAddress(liquidityPool) {
97-
manager.collect(
98-
INonfungiblePositionManager.CollectParams({
99-
tokenId: lpToTokenId[liquidityPool],
100-
recipient: msg.sender,
101-
amount0Max: type(uint128).max,
102-
amount1Max: type(uint128).max
103-
})
104-
); // Collects fees from the specified liquidity pool
105-
}
10672
}

contracts/BNBPartyModifiers.sol

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.0;
33

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

67
/// @title BNBPartyModifiers
78
/// @notice This abstract contract provides various modifiers used in the BNBParty system to enforce conditions on function calls.
8-
abstract contract BNBPartyModifiers is BNBPartyState {
9-
/// @notice Restricts function access to liquidity pools that are part of a party
10-
/// @dev Reverts if the caller is not a registered party liquidity pool
11-
modifier onlyParty() {
12-
if (!isParty[msg.sender]) revert LPNotAtParty();
13-
_;
14-
}
15-
9+
abstract contract BNBPartyModifiers is IBNBPartyFactory{
1610
/// @notice Ensures the provided address is not a zero address
1711
/// @param _address Address to be checked
1812
/// @dev Reverts if the address is zero
@@ -23,8 +17,8 @@ abstract contract BNBPartyModifiers is BNBPartyState {
2317

2418
/// @notice Ensures the amount of BNB sent is sufficient to cover the token creation fee
2519
/// @dev Reverts if the sent BNB is less than the required fee
26-
modifier insufficientBNB() {
27-
if (msg.value < party.createTokenFee) revert InsufficientBNB();
20+
modifier insufficientBNB(uint256 fee) {
21+
if (msg.value < fee) revert InsufficientBNB();
2822
_;
2923
}
3024

contracts/BNBPartyState.sol

+28-24
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22
pragma solidity ^0.8.0;
33

44
import "@openzeppelin/contracts/access/Ownable.sol";
5-
import "@bnb-party/v3-periphery/contracts/interfaces/ISwapRouter.sol";
65
import "./interfaces/ISqrtPriceCalculator.sol";
76
import "./interfaces/INonfungiblePositionManager.sol";
8-
import "./interfaces/IBNBPartyFactory.sol";
97
import "./interfaces/IWBNB.sol";
8+
import "./BNBPartyModifiers.sol";
109

1110
/// @title BNBPartyState
1211
/// @notice This abstract contract handles the state variables and initial setup for the BNBParty system.
13-
abstract contract BNBPartyState is IBNBPartyFactory, Ownable {
12+
abstract contract BNBPartyState is BNBPartyModifiers, Ownable {
1413
INonfungiblePositionManager public BNBPositionManager; // BNB Party position manager
1514
INonfungiblePositionManager public positionManager; // Default Pancakeswap V3 position manager
1615
ISwapRouter public BNBSwapRouter; // V3 swap router
1716
ISwapRouter public swapRouter; // Pancakeswap V3 router
1817
mapping(address => bool) public isParty; // Mapping to track if a LiquidityPool is a party
1918
mapping(address => uint256) public lpToTokenId; // Mapping from LiquidityPool to its NFT tokenId
2019
mapping(address => address) public lpToCreator; // Mapping from LiquidityPool to its creator
21-
mapping(address => bool) public isTokenOnPartyLP; // Mapping to track if a token is part of a party
22-
uint256 constant FEE_GROWTH_GLOBAL_SCALE = 2**128;
20+
mapping(address => bool) public isTokenTargetReached; // Mapping to track if a token has reached its target
21+
uint256 constant public FEE_GROWTH_GLOBAL_SCALE = 2**128;
2322

2423
Party public party; // store party parameters
2524

@@ -29,27 +28,32 @@ abstract contract BNBPartyState is IBNBPartyFactory, Ownable {
2928
/// @notice Constructor to initialize the BNBPartyState contract
3029
/// @param _party Struct containing party parameters
3130
/// @param _WBNB Address of the Wrapped BNB token contract
32-
constructor(Party memory _party, IWBNB _WBNB, ISqrtPriceCalculator _sqrtPriceCalculator) Ownable(_msgSender()) {
33-
if (address(_WBNB) == address(0)) {
34-
revert ZeroAddress(); // Reverts if the WBNB address is zero
35-
}
36-
if(address(_sqrtPriceCalculator) == address(0)) {
37-
revert ZeroAddress(); // Reverts if the sqrt price calculator address is zero
38-
}
39-
if (_party.partyTarget == 0) {
40-
revert ZeroAmount(); // Reverts if the party target is zero
41-
}
42-
if (_party.initialTokenAmount == 0) {
43-
revert ZeroAmount(); // Reverts if the initial token amount is zero
44-
}
45-
if (_party.partyTarget <= (_party.bonusPartyCreator + _party.bonusTargetReach + _party.targetReachFee)) {
46-
revert BonusGreaterThanTarget(); // Reverts if the party target is less than or equal to the sum of bonuses and fees
47-
}
48-
if (_party.sqrtPriceX96 == 0) {
49-
revert ZeroAmount(); // Reverts if the sqrt price is zero
50-
}
31+
constructor(
32+
Party memory _party,
33+
IWBNB _WBNB,
34+
ISqrtPriceCalculator _sqrtPriceCalculator
35+
) Ownable(_msgSender()) {
36+
_constructorValidation(_party, _WBNB, _sqrtPriceCalculator);
5137
party = _party;
5238
WBNB = _WBNB;
5339
sqrtPriceCalculator = _sqrtPriceCalculator;
5440
}
41+
42+
function _constructorValidation(
43+
Party memory _party,
44+
IWBNB _WBNB,
45+
ISqrtPriceCalculator _sqrtPriceCalculator
46+
)
47+
private
48+
pure
49+
notZeroAddress(address(_WBNB))
50+
notZeroAddress(address(_sqrtPriceCalculator))
51+
notZeroAmount(_party.partyTarget)
52+
notZeroAmount(_party.initialTokenAmount)
53+
notZeroAmount(_party.sqrtPriceX96)
54+
{
55+
if (_party.partyTarget <= (_party.bonusPartyCreator + _party.bonusTargetReach + _party.targetReachFee)) {
56+
revert BonusGreaterThanTarget(); // Reverts if the party target is less than or equal to the sum of bonuses and fees
57+
}
58+
}
5559
}

contracts/BNBPartySwaps.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ abstract contract BNBPartySwaps is BNBPartyView {
8181
/// @return router The address of the swap router
8282
/// @return fee The fee amount for the swap
8383
function _getRouterAndFee(address token) internal view returns (ISwapRouter router, uint24 fee) {
84-
if (isTokenOnPartyLP[token]) {
85-
router = BNBSwapRouter;
86-
fee = party.partyLpFee;
87-
} else {
84+
if (isTokenTargetReached[token]) {
8885
router = swapRouter;
8986
fee = party.lpFee;
87+
} else {
88+
router = BNBSwapRouter;
89+
fee = party.partyLpFee;
9090
}
9191
}
9292
}

0 commit comments

Comments
 (0)