|
| 1 | +import { ethers, waffle } from 'hardhat' |
| 2 | +import { BigNumber, constants, Contract, ContractTransaction } from 'ethers' |
| 3 | +import { |
| 4 | + IWETH9, |
| 5 | + MockTimeNonfungiblePositionManager, |
| 6 | + MockTimeSwapRouter, |
| 7 | + PairFlash, |
| 8 | + IUniswapV3Pool, |
| 9 | + TestERC20, |
| 10 | + TestERC20Metadata, |
| 11 | + IUniswapV3Factory, |
| 12 | + NFTDescriptor, |
| 13 | + Quoter, |
| 14 | + SwapRouter, |
| 15 | +} from '../typechain' |
| 16 | +import completeFixture from './shared/completeFixture' |
| 17 | +import { FeeAmount, MaxUint128, TICK_SPACINGS } from './shared/constants' |
| 18 | +import { encodePriceSqrt } from './shared/encodePriceSqrt' |
| 19 | +import snapshotGasCost from './shared/snapshotGasCost' |
| 20 | + |
| 21 | +import { expect } from './shared/expect' |
| 22 | +import { getMaxTick, getMinTick } from './shared/ticks' |
| 23 | +import { computePoolAddress } from './shared/computePoolAddress' |
| 24 | + |
| 25 | +describe('PairFlash test', () => { |
| 26 | + const provider = waffle.provider |
| 27 | + const wallets = waffle.provider.getWallets() |
| 28 | + const wallet = wallets[0] |
| 29 | + |
| 30 | + let flash: PairFlash |
| 31 | + let nft: MockTimeNonfungiblePositionManager |
| 32 | + let token0: TestERC20 |
| 33 | + let token1: TestERC20 |
| 34 | + let factory: IUniswapV3Factory |
| 35 | + let quoter: Quoter |
| 36 | + |
| 37 | + async function createPool(tokenAddressA: string, tokenAddressB: string, fee: FeeAmount, price: BigNumber) { |
| 38 | + if (tokenAddressA.toLowerCase() > tokenAddressB.toLowerCase()) |
| 39 | + [tokenAddressA, tokenAddressB] = [tokenAddressB, tokenAddressA] |
| 40 | + |
| 41 | + await nft.createAndInitializePoolIfNecessary(tokenAddressA, tokenAddressB, fee, price) |
| 42 | + |
| 43 | + const liquidityParams = { |
| 44 | + token0: tokenAddressA, |
| 45 | + token1: tokenAddressB, |
| 46 | + fee: fee, |
| 47 | + tickLower: getMinTick(TICK_SPACINGS[fee]), |
| 48 | + tickUpper: getMaxTick(TICK_SPACINGS[fee]), |
| 49 | + recipient: wallet.address, |
| 50 | + amount0Desired: 1000000, |
| 51 | + amount1Desired: 1000000, |
| 52 | + amount0Min: 0, |
| 53 | + amount1Min: 0, |
| 54 | + deadline: 1, |
| 55 | + } |
| 56 | + |
| 57 | + return nft.mint(liquidityParams) |
| 58 | + } |
| 59 | + |
| 60 | + const flashFixture = async () => { |
| 61 | + const { router, tokens, factory, weth9, nft } = await completeFixture(wallets, provider) |
| 62 | + const token0 = tokens[0] |
| 63 | + const token1 = tokens[1] |
| 64 | + |
| 65 | + const flashContractFactory = await ethers.getContractFactory('PairFlash') |
| 66 | + const flash = (await flashContractFactory.deploy(router.address, factory.address, weth9.address)) as PairFlash |
| 67 | + |
| 68 | + const quoterFactory = await ethers.getContractFactory('Quoter') |
| 69 | + const quoter = (await quoterFactory.deploy(factory.address, weth9.address)) as Quoter |
| 70 | + |
| 71 | + return { |
| 72 | + token0, |
| 73 | + token1, |
| 74 | + flash, |
| 75 | + factory, |
| 76 | + weth9, |
| 77 | + nft, |
| 78 | + quoter, |
| 79 | + router, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + let loadFixture: ReturnType<typeof waffle.createFixtureLoader> |
| 84 | + |
| 85 | + before('create fixture loader', async () => { |
| 86 | + loadFixture = waffle.createFixtureLoader(wallets) |
| 87 | + }) |
| 88 | + |
| 89 | + beforeEach('load fixture', async () => { |
| 90 | + ;({ factory, token0, token1, flash, nft, quoter } = await loadFixture(flashFixture)) |
| 91 | + |
| 92 | + await token0.approve(nft.address, MaxUint128) |
| 93 | + await token1.approve(nft.address, MaxUint128) |
| 94 | + await createPool(token0.address, token1.address, FeeAmount.LOW, encodePriceSqrt(5, 10)) |
| 95 | + await createPool(token0.address, token1.address, FeeAmount.MEDIUM, encodePriceSqrt(1, 1)) |
| 96 | + await createPool(token0.address, token1.address, FeeAmount.HIGH, encodePriceSqrt(20, 10)) |
| 97 | + }) |
| 98 | + |
| 99 | + describe('flash', () => { |
| 100 | + it('test correct transfer events', async () => { |
| 101 | + //choose amountIn to test |
| 102 | + const amount0In = 1000 |
| 103 | + const amount1In = 1000 |
| 104 | + |
| 105 | + const fee0 = Math.ceil((amount0In * FeeAmount.MEDIUM) / 1000000) |
| 106 | + const fee1 = Math.ceil((amount1In * FeeAmount.MEDIUM) / 1000000) |
| 107 | + |
| 108 | + const flashParams = { |
| 109 | + token0: token0.address, |
| 110 | + token1: token1.address, |
| 111 | + fee1: FeeAmount.MEDIUM, |
| 112 | + amount0: amount0In, |
| 113 | + amount1: amount1In, |
| 114 | + fee2: FeeAmount.LOW, |
| 115 | + fee3: FeeAmount.HIGH, |
| 116 | + } |
| 117 | + // pool1 is the borrow pool |
| 118 | + const pool1 = computePoolAddress(factory.address, [token0.address, token1.address], FeeAmount.MEDIUM) |
| 119 | + const pool2 = computePoolAddress(factory.address, [token0.address, token1.address], FeeAmount.LOW) |
| 120 | + const pool3 = computePoolAddress(factory.address, [token0.address, token1.address], FeeAmount.HIGH) |
| 121 | + |
| 122 | + const expectedAmountOut0 = await quoter.callStatic.quoteExactInputSingle( |
| 123 | + token1.address, |
| 124 | + token0.address, |
| 125 | + FeeAmount.LOW, |
| 126 | + amount1In, |
| 127 | + encodePriceSqrt(20, 10) |
| 128 | + ) |
| 129 | + const expectedAmountOut1 = await quoter.callStatic.quoteExactInputSingle( |
| 130 | + token0.address, |
| 131 | + token1.address, |
| 132 | + FeeAmount.HIGH, |
| 133 | + amount0In, |
| 134 | + encodePriceSqrt(5, 10) |
| 135 | + ) |
| 136 | + |
| 137 | + await expect(flash.initFlash(flashParams)) |
| 138 | + .to.emit(token0, 'Transfer') |
| 139 | + .withArgs(pool1, flash.address, amount0In) |
| 140 | + .to.emit(token1, 'Transfer') |
| 141 | + .withArgs(pool1, flash.address, amount1In) |
| 142 | + .to.emit(token0, 'Transfer') |
| 143 | + .withArgs(pool2, flash.address, expectedAmountOut0) |
| 144 | + .to.emit(token1, 'Transfer') |
| 145 | + .withArgs(pool3, flash.address, expectedAmountOut1) |
| 146 | + .to.emit(token0, 'Transfer') |
| 147 | + .withArgs(flash.address, wallet.address, expectedAmountOut0.toNumber() - amount0In - fee0) |
| 148 | + .to.emit(token1, 'Transfer') |
| 149 | + .withArgs(flash.address, wallet.address, expectedAmountOut1.toNumber() - amount1In - fee1) |
| 150 | + }) |
| 151 | + |
| 152 | + it('gas', async () => { |
| 153 | + const amount0In = 1000 |
| 154 | + const amount1In = 1000 |
| 155 | + |
| 156 | + const flashParams = { |
| 157 | + token0: token0.address, |
| 158 | + token1: token1.address, |
| 159 | + fee1: FeeAmount.MEDIUM, |
| 160 | + amount0: amount0In, |
| 161 | + amount1: amount1In, |
| 162 | + fee2: FeeAmount.LOW, |
| 163 | + fee3: FeeAmount.HIGH, |
| 164 | + } |
| 165 | + await snapshotGasCost(flash.initFlash(flashParams)) |
| 166 | + }) |
| 167 | + }) |
| 168 | +}) |
0 commit comments