From 6b4f591a1933dc454cf938c5980eea9eb90bf236 Mon Sep 17 00:00:00 2001 From: Andrew Dmytrenko Date: Mon, 2 Sep 2024 15:03:03 +0300 Subject: [PATCH] add more tests --- test/BNBPartyFactory.ts | 99 +++++++++++++++++++++++++++++++++++++++++ test/WithdrawFee.ts | 23 +++++----- test/helper.ts | 43 ++++++++++++++++-- 3 files changed, 151 insertions(+), 14 deletions(-) diff --git a/test/BNBPartyFactory.ts b/test/BNBPartyFactory.ts index 3d21235..f75cc1e 100644 --- a/test/BNBPartyFactory.ts +++ b/test/BNBPartyFactory.ts @@ -13,6 +13,7 @@ import { BNBSwapRouter, weth9, deployContracts, + deployBNBPartyFactory, } from "./helper" const POOL_BYTECODE_HASH = keccak256(bytecode) @@ -79,6 +80,104 @@ describe("BNBPartyFactory", function () { expect(balanceAfter).to.be.equal(balanceBefore + tokenCreationFee) }) + it("should revert WBNB zero address", async function () { + const sqrtAddress = "0x0000000000000000000000000000000000000001" + await expect( + deployBNBPartyFactory( + partyTarget, + tokenCreationFee, + returnFeeAmount, + bonusFee, + targetReachFee, + initialTokenAmount, + sqrtPriceX96, + ethers.ZeroAddress, + sqrtAddress + ) + ).to.be.revertedWithCustomError(bnbPartyFactory, "ZeroAddress") + }) + + it("should revert sqrtPriceCalculator zero address", async function () { + const WBNB = "0x0000000000000000000000000000000000000001" + await expect( + deployBNBPartyFactory( + partyTarget, + tokenCreationFee, + returnFeeAmount, + bonusFee, + targetReachFee, + initialTokenAmount, + sqrtPriceX96, + WBNB, + ethers.ZeroAddress + ) + ).to.be.revertedWithCustomError(bnbPartyFactory, "ZeroAddress") + }) + + it("should revert zero target", async function () { + await expect( + deployBNBPartyFactory( + 0n, + tokenCreationFee, + returnFeeAmount, + bonusFee, + targetReachFee, + initialTokenAmount, + sqrtPriceX96, + await weth9.getAddress(), + await v3Factory.getAddress() + ) + ).to.be.revertedWithCustomError(bnbPartyFactory, "ZeroAmount") + }) + + it("should revert zero initialTokenAmount", async function () { + await expect( + deployBNBPartyFactory( + partyTarget, + tokenCreationFee, + returnFeeAmount, + bonusFee, + targetReachFee, + "0", + sqrtPriceX96, + await weth9.getAddress(), + await v3Factory.getAddress() + ) + ).to.be.revertedWithCustomError(bnbPartyFactory, "ZeroAmount") + }) + + it("should revert zero sqrtPriceX96", async function () { + await expect( + deployBNBPartyFactory( + partyTarget, + tokenCreationFee, + returnFeeAmount, + bonusFee, + targetReachFee, + initialTokenAmount, + "0", + await weth9.getAddress(), + await v3Factory.getAddress() + ) + ).to.be.revertedWithCustomError(bnbPartyFactory, "ZeroAmount") + }) + + it("should revert if target is less than fees", async function () { + await expect( + deployBNBPartyFactory( + bonusFee, + tokenCreationFee, + returnFeeAmount, + bonusFee, + targetReachFee, + initialTokenAmount, + sqrtPriceX96, + await weth9.getAddress(), + await v3Factory.getAddress() + ) + ).to.be.revertedWithCustomError(bnbPartyFactory, "BonusGreaterThanTarget") + }) + it("should revert if not enough BNB is sent", async function () { await expect( bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee - 1n }) diff --git a/test/WithdrawFee.ts b/test/WithdrawFee.ts index bcd96c5..13b48e9 100644 --- a/test/WithdrawFee.ts +++ b/test/WithdrawFee.ts @@ -2,14 +2,7 @@ import { expect } from "chai" import { ethers } from "hardhat" import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers" import { IUniswapV3Pool, MockContract } from "../typechain-types" -import { - FeeAmount, - bnbPartyFactory, - v3PartyFactory, - BNBPositionManager, - weth9, - deployContracts, -} from "./helper" +import { FeeAmount, bnbPartyFactory, v3PartyFactory, BNBPositionManager, weth9, deployContracts } from "./helper" describe("Withdraw fees", function () { let MEME: string @@ -63,7 +56,15 @@ describe("Withdraw fees", function () { const partyLP = await v3PartyFactory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH) await bnbPartyFactory.withdrawPartyLPFee([partyLP]) const balanceAfter = await weth9.balanceOf(await signers[0].getAddress()) - expect(balanceAfter).to.be.equal(balanceBefore + expectedFee) + expect(balanceAfter).to.be.equal(balanceBefore + expectedFee - 1n) + }) + + it("should return zero if pool is zero address", async () => { + expect(await bnbPartyFactory.getFeeGrowthInsideLastX128(ethers.ZeroAddress, BNBPositionManager)).to.be.deep.equal([ 0n, 0n ]) + }) + + it("should return zero if position manager is zero address", async () => { + expect(await bnbPartyFactory.getFeeGrowthInsideLastX128(lpAddress, ethers.ZeroAddress)).to.be.deep.equal([0n, 0n]) }) it("should revert LPNotAtParty", async () => { @@ -102,8 +103,8 @@ describe("Withdraw fees", function () { expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 20n - 1n) // 1 % fee }) - it("isToken0WBNB should return false if token0 is not WBNB", async () => { - expect(await bnbPartyFactory.isToken0WBNB(lpAddress)).to.be.false + it("isToken0WBNB should return true if token0 is WBNB", async () => { + expect(await bnbPartyFactory.isToken0WBNB(lpAddress)).to.be.true }) it("isToken0WBNB should revert if set zero address", async () => { diff --git a/test/helper.ts b/test/helper.ts index f54fd60..22822db 100644 --- a/test/helper.ts +++ b/test/helper.ts @@ -63,7 +63,7 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) { bonusPartyCreator: bonusFee, targetReachFee: targetReachFee, partyTicks: { tickLower: "-214200", tickUpper: "195600" }, - lpTicks: { tickLower: "-214200", tickUpper: "201400" } + lpTicks: { tickLower: "-214200", tickUpper: "201400" }, }, await weth9.getAddress(), await sqrtPriceCalculator.getAddress() @@ -71,7 +71,10 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) { // Deploy Uniswap V3 Factory const v3PartyFactoryContract = await ethers.getContractFactory(FactoryArtifact.abi, FactoryArtifact.bytecode) - const v3FactoryContract = await ethers.getContractFactory(ClassicFactoryArtifact.abi, ClassicFactoryArtifact.bytecode) + const v3FactoryContract = await ethers.getContractFactory( + ClassicFactoryArtifact.abi, + ClassicFactoryArtifact.bytecode + ) v3Factory = (await v3FactoryContract.deploy()) as UniswapV3Factory v3PartyFactory = (await v3PartyFactoryContract.deploy(await bnbPartyFactory.getAddress())) as UniswapV3Factory @@ -81,7 +84,10 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) { tokenPositionDescriptor = (await TokenPositionDescriptor.deploy()) as MockNonfungibleTokenPositionDescriptor // Deploy Position Manager - const ManagerContract = await ethers.getContractFactory(ClassicNonfungiblePositionManager.abi, ClassicNonfungiblePositionManager.bytecode) + const ManagerContract = await ethers.getContractFactory( + ClassicNonfungiblePositionManager.abi, + ClassicNonfungiblePositionManager.bytecode + ) positionManager = (await ManagerContract.deploy( await v3Factory.getAddress(), await weth9.getAddress(), @@ -114,3 +120,34 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) { await bnbPartyFactory.setBNBPartySwapRouter(await BNBSwapRouter.getAddress()) await bnbPartyFactory.setSwapRouter(await swapRouter.getAddress()) } + +export async function deployBNBPartyFactory( + partyTarget: bigint, + tokenCreationFee: bigint, + returnFeeAmount: bigint, + bonusFee: bigint, + targetReachFee: bigint, + initialTokenAmount: string, + sqrtPriceX96: string, + WBNB: string, + sqrtPriceCalculator: string +) { + const BNBPartyFactoryContract = await ethers.getContractFactory("BNBPartyFactory") + return BNBPartyFactoryContract.deploy( + { + partyTarget: partyTarget, + createTokenFee: tokenCreationFee, + partyLpFee: FeeAmount.HIGH, + lpFee: FeeAmount.HIGH, + initialTokenAmount: initialTokenAmount, + sqrtPriceX96: sqrtPriceX96, + bonusTargetReach: returnFeeAmount, + bonusPartyCreator: bonusFee, + targetReachFee: targetReachFee, + partyTicks: { tickLower: "-214200", tickUpper: "195600" }, + lpTicks: { tickLower: "-214200", tickUpper: "201400" }, + }, + WBNB, + sqrtPriceCalculator + ) +}