2021-12-02 17:31:51 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
import hre from "hardhat";
|
|
|
|
const { web3, deployments, waffle, ethers } = hre;
|
2021-12-06 00:14:38 +00:00
|
|
|
const { provider, deployContract } = waffle;
|
2021-12-02 17:31:51 +00:00
|
|
|
|
2021-12-06 00:14:38 +00:00
|
|
|
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
|
|
|
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
|
|
|
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
|
|
|
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
2021-12-02 17:31:51 +00:00
|
|
|
|
2021-12-09 19:32:08 +00:00
|
|
|
import addresses from "../../../scripts/constant/addresses";
|
2021-12-10 17:29:55 +00:00
|
|
|
import abis from "../../../scripts/constant/abis";
|
2021-12-02 17:31:51 +00:00
|
|
|
import { tokens } from "../../../scripts/constant/tokens";
|
2021-12-06 00:14:38 +00:00
|
|
|
import { Signer, Contract, BigNumber } from "ethers";
|
2021-12-02 17:31:51 +00:00
|
|
|
|
2021-12-06 00:14:38 +00:00
|
|
|
import { ConnectV2YearnV2__factory } from "../../../typechain";
|
2021-12-02 17:31:51 +00:00
|
|
|
|
2021-12-06 00:14:38 +00:00
|
|
|
const toBytes32 = (bn: BigNumber) => {
|
|
|
|
return ethers.utils.hexlify(ethers.utils.zeroPad(bn.toHexString(), 32));
|
2021-12-02 17:31:51 +00:00
|
|
|
};
|
2021-12-06 00:14:38 +00:00
|
|
|
|
|
|
|
const setStorageAt = async (address: string, index: string, value: string) => {
|
|
|
|
await ethers.provider.send("hardhat_setStorageAt", [address, index, value]);
|
|
|
|
await ethers.provider.send("evm_mine", []); // Just mines to the next block
|
2021-12-02 17:31:51 +00:00
|
|
|
};
|
|
|
|
|
2021-12-06 00:14:38 +00:00
|
|
|
describe("Yearn", function() {
|
|
|
|
const connectorName = "YEARN-TEST-A";
|
|
|
|
|
|
|
|
let dsaWallet0: any;
|
|
|
|
let masterSigner: Signer;
|
|
|
|
let instaConnectorsV2: Contract;
|
|
|
|
let connector: Contract;
|
|
|
|
|
|
|
|
const wallets = provider.getWallets();
|
|
|
|
const [wallet0, wallet1, wallet2, wallet3] = wallets;
|
|
|
|
before(async () => {
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_reset",
|
|
|
|
params: [
|
|
|
|
{
|
|
|
|
forking: {
|
|
|
|
// @ts-ignore
|
|
|
|
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
|
|
|
blockNumber: 12996975,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
masterSigner = await getMasterSigner();
|
|
|
|
instaConnectorsV2 = await ethers.getContractAt(
|
|
|
|
abis.core.connectorsV2,
|
2021-12-08 07:44:19 +00:00
|
|
|
addresses.mainnet.core.connectorsV2
|
2021-12-06 00:14:38 +00:00
|
|
|
);
|
|
|
|
connector = await deployAndEnableConnector({
|
|
|
|
connectorName,
|
|
|
|
contractArtifact: ConnectV2YearnV2__factory,
|
|
|
|
signer: masterSigner,
|
|
|
|
connectors: instaConnectorsV2,
|
|
|
|
});
|
|
|
|
console.log("Connector address", connector.address);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should have contracts deployed.", async function() {
|
|
|
|
expect(!!instaConnectorsV2.address).to.be.true;
|
|
|
|
expect(!!connector.address).to.be.true;
|
|
|
|
expect(!!(await masterSigner.getAddress())).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("DSA wallet setup", function() {
|
|
|
|
it("Should build DSA v2", async function() {
|
|
|
|
dsaWallet0 = await buildDSAv2(wallet0.address);
|
|
|
|
expect(!!dsaWallet0.address).to.be.true;
|
2021-12-02 17:31:51 +00:00
|
|
|
});
|
|
|
|
|
2021-12-06 00:14:38 +00:00
|
|
|
it("Deposit ETH into DSA wallet", async function() {
|
|
|
|
await wallet0.sendTransaction({
|
|
|
|
to: dsaWallet0.address,
|
|
|
|
value: ethers.utils.parseEther("10"),
|
|
|
|
});
|
|
|
|
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
|
|
|
|
ethers.utils.parseEther("10")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Main", function() {
|
|
|
|
it("Should increase the DAI balance to 100 DAI", async function() {
|
|
|
|
const DAI = new ethers.Contract(
|
|
|
|
tokens.dai.address,
|
|
|
|
abis.basic.erc20,
|
|
|
|
ethers.provider
|
|
|
|
);
|
|
|
|
const DAI_SLOT = 2;
|
|
|
|
const locallyManipulatedBalance = ethers.utils.parseEther("100");
|
|
|
|
|
|
|
|
// Get storage slot index
|
|
|
|
const index = ethers.utils.solidityKeccak256(
|
|
|
|
["uint256", "uint256"],
|
|
|
|
[dsaWallet0.address, DAI_SLOT]
|
|
|
|
);
|
|
|
|
// Manipulate local balance (needs to be bytes32 string)
|
|
|
|
await setStorageAt(
|
|
|
|
tokens.dai.address,
|
|
|
|
index.toString(),
|
|
|
|
toBytes32(locallyManipulatedBalance).toString()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get DAI balance
|
|
|
|
const balance = await DAI.balanceOf(dsaWallet0.address);
|
|
|
|
expect(
|
|
|
|
await ethers.BigNumber.from(balance).eq(ethers.utils.parseEther("100"))
|
|
|
|
);
|
2021-12-02 17:31:51 +00:00
|
|
|
});
|
|
|
|
|
2021-12-06 00:14:38 +00:00
|
|
|
it("Should deposit and withdraw 50 DAI in/out the Yearn Vault", async function() {
|
|
|
|
const DAI = new ethers.Contract(
|
|
|
|
tokens.dai.address,
|
|
|
|
abis.basic.erc20,
|
|
|
|
ethers.provider
|
|
|
|
);
|
|
|
|
const DAI_VAULT = "0xdA816459F1AB5631232FE5e97a05BBBb94970c95";
|
|
|
|
const amount = ethers.utils.parseEther("50"); // 50 DAI
|
|
|
|
const setId = "132456";
|
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "deposit",
|
|
|
|
args: [DAI_VAULT, amount, 0, setId],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "withdraw",
|
|
|
|
args: [DAI_VAULT, amount, setId, 0],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0
|
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet0.address);
|
|
|
|
await tx.wait();
|
|
|
|
|
|
|
|
// Get DAI balance
|
|
|
|
const balance = await DAI.balanceOf(dsaWallet0.address);
|
|
|
|
expect(
|
|
|
|
await ethers.BigNumber.from(balance).eq(ethers.utils.parseEther("100"))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should deposit 70 DAI in the Yearn Vault", async function() {
|
|
|
|
const DAI_VAULT = "0xdA816459F1AB5631232FE5e97a05BBBb94970c95";
|
|
|
|
const DAI = new ethers.Contract(
|
|
|
|
tokens.dai.address,
|
|
|
|
abis.basic.erc20,
|
|
|
|
ethers.provider
|
|
|
|
);
|
|
|
|
const YVDAI = new ethers.Contract(
|
|
|
|
DAI_VAULT,
|
|
|
|
abis.basic.erc20,
|
|
|
|
ethers.provider
|
|
|
|
);
|
|
|
|
const amount = ethers.utils.parseEther("70"); // 70 DAI
|
|
|
|
const setId = "568445";
|
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "deposit",
|
|
|
|
args: [DAI_VAULT, amount, 0, setId],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0
|
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet0.address);
|
|
|
|
await tx.wait();
|
|
|
|
|
|
|
|
// Get DAI balance
|
|
|
|
const yvDAIBalance = await YVDAI.balanceOf(dsaWallet0.address);
|
|
|
|
const daiBalance = await DAI.balanceOf(dsaWallet0.address);
|
|
|
|
const correctDaiBalance = ethers.BigNumber.from(daiBalance).eq(
|
|
|
|
ethers.utils.parseEther("30")
|
|
|
|
);
|
|
|
|
const correctYVDaiBalance = ethers.BigNumber.from(yvDAIBalance).lte(
|
|
|
|
ethers.utils.parseEther("70")
|
|
|
|
);
|
|
|
|
expect(correctDaiBalance && correctYVDaiBalance);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|