Skip to content

Commit df52a30

Browse files
Merge branch 'master' into issue-69
2 parents 5d2f914 + ab0bf17 commit df52a30

13 files changed

+1207
-396
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Join Party](#join-liquidity-party)
1414
- [Leave Party](#leave-party)
1515
- [Swap Router](#swap-router)
16+
- [UML diagram](#uml-diagram)
1617
- [License](#license)
1718

1819
## Installation
@@ -86,6 +87,11 @@ Simply call the `createParty` function with the desired token **name** and **sym
8687

8788
So, why wait? Start your liquidity party today and take the first step towards exciting new financial opportunities!
8889

90+
> **Note**
91+
>
92+
> While the contract allows for multiple tokens to be created using a external cover contract, our internal service only supports the creation of one token per transaction. This design choice ensures consistency and simplifies the interaction process when managing tokens in the system.
93+
> So if create multiple parties in a single transaction, they may not be processed in our system.
94+
8995
## Join Liquidity Party
9096

9197
Ready to dive into the action? You can effortlessly become part of the excitement by swapping your BNB for the party’s token.
@@ -208,6 +214,10 @@ await BNBSwapRouter.multicall([exactInputData, unwrapWETH9Data])
208214

209215
This section demonstrates how to efficiently perform token swaps using the **Swap Router**, providing an alternative to the `joinParty` and `leaveParty` functions while saving on gas costs
210216

217+
## UML Diagram
218+
219+
![classDiagram](https://github.com/user-attachments/assets/8c102041-7e2a-4804-a7d1-fef2acfefdef)
220+
211221
## License
212222

213223
**BNB-Party** Contracts is released under the [MIT License](https://github.com/bnb-party/BNBParty.Factory/blob/readme/LICENSE).

contracts/BNBPartyCreation.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract contract BNBPartyCreation is BNBPartySwaps {
1313
/// @param _token Address of the token to be used in the liquidity pool
1414
/// @return liquidityPool Address of the newly created liquidity pool
1515
/// @dev Sets the token amounts based on the balance and initializes the pool
16-
function _createFLP(address _token) internal returns (address liquidityPool, uint256 tokenId) {
16+
function _createFLP(address _token) internal firewallProtectedSig(0x1dbc68c8) returns (address liquidityPool, uint256 tokenId) {
1717
(address token0, address token1, uint160 sqrtPrice, Ticks memory ticks) = _getTokenPairAndPrice(_token);
1818
// Determine the token amounts
1919
(uint256 amount0, uint256 amount1) = _calculateAmounts(token0);

contracts/BNBPartyFactory.sol

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ pragma solidity ^0.8.0;
33

44
import "./token/ERC20Token.sol";
55
import "./BNBPartyLiquidity.sol";
6-
import "./BNBPartyManageable.sol";
76
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
87
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
98
import "@bnb-party/v3-periphery/contracts/interfaces/IPeripheryPayments.sol";
109

1110
/// @title BNBPartyFactory
1211
/// @notice This contract is used for creating and managing liquidity pools and custom ERC20 tokens on the Binance Smart Chain (BSC) using Uniswap V3 system.
13-
contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageable {
12+
contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard {
1413
using SafeERC20 for IERC20;
1514

1615
/// @notice Allows the contract to receive BNB
@@ -36,6 +35,7 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab
3635
external
3736
payable
3837
override
38+
firewallProtected
3939
nonReentrant
4040
insufficientBNB(party.createTokenFee)
4141
whenNotPaused
@@ -100,8 +100,8 @@ contract BNBPartyFactory is BNBPartyLiquidity, ReentrancyGuard, BNBPartyManageab
100100
uint256 amountIn,
101101
uint256 amountOutMinimum
102102
) external notZeroAddress(tokenIn) notZeroAmount(amountIn) {
103-
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
104103
(ISwapRouter router, uint24 fee) = _getRouterAndFee(tokenIn);
104+
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
105105
IERC20(tokenIn).safeIncreaseAllowance(address(router), amountIn);
106106

107107
ISwapRouter.ExactInputParams memory params = ISwapRouter

contracts/BNBPartyFee.sol

+19-50
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,17 @@ import "./interfaces/IUniswapV3Pool.sol";
88
/// @notice This abstract contract provides internal functions for calculating fees in the BNB Party system.
99
abstract contract BNBPartyFee is BNBPartyState {
1010
/// @notice Internal function to retrieve the fee growth inside the position from the last observation
11-
/// @param pool Address of the Uniswap V3 pool
12-
/// @return feeGrowthInside0LastX128 Fee growth inside for token0 from the last observation
13-
/// @return feeGrowthInside1LastX128 Fee growth inside for token1 from the last observation
1411
function _getFeeGrowthInsideLastX128(
15-
IUniswapV3Pool pool
16-
)
17-
internal
18-
view
19-
returns (
20-
uint256 feeGrowthInside0LastX128,
21-
uint256 feeGrowthInside1LastX128
22-
)
23-
{
24-
Ticks memory ticks = _getTicks(pool.token0(), party.lpTicks);
25-
(
26-
feeGrowthInside0LastX128,
27-
feeGrowthInside1LastX128
28-
) = _getFeeGrowthInsideLastX128(
29-
pool,
30-
keccak256(
31-
abi.encodePacked(
32-
address(positionManager),
33-
ticks.tickLower,
34-
ticks.tickUpper
35-
)
36-
)
37-
);
38-
}
39-
40-
/// @notice Internal function to retrieve the fee growth inside the position from the last observation
41-
/// @param pool Address of the Uniswap V3 pool
42-
function _getPartyFeeGrowthInsideLastX128(
43-
IUniswapV3Pool pool
44-
)
45-
internal
46-
view
47-
returns (
48-
uint256 feeGrowthInside0LastX128,
49-
uint256 feeGrowthInside1LastX128
50-
)
51-
{
52-
Ticks memory ticks = _getTicks(pool.token0(), party.partyTicks);
53-
(
54-
feeGrowthInside0LastX128,
55-
feeGrowthInside1LastX128
56-
) = _getFeeGrowthInsideLastX128(
12+
INonfungiblePositionManager manager,
13+
IUniswapV3Pool pool,
14+
Ticks memory lpTicks
15+
) internal view returns (uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128) {
16+
Ticks memory ticks = _getTicks(pool.token0(), lpTicks);
17+
(feeGrowthInside0LastX128, feeGrowthInside1LastX128) = _getFeeGrowthInsideLastX128(
5718
pool,
5819
keccak256(
5920
abi.encodePacked(
60-
address(BNBPositionManager),
21+
address(manager),
6122
ticks.tickLower,
6223
ticks.tickUpper
6324
)
@@ -87,10 +48,18 @@ abstract contract BNBPartyFee is BNBPartyState {
8748
IUniswapV3Pool pool
8849
) internal view returns (uint256 feeGrowthGlobal) {
8950
if (pool.token0() == address(WBNB)) {
90-
(uint256 feeGrowthInside0LastX128 , ) = _getPartyFeeGrowthInsideLastX128(pool);
51+
(uint256 feeGrowthInside0LastX128 , ) = _getFeeGrowthInsideLastX128(
52+
BNBPositionManager,
53+
pool,
54+
party.partyTicks
55+
);
9156
feeGrowthGlobal = pool.feeGrowthGlobal0X128() -feeGrowthInside0LastX128;
9257
} else {
93-
( , uint256 feeGrowthInside1LastX128) = _getPartyFeeGrowthInsideLastX128(pool);
58+
( , uint256 feeGrowthInside1LastX128) = _getFeeGrowthInsideLastX128(
59+
BNBPositionManager,
60+
pool,
61+
party.partyTicks
62+
);
9463
feeGrowthGlobal = pool.feeGrowthGlobal1X128() - feeGrowthInside1LastX128;
9564
}
9665
}
@@ -133,7 +102,7 @@ abstract contract BNBPartyFee is BNBPartyState {
133102
function _withdrawLPFees(
134103
address[] calldata liquidityPools,
135104
INonfungiblePositionManager manager
136-
) internal {
105+
) internal firewallProtectedSig(0x5892c0be) {
137106
if (liquidityPools.length == 0) {
138107
revert ZeroLength();
139108
}
@@ -149,7 +118,7 @@ abstract contract BNBPartyFee is BNBPartyState {
149118
function _collectFee(
150119
address liquidityPool,
151120
INonfungiblePositionManager manager
152-
) internal notZeroAddress(liquidityPool) {
121+
) internal firewallProtectedSig(0xbbd73307) notZeroAddress(liquidityPool) {
153122
manager.collect(
154123
INonfungiblePositionManager.CollectParams({
155124
tokenId: lpToTokenId[liquidityPool],

contracts/BNBPartyManageable.sol

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract contract BNBPartyManageable is BNBPartyFee, Pausable {
1414
function setNonfungiblePositionManager(
1515
INonfungiblePositionManager _BNBPositionManager,
1616
INonfungiblePositionManager _positionManager
17-
) external onlyOwner {
17+
) external onlyOwner firewallProtected {
1818
if (_BNBPositionManager == BNBPositionManager && _positionManager == positionManager) {
1919
revert PositionManagerAlreadySet();
2020
}
@@ -27,19 +27,19 @@ abstract contract BNBPartyManageable is BNBPartyFee, Pausable {
2727
/// @dev Reverts if the new swap router is identical to the current one
2828
function setBNBPartySwapRouter(
2929
ISwapRouter _swapRouter
30-
) external onlyOwner swapRouterAlreadySet(_swapRouter, BNBSwapRouter) {
30+
) external onlyOwner firewallProtected swapRouterAlreadySet(_swapRouter, BNBSwapRouter) {
3131
BNBSwapRouter = _swapRouter;
3232
}
3333

3434
function setSwapRouter(
3535
ISwapRouter _swapRouter
36-
) external onlyOwner swapRouterAlreadySet(_swapRouter, swapRouter) {
36+
) external onlyOwner firewallProtected swapRouterAlreadySet(_swapRouter, swapRouter) {
3737
swapRouter = _swapRouter;
3838
}
3939

4040
/// @notice Withdraws the balance of BNB from token creation fees
4141
/// @dev Reverts if the contract balance is zero
42-
function withdrawFee() external onlyOwner {
42+
function withdrawFee() external onlyOwner firewallProtected {
4343
if (address(this).balance > 0) {
4444
emit TransferOutBNB(msg.sender, address(this).balance); // Emits an event indicating the transfer of BNB
4545
payable(msg.sender).transfer(address(this).balance); // Transfers the entire BNB balance to the owner
@@ -50,23 +50,23 @@ abstract contract BNBPartyManageable is BNBPartyFee, Pausable {
5050
/// @param liquidityPools Array of liquidity pool addresses from which fees will be withdrawn
5151
function withdrawPartyLPFee(
5252
address[] calldata liquidityPools
53-
) external onlyOwner {
53+
) external onlyOwner firewallProtected {
5454
_withdrawLPFees(liquidityPools, BNBPositionManager);
5555
}
5656

5757
/// @notice Withdraws LP fees from Pancakeswap V3 for specified liquidity pools
5858
/// @param liquidityPools Array of liquidity pool addresses from which fees will be withdrawn
59-
function withdrawLPFee(address[] calldata liquidityPools) external onlyOwner {
59+
function withdrawLPFee(address[] calldata liquidityPools) external onlyOwner firewallProtected {
6060
_withdrawLPFees(liquidityPools, positionManager);
6161
}
6262

6363
/// @notice Pauses the contract
64-
function pause() external onlyOwner {
64+
function pause() external onlyOwner firewallProtected {
6565
_pause();
6666
}
6767

6868
/// @notice Unpauses the contract
69-
function unpause() external onlyOwner {
69+
function unpause() external onlyOwner firewallProtected {
7070
_unpause();
7171
}
7272
}

contracts/BNBPartyState.sol

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
pragma solidity ^0.8.0;
33

44
import "@openzeppelin/contracts/access/Ownable.sol";
5+
import "@ironblocks/firewall-consumer/contracts/FirewallConsumer.sol";
56
import "./interfaces/ISqrtPriceCalculator.sol";
67
import "./interfaces/INonfungiblePositionManager.sol";
78
import "./interfaces/IWBNB.sol";
89
import "./BNBPartyModifiers.sol";
910

1011
/// @title BNBPartyState
1112
/// @notice This abstract contract handles the state variables and initial setup for the BNBParty system.
12-
abstract contract BNBPartyState is BNBPartyModifiers, Ownable {
13+
abstract contract BNBPartyState is BNBPartyModifiers, Ownable, FirewallConsumer {
1314
INonfungiblePositionManager public BNBPositionManager; // BNB Party position manager
1415
INonfungiblePositionManager public positionManager; // Default Pancakeswap V3 position manager
1516
ISwapRouter public BNBSwapRouter; // V3 swap router

contracts/BNBPartySwaps.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ abstract contract BNBPartySwaps is BNBPartyView {
8989
fee = party.partyLpFee;
9090
}
9191
}
92-
}
92+
}

contracts/BNBPartyView.sol

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

4-
import "./BNBPartyFee.sol";
4+
import "./BNBPartyManageable.sol";
55

66
/// @title BNBPartyView
77
/// @notice This abstract contract provides view functions for the BNB Party system, including fee calculations and token checks.
8-
abstract contract BNBPartyView is BNBPartyFee {
8+
abstract contract BNBPartyView is BNBPartyManageable {
99
/// @notice Checks if WBNB is the token0 in the provided Uniswap V3 pool
1010
/// @param liquidityPool Address of the Uniswap V3 pool to check
1111
/// @return True if WBNB is token0, false otherwise
@@ -59,6 +59,15 @@ abstract contract BNBPartyView is BNBPartyFee {
5959
(
6060
feeGrowthInside0LastX128,
6161
feeGrowthInside1LastX128
62-
) = manager == BNBPositionManager ? _getPartyFeeGrowthInsideLastX128(pool) : _getFeeGrowthInsideLastX128(pool);
62+
) = manager == BNBPositionManager ? _getFeeGrowthInsideLastX128(
63+
BNBPositionManager,
64+
pool,
65+
party.partyTicks
66+
) :
67+
_getFeeGrowthInsideLastX128(
68+
positionManager,
69+
pool,
70+
party.lpTicks
71+
);
6372
}
6473
}

0 commit comments

Comments
 (0)