2023-08-08 22:20:54 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
import hre, { network } from "hardhat";
|
|
|
|
const { web3, deployments, waffle, ethers } = hre;
|
|
|
|
const { provider, deployContract } = waffle;
|
|
|
|
|
|
|
|
import type { Signer, Contract } from "ethers";
|
|
|
|
import { parseEther, parseUnits } from "ethers/lib/utils";
|
|
|
|
import { BigNumber } from "bignumber.js";
|
|
|
|
|
|
|
|
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
|
|
|
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
|
|
|
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
|
|
|
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
|
|
|
import { addresses } from "../../../scripts/tests/mainnet/addresses";
|
|
|
|
import { abis } from "../../../scripts/constant/abis";
|
|
|
|
import { tokens } from "../../../scripts/tests/mainnet/tokens";
|
|
|
|
import { ConnectV2BasicERC4626__factory, IERC4626__factory, IERC20Minimal__factory } from "../../../typechain";
|
|
|
|
|
|
|
|
describe("BASIC-D", function () {
|
|
|
|
const connectorName = "BASIC-D";
|
|
|
|
|
|
|
|
let dsaWallet0: any;
|
|
|
|
let masterSigner: Signer;
|
|
|
|
let instaConnectorsV2: Contract;
|
|
|
|
let connector: any;
|
|
|
|
let wallet: any;
|
|
|
|
|
|
|
|
const account = "0x075e72a5edf65f0a5f44699c7654c1a76941ddc8";
|
|
|
|
const sDAIaddress = "0x83f20f44975d03b1b09e64809b757c47f942beea";
|
|
|
|
let signer: any;
|
|
|
|
|
|
|
|
const daiContract = new ethers.Contract(tokens.dai.address, IERC20Minimal__factory.abi, ethers.provider);
|
2023-08-09 15:44:42 +00:00
|
|
|
const erc4626Contract = new ethers.Contract(sDAIaddress, IERC4626__factory.abi, ethers.provider);
|
2023-08-08 22:20:54 +00:00
|
|
|
|
|
|
|
const wallets = provider.getWallets();
|
|
|
|
const [wallet0] = wallets;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_reset",
|
|
|
|
params: [
|
|
|
|
{
|
|
|
|
forking: {
|
|
|
|
// @ts-ignore
|
2023-08-13 19:18:28 +00:00
|
|
|
jsonRpcUrl: hre.config.networks.hardhat.forking?.url,
|
|
|
|
blockNumber: 17907926
|
2023-08-08 22:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
masterSigner = await getMasterSigner();
|
|
|
|
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
|
|
|
|
connector = await deployAndEnableConnector({
|
|
|
|
connectorName,
|
|
|
|
contractArtifact: ConnectV2BasicERC4626__factory,
|
|
|
|
signer: masterSigner,
|
|
|
|
connectors: instaConnectorsV2
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("Connector address", connector.address);
|
|
|
|
|
|
|
|
await hre.network.provider.send("hardhat_setBalance", [account, ethers.utils.parseEther("10").toHexString()]);
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_impersonateAccount",
|
|
|
|
params: [account]
|
|
|
|
});
|
|
|
|
|
|
|
|
signer = await ethers.getSigner(account);
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
wallet = await ethers.getSigner(dsaWallet0.address);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Deposit ETH into DSA wallet", async function () {
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_impersonateAccount",
|
|
|
|
params: [wallet.address]
|
|
|
|
});
|
|
|
|
|
|
|
|
await wallet0.sendTransaction({
|
|
|
|
to: dsaWallet0.address,
|
|
|
|
value: ethers.utils.parseEther("10")
|
|
|
|
});
|
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
let txRes = await daiContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("1000"));
|
2023-08-08 22:20:54 +00:00
|
|
|
await txRes.wait();
|
2023-08-13 20:30:50 +00:00
|
|
|
// expect(await daiContract.balanceOf(dsaWallet0.address)).to.be.eq(ethers.utils.parseEther("10000"));
|
2023-08-08 22:20:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Main", function () {
|
2023-08-13 20:30:50 +00:00
|
|
|
// it("Calculate Total Asset and Total Supply", async () => {
|
|
|
|
// const totalAsset = await erc4626Contract.totalAssets();
|
|
|
|
// const totalSupply = await erc4626Contract.totalSupply();
|
|
|
|
// console.log("totalAsset :>> ", totalAsset);
|
|
|
|
// console.log("totalSupply :>> ", totalSupply);
|
|
|
|
// });
|
2023-08-08 22:20:54 +00:00
|
|
|
it("should deposit asset to ERC4626", async () => {
|
2023-08-09 15:44:42 +00:00
|
|
|
const assets = ethers.utils.parseEther("1");
|
2023-08-21 12:23:11 +00:00
|
|
|
|
|
|
|
// Returns the amount of shares for assets
|
2023-08-09 15:44:42 +00:00
|
|
|
const previewDeposit = await erc4626Contract.previewDeposit(assets);
|
2023-08-21 12:23:11 +00:00
|
|
|
console.log("previewDeposit :>> ", previewDeposit.toString());
|
2023-08-09 15:44:42 +00:00
|
|
|
|
|
|
|
const maxDeposit = await erc4626Contract.maxDeposit(dsaWallet0.address);
|
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
let minSharesPerToken = ethers.utils.parseUnits("0.95");
|
2023-08-13 19:18:28 +00:00
|
|
|
|
2023-08-13 20:30:50 +00:00
|
|
|
const beforebalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
2023-08-21 12:23:11 +00:00
|
|
|
console.log("Share before balance :>> ", beforebalance.toString());
|
2023-08-13 19:18:28 +00:00
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
let spells = [
|
2023-08-11 05:42:00 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "deposit",
|
2023-08-13 19:18:28 +00:00
|
|
|
args: [sDAIaddress, assets, minSharesPerToken, 0, 0]
|
2023-08-09 15:44:42 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
|
|
|
let receipt = await tx.wait();
|
2023-08-13 19:18:28 +00:00
|
|
|
|
|
|
|
const afterbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
2023-08-21 12:23:11 +00:00
|
|
|
console.log("Share after balance :>> ", afterbalance.toString());
|
|
|
|
|
|
|
|
expect(afterbalance.sub(beforebalance)).to.be.lte(previewDeposit)
|
2023-08-13 19:18:28 +00:00
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
// In case of not satisfying min rate
|
|
|
|
minSharesPerToken = ethers.utils.parseUnits("1");
|
|
|
|
spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "deposit",
|
|
|
|
args: [sDAIaddress, assets, minSharesPerToken, 0, 0]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
await expect(dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address)).to.be.reverted;
|
|
|
|
|
2023-08-09 15:44:42 +00:00
|
|
|
});
|
|
|
|
it("should mint asset to ERC4626", async () => {
|
2023-08-21 12:23:11 +00:00
|
|
|
const beforeBalance = await daiContract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("token balance before :>> ", beforeBalance.toString());
|
|
|
|
const beforeSharebalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("share balance before :>> ", beforeSharebalance.toString());
|
|
|
|
|
|
|
|
const shares = ethers.utils.parseEther("1");
|
|
|
|
// Returns token amount for shares
|
2023-08-09 15:44:42 +00:00
|
|
|
const previewMint = await erc4626Contract.previewMint(shares);
|
2023-08-21 12:23:11 +00:00
|
|
|
console.log("Token amount preview Mint :>> ", previewMint.toString());
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
let maxTokenPerShares = ethers.utils.parseUnits("1.1");
|
|
|
|
|
|
|
|
let spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "mint",
|
2023-08-21 12:23:11 +00:00
|
|
|
args: [sDAIaddress, shares, maxTokenPerShares, 0, 0]
|
2023-08-14 14:06:30 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
|
|
|
let receipt = await tx.wait();
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const afterbalance = await daiContract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("token balance after :>> ", afterbalance.toString());
|
|
|
|
const afterSharebalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("share balance after :>> ", afterSharebalance.toString());
|
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
// In case of not satisfying max rate
|
|
|
|
maxTokenPerShares = ethers.utils.parseUnits("1");
|
|
|
|
|
|
|
|
spells = [
|
2023-08-11 05:42:00 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "mint",
|
2023-08-21 12:23:11 +00:00
|
|
|
args: [sDAIaddress, shares, maxTokenPerShares, 0, 0]
|
2023-08-08 22:20:54 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
await expect(dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address)).to.be.reverted;
|
|
|
|
|
2023-08-08 22:20:54 +00:00
|
|
|
});
|
2023-08-21 12:23:11 +00:00
|
|
|
it("should Max redeem", async () => {
|
|
|
|
const balance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("Share balance :>> ", balance.toString());
|
2023-08-09 15:44:42 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
// Returns max Shares
|
2023-08-09 15:44:42 +00:00
|
|
|
const maxRedeem: BigNumber = await erc4626Contract.maxRedeem(dsaWallet0.address);
|
2023-08-21 12:23:11 +00:00
|
|
|
console.log("maxRedeem :>> ", maxRedeem.toString());
|
2023-08-08 22:20:54 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
let minTokenPerShares = ethers.utils.parseUnits("1.01");
|
|
|
|
|
2023-08-09 15:44:42 +00:00
|
|
|
const setId = "83478237";
|
2023-08-14 14:06:30 +00:00
|
|
|
let spells = [
|
2023-08-11 05:42:00 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "redeem",
|
2023-08-21 12:23:11 +00:00
|
|
|
args: [sDAIaddress, ethers.constants.MaxUint256, minTokenPerShares, dsaWallet0.address, 0, setId]
|
2023-08-09 15:44:42 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
|
|
|
let receipt = await tx.wait();
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const afterUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("afterUnderbalance :>> ", afterUnderbalance.toString());
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const afterVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("afterVaultbalance :>> ", afterVaultbalance.toString());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should Revert for not satisfying min redeem rate", async () => {
|
|
|
|
const balance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("Share balance :>> ", balance.toString());
|
|
|
|
|
|
|
|
let spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "deposit",
|
|
|
|
args: [sDAIaddress, ethers.utils.parseEther("1"), ethers.utils.parseUnits("0.95"), 0, 0]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
|
|
|
let receipt = await tx.wait();
|
|
|
|
|
|
|
|
// Returns max Shares
|
|
|
|
const maxRedeem: BigNumber = await erc4626Contract.maxRedeem(dsaWallet0.address);
|
|
|
|
console.log("maxRedeem :>> ", maxRedeem.toString());
|
|
|
|
|
|
|
|
const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
|
|
|
|
|
|
|
|
const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
|
|
|
|
|
|
|
|
expect(beforeVaultbalance).to.be.gte("950000000000000000")
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
// In case of not satisfying min rate
|
2023-08-21 12:23:11 +00:00
|
|
|
let minTokenPerShares = ethers.utils.parseUnits("1.5");
|
2023-08-14 14:06:30 +00:00
|
|
|
|
|
|
|
spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "redeem",
|
2023-08-21 12:23:11 +00:00
|
|
|
args: [sDAIaddress, ethers.constants.MaxUint256, minTokenPerShares, dsaWallet0.address, 0, 0]
|
2023-08-14 14:06:30 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
await expect(dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address)).to.be.reverted;
|
|
|
|
|
2023-08-09 15:44:42 +00:00
|
|
|
});
|
2023-08-21 12:23:11 +00:00
|
|
|
|
2023-08-09 15:44:42 +00:00
|
|
|
it("should withdraw asset to ERC4626", async () => {
|
2023-08-13 20:30:50 +00:00
|
|
|
const maxWithdraw: BigNumber = await erc4626Contract.maxWithdraw(dsaWallet0.address);
|
2023-08-21 12:23:11 +00:00
|
|
|
console.log("maxWithdraw :>> ", maxWithdraw.toString());
|
2023-08-09 15:44:42 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
let maxSharesPerToken = ethers.utils.parseUnits("0.975");
|
2023-08-14 14:06:30 +00:00
|
|
|
|
2023-08-09 15:44:42 +00:00
|
|
|
const setId = "83478237";
|
2023-08-14 14:06:30 +00:00
|
|
|
let spells = [
|
2023-08-11 05:42:00 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "withdraw",
|
2023-08-21 12:23:11 +00:00
|
|
|
args: [sDAIaddress, maxWithdraw, maxSharesPerToken, dsaWallet0.address, 0, setId]
|
2023-08-09 15:44:42 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2023-08-14 14:06:30 +00:00
|
|
|
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
|
|
|
let receipt = await tx.wait();
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const afterUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("afterUnderbalance :>> ", afterUnderbalance.toString());
|
2023-08-13 20:30:50 +00:00
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
const afterVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("afterVaultbalance :>> ", afterVaultbalance.toString());
|
2023-08-14 14:06:30 +00:00
|
|
|
|
|
|
|
// In case of not satisfying min rate
|
|
|
|
|
2023-08-21 12:23:11 +00:00
|
|
|
maxSharesPerToken = ethers.utils.parseUnits("0.95");
|
2023-08-14 14:06:30 +00:00
|
|
|
|
|
|
|
spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "withdraw",
|
2023-08-21 12:23:11 +00:00
|
|
|
args: [sDAIaddress, maxWithdraw, maxSharesPerToken, dsaWallet0.address, 0, setId]
|
2023-08-14 14:06:30 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
await expect(dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address)).to.be.reverted;
|
|
|
|
|
2023-08-09 15:44:42 +00:00
|
|
|
});
|
2023-08-08 22:20:54 +00:00
|
|
|
});
|
|
|
|
});
|