2024-01-10 16:01:52 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
import hre from "hardhat";
|
|
|
|
import { abis } from "../../../scripts/constant/abis";
|
|
|
|
import { addresses } from "../../../scripts/tests/mainnet/addresses";
|
|
|
|
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
|
|
|
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
|
|
|
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
|
|
|
import { ConnectV2Fluid, ConnectV2Fluid__factory } from "../../../typechain";
|
2024-02-19 21:49:52 +00:00
|
|
|
import { parseEther, parseUnits } from "@ethersproject/units";
|
2024-01-10 16:01:52 +00:00
|
|
|
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
|
|
|
import { constants } from "../../../scripts/constant/constant";
|
2024-02-19 21:49:52 +00:00
|
|
|
import { network, ethers } from "hardhat";
|
2024-01-10 16:01:52 +00:00
|
|
|
import type { Signer, Contract } from "ethers";
|
2024-02-19 21:49:52 +00:00
|
|
|
import { BigNumber } from "bignumber.js";
|
2024-01-10 16:01:52 +00:00
|
|
|
|
|
|
|
describe("Fluid", function () {
|
|
|
|
const connectorName = "FLUID";
|
|
|
|
let connector: any;
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
let wallet0: Signer, wallet1: Signer, wstethHolderSigner: Signer;
|
|
|
|
let nftId = "6";
|
2024-01-10 16:01:52 +00:00
|
|
|
let dsaWallet0: any;
|
2024-02-19 21:49:52 +00:00
|
|
|
let instaConnectorsV2: any;
|
2024-01-10 16:01:52 +00:00
|
|
|
let masterSigner: Signer;
|
2024-02-19 21:49:52 +00:00
|
|
|
|
|
|
|
const vaultWstethEth = "0x28680f14C4Bb86B71119BC6e90E4e6D87E6D1f51";
|
|
|
|
|
|
|
|
const wstethHolder = "0x17170904077C84F26c190eC05fF414B7045F4652";
|
|
|
|
|
|
|
|
const WSTETH = "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0";
|
|
|
|
|
|
|
|
const erc20Abi = [
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
name: "_spender",
|
|
|
|
type: "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "_value",
|
|
|
|
type: "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
name: "approve",
|
|
|
|
outputs: [
|
|
|
|
{
|
|
|
|
name: "",
|
|
|
|
type: "bool"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "nonpayable",
|
|
|
|
type: "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
constant: true,
|
|
|
|
inputs: [],
|
|
|
|
name: "totalSupply",
|
|
|
|
outputs: [
|
|
|
|
{
|
|
|
|
name: "",
|
|
|
|
type: "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "view",
|
|
|
|
type: "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
constant: true,
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
name: "_owner",
|
|
|
|
type: "address"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
name: "balanceOf",
|
|
|
|
outputs: [
|
|
|
|
{
|
|
|
|
name: "balance",
|
|
|
|
type: "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "view",
|
|
|
|
type: "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
name: "_to",
|
|
|
|
type: "address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "_value",
|
|
|
|
type: "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
name: "transfer",
|
|
|
|
outputs: [
|
|
|
|
{
|
|
|
|
name: "",
|
|
|
|
type: "bool"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "nonpayable",
|
|
|
|
type: "function"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const wstethToken = new ethers.Contract(WSTETH, erc20Abi);
|
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
before(async () => {
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_reset",
|
|
|
|
params: [
|
|
|
|
{
|
|
|
|
forking: {
|
|
|
|
// @ts-ignore
|
|
|
|
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
2024-02-19 21:49:52 +00:00
|
|
|
blockNumber: 19261868,
|
2024-01-10 16:01:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2024-02-19 21:49:52 +00:00
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
[wallet0, wallet1] = await ethers.getSigners();
|
|
|
|
masterSigner = await getMasterSigner();
|
|
|
|
instaConnectorsV2 = await ethers.getContractAt(
|
|
|
|
abis.core.connectorsV2,
|
2024-02-19 21:49:52 +00:00
|
|
|
addresses.core.connectorsV2,
|
|
|
|
masterSigner
|
2024-01-10 16:01:52 +00:00
|
|
|
);
|
2024-02-19 21:49:52 +00:00
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
connector = await deployAndEnableConnector({
|
|
|
|
connectorName,
|
|
|
|
contractArtifact: ConnectV2Fluid__factory,
|
|
|
|
signer: masterSigner,
|
|
|
|
connectors: instaConnectorsV2,
|
|
|
|
});
|
|
|
|
console.log("Connector address", connector.address);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have contracts deployed", async () => {
|
|
|
|
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.getAddress());
|
|
|
|
expect(!!dsaWallet0.address).to.be.true;
|
|
|
|
});
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
it("Deposit 20 Wsteth into DSA wallet", async function () {
|
2024-01-10 16:01:52 +00:00
|
|
|
await wallet0.sendTransaction({
|
2024-02-19 21:49:52 +00:00
|
|
|
to: wstethHolder,
|
|
|
|
value: parseEther("200"),
|
|
|
|
});
|
|
|
|
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_impersonateAccount",
|
|
|
|
params: [wstethHolder]
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
2024-02-19 21:49:52 +00:00
|
|
|
|
|
|
|
wstethHolderSigner = await ethers.getSigner(wstethHolder);
|
|
|
|
|
|
|
|
await wstethToken.connect(wstethHolderSigner).transfer(dsaWallet0.address, ethers.utils.parseEther("20"));
|
|
|
|
|
|
|
|
expect(await wstethToken.connect(wstethHolderSigner).balanceOf(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("20"));
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
// 200 wsteth
|
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
describe("Main", function () {
|
2024-02-19 21:49:52 +00:00
|
|
|
it("should deposit 10 wsteth in Fluid", async function () {
|
|
|
|
const amtDeposit = parseEther("10");
|
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
2024-02-19 21:49:52 +00:00
|
|
|
method: "operate",
|
|
|
|
args: [
|
|
|
|
vaultWstethEth,
|
|
|
|
'0', // new nft
|
|
|
|
amtDeposit, // +10 collateral
|
|
|
|
'0', // 0 debt
|
|
|
|
'0'
|
|
|
|
],
|
2024-01-10 16:01:52 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0
|
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet1.getAddress());
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
const receipt = await tx.wait();
|
|
|
|
|
|
|
|
const eventName = "LogOperate(address,uint256,int256,int256)";
|
|
|
|
const eventSignatureHash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(eventName));
|
2024-01-10 16:01:52 +00:00
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
const log = receipt.logs.find((log: { topics: string[]; }) => log.topics[0] === eventSignatureHash);
|
|
|
|
|
|
|
|
// expect(await ethers.provider.getBalance(dsaWallet0.address)).to.gte(
|
|
|
|
// parseEther("1000")
|
|
|
|
// );
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
2024-02-19 21:49:52 +00:00
|
|
|
// 90 wsteth
|
2024-01-10 16:01:52 +00:00
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
it("should deposit max wsteth in Fluid", async function () {
|
2024-01-10 16:01:52 +00:00
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
2024-02-19 21:49:52 +00:00
|
|
|
method: "operate",
|
|
|
|
args: [
|
|
|
|
vaultWstethEth, // matic-usdc vault
|
|
|
|
nftId, // new nft
|
|
|
|
ethers.constants.MaxInt256, // + max collateral
|
|
|
|
0, // 0 debt
|
|
|
|
0
|
|
|
|
],
|
2024-01-10 16:01:52 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0
|
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet1.getAddress());
|
2024-02-19 21:49:52 +00:00
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
await tx.wait();
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
// expect(await ethers.provider.getBalance(dsaWallet0.address)).to.lte(
|
|
|
|
// parseEther("1")
|
|
|
|
// );
|
|
|
|
});
|
2024-01-10 16:01:52 +00:00
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
// 0 wsteth
|
2024-01-10 16:01:52 +00:00
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
it("Should borrow 1 ETH", async function () {
|
|
|
|
const amtBorrow = parseEther("1"); // 1 eth
|
|
|
|
|
|
|
|
const spells = [
|
2024-01-10 16:01:52 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
2024-02-19 21:49:52 +00:00
|
|
|
method: "operate",
|
|
|
|
args: [
|
|
|
|
vaultWstethEth,
|
|
|
|
nftId,
|
|
|
|
0,
|
|
|
|
amtBorrow,
|
|
|
|
0
|
|
|
|
],
|
2024-01-10 16:01:52 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
const tx = await dsaWallet0
|
2024-01-10 16:01:52 +00:00
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet1.getAddress());
|
|
|
|
await tx.wait();
|
2024-02-19 21:49:52 +00:00
|
|
|
|
|
|
|
// expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
|
|
|
|
// parseEther("0.099")
|
|
|
|
// );
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
// 0 wsteth, 1 eth
|
|
|
|
|
|
|
|
it("Should payback max eth", async function () {
|
|
|
|
await network.provider.send("hardhat_setBalance", [
|
|
|
|
dsaWallet0.address,
|
|
|
|
ethers.utils.parseEther("2.0").toHexString(),
|
|
|
|
]);
|
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
2024-02-19 21:49:52 +00:00
|
|
|
method: "operate",
|
|
|
|
args: [
|
|
|
|
vaultWstethEth,
|
|
|
|
nftId,
|
|
|
|
0,
|
|
|
|
ethers.constants.MinInt256,
|
|
|
|
new BigNumber(parseEther("1").toString()),
|
|
|
|
],
|
2024-01-10 16:01:52 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0
|
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet1.getAddress());
|
|
|
|
await tx.wait();
|
2024-02-19 21:49:52 +00:00
|
|
|
|
|
|
|
// expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
|
|
|
|
// ethers.utils.parseEther("0.2")
|
|
|
|
// );
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
// 0 wsteth, 1 eth
|
|
|
|
|
|
|
|
it("Should withdraw 2 wsteth", async function () {
|
|
|
|
const amt = new BigNumber(parseEther("2").toString()).multipliedBy(-1); // 100 Matic
|
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
2024-02-19 21:49:52 +00:00
|
|
|
method: "operate",
|
|
|
|
args: [
|
|
|
|
vaultWstethEth,
|
|
|
|
nftId,
|
|
|
|
amt,
|
|
|
|
0, // 0 debt
|
|
|
|
0
|
|
|
|
],
|
2024-01-10 16:01:52 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0
|
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet1.getAddress());
|
2024-02-19 21:49:52 +00:00
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
await tx.wait();
|
2024-02-19 21:49:52 +00:00
|
|
|
|
|
|
|
// expect(await ethers.provider.getBalance(dsaWallet0.address)).to.gte(
|
|
|
|
// parseEther("100")
|
|
|
|
// );
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
|
|
|
|
2024-02-19 21:49:52 +00:00
|
|
|
// 2 wsteth, 1 eth
|
|
|
|
|
|
|
|
it("Should withdraw max wsteth", async function () {
|
2024-01-10 16:01:52 +00:00
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
2024-02-19 21:49:52 +00:00
|
|
|
method: "operate",
|
|
|
|
args: [
|
|
|
|
vaultWstethEth,
|
|
|
|
nftId,
|
|
|
|
ethers.constants.MinInt256, // min integer value
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
],
|
2024-01-10 16:01:52 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0
|
|
|
|
.connect(wallet0)
|
|
|
|
.cast(...encodeSpells(spells), wallet1.getAddress());
|
2024-02-19 21:49:52 +00:00
|
|
|
|
2024-01-10 16:01:52 +00:00
|
|
|
await tx.wait();
|
2024-02-19 21:49:52 +00:00
|
|
|
|
|
|
|
// expect(await ethers.provider.getBalance(dsaWallet0.address)).to.eq(
|
|
|
|
// parseEther("2000")
|
|
|
|
// );
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
2024-02-19 21:49:52 +00:00
|
|
|
|
|
|
|
// wsteth, 1 eth
|
2024-01-10 16:01:52 +00:00
|
|
|
});
|
|
|
|
});
|