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 2 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
21 changes: 11 additions & 10 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,13 +25,12 @@ const csv = createCsvWriter({
],
})

async function createLiquidityPool() {
async function createLiquidityPool(wethAddress: 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
return { MEME, position }
}
Expand All @@ -45,7 +44,8 @@ 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, weth9Address: string) {
const weth9 = await ethers.getContractAt("IWBNB", weth9Address)
const [MEMEAmount, WBNBAmount, wethAddress] = await Promise.all([
token.balanceOf(lpAddress),
weth9.balanceOf(lpAddress),
Expand Down Expand Up @@ -108,32 +108,33 @@ async function logData(

async function test() {
const target = ethers.parseEther("13")
await deployContracts(target)
const { MEME, position } = await createLiquidityPool()
const wbnbAddresses = await maxAndMinWBNB()
await deployContracts(target, wbnbAddresses.maxAddress)
const { MEME, position } = await createLiquidityPool(wbnbAddresses.maxAddress)
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, wbnbAddresses.maxAddress)
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, wbnbAddresses.maxAddress)
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(wbnbAddresses.maxAddress, 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, wbnbAddresses.maxAddress)
await logData(i, MEMEAmount, WBNBAmount, sqrtPriceX96, priceMemeInWbnb, priceWbnbInMeme, initialMEMEAmount)
}
}
Expand Down
42 changes: 32 additions & 10 deletions test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ethers } from "hardhat"
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"
import { BNBPartyFactory } from "../typechain-types/contracts/BNBPartyFactory"
import { UniswapV3Factory } from "../typechain-types/@bnb-party/v3-core/contracts/UniswapV3Factory"
import { NonfungiblePositionManager } from "../typechain-types/@bnb-party/v3-periphery/contracts/NonfungiblePositionManager"
Expand Down Expand Up @@ -37,16 +36,18 @@ export let BNBSwapRouter: SwapRouter
export let swapRouter: SwapRouter
export let weth9: IWBNB

export async function deployContracts(partyTarget = ethers.parseEther("90")) {
export async function deployContracts(partyTarget = ethers.parseEther("90"), weth9Address: string = "") {
const tokenCreationFee = ethers.parseUnits("1", 16) // 0.01 BNB token creation fee
const returnFeeAmount = ethers.parseUnits("5", 16) // 0.05 BNB return fee (bonusTargetReach)
const bonusFee = ethers.parseUnits("1", 17) // 0.01 BNB bonus fee (bonusPartyCreator)
const bonusFee = ethers.parseUnits("1", 17) // 0.1 BNB bonus fee (bonusPartyCreator)
const targetReachFee = ethers.parseUnits("8.5", 17) // 0.85 BNB target reach fee
const initialTokenAmount = "1000000000000000000000000000"
const sqrtPriceX96 = "1252685732681638336686364"
// Deploy WETH9
const WETH9 = await ethers.getContractFactory(WETH9Artifact.abi, WETH9Artifact.bytecode)
weth9 = (await WETH9.deploy()) as IWBNB
if (weth9Address === "") {
weth9 = await deployWBNB()
weth9Address = await weth9.getAddress()
}
const sqrtPriceCalculatorContract = await ethers.getContractFactory("SqrtPriceCalculator")
const sqrtPriceCalculator = (await sqrtPriceCalculatorContract.deploy()) as SqrtPriceCalculator
// Deploy BNBPartyFactory
Expand All @@ -65,7 +66,7 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) {
partyTicks: { tickLower: "-214200", tickUpper: "195600" },
lpTicks: { tickLower: "-214200", tickUpper: "201400" },
},
await weth9.getAddress(),
weth9Address,
await sqrtPriceCalculator.getAddress()
)) as BNBPartyFactory

Expand All @@ -90,26 +91,26 @@ export async function deployContracts(partyTarget = ethers.parseEther("90")) {
)
positionManager = (await ManagerContract.deploy(
await v3Factory.getAddress(),
await weth9.getAddress(),
weth9Address,
await tokenPositionDescriptor.getAddress()
)) as NonfungiblePositionManager

const PositionManagerContract = await ethers.getContractFactory("NonfungiblePositionManager")
BNBPositionManager = (await PositionManagerContract.deploy(
await v3PartyFactory.getAddress(),
await weth9.getAddress(),
weth9Address,
await tokenPositionDescriptor.getAddress()
)) as NonfungiblePositionManager

// Deploy Swap Router
const SwapRouterContract = await ethers.getContractFactory("SwapRouter")
BNBSwapRouter = (await SwapRouterContract.deploy(
await v3PartyFactory.getAddress(),
await weth9.getAddress()
weth9Address
)) as SwapRouter

const routerContract = await ethers.getContractFactory(ClassicSwapRouter.abi, ClassicSwapRouter.bytecode)
swapRouter = (await routerContract.deploy(await v3Factory.getAddress(), await weth9.getAddress())) as SwapRouter
swapRouter = (await routerContract.deploy(await v3Factory.getAddress(), weth9Address)) as SwapRouter

// Set Position Manager in BNBPartyFactory
await bnbPartyFactory.setNonfungiblePositionManager(
Expand Down Expand Up @@ -151,3 +152,24 @@ export async function deployBNBPartyFactory(
sqrtPriceCalculator
)
}

export async function maxAndMinWBNB() {
const deploymentCount = 100;
let maxAddress = ethers.ZeroAddress;
let minAddress = '0xffffffffffffffffffffffffffffffffffffffff'; // A large value to start with

for (let i = 0; i < deploymentCount; i++) {
const wbnb = await deployWBNB();
const address = await wbnb.getAddress();

// Update maxAddress and minAddress based on comparison
maxAddress = address > maxAddress ? address : maxAddress;
minAddress = address < minAddress ? address : minAddress;
}
return { maxAddress: maxAddress, minAddress: minAddress };
}

async function deployWBNB(): Promise<IWBNB> {
const WBNBFactory = await ethers.getContractFactory(WETH9Artifact.abi, WETH9Artifact.bytecode)
return (await WBNBFactory.deploy()) as IWBNB
}