Skip to content

Commit c1ef89e

Browse files
Merge 508a0af into d4f3833
2 parents d4f3833 + 508a0af commit c1ef89e

10 files changed

+66
-30
lines changed

arguments.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = [
99
bonusTargetReach: "50000000000000000n",
1010
bonusPartyCreator: "100000000000000000",
1111
targetReachFee: "850000000000000000",
12-
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
13-
lpTicks: { tickLower: "-214200", tickUpper: "201400" }
12+
partyTicks: { tickLower: "-195600", tickUpper: "214200" },
13+
lpTicks: { tickLower: "-201400", tickUpper: "214200" }
1414
},
1515
"0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd",
1616
"0xFe3355feD445709971277BC3842E0f581Bf4146b"

contracts/BNBPartyCreation.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract contract BNBPartyCreation is BNBPartySwaps {
1414
/// @return liquidityPool Address of the newly created liquidity pool
1515
/// @dev Sets the token amounts based on the balance and initializes the pool
1616
function _createFLP(address _token) internal returns (address liquidityPool) {
17-
(address token0, address token1, uint160 sqrtPrice) = _getTokenPairAndPrice(_token);
17+
(address token0, address token1, uint160 sqrtPrice, Ticks memory ticks) = _getTokenPairAndPrice(_token);
1818
// Determine the token amounts
1919
(uint256 amount0, uint256 amount1) = _calculateAmounts(token0);
2020
IERC20(_token).safeIncreaseAllowance(address(BNBPositionManager), party.initialTokenAmount);
@@ -26,7 +26,7 @@ abstract contract BNBPartyCreation is BNBPartySwaps {
2626
amount1,
2727
sqrtPrice,
2828
party.partyLpFee,
29-
party.partyTicks
29+
ticks
3030
);
3131
isParty[liquidityPool] = true; // Mark the liquidity pool as a party pool
3232
isTokenOnPartyLP[_token] = true; // Mark the token as part of the party LP

contracts/BNBPartyFee.sol

+43-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
2121
uint256 feeGrowthInside1LastX128
2222
)
2323
{
24+
Ticks memory ticks = _getTicks(pool.token0(), party.lpTicks);
2425
(
2526
feeGrowthInside0LastX128,
2627
feeGrowthInside1LastX128
@@ -29,8 +30,8 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
2930
keccak256(
3031
abi.encodePacked(
3132
address(positionManager),
32-
party.lpTicks.tickLower,
33-
party.lpTicks.tickUpper
33+
ticks.tickLower,
34+
ticks.tickUpper
3435
)
3536
)
3637
);
@@ -48,6 +49,7 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
4849
uint256 feeGrowthInside1LastX128
4950
)
5051
{
52+
Ticks memory ticks = _getTicks(pool.token0(), party.partyTicks);
5153
(
5254
feeGrowthInside0LastX128,
5355
feeGrowthInside1LastX128
@@ -56,8 +58,8 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
5658
keccak256(
5759
abi.encodePacked(
5860
address(BNBPositionManager),
59-
party.partyTicks.tickLower,
60-
party.partyTicks.tickUpper
61+
ticks.tickLower,
62+
ticks.tickUpper
6163
)
6264
)
6365
);
@@ -81,13 +83,46 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
8183

8284
/// @notice Internal function to calculate the global fee growth
8385
/// @param pool Address of the Uniswap V3 pool
84-
function _calculateFeeGrowthGlobal(IUniswapV3Pool pool) internal view returns (uint256 feeGrowthGlobal) {
86+
function _calculateFeeGrowthGlobal(
87+
IUniswapV3Pool pool
88+
) internal view returns (uint256 feeGrowthGlobal) {
8589
if (pool.token0() == address(WBNB)) {
86-
(uint256 feeGrowthInside0LastX128,) = _getPartyFeeGrowthInsideLastX128(pool);
87-
feeGrowthGlobal = pool.feeGrowthGlobal0X128() - feeGrowthInside0LastX128;
90+
(uint256 feeGrowthInside0LastX128 , ) = _getPartyFeeGrowthInsideLastX128(pool);
91+
feeGrowthGlobal = pool.feeGrowthGlobal0X128() -feeGrowthInside0LastX128;
8892
} else {
89-
(,uint256 feeGrowthInside1LastX128) = _getPartyFeeGrowthInsideLastX128(pool);
93+
( , uint256 feeGrowthInside1LastX128) = _getPartyFeeGrowthInsideLastX128(pool);
9094
feeGrowthGlobal = pool.feeGrowthGlobal1X128() - feeGrowthInside1LastX128;
9195
}
9296
}
97+
98+
/// @notice Invert the ticks
99+
/// @param tickLower Lower tick
100+
/// @param tickUpper Upper tick
101+
/// @return ticks struct with inverted ticks
102+
function _invertTicks(
103+
int24 tickLower,
104+
int24 tickUpper
105+
) internal pure returns (Ticks memory ticks) {
106+
ticks.tickLower = -tickUpper;
107+
ticks.tickUpper = -tickLower;
108+
}
109+
110+
/// @notice Internal function to retrieve the Ticks based on the token address
111+
/// @param token0 Address of the token0
112+
/// @param ticks The Ticks struct with lower and upper ticks
113+
/// @return adjustedTicks The Ticks struct adjusted based on token address
114+
function _getTicks(
115+
address token0,
116+
Ticks memory ticks
117+
)
118+
internal
119+
view
120+
returns (Ticks memory adjustedTicks)
121+
{
122+
if (address(WBNB) == token0) {
123+
adjustedTicks = _invertTicks(ticks.tickLower, ticks.tickUpper);
124+
} else {
125+
adjustedTicks = ticks;
126+
}
127+
}
93128
}

contracts/BNBPartyLiquidity.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract contract BNBPartyLiquidity is BNBPartyLiquidityHelper {
3838
newSqrtPriceX96 = sqrtPriceCalculator.getNextSqrtPriceFromAmount1RoundingDown(
3939
sqrtPriceX96,
4040
liquidity,
41-
unwrapAmount / 2,
41+
unwrapAmount,
4242
false
4343
);
4444
}
@@ -47,7 +47,7 @@ abstract contract BNBPartyLiquidity is BNBPartyLiquidityHelper {
4747
IERC20(token0).safeIncreaseAllowance(address(positionManager), amount0);
4848
IERC20(token1).safeIncreaseAllowance(address(positionManager), amount1);
4949
// Create new Liquidity Pool
50-
_createLP(positionManager, token0, token1, amount0, amount1, newSqrtPriceX96, party.lpFee, party.lpTicks);
50+
_createLP(positionManager, token0, token1, amount0, amount1, newSqrtPriceX96, party.lpFee, _getTicks(token0, party.lpTicks));
5151

5252
// Send bonuses
5353
_unwrapAndSendBNB(recipient, unwrapAmount);

contracts/BNBPartySwaps.sol

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ abstract contract BNBPartySwaps is BNBPartyView {
5757
/// @return token0 Address of the first token in the pair
5858
/// @return token1 Address of the second token in the pair
5959
/// @return sqrtPriceX96 The sqrt price of the token pair
60+
/// @return ticks The ticks for the token pair
6061
function _getTokenPairAndPrice(
6162
address _token
62-
) internal view returns (address token0, address token1, uint160 sqrtPriceX96) {
63+
) internal view returns (address token0, address token1, uint160 sqrtPriceX96, Ticks memory ticks) {
6364
if (_token < address(WBNB)) {
64-
return (_token, address(WBNB), party.sqrtPriceX96);
65+
return (_token, address(WBNB), party.sqrtPriceX96, Ticks(party.partyTicks.tickLower, party.partyTicks.tickUpper));
6566
} else {
66-
return (address(WBNB), _token, _reverseSqrtPrice(party.sqrtPriceX96));
67+
return (address(WBNB), _token, _reverseSqrtPrice(party.sqrtPriceX96), _invertTicks(party.partyTicks.tickLower, party.partyTicks.tickUpper));
6768
}
6869
}
6970

hardhat.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const LOW_OPTIMIZER_COMPILER_SETTINGS = {
2323
}
2424

2525
const BNB_FACTORY_COMPILER_SETTINGS = {
26-
version: "0.8.24",
26+
version: "0.8.25",
2727
settings: {
2828
evmVersion: "istanbul",
2929
optimizer: {

scripts/deploy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ async function main() {
3333
bonusTargetReach: returnFeeAmount,
3434
bonusPartyCreator: bonusFee,
3535
targetReachFee: targetReachFee,
36-
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
37-
lpTicks: { tickLower: "-214200", tickUpper: "201400" }
36+
partyTicks: { tickLower: "-195600", tickUpper: "214200" },
37+
lpTicks: { tickLower: "-201400", tickUpper: "214200" }
3838
},
3939
tWBNB,
4040
await sqrtPriceCalculator.getAddress()

test/BNBPartyFactory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ describe("BNBPartyFactory", function () {
4747
expect((await bnbPartyFactory.party()).lpFee).to.equal(FeeAmount.HIGH)
4848
expect((await bnbPartyFactory.party()).partyLpFee).to.equal(FeeAmount.HIGH)
4949
expect((await bnbPartyFactory.party()).createTokenFee).to.equal(tokenCreationFee)
50-
expect((await bnbPartyFactory.party()).partyTicks.tickUpper).to.equal("195600")
51-
expect((await bnbPartyFactory.party()).partyTicks.tickLower).to.equal("-214200")
50+
expect((await bnbPartyFactory.party()).partyTicks.tickUpper).to.equal("214200")
51+
expect((await bnbPartyFactory.party()).partyTicks.tickLower).to.equal("-195600")
5252
})
5353

5454
it("should create party LP", async function () {

test/WithdrawFee.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("Withdraw fees", function () {
5454
const partyLP = await v3PartyFactory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
5555
await bnbPartyFactory.withdrawPartyLPFee([partyLP])
5656
const balanceAfter = await wbnb.balanceOf(await signers[0].getAddress())
57-
expect(balanceAfter).to.be.equal(balanceBefore + expectedFee)
57+
expect(balanceAfter).to.be.equal(balanceBefore + expectedFee - 1n)
5858
})
5959

6060
it("should return zero if pool is zero address", async () => {
@@ -68,11 +68,11 @@ describe("Withdraw fees", function () {
6868
it("should return fee from second lp", async () => {
6969
await bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget }) // create second lp
7070
await bnbPartyFactory.joinParty(MEME, 0, { value: ethers.parseEther("1") }) // make swap for fee
71-
const secondLP = await v3Factory.getPool(MEME, await wbnb.getAddress(), FeeAmount.HIGH)
71+
const secondLP = await v3Factory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
7272
const lpPool = (await ethers.getContractAt("UniswapV3Pool", secondLP)) as any as IUniswapV3Pool
7373
const token0 = await lpPool.token0()
7474
await bnbPartyFactory.withdrawLPFee([secondLP])
75-
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(secondLP, positionManager)
75+
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(secondLP, await positionManager.getAddress())
7676
const fee = collectedFee.feeGrowthInside0LastX128 == 0n ? collectedFee.feeGrowthInside1LastX128 : collectedFee.feeGrowthInside0LastX128
7777
if (token0 == (await wbnb.getAddress())) {
7878
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128()
@@ -119,7 +119,7 @@ describe("Withdraw fees", function () {
119119
(await lpPool.feeGrowthGlobal0X128()) > 0
120120
? await lpPool.feeGrowthGlobal0X128()
121121
: await lpPool.feeGrowthGlobal1X128()
122-
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 20n) // 1 % fee
122+
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 20n - 1n) // 1 % fee
123123
})
124124

125125
it("isToken0WBNB should return true if token0 is WBNB", async () => {

test/helper.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export async function deployContracts(partyTarget = ethers.parseEther("90"), wbn
6363
bonusTargetReach: returnFeeAmount,
6464
bonusPartyCreator: bonusFee,
6565
targetReachFee: targetReachFee,
66-
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
67-
lpTicks: { tickLower: "-214200", tickUpper: "201400" },
66+
partyTicks: { tickLower: "-195600", tickUpper: "214200" },
67+
lpTicks: { tickLower: "-201400", tickUpper: "214200" }
6868
},
6969
wbnbAddress,
7070
await sqrtPriceCalculator.getAddress()
@@ -145,8 +145,8 @@ export async function deployBNBPartyFactory(
145145
bonusTargetReach: returnFeeAmount,
146146
bonusPartyCreator: bonusFee,
147147
targetReachFee: targetReachFee,
148-
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
149-
lpTicks: { tickLower: "-214200", tickUpper: "201400" },
148+
partyTicks: { tickLower: "-195600", tickUpper: "214200" },
149+
lpTicks: { tickLower: "-201400", tickUpper: "214200" }
150150
},
151151
WBNB,
152152
sqrtPriceCalculator

0 commit comments

Comments
 (0)