Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add maxAndMinWBNB #62

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ npx truffle dashboard
npx hardhat run ./scripts/deploy.ts --network truffleDashboard
```

**Run Curve tests:**

```
npx hardhat run ./scripts/curve.ts
```

## Create Party

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:
Expand Down
28 changes: 15 additions & 13 deletions scripts/curve.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from "hardhat"
import { FeeAmount, v3PartyFactory, deployContracts, weth9, bnbPartyFactory, BNBPositionManager, v3Factory, positionManager } from "../test/helper"
import { FeeAmount, v3PartyFactory, deployContracts, bnbPartyFactory, BNBPositionManager, v3Factory, positionManager, maxAndMinWBNB } from "../test/helper"
import { IUniswapV3Pool } from "../typechain-types"
import BigNumber from "bignumber.js"
import * as csvWriter from "csv-writer"
Expand All @@ -25,14 +25,13 @@ const csv = createCsvWriter({
],
})

async function createLiquidityPool() {
async function createLiquidityPool(wbnbAddress: string) {
const tokenCreationFee = ethers.parseUnits("1", 16)
await bnbPartyFactory.createParty("MEME", "MEME", { value: tokenCreationFee })
const tokenId = await BNBPositionManager.totalSupply()
const position = await BNBPositionManager.positions(tokenId)

const wethAddress = await weth9.getAddress()
const MEME = position.token1 === wethAddress ? position.token0 : position.token1
const MEME = position.token1 === wbnbAddress ? position.token0 : position.token1
return { MEME, position }
}

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

async function getTokenBalances(lpAddress: string, token: any) {
async function getTokenBalances(lpAddress: string, token: any, wbnbAddress: string) {
const wbnb = await ethers.getContractAt("IWBNB", wbnbAddress)
const [MEMEAmount, WBNBAmount, wethAddress] = await Promise.all([
token.balanceOf(lpAddress),
weth9.balanceOf(lpAddress),
weth9.getAddress(),
wbnb.balanceOf(lpAddress),
wbnb.getAddress(),
])

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

async function test() {
const target = ethers.parseEther("13")
await deployContracts(target)
const { MEME, position } = await createLiquidityPool()
const wbnbAddresses = await maxAndMinWBNB()
const wbnbAddress = wbnbAddresses.maxAddress
await deployContracts(target, wbnbAddress)
const { MEME, position } = await createLiquidityPool(wbnbAddress)
const token = await ethers.getContractAt("ERC20Token", MEME)
const lpAddress = await v3PartyFactory.getPool(position.token0, position.token1, FeeAmount.HIGH)
lpContract = (await ethers.getContractAt("UniswapV3Pool", lpAddress)) as any as IUniswapV3Pool

const { MEMEAmount: initialMEMEAmount, } = await getTokenBalances(lpAddress, token)
const { MEMEAmount: initialMEMEAmount, } = await getTokenBalances(lpAddress, token, wbnbAddress)
const segments = 26
for (let i = 0; i <= segments; ++i) {
const swapAmount = ethers.parseUnits("5.06", 17)
if( i !== 0) await bnbPartyFactory.joinParty(MEME, 0, { value: swapAmount })
const isParty = await bnbPartyFactory.isTokenOnPartyLP(MEME)
if (isParty) {
const { MEMEAmount, WBNBAmount } = await getTokenBalances(lpAddress, token)
const { MEMEAmount, WBNBAmount } = await getTokenBalances(lpAddress, token, wbnbAddress)
const slot0 = await lpContract.slot0()
const sqrtPriceX96 = new BigNumber(slot0.sqrtPriceX96.toString())
const { priceMemeInWbnb, priceWbnbInMeme } = calculatePrices(sqrtPriceX96, await lpContract.token0(), await lpContract.token1(), MEME)
await logData(i, MEMEAmount, WBNBAmount, sqrtPriceX96, priceMemeInWbnb, priceWbnbInMeme, initialMEMEAmount)
}
else {
const newLPPool = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
const newLPPool = await v3Factory.getPool(wbnbAddress, MEME, FeeAmount.HIGH)
const lpContract = (await ethers.getContractAt("UniswapV3Pool", newLPPool)) as any as IUniswapV3Pool
const slot0 = await lpContract.slot0()
const sqrtPriceX96 = new BigNumber(slot0.sqrtPriceX96.toString())
const { priceMemeInWbnb, priceWbnbInMeme } = calculatePrices(sqrtPriceX96, await lpContract.token0(), await lpContract.token1(), MEME)
const { MEMEAmount, WBNBAmount } = await getTokenBalances(newLPPool, token)
const { MEMEAmount, WBNBAmount } = await getTokenBalances(newLPPool, token, wbnbAddress)
await logData(i, MEMEAmount, WBNBAmount, sqrtPriceX96, priceMemeInWbnb, priceWbnbInMeme, initialMEMEAmount)
}
}
Expand Down
14 changes: 7 additions & 7 deletions test/BNBPartyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
positionManager,
BNBPositionManager,
BNBSwapRouter,
weth9,
wbnb,
deployContracts,
deployBNBPartyFactory,
} from "./helper"
Expand Down Expand Up @@ -41,7 +41,7 @@ describe("BNBPartyFactory", function () {
expect((await bnbPartyFactory.party()).partyTarget).to.equal(partyTarget)
expect((await bnbPartyFactory.party()).initialTokenAmount).to.equal(initialTokenAmount)
expect((await bnbPartyFactory.party()).sqrtPriceX96).to.equal(sqrtPriceX96)
expect(await bnbPartyFactory.WBNB()).to.equal(await weth9.getAddress())
expect(await bnbPartyFactory.WBNB()).to.equal(await wbnb.getAddress())
expect((await bnbPartyFactory.party()).bonusTargetReach).to.equal(returnFeeAmount)
expect((await bnbPartyFactory.party()).bonusPartyCreator).to.equal(bonusFee)
expect((await bnbPartyFactory.party()).lpFee).to.equal(FeeAmount.HIGH)
Expand Down Expand Up @@ -101,7 +101,7 @@ describe("BNBPartyFactory", function () {
await bnbPartyFactory.createParty(name, symbol, { value: tokenCreationFee })
tokenId = ((await BNBPositionManager.totalSupply()) - 1n).toString()
position = await BNBPositionManager.positions(tokenId)
MEME = position.token1 == (await weth9.getAddress()) ? position.token0 : position.token1
MEME = position.token1 == (await wbnb.getAddress()) ? position.token0 : position.token1
})

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

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

it("should send MEME to new LP", async () => {
const token = await ethers.getContractAt("ERC20Token", MEME)
const oldLPPool = await v3PartyFactory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
const oldLPPool = await v3PartyFactory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
await bnbPartyFactory.joinParty(MEME, 0, { value: BNBToTarget })
const oldPoolBalance = await token.balanceOf(oldLPPool)
const newLPPool = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
const newLPPool = await v3Factory.getPool(await wbnb.getAddress(), MEME, FeeAmount.HIGH)
const newBalance = await token.balanceOf(newLPPool)
const userBalance = await token.balanceOf(await signers[0].getAddress())
const totalSupply = await token.totalSupply()
Expand Down
34 changes: 17 additions & 17 deletions test/SwapRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
v3PartyFactory,
BNBPositionManager,
BNBSwapRouter,
weth9,
wbnb,
deployContracts,
} from "./helper"

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

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

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

const lpBalanceBefore = await weth9.balanceOf(lpAddress)
const lpBalanceBefore = await wbnb.balanceOf(lpAddress)
await bnbPartyFactory.leaveParty(MEME, amountIn, 0)
const lpBalanceAfter = await weth9.balanceOf(lpAddress)
const lpBalanceAfter = await wbnb.balanceOf(lpAddress)

expect(lpBalanceBefore).to.be.gt(lpBalanceAfter)
})

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

const params = {
path: path,
Expand All @@ -95,16 +95,16 @@ describe("Smart Router", function () {
}

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

expect(balanceAfter).to.be.gt(balanceBefore)
})

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

const params = {
path: path,
Expand All @@ -121,19 +121,19 @@ describe("Smart Router", function () {
await signers[1].getAddress(),
])
const balanceBefore = await ethers.provider.getBalance(await signers[1].getAddress())
await expect(await BNBSwapRouter.multicall([exactInputData, unwrapWETH9Data])).to.emit(weth9, "Withdrawal")
await expect(await BNBSwapRouter.multicall([exactInputData, unwrapWETH9Data])).to.emit(wbnb, "Withdrawal")
const balanceAfter = await ethers.provider.getBalance(await signers[1].getAddress())
expect(balanceAfter).to.be.gt(balanceBefore)
})

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

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

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

Expand Down
20 changes: 10 additions & 10 deletions test/WithdrawFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "chai"
import { ethers } from "hardhat"
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
import { IUniswapV3Pool } from "../typechain-types"
import { FeeAmount, bnbPartyFactory, v3PartyFactory, BNBPositionManager, weth9, deployContracts, v3Factory, positionManager } from "./helper"
import { FeeAmount, bnbPartyFactory, v3PartyFactory, BNBPositionManager, wbnb, deployContracts, v3Factory, positionManager } from "./helper"

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

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

Expand All @@ -68,13 +68,13 @@ describe("Withdraw fees", function () {
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 secondLP = await v3Factory.getPool(MEME, await wbnb.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())) {
if (token0 == (await wbnb.getAddress())) {
const feeGrowthGlobalX128 = await lpPool.feeGrowthGlobal0X128()
expect(feeGrowthGlobalX128).to.be.deep.equal(fee)
} else {
Expand Down Expand Up @@ -134,7 +134,7 @@ describe("Withdraw fees", function () {
await bnbPartyFactory.withdrawPartyLPFee([lpAddress])
let liquidity = await lpPool.liquidity()
let feeGrowthGlobalX128 =
position.token1 == (await weth9.getAddress())
position.token1 == (await wbnb.getAddress())
? await lpPool.feeGrowthGlobal1X128()
: await lpPool.feeGrowthGlobal0X128()
const collectedFee = await bnbPartyFactory.getFeeGrowthInsideLastX128(lpAddress, BNBPositionManager)
Expand Down
Loading