Skip to content

Commit a14d9f8

Browse files
Merge pull request #62 from bnb-party/issue_61
add maxAndMinWBNB
2 parents 41fed5a + 10fca9f commit a14d9f8

File tree

7 files changed

+96
-66
lines changed

7 files changed

+96
-66
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ npx truffle dashboard
5151
npx hardhat run ./scripts/deploy.ts --network truffleDashboard
5252
```
5353

54+
**Run Curve tests:**
55+
56+
```
57+
npx hardhat run ./scripts/curve.ts
58+
```
59+
5460
## Create Party
5561

5662
Welcome to the exciting world of liquidity and token creation! With the `Create Party`, you can effortlessly launch your very own liquidity party, complete with a fresh new token and an initial liquidity pool. Here’s how it works:

scripts/curve.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ethers } from "hardhat"
2-
import { FeeAmount, v3PartyFactory, deployContracts, weth9, bnbPartyFactory, BNBPositionManager, v3Factory, positionManager } from "../test/helper"
2+
import { FeeAmount, v3PartyFactory, deployContracts, bnbPartyFactory, BNBPositionManager, v3Factory, positionManager, maxAndMinWBNB } from "../test/helper"
33
import { IUniswapV3Pool } from "../typechain-types"
44
import BigNumber from "bignumber.js"
55
import * as csvWriter from "csv-writer"
@@ -25,14 +25,13 @@ const csv = createCsvWriter({
2525
],
2626
})
2727

28-
async function createLiquidityPool() {
28+
async function createLiquidityPool(wbnbAddress: string) {
2929
const tokenCreationFee = ethers.parseUnits("1", 16)
3030
await bnbPartyFactory.createParty("MEME", "MEME", { value: tokenCreationFee })
3131
const tokenId = await BNBPositionManager.totalSupply()
3232
const position = await BNBPositionManager.positions(tokenId)
3333

34-
const wethAddress = await weth9.getAddress()
35-
const MEME = position.token1 === wethAddress ? position.token0 : position.token1
34+
const MEME = position.token1 === wbnbAddress ? position.token0 : position.token1
3635
return { MEME, position }
3736
}
3837

@@ -45,11 +44,12 @@ function calculatePrices(sqrtPriceX96: BigNumber, token0: string, token1: string
4544
: { priceMemeInWbnb: priceToken1InToken0, priceWbnbInMeme: priceToken0InToken1 }
4645
}
4746

48-
async function getTokenBalances(lpAddress: string, token: any) {
47+
async function getTokenBalances(lpAddress: string, token: any, wbnbAddress: string) {
48+
const wbnb = await ethers.getContractAt("IWBNB", wbnbAddress)
4949
const [MEMEAmount, WBNBAmount, wethAddress] = await Promise.all([
5050
token.balanceOf(lpAddress),
51-
weth9.balanceOf(lpAddress),
52-
weth9.getAddress(),
51+
wbnb.balanceOf(lpAddress),
52+
wbnb.getAddress(),
5353
])
5454

5555
const lpPool = await ethers.getContractAt("UniswapV3Pool", lpAddress)
@@ -108,32 +108,34 @@ async function logData(
108108

109109
async function test() {
110110
const target = ethers.parseEther("13")
111-
await deployContracts(target)
112-
const { MEME, position } = await createLiquidityPool()
111+
const wbnbAddresses = await maxAndMinWBNB()
112+
const wbnbAddress = wbnbAddresses.maxAddress
113+
await deployContracts(target, wbnbAddress)
114+
const { MEME, position } = await createLiquidityPool(wbnbAddress)
113115
const token = await ethers.getContractAt("ERC20Token", MEME)
114116
const lpAddress = await v3PartyFactory.getPool(position.token0, position.token1, FeeAmount.HIGH)
115117
lpContract = (await ethers.getContractAt("UniswapV3Pool", lpAddress)) as any as IUniswapV3Pool
116118

117-
const { MEMEAmount: initialMEMEAmount, } = await getTokenBalances(lpAddress, token)
119+
const { MEMEAmount: initialMEMEAmount, } = await getTokenBalances(lpAddress, token, wbnbAddress)
118120
const segments = 26
119121
for (let i = 0; i <= segments; ++i) {
120122
const swapAmount = ethers.parseUnits("5.06", 17)
121123
if( i !== 0) await bnbPartyFactory.joinParty(MEME, 0, { value: swapAmount })
122124
const isParty = await bnbPartyFactory.isTokenOnPartyLP(MEME)
123125
if (isParty) {
124-
const { MEMEAmount, WBNBAmount } = await getTokenBalances(lpAddress, token)
126+
const { MEMEAmount, WBNBAmount } = await getTokenBalances(lpAddress, token, wbnbAddress)
125127
const slot0 = await lpContract.slot0()
126128
const sqrtPriceX96 = new BigNumber(slot0.sqrtPriceX96.toString())
127129
const { priceMemeInWbnb, priceWbnbInMeme } = calculatePrices(sqrtPriceX96, await lpContract.token0(), await lpContract.token1(), MEME)
128130
await logData(i, MEMEAmount, WBNBAmount, sqrtPriceX96, priceMemeInWbnb, priceWbnbInMeme, initialMEMEAmount)
129131
}
130132
else {
131-
const newLPPool = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
133+
const newLPPool = await v3Factory.getPool(wbnbAddress, MEME, FeeAmount.HIGH)
132134
const lpContract = (await ethers.getContractAt("UniswapV3Pool", newLPPool)) as any as IUniswapV3Pool
133135
const slot0 = await lpContract.slot0()
134136
const sqrtPriceX96 = new BigNumber(slot0.sqrtPriceX96.toString())
135137
const { priceMemeInWbnb, priceWbnbInMeme } = calculatePrices(sqrtPriceX96, await lpContract.token0(), await lpContract.token1(), MEME)
136-
const { MEMEAmount, WBNBAmount } = await getTokenBalances(newLPPool, token)
138+
const { MEMEAmount, WBNBAmount } = await getTokenBalances(newLPPool, token, wbnbAddress)
137139
await logData(i, MEMEAmount, WBNBAmount, sqrtPriceX96, priceMemeInWbnb, priceWbnbInMeme, initialMEMEAmount)
138140
}
139141
}

test/BNBPartyFactory.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
positionManager,
1212
BNBPositionManager,
1313
BNBSwapRouter,
14-
weth9,
14+
wbnb,
1515
deployContracts,
1616
deployBNBPartyFactory,
1717
} from "./helper"
@@ -41,7 +41,7 @@ describe("BNBPartyFactory", function () {
4141
expect((await bnbPartyFactory.party()).partyTarget).to.equal(partyTarget)
4242
expect((await bnbPartyFactory.party()).initialTokenAmount).to.equal(initialTokenAmount)
4343
expect((await bnbPartyFactory.party()).sqrtPriceX96).to.equal(sqrtPriceX96)
44-
expect(await bnbPartyFactory.WBNB()).to.equal(await weth9.getAddress())
44+
expect(await bnbPartyFactory.WBNB()).to.equal(await wbnb.getAddress())
4545
expect((await bnbPartyFactory.party()).bonusTargetReach).to.equal(returnFeeAmount)
4646
expect((await bnbPartyFactory.party()).bonusPartyCreator).to.equal(bonusFee)
4747
expect((await bnbPartyFactory.party()).lpFee).to.equal(FeeAmount.HIGH)
@@ -101,7 +101,7 @@ describe("BNBPartyFactory", function () {
101101
await bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
102102
tokenId = ((await BNBPositionManager.totalSupply()) - 1n).toString()
103103
position = await BNBPositionManager.positions(tokenId)
104-
MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
104+
MEME = position.token1 == (await wbnb.getAddress()) ? position.token0 : position.token1
105105
})
106106

107107
it("should create second liquidity pool", async () => {
@@ -131,18 +131,18 @@ describe("BNBPartyFactory", function () {
131131

132132
it("should send WBNB to new LP", async () => {
133133
await bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget })
134-
const lpAddress = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
135-
const balance = await weth9.balanceOf(lpAddress)
134+
const lpAddress = await v3Factory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
135+
const balance = await wbnb.balanceOf(lpAddress)
136136
const percentFee = ethers.parseEther("0.14") // target 13 + 1 BNB - 1% fee
137137
expect(balance).to.be.equal(BNBToTarget - returnFeeAmount - bonusFee - targetReachFee - percentFee - 1n)
138138
})
139139

140140
it("should send MEME to new LP", async () => {
141141
const token = await ethers.getContractAt("ERC20Token", MEME)
142-
const oldLPPool = await v3PartyFactory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
142+
const oldLPPool = await v3PartyFactory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
143143
await bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget })
144144
const oldPoolBalance = await token.balanceOf(oldLPPool)
145-
const newLPPool = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
145+
const newLPPool = await v3Factory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
146146
const newBalance = await token.balanceOf(newLPPool)
147147
const userBalance = await token.balanceOf(await signers[0].getAddress())
148148
const totalSupply = await token.totalSupply()

test/SwapRouter.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
v3PartyFactory,
1010
BNBPositionManager,
1111
BNBSwapRouter,
12-
weth9,
12+
wbnb,
1313
deployContracts,
1414
} from "./helper"
1515

@@ -33,18 +33,18 @@ describe("Smart Router", function () {
3333
tokenId = (await BNBPositionManager.totalSupply()).toString()
3434
deadline = Math.floor(Date.now() / 1000) + 60 * 20 // 20 minutes from now
3535
position = await BNBPositionManager.positions(tokenId)
36-
MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
37-
lpAddress = await v3PartyFactory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
36+
MEME = position.token1 == (await wbnb.getAddress()) ? position.token0 : position.token1
37+
lpAddress = await v3PartyFactory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
3838
MEMEToken = await ethers.getContractAt("ERC20", MEME)
3939
await MEMEToken.approve(await bnbPartyFactory.getAddress(), ethers.parseEther("1000000"))
4040
await MEMEToken.approve(await BNBSwapRouter.getAddress(), ethers.parseEther("10000000"))
4141
})
4242

4343
it("should increase wbnb on party lp after join party", async () => {
4444
const amountIn = ethers.parseUnits("5", 17)
45-
const lpBalanceBefore = await weth9.balanceOf(lpAddress)
45+
const lpBalanceBefore = await wbnb.balanceOf(lpAddress)
4646
await bnbPartyFactory.joinParty(MEME, 0, { value: amountIn })
47-
const lpBalanceAfter = await weth9.balanceOf(lpAddress)
47+
const lpBalanceAfter = await wbnb.balanceOf(lpAddress)
4848
expect(lpBalanceAfter).to.be.equal(lpBalanceBefore + amountIn)
4949
})
5050

@@ -75,16 +75,16 @@ describe("Smart Router", function () {
7575
it("should deacrease wbnb on party lp after leave party", async () => {
7676
const amountIn = ethers.parseUnits("1", 16)
7777

78-
const lpBalanceBefore = await weth9.balanceOf(lpAddress)
78+
const lpBalanceBefore = await wbnb.balanceOf(lpAddress)
7979
await bnbPartyFactory.leaveParty(MEME, amountIn, 0)
80-
const lpBalanceAfter = await weth9.balanceOf(lpAddress)
80+
const lpBalanceAfter = await wbnb.balanceOf(lpAddress)
8181

8282
expect(lpBalanceBefore).to.be.gt(lpBalanceAfter)
8383
})
8484

8585
it("BNB -> WBNB -> MEME exactInput call", async () => {
8686
const amountIn = ethers.parseUnits("1", 18)
87-
const path = getDataHexString(await weth9.getAddress(), MEME)
87+
const path = getDataHexString(await wbnb.getAddress(), MEME)
8888

8989
const params = {
9090
path: path,
@@ -95,16 +95,16 @@ describe("Smart Router", function () {
9595
}
9696

9797
const balanceBefore = await MEMEToken.balanceOf(await signers[0].getAddress())
98-
await expect(await BNBSwapRouter.exactInput(params, { value: amountIn })).to.emit(weth9, "Deposit")
98+
await expect(await BNBSwapRouter.exactInput(params, { value: amountIn })).to.emit(wbnb, "Deposit")
9999
const balanceAfter = await MEMEToken.balanceOf(await signers[0].getAddress())
100100

101101
expect(balanceAfter).to.be.gt(balanceBefore)
102102
})
103103

104104
it("MEME -> WBNB -> BNB multicall", async function () {
105105
const amountIn = ethers.parseUnits("1", 17)
106-
const MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
107-
const path = getDataHexString(MEME, await weth9.getAddress())
106+
const MEME = position.token1 == (await wbnb.getAddress()) ? position.token0 : position.token1
107+
const path = getDataHexString(MEME, await wbnb.getAddress())
108108

109109
const params = {
110110
path: path,
@@ -121,19 +121,19 @@ describe("Smart Router", function () {
121121
await signers[1].getAddress(),
122122
])
123123
const balanceBefore = await ethers.provider.getBalance(await signers[1].getAddress())
124-
await expect(await BNBSwapRouter.multicall([exactInputData, unwrapWETH9Data])).to.emit(weth9, "Withdrawal")
124+
await expect(await BNBSwapRouter.multicall([exactInputData, unwrapWETH9Data])).to.emit(wbnb, "Withdrawal")
125125
const balanceAfter = await ethers.provider.getBalance(await signers[1].getAddress())
126126
expect(balanceAfter).to.be.gt(balanceBefore)
127127
})
128128

129129
it("WBNB -> MEME exactInput call", async () => {
130130
const amountIn = ethers.parseUnits("1", 17)
131131
const amountOutMinimum = 0 // For testing, accept any amount out
132-
const path = getDataHexString(await weth9.getAddress(), MEME)
132+
const path = getDataHexString(await wbnb.getAddress(), MEME)
133133

134134
const deadline = Math.floor(Date.now() / 1000) + 60 * 20 // 20 minutes from now
135-
await weth9.deposit({ value: amountIn })
136-
await weth9.approve(await BNBSwapRouter.getAddress(), amountIn)
135+
await wbnb.deposit({ value: amountIn })
136+
await wbnb.approve(await BNBSwapRouter.getAddress(), amountIn)
137137

138138
const params = {
139139
path: path,
@@ -156,9 +156,9 @@ describe("Smart Router", function () {
156156
await tx.wait()
157157
const events = await bnbPartyFactory.queryFilter(bnbPartyFactory.filters["StartParty(address,address,address)"])
158158
const tokenAddress = events[events.length - 1].args.tokenAddress
159-
const lpAddress = await v3PartyFactory.getPool(await weth9.getAddress(), tokenAddress, FeeAmount.HIGH)
159+
const lpAddress = await v3PartyFactory.getPool(await wbnb.getAddress(), tokenAddress, FeeAmount.HIGH)
160160
// check liquidity pool balance
161-
const liquidityPoolBalance = await weth9.balanceOf(lpAddress)
161+
const liquidityPoolBalance = await wbnb.balanceOf(lpAddress)
162162
expect(liquidityPoolBalance).to.be.equal(amountIn - tokenCreationFee)
163163
})
164164

test/WithdrawFee.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "chai"
22
import { ethers } from "hardhat"
33
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
44
import { IUniswapV3Pool } from "../typechain-types"
5-
import { FeeAmount, bnbPartyFactory, v3PartyFactory, BNBPositionManager, weth9, deployContracts, v3Factory, positionManager } from "./helper"
5+
import { FeeAmount, bnbPartyFactory, v3PartyFactory, BNBPositionManager, wbnb, deployContracts, v3Factory, positionManager } from "./helper"
66

77
describe("Withdraw fees", function () {
88
let MEME: string
@@ -23,8 +23,8 @@ describe("Withdraw fees", function () {
2323
await bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
2424
tokenId = (await BNBPositionManager.totalSupply()).toString()
2525
position = await BNBPositionManager.positions(tokenId)
26-
MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
27-
lpAddress = await v3PartyFactory.getPool(MEME, await weth9.getAddress(), FeeAmount.HIGH)
26+
MEME = position.token1 == (await wbnb.getAddress()) ? position.token0 : position.token1
27+
lpAddress = await v3PartyFactory.getPool(MEME, await wbnb.getAddress(), FeeAmount.HIGH)
2828
})
2929

3030
it("should withdraw token creation fee", async () => {
@@ -45,15 +45,15 @@ describe("Withdraw fees", function () {
4545
await bnbPartyFactory.connect(signers[1]).createParty(name, symbol, { value: tokenCreationFee })
4646
tokenId = (await BNBPositionManager.totalSupply()).toString()
4747
position = await BNBPositionManager.positions(tokenId)
48-
MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
48+
MEME = position.token1 == (await wbnb.getAddress()) ? position.token0 : position.token1
4949
await bnbPartyFactory.connect(signers[1]).joinParty(MEME, 0, { value: ethers.parseEther("10") })
5050
position = await BNBPositionManager.positions(tokenId)
5151
// 1% bnb party fee 10 ether = 0.1 ether
5252
const expectedFee = ethers.parseEther("0.1")
53-
const balanceBefore = await weth9.balanceOf(await signers[0].getAddress())
54-
const partyLP = await v3PartyFactory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
53+
const balanceBefore = await wbnb.balanceOf(await signers[0].getAddress())
54+
const partyLP = await v3PartyFactory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
5555
await bnbPartyFactory.withdrawPartyLPFee([partyLP])
56-
const balanceAfter = await weth9.balanceOf(await signers[0].getAddress())
56+
const balanceAfter = await wbnb.balanceOf(await signers[0].getAddress())
5757
expect(balanceAfter).to.be.equal(balanceBefore + expectedFee)
5858
})
5959

@@ -68,13 +68,13 @@ describe("Withdraw fees", function () {
6868
it("should return fee from second lp", async () => {
6969
await bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget }) // create second lp
7070
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)
71+
const secondLP = await v3Factory.getPool(MEME, await wbnb.getAddress(), FeeAmount.HIGH)
7272
const lpPool = (await ethers.getContractAt("UniswapV3Pool", secondLP)) as any as IUniswapV3Pool
7373
const token0 = await lpPool.token0()
7474
await bnbPartyFactory.withdrawLPFee([secondLP])
7575
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(secondLP, positionManager)
7676
const fee = collectedFee.feeGrowthInside0LastX128 == 0n ? collectedFee.feeGrowthInside1LastX128 : collectedFee.feeGrowthInside0LastX128
77-
if (token0 == (await weth9.getAddress())) {
77+
if (token0 == (await wbnb.getAddress())) {
7878
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128()
7979
expect(feeGrowthGlobalX128).to.be.deep.equal(fee)
8080
} else {
@@ -134,7 +134,7 @@ describe("Withdraw fees", function () {
134134
await bnbPartyFactory.withdrawPartyLPFee([lpAddress])
135135
let liquidity = await lpPool.liquidity()
136136
let feeGrowthGlobalX128 =
137-
position.token1 == (await weth9.getAddress())
137+
position.token1 == (await wbnb.getAddress())
138138
? await lpPool.feeGrowthGlobal1X128()
139139
: await lpPool.feeGrowthGlobal0X128()
140140
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(lpAddress, BNBPositionManager)

0 commit comments

Comments
 (0)