dsa-connectors/test/mainnet/uniswap-sell-beta/uniswap-sell-beta.js

129 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-11-19 14:22:34 +00:00
const { expect } = require("chai");
const hre = require("hardhat");
const { web3, deployments, waffle, ethers } = hre;
const { provider, deployContract } = waffle;
2021-11-19 15:54:42 +00:00
const USDC_ADDR = "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8";
const WETH_ADDR = "0x82af49447d8a07e3bd95bd0d56f35241523fbab1";
2021-11-19 14:22:34 +00:00
describe("Uniswap-sell-beta", function() {
let UniswapSellBeta, uniswapSellBeta;
2021-11-19 20:29:08 +00:00
2021-11-20 10:30:56 +00:00
async function setBalance(address) {
2021-11-19 20:29:08 +00:00
await network.provider.send("hardhat_setBalance", [
2021-11-20 10:30:56 +00:00
address,
2021-11-19 20:29:08 +00:00
ethers.utils.parseEther("10.0").toHexString(),
]);
2021-11-20 10:30:56 +00:00
}
2021-11-19 20:29:08 +00:00
2021-11-20 10:30:56 +00:00
async function impersonate(owner, account, token0, decimals) {
const tokenArtifact = await artifacts.readArtifact(
"@openzeppelin/contracts/token/ERC20/IERC20.sol:IERC20"
);
setBalance(owner);
setBalance(account);
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [account],
});
const signer = await ethers.getSigner(account);
2021-11-19 20:29:08 +00:00
const token = new ethers.Contract(
2021-11-20 10:30:56 +00:00
token0,
2021-11-19 20:29:08 +00:00
tokenArtifact.abi,
ethers.provider
);
2021-11-20 10:30:56 +00:00
// console.log((await token.balanceOf(account)).toString());
2021-11-19 20:29:08 +00:00
await token
.connect(signer)
2021-11-20 10:30:56 +00:00
.transfer(owner, ethers.utils.parseUnits("10", decimals));
await hre.network.provider.request({
method: "hardhat_stopImpersonatingAccount",
params: [account],
});
2021-11-20 10:30:56 +00:00
}
beforeEach(async () => {
const account0 = "0x36cc7B13029B5DEe4034745FB4F24034f3F2ffc6";
const account1 = "0xce2cc46682e9c6d5f174af598fb4931a9c0be68e";
const [owner, add1, add2] = await ethers.getSigners();
await impersonate(owner.address, account1, USDC_ADDR, 6);
await impersonate(owner.address, account0, WETH_ADDR, 18);
2021-11-19 14:22:34 +00:00
UniswapSellBeta = await ethers.getContractFactory(
2021-11-20 10:30:56 +00:00
"ConnectV2UniswapSellBeta"
2021-11-19 14:22:34 +00:00
);
uniswapSellBeta = await UniswapSellBeta.deploy();
await uniswapSellBeta.deployed();
});
it("Should have contracts deployed.", async function() {
2021-11-19 15:54:42 +00:00
expect(uniswapSellBeta.address).to.exist;
2021-11-19 14:22:34 +00:00
});
2021-11-20 10:30:56 +00:00
it("Should swap WETH with USDC", async () => {
const [owner, add1, add2] = await ethers.getSigners();
const tokenArtifact = await artifacts.readArtifact(
"@openzeppelin/contracts/token/ERC20/IERC20.sol:IERC20"
);
const token = new ethers.Contract(
WETH_ADDR,
tokenArtifact.abi,
ethers.provider
);
const signer = await ethers.getSigner(owner.address);
await token
.connect(signer)
.transfer(uniswapSellBeta.address, ethers.utils.parseUnits("10.0", 18));
const tx = await uniswapSellBeta.sell(
WETH_ADDR,
USDC_ADDR,
3000,
ethers.utils.parseUnits("10.0", 18),
2021-11-20 12:27:45 +00:00
0
2021-11-20 10:30:56 +00:00
);
// console.log(tx);
});
it("Should swap USDC with WETH", async () => {
const [owner, add1, add2] = await ethers.getSigners();
2021-11-19 21:55:52 +00:00
const tokenArtifact = await artifacts.readArtifact(
"@openzeppelin/contracts/token/ERC20/IERC20.sol:IERC20"
);
const token = new ethers.Contract(
USDC_ADDR,
tokenArtifact.abi,
ethers.provider
);
const signer = await ethers.getSigner(owner.address);
await token
.connect(signer)
.transfer(uniswapSellBeta.address, ethers.utils.parseUnits("10.0", 6));
2021-11-19 14:22:34 +00:00
const tx = await uniswapSellBeta.sell(
USDC_ADDR,
2021-11-20 12:27:45 +00:00
WETH_ADDR,
3000,
2021-11-19 21:55:52 +00:00
ethers.utils.parseUnits("10.0", 6),
2021-11-20 12:27:45 +00:00
0
2021-11-19 14:22:34 +00:00
);
2021-11-20 10:30:56 +00:00
// console.log(tx);
2021-11-19 14:22:34 +00:00
});
});