1
1
import { ethers } from "hardhat"
2
- import { FeeAmount , v3PartyFactory , deployContracts , bnbPartyFactory , BNBPositionManager , v3Factory , positionManager , maxAndMinWBNB } from "../test/helper"
2
+ import { FeeAmount , v3PartyFactory , deployContracts , bnbPartyFactory , positionManager , BNBPositionManager , v3Factory , maxAndMinWBNB } from "../test/helper"
3
3
import { IUniswapV3Pool } from "../typechain-types"
4
4
import BigNumber from "bignumber.js"
5
5
import * as csvWriter from "csv-writer"
6
+ import { getPrice } from "./get-price"
6
7
7
8
const BNB_PRICE = 600 // BNB price in USD
8
9
let lpContract : IUniswapV3Pool
@@ -15,8 +16,8 @@ const csv = createCsvWriter({
15
16
{ id : "MEMEAmount" , title : "MEME Amount" } ,
16
17
{ id : "WBNBAmount" , title : "WBNB Amount" } ,
17
18
{ id : "SqrtPriceX96" , title : "sqrtPriceX96" } ,
18
- { id : "priceMemeInWbnb" , title : "Price of MEME in WBNB " } ,
19
- { id : "priceWbnbInMeme" , title : "Price of WBNB in MEME " } ,
19
+ { id : "priceMemeInWbnb" , title : "Amount of WBNB required to purchase one unit of the MEME " } ,
20
+ { id : "priceWbnbInMeme" , title : "Amount of MEME tokens required to purchase one unit of WBNB " } ,
20
21
{ id : "wbnbValueInLp" , title : "WBNB Value in USD" } ,
21
22
{ id : "memeValueInLp" , title : "MEME Value in USD" } ,
22
23
{ id : "marketCap" , title : "Market Cap in USD" } ,
@@ -35,42 +36,36 @@ async function createLiquidityPool(wbnbAddress: string) {
35
36
return { MEME , position }
36
37
}
37
38
38
- function calculatePrices ( sqrtPriceX96 : BigNumber , token0 : string , token1 : string , meme : string ) {
39
- const priceX96 = sqrtPriceX96 . pow ( 2 )
40
- const priceToken0InToken1 = priceX96 . dividedBy ( new BigNumber ( 2 ) . pow ( 192 ) )
41
- const priceToken1InToken0 = new BigNumber ( 1 ) . div ( priceToken0InToken1 )
42
- return token0 === meme
43
- ? { priceMemeInWbnb : priceToken0InToken1 , priceWbnbInMeme : priceToken1InToken0 }
44
- : { priceMemeInWbnb : priceToken1InToken0 , priceWbnbInMeme : priceToken0InToken1 }
45
- }
46
-
47
39
async function getTokenBalances ( lpAddress : string , token : any , wbnbAddress : string ) {
48
40
const wbnb = await ethers . getContractAt ( "IWBNB" , wbnbAddress )
49
- const [ MEMEAmount , WBNBAmount , wethAddress ] = await Promise . all ( [
50
- token . balanceOf ( lpAddress ) ,
51
- wbnb . balanceOf ( lpAddress ) ,
52
- wbnb . getAddress ( ) ,
53
- ] )
41
+ const [ MEMEAmount , WBNBAmount ] = await Promise . all ( [ token . balanceOf ( lpAddress ) , wbnb . balanceOf ( lpAddress ) ] )
54
42
55
43
const lpPool = await ethers . getContractAt ( "UniswapV3Pool" , lpAddress )
56
44
const token0 = await lpPool . token0 ( )
57
- const isPartyPool = await bnbPartyFactory . isTokenOnPartyLP ( token0 === wethAddress ? await token . getAddress ( ) : token0 )
45
+ const isPartyPool = await bnbPartyFactory . isTokenOnPartyLP (
46
+ token0 === wbnbAddress ? await token . getAddress ( ) : token0
47
+ )
48
+
58
49
const [ feeGrowthGlobal0X128 , feeGrowthGlobal1X128 , liquidity , getFeeGlobal ] = await Promise . all ( [
59
50
lpPool . feeGrowthGlobal0X128 ( ) ,
60
51
lpPool . feeGrowthGlobal1X128 ( ) ,
61
52
lpPool . liquidity ( ) ,
62
53
bnbPartyFactory . getFeeGrowthInsideLastX128 ( lpAddress , isPartyPool ? BNBPositionManager : positionManager ) ,
63
54
] )
55
+
64
56
let wbnbFee , memeFee
65
- if ( token0 === wethAddress ) {
57
+ if ( token0 === wbnbAddress ) {
66
58
wbnbFee = await bnbPartyFactory . calculateFees ( liquidity , feeGrowthGlobal0X128 - getFeeGlobal . feeGrowthInside0LastX128 )
67
59
memeFee = await bnbPartyFactory . calculateFees ( liquidity , feeGrowthGlobal1X128 - getFeeGlobal . feeGrowthInside1LastX128 )
68
60
} else {
69
61
memeFee = await bnbPartyFactory . calculateFees ( liquidity , feeGrowthGlobal0X128 - getFeeGlobal . feeGrowthInside0LastX128 )
70
62
wbnbFee = await bnbPartyFactory . calculateFees ( liquidity , feeGrowthGlobal1X128 - getFeeGlobal . feeGrowthInside1LastX128 )
71
63
}
72
64
73
- return { WBNBAmount : new BigNumber ( ( WBNBAmount - wbnbFee ) . toString ( ) ) , MEMEAmount : new BigNumber ( ( MEMEAmount - memeFee ) . toString ( ) ) }
65
+ return {
66
+ WBNBAmount : new BigNumber ( ( WBNBAmount - wbnbFee ) . toString ( ) ) ,
67
+ MEMEAmount : new BigNumber ( ( MEMEAmount - memeFee ) . toString ( ) ) ,
68
+ }
74
69
}
75
70
76
71
async function logData (
@@ -84,7 +79,10 @@ async function logData(
84
79
) {
85
80
const wbnbValueUSD = WBNBAmount . div ( new BigNumber ( 10 ) . pow ( 18 ) ) . multipliedBy ( BNB_PRICE )
86
81
const memeValueUSD = MEMEAmount . div ( new BigNumber ( 10 ) . pow ( 18 ) ) . multipliedBy ( priceMemeInWbnb ) . multipliedBy ( BNB_PRICE )
87
- const marketCap = initialMEMEAmount . div ( new BigNumber ( 10 ) . pow ( 18 ) ) . multipliedBy ( priceMemeInWbnb ) . multipliedBy ( BNB_PRICE )
82
+ const marketCap = initialMEMEAmount
83
+ . div ( new BigNumber ( 10 ) . pow ( 18 ) )
84
+ . multipliedBy ( priceMemeInWbnb )
85
+ . multipliedBy ( BNB_PRICE )
88
86
const remainingMEMEPercentage = MEMEAmount . div ( initialMEMEAmount ) . multipliedBy ( 100 ) . toFixed ( 2 )
89
87
const memeMarketCapInBnb = initialMEMEAmount . div ( new BigNumber ( 10 ) . pow ( 18 ) ) . multipliedBy ( priceMemeInWbnb )
90
88
@@ -93,8 +91,8 @@ async function logData(
93
91
MEMEAmount : MEMEAmount . toString ( ) ,
94
92
WBNBAmount : WBNBAmount . toString ( ) ,
95
93
SqrtPriceX96 : sqrtPriceX96 . toString ( ) ,
96
- priceMemeInWbnb : priceMemeInWbnb . toString ( ) ,
97
- priceWbnbInMeme : priceWbnbInMeme . toString ( ) ,
94
+ priceMemeInWbnb : priceMemeInWbnb . toString ( 10 ) ,
95
+ priceWbnbInMeme : priceWbnbInMeme . toString ( 10 ) ,
98
96
wbnbValueInLp : wbnbValueUSD . toString ( ) ,
99
97
memeValueInLp : memeValueUSD . toString ( ) ,
100
98
marketCap : marketCap . toString ( ) ,
@@ -109,32 +107,43 @@ async function logData(
109
107
async function test ( ) {
110
108
const target = ethers . parseEther ( "13" )
111
109
const wbnbAddresses = await maxAndMinWBNB ( )
112
- const wbnbAddress = wbnbAddresses . maxAddress
110
+ const wbnbAddress = wbnbAddresses . minAddress
113
111
await deployContracts ( target , wbnbAddress )
114
112
const { MEME , position } = await createLiquidityPool ( wbnbAddress )
115
113
const token = await ethers . getContractAt ( "ERC20Token" , MEME )
116
114
const lpAddress = await v3PartyFactory . getPool ( position . token0 , position . token1 , FeeAmount . HIGH )
117
115
lpContract = ( await ethers . getContractAt ( "UniswapV3Pool" , lpAddress ) ) as any as IUniswapV3Pool
118
116
119
- const { MEMEAmount : initialMEMEAmount , } = await getTokenBalances ( lpAddress , token , wbnbAddress )
117
+ const { MEMEAmount : initialMEMEAmount } = await getTokenBalances ( lpAddress , token , wbnbAddress )
120
118
const segments = 26
121
119
for ( let i = 0 ; i <= segments ; ++ i ) {
122
120
const swapAmount = ethers . parseUnits ( "5.06" , 17 )
123
- if ( i !== 0 ) await bnbPartyFactory . joinParty ( MEME , 0 , { value : swapAmount } )
121
+ if ( i !== 0 ) await bnbPartyFactory . joinParty ( MEME , 0 , { value : swapAmount } )
124
122
const isParty = await bnbPartyFactory . isTokenOnPartyLP ( MEME )
125
123
if ( isParty ) {
126
124
const { MEMEAmount, WBNBAmount } = await getTokenBalances ( lpAddress , token , wbnbAddress )
127
125
const slot0 = await lpContract . slot0 ( )
128
126
const sqrtPriceX96 = new BigNumber ( slot0 . sqrtPriceX96 . toString ( ) )
129
- const { priceMemeInWbnb, priceWbnbInMeme } = calculatePrices ( sqrtPriceX96 , await lpContract . token0 ( ) , await lpContract . token1 ( ) , MEME )
127
+ let { priceToken0InToken1 : priceMemeInWbnb , priceToken1InToken0 : priceWbnbInMeme } = getPrice ( sqrtPriceX96 )
128
+ if ( wbnbAddress === wbnbAddresses . minAddress ) {
129
+ // replace the price of MEME in WBNB with the price of WBNB in MEME
130
+ const temp = priceMemeInWbnb
131
+ priceMemeInWbnb = priceWbnbInMeme
132
+ priceWbnbInMeme = temp
133
+ }
130
134
await logData ( i , MEMEAmount , WBNBAmount , sqrtPriceX96 , priceMemeInWbnb , priceWbnbInMeme , initialMEMEAmount )
131
- }
132
- else {
135
+ } else {
133
136
const newLPPool = await v3Factory . getPool ( wbnbAddress , MEME , FeeAmount . HIGH )
134
137
const lpContract = ( await ethers . getContractAt ( "UniswapV3Pool" , newLPPool ) ) as any as IUniswapV3Pool
135
138
const slot0 = await lpContract . slot0 ( )
136
139
const sqrtPriceX96 = new BigNumber ( slot0 . sqrtPriceX96 . toString ( ) )
137
- const { priceMemeInWbnb, priceWbnbInMeme } = calculatePrices ( sqrtPriceX96 , await lpContract . token0 ( ) , await lpContract . token1 ( ) , MEME )
140
+ let { priceToken0InToken1 : priceMemeInWbnb , priceToken1InToken0 : priceWbnbInMeme } = getPrice ( sqrtPriceX96 )
141
+ if ( wbnbAddress === wbnbAddresses . minAddress ) {
142
+ // replace the price of MEME in WBNB with the price of WBNB in MEME
143
+ const temp = priceMemeInWbnb
144
+ priceMemeInWbnb = priceWbnbInMeme
145
+ priceWbnbInMeme = temp
146
+ }
138
147
const { MEMEAmount, WBNBAmount } = await getTokenBalances ( newLPPool , token , wbnbAddress )
139
148
await logData ( i , MEMEAmount , WBNBAmount , sqrtPriceX96 , priceMemeInWbnb , priceWbnbInMeme , initialMEMEAmount )
140
149
}
0 commit comments