Skip to content

Commit

Permalink
make ticks more predictable
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Sep 12, 2024
1 parent 336c134 commit c1d7ca4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
45 changes: 25 additions & 20 deletions contracts/BNBPartyFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
uint256 feeGrowthInside1LastX128
)
{
Ticks memory ticks;
if (address(WBNB) == pool.token0()) {
ticks.tickLower = party.lpTicks.tickLower;
ticks.tickUpper = party.lpTicks.tickUpper;
} else {
ticks = _invertTicks(
party.lpTicks.tickLower,
party.lpTicks.tickUpper
);
}
Ticks memory ticks = _getTicks(pool.token0(), party.lpTicks);
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
Expand Down Expand Up @@ -58,16 +49,7 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
uint256 feeGrowthInside1LastX128
)
{
Ticks memory ticks;
if (address(WBNB) == pool.token0()) {
ticks.tickLower = party.partyTicks.tickLower;
ticks.tickUpper = party.partyTicks.tickUpper;
} else {
ticks = _invertTicks(
party.partyTicks.tickLower,
party.partyTicks.tickUpper
);
}
Ticks memory ticks = _getTicks(pool.token0(), party.partyTicks);
(
feeGrowthInside0LastX128,
feeGrowthInside1LastX128
Expand Down Expand Up @@ -113,11 +95,34 @@ abstract contract BNBPartyFee is BNBPartyModifiers {
}
}

/// @notice Invert the ticks
/// @param tickLower Lower tick
/// @param tickUpper Upper tick
/// @return ticks struct with inverted ticks
function _invertTicks(
int24 tickLower,
int24 tickUpper
) internal pure returns (Ticks memory ticks) {
ticks.tickLower = -tickUpper;
ticks.tickUpper = -tickLower;
}

/// @notice Internal function to retrieve the Ticks based on the token address
/// @param token0 Address of the token0
/// @param ticks The Ticks struct with lower and upper ticks
/// @return adjustedTicks The Ticks struct adjusted based on token address
function _getTicks(
address token0,
Ticks memory ticks
)
internal
view
returns (Ticks memory adjustedTicks)
{
if (address(WBNB) == token0) {
adjustedTicks = _invertTicks(ticks.tickLower, ticks.tickUpper);
} else {
adjustedTicks = ticks;
}
}
}
2 changes: 1 addition & 1 deletion contracts/BNBPartyLiquidity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract contract BNBPartyLiquidity is BNBPartyLiquidityHelper {
IERC20(token0).safeIncreaseAllowance(address(positionManager), amount0);
IERC20(token1).safeIncreaseAllowance(address(positionManager), amount1);
// Create new Liquidity Pool
_createLP(positionManager, token0, token1, amount0, amount1, newSqrtPriceX96, party.lpFee, token0 == address(WBNB) ? party.lpTicks : _invertTicks(party.lpTicks.tickLower, party.lpTicks.tickUpper));
_createLP(positionManager, token0, token1, amount0, amount1, newSqrtPriceX96, party.lpFee, token1 == address(WBNB) ? party.lpTicks : _invertTicks(party.lpTicks.tickLower, party.lpTicks.tickUpper));

// Send bonuses
_unwrapAndSendBNB(recipient, unwrapAmount);
Expand Down
4 changes: 2 additions & 2 deletions contracts/BNBPartySwaps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ abstract contract BNBPartySwaps is BNBPartyView {
address _token
) internal view returns (address token0, address token1, uint160 sqrtPriceX96, Ticks memory ticks) {
if (_token < address(WBNB)) {
return (_token, address(WBNB), party.sqrtPriceX96, _invertTicks(party.partyTicks.tickLower, party.partyTicks.tickUpper));
return (_token, address(WBNB), party.sqrtPriceX96, Ticks(party.partyTicks.tickLower, party.partyTicks.tickUpper));
} else {
return (address(WBNB), _token, _reverseSqrtPrice(party.sqrtPriceX96), Ticks(party.partyTicks.tickLower, party.partyTicks.tickUpper));
return (address(WBNB), _token, _reverseSqrtPrice(party.sqrtPriceX96), _invertTicks(party.partyTicks.tickLower, party.partyTicks.tickUpper));
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/BNBPartyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe("BNBPartyFactory", function () {
expect((await bnbPartyFactory.party()).lpFee).to.equal(FeeAmount.HIGH)
expect((await bnbPartyFactory.party()).partyLpFee).to.equal(FeeAmount.HIGH)
expect((await bnbPartyFactory.party()).createTokenFee).to.equal(tokenCreationFee)
expect((await bnbPartyFactory.party()).partyTicks.tickUpper).to.equal("195600")
expect((await bnbPartyFactory.party()).partyTicks.tickLower).to.equal("-214200")
expect((await bnbPartyFactory.party()).partyTicks.tickUpper).to.equal("214200")
expect((await bnbPartyFactory.party()).partyTicks.tickLower).to.equal("-195600")
})

it("should create party LP", async function () {
Expand Down
4 changes: 2 additions & 2 deletions test/WithdrawFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ describe("Withdraw fees", function () {
it("should return fee from second lp", async () => {
await bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget }) // create second lp
await bnbPartyFactory.joinParty(MEME, 0, { value: ethers.parseEther("1") }) // make swap for fee
const secondLP = await v3Factory.getPool(MEME, await wbnb.getAddress(), FeeAmount.HIGH)
const secondLP = await v3Factory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
const lpPool = (await ethers.getContractAt("UniswapV3Pool", secondLP)) as any as IUniswapV3Pool
const token0 = await lpPool.token0()
await bnbPartyFactory.withdrawLPFee([secondLP])
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(secondLP, positionManager)
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(secondLP, await positionManager.getAddress())
const fee = collectedFee.feeGrowthInside0LastX128 == 0n ? collectedFee.feeGrowthInside1LastX128 : collectedFee.feeGrowthInside0LastX128
if (token0 == (await wbnb.getAddress())) {
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128()
Expand Down
4 changes: 2 additions & 2 deletions test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export async function deployContracts(partyTarget = ethers.parseEther("90"), wbn
bonusTargetReach: returnFeeAmount,
bonusPartyCreator: bonusFee,
targetReachFee: targetReachFee,
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
lpTicks: { tickLower: "-214200", tickUpper: "201400" }
partyTicks: { tickLower: "-195600", tickUpper: "214200" },
lpTicks: { tickLower: "-201400", tickUpper: "214200" }
},
wbnbAddress,
await sqrtPriceCalculator.getAddress()
Expand Down

0 comments on commit c1d7ca4

Please sign in to comment.