mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
fixed test case logic
This commit is contained in:
parent
df450aa6f1
commit
985a8d3698
|
@ -8,84 +8,34 @@ import { defaultAbiCoder } from "@ethersproject/abi";
|
||||||
import { BigNumber } from "bignumber.js";
|
import { BigNumber } from "bignumber.js";
|
||||||
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
||||||
import { addresses } from "../../../scripts/tests/polygon/addresses";
|
import { addresses } from "../../../scripts/tests/polygon/addresses";
|
||||||
import { tokens } from "../../../scripts/tests/polygon/tokens";
|
|
||||||
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
||||||
import { abis } from "../../../scripts/constant/abis";
|
import { abis } from "../../../scripts/constant/abis";
|
||||||
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
||||||
import { parseEther, parseUnits } from "ethers/lib/utils";
|
import { parseEther, parseUnits } from "ethers/lib/utils";
|
||||||
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
||||||
import encodeFlashcastData from "../../../scripts/tests/encodeFlashcastData";
|
import encodeFlashcastData from "../../../scripts/tests/encodeFlashcastData";
|
||||||
import { ConnectV2AaveV3ImportPermitPolygon__factory } from "../../../typechain";
|
import { ConnectV2AaveV3ImportPermitPolygon__factory, IERC20__factory } from "../../../typechain";
|
||||||
import { parse } from "path/posix";
|
|
||||||
import { aave } from "dsa-connect/dist/abi/connectors/v1";
|
const ABI = [
|
||||||
const { provider } = waffle;
|
"function DOMAIN_SEPARATOR() public view returns (bytes32)",
|
||||||
|
"function balanceOf(address account) public view returns (uint256)",
|
||||||
|
"function nonces(address owner) public view returns (uint256)"
|
||||||
|
];
|
||||||
|
|
||||||
const aDaiAddress = "0x82E64f49Ed5EC1bC6e43DAD4FC8Af9bb3A2312EE";
|
const aDaiAddress = "0x82E64f49Ed5EC1bC6e43DAD4FC8Af9bb3A2312EE";
|
||||||
const aEthAddress = "0xe50fA9b3c56FfB159cB0FCA61F5c9D750e8128c8";
|
|
||||||
const aaveAddress = "0x794a61358D6845594F94dc1DB02A252b5b4814aD";
|
const aaveAddress = "0x794a61358D6845594F94dc1DB02A252b5b4814aD";
|
||||||
const daiAddress = tokens.dai.address;
|
const account = "0xf04adbf75cdfc5ed26eea4bbbb991db002036bdd";
|
||||||
|
const DAI = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063";
|
||||||
|
const USDC = "0x2791bca1f2de4661ed88a30c99a7a9449aa84174";
|
||||||
const mnemonic = "test test test test test test test test test test test junk";
|
const mnemonic = "test test test test test test test test test test test junk";
|
||||||
|
|
||||||
describe("Import Aave", async function () {
|
const token = new ethers.Contract(DAI, IERC20__factory.abi);
|
||||||
|
const aDai = new ethers.Contract(aDaiAddress, ABI);
|
||||||
|
const usdcToken = new ethers.Contract(USDC, IERC20__factory.abi);
|
||||||
|
|
||||||
|
describe("Import Aave", function () {
|
||||||
const connectorName = "AAVE-V3-IMPORT-PERMIT-X";
|
const connectorName = "AAVE-V3-IMPORT-PERMIT-X";
|
||||||
|
let signer: any, wallet0: any;
|
||||||
const aEthAbi = [
|
|
||||||
{
|
|
||||||
inputs: [{ internalType: "address", name: "user", type: "address" }],
|
|
||||||
name: "balanceOf",
|
|
||||||
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
|
|
||||||
stateMutability: "view",
|
|
||||||
type: "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inputs: [
|
|
||||||
{ internalType: "address", name: "caller", type: "address" },
|
|
||||||
{ internalType: "address", name: "onBehalfOf", type: "address" },
|
|
||||||
{ internalType: "uint256", name: "amount", type: "uint256" },
|
|
||||||
{ internalType: "uint256", name: "index", type: "uint256" }
|
|
||||||
],
|
|
||||||
name: "mint",
|
|
||||||
outputs: [{ internalType: "bool", name: "", type: "bool" }],
|
|
||||||
stateMutability: "nonpayable",
|
|
||||||
type: "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inputs: [{ internalType: "address", name: "owner", type: "address" }],
|
|
||||||
name: "nonces",
|
|
||||||
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
|
|
||||||
stateMutability: "view",
|
|
||||||
type: "function"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const aDaiAbi = [
|
|
||||||
{
|
|
||||||
inputs: [{ internalType: "address", name: "user", type: "address" }],
|
|
||||||
name: "balanceOf",
|
|
||||||
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
|
|
||||||
stateMutability: "view",
|
|
||||||
type: "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inputs: [
|
|
||||||
{ internalType: "address", name: "caller", type: "address" },
|
|
||||||
{ internalType: "address", name: "onBehalfOf", type: "address" },
|
|
||||||
{ internalType: "uint256", name: "amount", type: "uint256" },
|
|
||||||
{ internalType: "uint256", name: "index", type: "uint256" }
|
|
||||||
],
|
|
||||||
name: "mint",
|
|
||||||
outputs: [{ internalType: "bool", name: "", type: "bool" }],
|
|
||||||
stateMutability: "nonpayable",
|
|
||||||
type: "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inputs: [{ internalType: "address", name: "owner", type: "address" }],
|
|
||||||
name: "nonces",
|
|
||||||
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
|
|
||||||
stateMutability: "view",
|
|
||||||
type: "function"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const aaveAbi = [
|
const aaveAbi = [
|
||||||
{
|
{
|
||||||
|
@ -179,37 +129,39 @@ describe("Import Aave", async function () {
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const daiAbi = abis.basic.erc20;
|
let aEth: Contract;
|
||||||
|
|
||||||
let aEth: Contract, aDai: Contract, Dai: any;
|
|
||||||
let dsaWallet0: any;
|
let dsaWallet0: any;
|
||||||
let masterSigner: Signer;
|
let masterSigner: Signer;
|
||||||
let instaConnectorsV2: Contract;
|
let instaConnectorsV2: Contract;
|
||||||
let connector: any;
|
let connector: any;
|
||||||
|
|
||||||
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
|
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
|
||||||
console.log(wallet.address);
|
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
// await hre.network.provider.request({
|
await hre.network.provider.request({
|
||||||
// method: "hardhat_reset",
|
method: "hardhat_reset",
|
||||||
// params: [
|
params: [
|
||||||
// {
|
{
|
||||||
// forking: {
|
forking: {
|
||||||
// //@ts-ignore
|
//@ts-ignore
|
||||||
// jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
||||||
// blockNumber: await ethers.provider.getBlockNumber()
|
blockNumber: 26652016
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// ]
|
]
|
||||||
// });
|
});
|
||||||
masterSigner = await getMasterSigner();
|
masterSigner = await getMasterSigner();
|
||||||
console.log(await masterSigner.getAddress());
|
[wallet0] = await ethers.getSigners();
|
||||||
|
await hre.network.provider.send("hardhat_setBalance", [account, ethers.utils.parseEther("10").toHexString()]);
|
||||||
|
|
||||||
await hre.network.provider.send("hardhat_setBalance", [
|
await hre.network.provider.request({
|
||||||
wallet.address,
|
method: "hardhat_impersonateAccount",
|
||||||
ethers.utils.parseEther("10").toHexString()
|
params: [account]
|
||||||
]);
|
});
|
||||||
|
|
||||||
|
signer = await ethers.getSigner(account);
|
||||||
|
|
||||||
|
await token.connect(signer).transfer(wallet0.address, ethers.utils.parseEther("100"));
|
||||||
|
|
||||||
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
|
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
|
||||||
connector = await deployAndEnableConnector({
|
connector = await deployAndEnableConnector({
|
||||||
|
@ -219,41 +171,20 @@ describe("Import Aave", async function () {
|
||||||
connectors: instaConnectorsV2
|
connectors: instaConnectorsV2
|
||||||
});
|
});
|
||||||
|
|
||||||
//check
|
|
||||||
// const signerAddr = "0xDA9dfA130Df4dE4673b89022EE50ff26f6EA73Cf";
|
|
||||||
// await hre.network.provider.request({
|
|
||||||
// method: "hardhat_impersonateAccount",
|
|
||||||
// params: [signerAddr]
|
|
||||||
// });
|
|
||||||
// let sig = await ethers.getSigner(signerAddr);
|
|
||||||
|
|
||||||
console.log("Connector address", connector.address);
|
console.log("Connector address", connector.address);
|
||||||
aEth = new ethers.Contract(aEthAddress, aEthAbi);
|
|
||||||
aDai = new ethers.Contract(aDaiAddress, aDaiAbi);
|
|
||||||
Dai = new ethers.Contract(daiAddress, daiAbi);
|
|
||||||
const aave = new ethers.Contract(aaveAddress, aaveAbi);
|
const aave = new ethers.Contract(aaveAddress, aaveAbi);
|
||||||
|
|
||||||
//deposit ether to aave: ETH-A
|
// approve DAI to aavePool
|
||||||
await aave.connect(wallet).deposit(aEthAddress, parseEther("9"), wallet.address, 3228);
|
await token.connect(wallet0).approve(aaveAddress, parseEther("100"));
|
||||||
|
|
||||||
//borrow
|
//deposit DAI in aave
|
||||||
await aave.connect(wallet).borrow(aDaiAddress, parseUnits("100"), 1, 3228, wallet.address);
|
await aave.connect(wallet0).supply(DAI, parseEther("100"), wallet.address, 3228);
|
||||||
|
console.log("Supplied DAI on aave");
|
||||||
|
|
||||||
// //building dsaWallet
|
//borrow USDC from aave
|
||||||
dsaWallet0 = await buildDSAv2(wallet.address);
|
await aave.connect(wallet0).borrow(USDC, parseUnits("10", 6), 1, 3228, wallet.address);
|
||||||
console.log(dsaWallet0.address);
|
console.log("Borrowed USDC from aave");
|
||||||
await hre.network.provider.send("hardhat_setBalance", [
|
|
||||||
wallet.address,
|
|
||||||
ethers.utils.parseEther("10").toHexString()
|
|
||||||
]);
|
|
||||||
console.log("bro");
|
|
||||||
|
|
||||||
// deposit ETH to dsa wallet
|
|
||||||
await wallet.sendTransaction({
|
|
||||||
to: dsaWallet0.address,
|
|
||||||
value: ethers.utils.parseEther("10")
|
|
||||||
});
|
|
||||||
console.log(await ethers.provider.getBalance(dsaWallet0.address)); //should be 10
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Deployment", async () => {
|
describe("Deployment", async () => {
|
||||||
|
@ -262,39 +193,44 @@ describe("Import Aave", async function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("DSA wallet setup", async () => {
|
||||||
|
it("Should build DSA v2", async () => {
|
||||||
|
dsaWallet0 = await buildDSAv2(wallet.address);
|
||||||
|
expect(!!dsaWallet0.address).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
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("check user AAVE position", async () => {
|
describe("check user AAVE position", async () => {
|
||||||
it("Should check position of user", async () => {
|
it("Should check position of user", async () => {
|
||||||
const exchangeRate = 0.9289;
|
expect(await aDai.connect(wallet0).balanceOf(wallet.address)).to.be.gte(
|
||||||
expect(new BigNumber(await aEth.connect(wallet).balanceOf(wallet.address)).dividedBy(1e8).toFixed(0)).to.eq(
|
new BigNumber(100).multipliedBy(1e18).toString()
|
||||||
new BigNumber(9).dividedBy(exchangeRate).toFixed(0)
|
);
|
||||||
|
|
||||||
|
expect(await usdcToken.connect(wallet0).balanceOf(wallet.address)).to.be.gte(
|
||||||
|
new BigNumber(10).multipliedBy(1e6).toString()
|
||||||
);
|
);
|
||||||
expect(await Dai.connect(wallet).balanceOf(wallet.address)).to.eq("100000000000000000000");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Aave position migration", async () => {
|
describe("Aave position migration", async () => {
|
||||||
it("Should migrate Aave position", async () => {
|
it("Should migrate Aave position", async () => {
|
||||||
const name = "Aave ETH";
|
const DOMAIN_SEPARATOR = await aDai.connect(wallet0).DOMAIN_SEPARATOR();
|
||||||
const chainId = 137;
|
|
||||||
const DOMAIN_SEPARATOR = keccak256(
|
|
||||||
defaultAbiCoder.encode(
|
|
||||||
["bytes32", "bytes32", "bytes32", "uint256", "address"],
|
|
||||||
[
|
|
||||||
keccak256(
|
|
||||||
toUtf8Bytes("EIP712Domain(string name, string version, uint256 chainId, address verifyingContract)")
|
|
||||||
),
|
|
||||||
keccak256(toUtf8Bytes(name)),
|
|
||||||
keccak256(toUtf8Bytes("1")),
|
|
||||||
chainId,
|
|
||||||
aEth.address
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
const PERMIT_TYPEHASH = "0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9";
|
const PERMIT_TYPEHASH = "0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9";
|
||||||
let nonces = await aEth.connect(wallet).nonces(wallet.address);
|
|
||||||
|
let nonces = await aDai.connect(wallet0).nonces(wallet.address);
|
||||||
let nonce = nonces.toNumber();
|
let nonce = nonces.toNumber();
|
||||||
const amount = await aEth.connect(wallet).balanceOf(wallet.address);
|
const amount = new BigNumber(await aDai.connect(wallet0).balanceOf(wallet.address));
|
||||||
const expiry = Date.now() + 20 * 60;
|
const expiry = Date.now() + 20 * 60;
|
||||||
|
|
||||||
const digest = keccak256(
|
const digest = keccak256(
|
||||||
ethers.utils.solidityPack(
|
ethers.utils.solidityPack(
|
||||||
["bytes1", "bytes1", "bytes32", "bytes32"],
|
["bytes1", "bytes1", "bytes32", "bytes32"],
|
||||||
|
@ -305,7 +241,7 @@ describe("Import Aave", async function () {
|
||||||
keccak256(
|
keccak256(
|
||||||
defaultAbiCoder.encode(
|
defaultAbiCoder.encode(
|
||||||
["bytes32", "address", "address", "uint256", "uint256", "uint256"],
|
["bytes32", "address", "address", "uint256", "uint256", "uint256"],
|
||||||
[PERMIT_TYPEHASH, wallet.address, connector.address, amount, nonce, expiry]
|
[PERMIT_TYPEHASH, wallet.address, dsaWallet0.address, amount, nonce, expiry]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
@ -313,7 +249,7 @@ describe("Import Aave", async function () {
|
||||||
);
|
);
|
||||||
const { v, r, s } = ecsign(Buffer.from(digest.slice(2), "hex"), Buffer.from(wallet.privateKey.slice(2), "hex"));
|
const { v, r, s } = ecsign(Buffer.from(digest.slice(2), "hex"), Buffer.from(wallet.privateKey.slice(2), "hex"));
|
||||||
|
|
||||||
const amount0 = new BigNumber("100000007061117456728");
|
const amount0 = new BigNumber(await usdcToken.connect(wallet0).balanceOf(wallet.address));
|
||||||
const amountB = new BigNumber(amount0.toString()).multipliedBy(9).dividedBy(1e4);
|
const amountB = new BigNumber(amount0.toString()).multipliedBy(9).dividedBy(1e4);
|
||||||
const amountWithFee = amount0.plus(amountB);
|
const amountWithFee = amount0.plus(amountB);
|
||||||
|
|
||||||
|
@ -324,8 +260,8 @@ describe("Import Aave", async function () {
|
||||||
args: [
|
args: [
|
||||||
wallet.address,
|
wallet.address,
|
||||||
{
|
{
|
||||||
supplyTokens: ["ETH-A"],
|
supplyTokens: [DAI],
|
||||||
borrowTokens: ["DAI-A"],
|
borrowTokens: [USDC],
|
||||||
convertStable: false,
|
convertStable: false,
|
||||||
flashLoanFees: [amount.toFixed(0)]
|
flashLoanFees: [amount.toFixed(0)]
|
||||||
},
|
},
|
||||||
|
@ -335,7 +271,7 @@ describe("Import Aave", async function () {
|
||||||
{
|
{
|
||||||
connector: "INSTAPOOL-C",
|
connector: "INSTAPOOL-C",
|
||||||
method: "flashPayBack",
|
method: "flashPayBack",
|
||||||
args: [daiAddress, amountWithFee.toFixed(0), 0, 0]
|
args: [USDC, amountWithFee.toFixed(0), 0, 0]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -343,17 +279,16 @@ describe("Import Aave", async function () {
|
||||||
{
|
{
|
||||||
connector: "INSTAPOOL-C",
|
connector: "INSTAPOOL-C",
|
||||||
method: "flashBorrowAndCast",
|
method: "flashBorrowAndCast",
|
||||||
args: [daiAddress, amount0.toString(), 0, encodeFlashcastData(flashSpells), "0x"]
|
args: [USDC, amount0.toString(), 0, encodeFlashcastData(flashSpells), "0x"]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
const tx = await dsaWallet0.connect(wallet).cast(...encodeSpells(spells), wallet.address);
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet.address);
|
||||||
const receipt = await tx.wait();
|
const receipt = await tx.wait();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should check DSA COMPOUND position", async () => {
|
it("Should check DSA AAVE position", async () => {
|
||||||
const ethExchangeRate = 0.9289;
|
expect(await aDai.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(
|
||||||
expect(new BigNumber(await aEth.connect(wallet).balanceOf(dsaWallet0.address)).dividedBy(1e8).toFixed(0)).to.eq(
|
new BigNumber(100).multipliedBy(1e18).toString()
|
||||||
new BigNumber(9).dividedBy(ethExchangeRate).toFixed(0)
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user