Created
September 8, 2021 18:25
-
-
Save eugenioclrc/ff667ba021cbe2f739c778198cedde14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const UniswapV2FactoryArtifact = require('@uniswap/v2-core/build/UniswapV2Factory.json'); | |
const UniswapV2PairArtifact = require('@uniswap/v2-core/build/UniswapV2Pair.json'); | |
const UniswapV2Router02Artifact = require('@uniswap/v2-periphery/build/UniswapV2Router02.json'); | |
const WETH9Artifact = require('@uniswap/v2-periphery/build/WETH9.json'); | |
async function createMockWETH() { | |
// deploy mock WETH | |
const WETH9 = await ethers.getContractFactory( | |
WETH9Artifact.abi, | |
WETH9Artifact.bytecode | |
); | |
WETH = await WETH9.connect(unideployer).deploy(); | |
await WETH.deployed(); | |
return WETH; | |
} | |
async function createUniswapFactory(unideployer) { | |
// deploy uni factory | |
const UniswapV2Factory = await ethers.getContractFactory( | |
UniswapV2FactoryArtifact.abi, | |
UniswapV2FactoryArtifact.bytecode | |
); | |
const uniswapV2Factory = await UniswapV2Factory.connect(unideployer).deploy(unideployer.address); | |
await uniswapV2Factory.deployed(); | |
return uniswapV2Factory; | |
} | |
async function createUniswapRouter02(unideployer, uniswapV2Factory, WETH) { | |
// deploy uni router | |
const UniswapV2Router02 = await ethers.getContractFactory( | |
UniswapV2Router02Artifact.abi, | |
UniswapV2Router02Artifact.bytecode | |
); | |
const uniswapV2Router02 = await UniswapV2Router02.connect(unideployer).deploy(uniswapV2Factory.address, WETH.address); | |
await uniswapV2Router02.deployed(); | |
return uniswapV2Router02; | |
} | |
async function mockUniswap(unideployer) { | |
const WETH = await createMockWETH(unideployer); | |
const uniswapV2Factory = await createUniswapFactory(unideployer); | |
const uniswapV2Router02 = await createUniswapRouter02(unideployer, uniswapV2Factory, WETH) | |
// for event decoding | |
const UniswapV2PairContract = await ethers.getContractFactory( | |
UniswapV2PairArtifact.abi, | |
UniswapV2PairArtifact.bytecode | |
); | |
return { WETH, uniswapV2Factory, uniswapV2Router02, UniswapV2PairContract }; | |
} | |
async function mockLiquidity(unideployer, supplier, token0Contract, amount0, amountWeth) { | |
const { WETH, uniswapV2Factory, uniswapV2Router02, UniswapV2PairContract } = await mockUniswap(unideployer); | |
// get 10 WETH | |
await WETH.connect(supplier).deposit({value: amountWeth}); | |
await WETH.connect(supplier).approve(uniswapV2Router02.address, ethers.utils.parseEther('100000')); | |
await token0Contract.connect(supplier).approve(uniswapV2Router02.address, ethers.utils.parseEther('100000')); | |
// create weth / kwan | |
const tx = await uniswapV2Factory.createPair(token0Contract.address, WETH.address); | |
const { pair: pairCreated } = (await (await tx).wait()).events[0].args; | |
const uniswapToken0Weth = UniswapV2PairContract.attach(pairCreated); | |
await uniswapV2Router02.connect(supplier).addLiquidity( | |
token0Contract.address, | |
WETH.address, | |
amount0, | |
amountWeth, | |
1, | |
1, | |
supplier.address, | |
// wait time | |
'999999999999999999999999999999', | |
); | |
return { | |
WETH, uniswapV2Factory, uniswapV2Router02, uniswapV2Pair: uniswapToken0Weth | |
} | |
} | |
module.exports = { | |
mockUniswap, | |
mockLiquidity | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment