1
1
import { expect } from "chai"
2
2
import { ethers } from "hardhat"
3
3
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"
13
6
14
7
describe ( "Withdraw fees" , function ( ) {
15
8
let MEME : string
@@ -23,8 +16,6 @@ describe("Withdraw fees", function () {
23
16
const name = "Party"
24
17
const symbol = "Token"
25
18
const BNBToTarget : bigint = partyTarget + ethers . parseEther ( "1" )
26
- const tickLower = - 214200
27
- const tickUpper = 201400
28
19
29
20
beforeEach ( async ( ) => {
30
21
signers = await ethers . getSigners ( )
@@ -66,6 +57,32 @@ describe("Withdraw fees", function () {
66
57
expect ( balanceAfter ) . to . be . equal ( balanceBefore + expectedFee )
67
58
} )
68
59
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
+
69
86
it ( "should revert LPNotAtParty" , async ( ) => {
70
87
const mockContract = await ethers . getContractFactory ( "MockContract" )
71
88
const mock = await mockContract . deploy ( )
@@ -89,7 +106,7 @@ describe("Withdraw fees", function () {
89
106
const lpPool = ( await ethers . getContractAt ( "UniswapV3Pool" , lpAddress ) ) as any as IUniswapV3Pool
90
107
const liquidity = await lpPool . liquidity ( )
91
108
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
93
110
} )
94
111
95
112
it ( "calculateFees should return fee from 5 swaps" , async ( ) => {
@@ -98,19 +115,15 @@ describe("Withdraw fees", function () {
98
115
}
99
116
const lpPool = ( await ethers . getContractAt ( "UniswapV3Pool" , lpAddress ) ) as any as IUniswapV3Pool
100
117
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
107
123
} )
108
124
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
114
127
} )
115
128
116
129
it ( "should deacrease fee after withdraw" , async ( ) => {
0 commit comments