Skip to content

Commit

Permalink
Merge fc5a885 into f4fe321
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive authored Sep 2, 2024
2 parents f4fe321 + fc5a885 commit c127abf
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 91 deletions.
3 changes: 3 additions & 0 deletions .solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
skipFiles: ['./mock'],
}
67 changes: 2 additions & 65 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,33 +80,6 @@ describe("BNBPartyFactory", function () {
expect(balanceAfter).to.be.equal(balanceBefore + tokenCreationFee)
})

it("should revert if not enough BNB is sent", async function () {
await expect(
bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee - 1n })
).to.be.revertedWithCustomError(bnbPartyFactory, "InsufficientBNB")
})

it("should revert to Create Party if position manager is not set", async function () {
await bnbPartyFactory.setNonfungiblePositionManager(ethers.ZeroAddress, ethers.ZeroAddress)
await expect(
bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
).to.be.revertedWithCustomError(bnbPartyFactory, "ZeroAddress")
await bnbPartyFactory.setNonfungiblePositionManager(
await BNBPositionManager.getAddress(),
await positionManager.getAddress()
)
})

it("should revert if swap router is not set", async function () {
const amountIn = ethers.parseUnits("1", 18)
await bnbPartyFactory.setBNBPartySwapRouter(ethers.ZeroAddress)
await expect(bnbPartyFactory.createParty(name, symbol, { value: amountIn })).to.be.revertedWithCustomError(
bnbPartyFactory,
"ZeroAddress"
)
await bnbPartyFactory.setBNBPartySwapRouter(await BNBSwapRouter.getAddress())
})

it("should set pause", async function () {
await bnbPartyFactory.pause()
expect(await bnbPartyFactory.paused()).to.be.true
Expand All @@ -118,43 +92,6 @@ describe("BNBPartyFactory", function () {
expect(await bnbPartyFactory.paused()).to.be.false
})

it("should revert party creation if paused", async function () {
await bnbPartyFactory.pause()
await expect(
bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
).to.be.revertedWithCustomError(bnbPartyFactory, "EnforcedPause")
await bnbPartyFactory.unpause()
})

it("should revert join party if paused", async function () {
await bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
await bnbPartyFactory.pause()
const tokenId = (await BNBPositionManager.totalSupply()) - 1n
const position = await BNBPositionManager.positions(tokenId)
const MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
await expect(bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget })).to.be.revertedWithCustomError(
bnbPartyFactory,
"EnforcedPause"
)
await bnbPartyFactory.unpause()
})

it("should revert leave party if paused", async function () {
await bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
const tokenId = (await BNBPositionManager.totalSupply()) - 1n
const position = await BNBPositionManager.positions(tokenId)
const MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
const MEMEToken = await ethers.getContractAt("ERC20Token", MEME)
await MEMEToken.approve(await bnbPartyFactory.getAddress(), ethers.parseEther("1000000"))
await bnbPartyFactory.joinParty(MEME, 0, { value: tokenCreationFee })
await bnbPartyFactory.pause()
await expect(bnbPartyFactory.leaveParty(MEME, tokenCreationFee, 0)).to.be.revertedWithCustomError(
bnbPartyFactory,
"EnforcedPause"
)
await bnbPartyFactory.unpause()
})

describe("Second Liquidity Pool", function () {
let MEME: string
let tokenId: string
Expand Down Expand Up @@ -197,7 +134,7 @@ describe("BNBPartyFactory", function () {
const lpAddress = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
const balance = await weth9.balanceOf(lpAddress)
const percentFee = ethers.parseEther("0.14") // target 13 + 1 BNB - 1% fee
expect(balance).to.be.equal(BNBToTarget - returnFeeAmount - bonusFee - targetReachFee - percentFee - 2n)
expect(balance).to.be.equal(BNBToTarget - returnFeeAmount - bonusFee - targetReachFee - percentFee - 1n)
})

it("should send MEME to new LP", async () => {
Expand Down
59 changes: 36 additions & 23 deletions test/WithdrawFee.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
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 { IUniswapV3Pool } from "../typechain-types"
import { FeeAmount, bnbPartyFactory, v3PartyFactory, BNBPositionManager, weth9, deployContracts, v3Factory, positionManager } from "./helper"

describe("Withdraw fees", function () {
let MEME: string
Expand All @@ -23,8 +16,6 @@ describe("Withdraw fees", function () {
const name = "Party"
const symbol = "Token"
const BNBToTarget: bigint = partyTarget + ethers.parseEther("1")
const tickLower = -214200
const tickUpper = 201400

beforeEach(async () => {
signers = await ethers.getSigners()
Expand Down Expand Up @@ -66,6 +57,32 @@ describe("Withdraw fees", function () {
expect(balanceAfter).to.be.equal(balanceBefore + expectedFee)
})

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 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 weth9.getAddress(), 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 fee = collectedFee.feeGrowthInside0LastX128 == 0n ? collectedFee.feeGrowthInside1LastX128 : collectedFee.feeGrowthInside0LastX128
if (token0 == (await weth9.getAddress())) {
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128()
expect(feeGrowthGlobalX128).to.be.deep.equal(fee)
} else {
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal1X128()
expect(feeGrowthGlobalX128).to.be.deep.equal(fee)
}
})

it("should revert LPNotAtParty", async () => {
const mockContract = await ethers.getContractFactory("MockContract")
const mock = await mockContract.deploy()
Expand All @@ -89,7 +106,7 @@ describe("Withdraw fees", function () {
const lpPool = (await ethers.getContractAt("UniswapV3Pool", lpAddress)) as any as IUniswapV3Pool
const liquidity = await lpPool.liquidity()
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal1X128() > 0 ? await lpPool.feeGrowthGlobal1X128() : await lpPool.feeGrowthGlobal0X128()
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 100n) // 1 % fee
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 100n - 1n) // 1 % fee
})

it("calculateFees should return fee from 5 swaps", async () => {
Expand All @@ -98,19 +115,15 @@ describe("Withdraw fees", function () {
}
const lpPool = (await ethers.getContractAt("UniswapV3Pool", lpAddress)) as any as IUniswapV3Pool
const liquidity = await lpPool.liquidity()
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128() > 0 ? await lpPool.feeGrowthGlobal0X128() : await lpPool.feeGrowthGlobal1X128()
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
const feeGrowthGlobalX128 =
(await lpPool.feeGrowthGlobal0X128()) > 0
? await lpPool.feeGrowthGlobal0X128()
: await lpPool.feeGrowthGlobal1X128()
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 20n) // 1 % fee
})

it("isToken0WBNB should revert if set zero address", async () => {
await expect(bnbPartyFactory.isToken0WBNB(ethers.ZeroAddress)).to.be.revertedWithCustomError(
bnbPartyFactory,
"ZeroAddress"
)
it("isToken0WBNB should return true if token0 is WBNB", async () => {
expect(await bnbPartyFactory.isToken0WBNB(lpAddress)).to.be.true
})

it("should deacrease fee after withdraw", 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
)
}
Loading

0 comments on commit c127abf

Please sign in to comment.