dsa-connectors/test/arbitrum/uniswap-sell-beta/uniswap-sell-beta.ts

130 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-12-06 00:14:38 +00:00
import { BigNumberish } from "@ethersproject/bignumber";
import { Contract } from "@ethersproject/contracts";
import { expect } from "chai";
2021-12-06 00:14:38 +00:00
import hre, { artifacts } from "hardhat";
const { ethers } = hre;
2021-11-19 14:22:34 +00:00
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 () {
2021-12-06 00:14:38 +00:00
let UniswapSellBeta, uniswapSellBeta: Contract;
2021-11-19 20:29:08 +00:00
2021-12-06 00:14:38 +00:00
async function setBalance(address: any) {
await hre.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-12-06 00:14:38 +00:00
async function impersonate(owner: string, account: any, token0: string, decimals: BigNumberish | undefined) {
2021-11-20 10:30:56 +00:00
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 = "0xa067668661c84476afcdc6fa5d758c4c01c34352";
const account1 = "0x0db3fe3b770c95a0b99d1ed6f2627933466c0dd8";
2021-11-20 10:30:56 +00:00
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
});
});