Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Sep 2, 2024
1 parent f4fe321 commit 6b4f591
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 14 deletions.
99 changes: 99 additions & 0 deletions test/BNBPartyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BNBSwapRouter,
weth9,
deployContracts,
deployBNBPartyFactory,
} from "./helper"

const POOL_BYTECODE_HASH = keccak256(bytecode)
Expand Down Expand Up @@ -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 })
Expand Down
23 changes: 12 additions & 11 deletions test/WithdrawFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down
43 changes: 40 additions & 3 deletions test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ 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()
)) as BNBPartyFactory

// 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
Expand All @@ -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(),
Expand Down Expand Up @@ -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
)
}

0 comments on commit 6b4f591

Please sign in to comment.