Skip to content

Commit a772945

Browse files
Merge branch 'master' into curve
2 parents dc249b2 + 39398cd commit a772945

File tree

5 files changed

+316
-91
lines changed

5 files changed

+316
-91
lines changed

.solcover.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
skipFiles: ['./mock'],
3+
}

test/BNBPartyFactory.ts

+2-65
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
BNBSwapRouter,
1414
weth9,
1515
deployContracts,
16+
deployBNBPartyFactory,
1617
} from "./helper"
1718

1819
const POOL_BYTECODE_HASH = keccak256(bytecode)
@@ -79,33 +80,6 @@ describe("BNBPartyFactory", function () {
7980
expect(balanceAfter).to.be.equal(balanceBefore + tokenCreationFee)
8081
})
8182

82-
it("should revert if not enough BNB is sent", async function () {
83-
await expect(
84-
bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee - 1n })
85-
).to.be.revertedWithCustomError(bnbPartyFactory, "InsufficientBNB")
86-
})
87-
88-
it("should revert to Create Party if position manager is not set", async function () {
89-
await bnbPartyFactory.setNonfungiblePositionManager(ethers.ZeroAddress, ethers.ZeroAddress)
90-
await expect(
91-
bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
92-
).to.be.revertedWithCustomError(bnbPartyFactory, "ZeroAddress")
93-
await bnbPartyFactory.setNonfungiblePositionManager(
94-
await BNBPositionManager.getAddress(),
95-
await positionManager.getAddress()
96-
)
97-
})
98-
99-
it("should revert if swap router is not set", async function () {
100-
const amountIn = ethers.parseUnits("1", 18)
101-
await bnbPartyFactory.setBNBPartySwapRouter(ethers.ZeroAddress)
102-
await expect(bnbPartyFactory.createParty(name, symbol, { value: amountIn })).to.be.revertedWithCustomError(
103-
bnbPartyFactory,
104-
"ZeroAddress"
105-
)
106-
await bnbPartyFactory.setBNBPartySwapRouter(await BNBSwapRouter.getAddress())
107-
})
108-
10983
it("should set pause", async function () {
11084
await bnbPartyFactory.pause()
11185
expect(await bnbPartyFactory.paused()).to.be.true
@@ -118,43 +92,6 @@ describe("BNBPartyFactory", function () {
11892
expect(await bnbPartyFactory.paused()).to.be.false
11993
})
12094

121-
it("should revert party creation if paused", async function () {
122-
await bnbPartyFactory.pause()
123-
await expect(
124-
bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
125-
).to.be.revertedWithCustomError(bnbPartyFactory, "EnforcedPause")
126-
await bnbPartyFactory.unpause()
127-
})
128-
129-
it("should revert join party if paused", async function () {
130-
await bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
131-
await bnbPartyFactory.pause()
132-
const tokenId = (await BNBPositionManager.totalSupply()) - 1n
133-
const position = await BNBPositionManager.positions(tokenId)
134-
const MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
135-
await expect(bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget })).to.be.revertedWithCustomError(
136-
bnbPartyFactory,
137-
"EnforcedPause"
138-
)
139-
await bnbPartyFactory.unpause()
140-
})
141-
142-
it("should revert leave party if paused", async function () {
143-
await bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
144-
const tokenId = (await BNBPositionManager.totalSupply()) - 1n
145-
const position = await BNBPositionManager.positions(tokenId)
146-
const MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
147-
const MEMEToken = await ethers.getContractAt("ERC20Token", MEME)
148-
await MEMEToken.approve(await bnbPartyFactory.getAddress(), ethers.parseEther("1000000"))
149-
await bnbPartyFactory.joinParty(MEME, 0, { value: tokenCreationFee })
150-
await bnbPartyFactory.pause()
151-
await expect(bnbPartyFactory.leaveParty(MEME, tokenCreationFee, 0)).to.be.revertedWithCustomError(
152-
bnbPartyFactory,
153-
"EnforcedPause"
154-
)
155-
await bnbPartyFactory.unpause()
156-
})
157-
15895
describe("Second Liquidity Pool", function () {
15996
let MEME: string
16097
let tokenId: string
@@ -197,7 +134,7 @@ describe("BNBPartyFactory", function () {
197134
const lpAddress = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
198135
const balance = await weth9.balanceOf(lpAddress)
199136
const percentFee = ethers.parseEther("0.14") // target 13 + 1 BNB - 1% fee
200-
expect(balance).to.be.equal(BNBToTarget - returnFeeAmount - bonusFee - targetReachFee - percentFee - 2n)
137+
expect(balance).to.be.equal(BNBToTarget - returnFeeAmount - bonusFee - targetReachFee - percentFee - 1n)
201138
})
202139

203140
it("should send MEME to new LP", async () => {

test/WithdrawFee.ts

+36-23
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import { expect } from "chai"
22
import { ethers } from "hardhat"
33
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
4-
import { IUniswapV3Pool, MockContract } from "../typechain-types"
5-
import {
6-
FeeAmount,
7-
bnbPartyFactory,
8-
v3PartyFactory,
9-
BNBPositionManager,
10-
weth9,
11-
deployContracts,
12-
} from "./helper"
4+
import { IUniswapV3Pool } from "../typechain-types"
5+
import { FeeAmount, bnbPartyFactory, v3PartyFactory, BNBPositionManager, weth9, deployContracts, v3Factory, positionManager } from "./helper"
136

147
describe("Withdraw fees", function () {
158
let MEME: string
@@ -23,8 +16,6 @@ describe("Withdraw fees", function () {
2316
const name = "Party"
2417
const symbol = "Token"
2518
const BNBToTarget: bigint = partyTarget + ethers.parseEther("1")
26-
const tickLower = -214200
27-
const tickUpper = 201400
2819

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

60+
it("should return zero if pool is zero address", async () => {
61+
expect(await bnbPartyFactory.getFeeGrowthInsideLastX128(ethers.ZeroAddress, BNBPositionManager)).to.be.deep.equal([ 0n, 0n ])
62+
})
63+
64+
it("should return zero if position manager is zero address", async () => {
65+
expect(await bnbPartyFactory.getFeeGrowthInsideLastX128(lpAddress, ethers.ZeroAddress)).to.be.deep.equal([0n, 0n,])
66+
})
67+
68+
it("should return fee from second lp", async () => {
69+
await bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget }) // create second lp
70+
await bnbPartyFactory.joinParty(MEME, 0, { value: ethers.parseEther("1") }) // make swap for fee
71+
const secondLP = await v3Factory.getPool(MEME, await weth9.getAddress(), FeeAmount.HIGH)
72+
const lpPool = (await ethers.getContractAt("UniswapV3Pool", secondLP)) as any as IUniswapV3Pool
73+
const token0 = await lpPool.token0()
74+
await bnbPartyFactory.withdrawLPFee([secondLP])
75+
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(secondLP, positionManager)
76+
const fee = collectedFee.feeGrowthInside0LastX128 == 0n ? collectedFee.feeGrowthInside1LastX128 : collectedFee.feeGrowthInside0LastX128
77+
if (token0 == (await weth9.getAddress())) {
78+
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128()
79+
expect(feeGrowthGlobalX128).to.be.deep.equal(fee)
80+
} else {
81+
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal1X128()
82+
expect(feeGrowthGlobalX128).to.be.deep.equal(fee)
83+
}
84+
})
85+
6986
it("should revert LPNotAtParty", async () => {
7087
const mockContract = await ethers.getContractFactory("MockContract")
7188
const mock = await mockContract.deploy()
@@ -89,7 +106,7 @@ describe("Withdraw fees", function () {
89106
const lpPool = (await ethers.getContractAt("UniswapV3Pool", lpAddress)) as any as IUniswapV3Pool
90107
const liquidity = await lpPool.liquidity()
91108
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal1X128() > 0 ? await lpPool.feeGrowthGlobal1X128() : await lpPool.feeGrowthGlobal0X128()
92-
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 100n) // 1 % fee
109+
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 100n - 1n) // 1 % fee
93110
})
94111

95112
it("calculateFees should return fee from 5 swaps", async () => {
@@ -98,19 +115,15 @@ describe("Withdraw fees", function () {
98115
}
99116
const lpPool = (await ethers.getContractAt("UniswapV3Pool", lpAddress)) as any as IUniswapV3Pool
100117
const liquidity = await lpPool.liquidity()
101-
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128() > 0 ? await lpPool.feeGrowthGlobal0X128() : await lpPool.feeGrowthGlobal1X128()
102-
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 20n - 1n) // 1 % fee
103-
})
104-
105-
it("isToken0WBNB should return false if token0 is not WBNB", async () => {
106-
expect(await bnbPartyFactory.isToken0WBNB(lpAddress)).to.be.false
118+
const feeGrowthGlobalX128 =
119+
(await lpPool.feeGrowthGlobal0X128()) > 0
120+
? await lpPool.feeGrowthGlobal0X128()
121+
: await lpPool.feeGrowthGlobal1X128()
122+
expect(await bnbPartyFactory.calculateFees(liquidity, feeGrowthGlobalX128)).to.be.equal(amountIn / 20n) // 1 % fee
107123
})
108124

109-
it("isToken0WBNB should revert if set zero address", async () => {
110-
await expect(bnbPartyFactory.isToken0WBNB(ethers.ZeroAddress)).to.be.revertedWithCustomError(
111-
bnbPartyFactory,
112-
"ZeroAddress"
113-
)
125+
it("isToken0WBNB should return true if token0 is WBNB", async () => {
126+
expect(await bnbPartyFactory.isToken0WBNB(lpAddress)).to.be.true
114127
})
115128

116129
it("should deacrease fee after withdraw", async () => {

test/helper.ts

+40-3
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) {
6363
bonusPartyCreator: bonusFee,
6464
targetReachFee: targetReachFee,
6565
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
66-
lpTicks: { tickLower: "-214200", tickUpper: "201400" }
66+
lpTicks: { tickLower: "-214200", tickUpper: "201400" },
6767
},
6868
await weth9.getAddress(),
6969
await sqrtPriceCalculator.getAddress()
7070
)) as BNBPartyFactory
7171

7272
// Deploy Uniswap V3 Factory
7373
const v3PartyFactoryContract = await ethers.getContractFactory(FactoryArtifact.abi, FactoryArtifact.bytecode)
74-
const v3FactoryContract = await ethers.getContractFactory(ClassicFactoryArtifact.abi, ClassicFactoryArtifact.bytecode)
74+
const v3FactoryContract = await ethers.getContractFactory(
75+
ClassicFactoryArtifact.abi,
76+
ClassicFactoryArtifact.bytecode
77+
)
7578
v3Factory = (await v3FactoryContract.deploy()) as UniswapV3Factory
7679

7780
v3PartyFactory = (await v3PartyFactoryContract.deploy(await bnbPartyFactory.getAddress())) as UniswapV3Factory
@@ -81,7 +84,10 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) {
8184
tokenPositionDescriptor = (await TokenPositionDescriptor.deploy()) as MockNonfungibleTokenPositionDescriptor
8285

8386
// Deploy Position Manager
84-
const ManagerContract = await ethers.getContractFactory(ClassicNonfungiblePositionManager.abi, ClassicNonfungiblePositionManager.bytecode)
87+
const ManagerContract = await ethers.getContractFactory(
88+
ClassicNonfungiblePositionManager.abi,
89+
ClassicNonfungiblePositionManager.bytecode
90+
)
8591
positionManager = (await ManagerContract.deploy(
8692
await v3Factory.getAddress(),
8793
await weth9.getAddress(),
@@ -114,3 +120,34 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) {
114120
await bnbPartyFactory.setBNBPartySwapRouter(await BNBSwapRouter.getAddress())
115121
await bnbPartyFactory.setSwapRouter(await swapRouter.getAddress())
116122
}
123+
124+
export async function deployBNBPartyFactory(
125+
partyTarget: bigint,
126+
tokenCreationFee: bigint,
127+
returnFeeAmount: bigint,
128+
bonusFee: bigint,
129+
targetReachFee: bigint,
130+
initialTokenAmount: string,
131+
sqrtPriceX96: string,
132+
WBNB: string,
133+
sqrtPriceCalculator: string
134+
) {
135+
const BNBPartyFactoryContract = await ethers.getContractFactory("BNBPartyFactory")
136+
return BNBPartyFactoryContract.deploy(
137+
{
138+
partyTarget: partyTarget,
139+
createTokenFee: tokenCreationFee,
140+
partyLpFee: FeeAmount.HIGH,
141+
lpFee: FeeAmount.HIGH,
142+
initialTokenAmount: initialTokenAmount,
143+
sqrtPriceX96: sqrtPriceX96,
144+
bonusTargetReach: returnFeeAmount,
145+
bonusPartyCreator: bonusFee,
146+
targetReachFee: targetReachFee,
147+
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
148+
lpTicks: { tickLower: "-214200", tickUpper: "201400" },
149+
},
150+
WBNB,
151+
sqrtPriceCalculator
152+
)
153+
}

0 commit comments

Comments
 (0)