dsa-connectors/test/polygon/aave/aaveV3-import-test.ts

369 lines
10 KiB
TypeScript
Raw Normal View History

2022-04-02 09:53:19 +00:00
import { expect, should } from "chai";
import hre, { ethers, waffle } from "hardhat";
import type { Signer, Contract } from "ethers";
import { ecsign, ecrecover, pubToAddress } from "ethereumjs-util";
import { keccak256 } from "@ethersproject/keccak256";
import { toUtf8Bytes } from "@ethersproject/strings";
import { defaultAbiCoder } from "@ethersproject/abi";
import { BigNumber } from "bignumber.js";
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
import { addresses } from "../../../scripts/tests/polygon/addresses";
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
import { abis } from "../../../scripts/constant/abis";
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
import { parseEther, parseUnits } from "ethers/lib/utils";
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
import encodeFlashcastData from "../../../scripts/tests/encodeFlashcastData";
2022-04-02 15:06:31 +00:00
import { ConnectV2AaveV3ImportPermitPolygon__factory, IERC20__factory } from "../../../typechain";
const ABI = [
"function DOMAIN_SEPARATOR() public view returns (bytes32)",
"function balanceOf(address account) public view returns (uint256)",
"function nonces(address owner) public view returns (uint256)"
];
2022-04-02 09:53:19 +00:00
const aDaiAddress = "0x82E64f49Ed5EC1bC6e43DAD4FC8Af9bb3A2312EE";
const aaveAddress = "0x794a61358D6845594F94dc1DB02A252b5b4814aD";
2022-04-02 15:06:31 +00:00
const account = "0xf04adbf75cdfc5ed26eea4bbbb991db002036bdd";
const DAI = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063";
const USDC = "0x2791bca1f2de4661ed88a30c99a7a9449aa84174";
2022-04-02 09:53:19 +00:00
const mnemonic = "test test test test test test test test test test test junk";
2022-04-03 12:01:16 +00:00
const erc20Abi = [
{
2022-04-03 20:31:04 +00:00
constant: false,
inputs: [
2022-04-03 12:01:16 +00:00
{
2022-04-03 20:31:04 +00:00
name: "_spender",
type: "address"
2022-04-03 12:01:16 +00:00
},
{
2022-04-03 20:31:04 +00:00
name: "_value",
type: "uint256"
2022-04-03 12:01:16 +00:00
}
],
2022-04-03 20:31:04 +00:00
name: "approve",
outputs: [
2022-04-03 12:01:16 +00:00
{
2022-04-03 20:31:04 +00:00
name: "",
type: "bool"
2022-04-03 12:01:16 +00:00
}
],
2022-04-03 20:31:04 +00:00
payable: false,
stateMutability: "nonpayable",
type: "function"
2022-04-03 12:01:16 +00:00
},
{
2022-04-03 20:31:04 +00:00
constant: true,
inputs: [],
name: "totalSupply",
outputs: [
2022-04-03 12:01:16 +00:00
{
2022-04-03 20:31:04 +00:00
name: "",
type: "uint256"
2022-04-03 12:01:16 +00:00
}
],
2022-04-03 20:31:04 +00:00
payable: false,
stateMutability: "view",
type: "function"
2022-04-03 12:01:16 +00:00
},
{
2022-04-03 20:31:04 +00:00
constant: true,
inputs: [
2022-04-03 12:01:16 +00:00
{
2022-04-03 20:31:04 +00:00
name: "_owner",
type: "address"
2022-04-03 12:01:16 +00:00
}
],
2022-04-03 20:31:04 +00:00
name: "balanceOf",
outputs: [
2022-04-03 12:01:16 +00:00
{
2022-04-03 20:31:04 +00:00
name: "balance",
type: "uint256"
2022-04-03 12:01:16 +00:00
}
],
2022-04-03 20:31:04 +00:00
payable: false,
stateMutability: "view",
type: "function"
2022-04-03 12:01:16 +00:00
},
{
2022-04-03 20:31:04 +00:00
constant: false,
inputs: [
2022-04-03 12:01:16 +00:00
{
2022-04-03 20:31:04 +00:00
name: "_to",
type: "address"
2022-04-03 12:01:16 +00:00
},
{
2022-04-03 20:31:04 +00:00
name: "_value",
type: "uint256"
2022-04-03 12:01:16 +00:00
}
],
2022-04-03 20:31:04 +00:00
name: "transfer",
outputs: [
2022-04-03 12:01:16 +00:00
{
2022-04-03 20:31:04 +00:00
name: "",
type: "bool"
2022-04-03 12:01:16 +00:00
}
],
2022-04-03 20:31:04 +00:00
payable: false,
stateMutability: "nonpayable",
type: "function"
2022-04-03 12:01:16 +00:00
}
];
2022-04-03 20:11:52 +00:00
2022-04-03 12:01:16 +00:00
const token = new ethers.Contract(DAI, erc20Abi);
2022-04-02 15:06:31 +00:00
const aDai = new ethers.Contract(aDaiAddress, ABI);
2022-04-03 12:01:16 +00:00
const usdcToken = new ethers.Contract(USDC, erc20Abi);
2022-04-02 09:53:19 +00:00
describe("Import Aave V3 with Permit", function () {
2022-04-02 15:06:31 +00:00
const connectorName = "AAVE-V3-IMPORT-PERMIT-X";
let signer: any, wallet0: any;
2022-04-02 09:53:19 +00:00
const aaveAbi = [
{
inputs: [
{
internalType: "address",
name: "asset",
type: "address"
},
{
internalType: "uint256",
name: "amount",
type: "uint256"
},
{
internalType: "uint256",
name: "interestRateMode",
type: "uint256"
},
{
internalType: "uint16",
name: "referralCode",
type: "uint16"
},
{
internalType: "address",
name: "onBehalfOf",
type: "address"
}
],
name: "borrow",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "asset",
type: "address"
},
{
internalType: "uint256",
name: "amount",
type: "uint256"
},
{
internalType: "address",
name: "onBehalfOf",
type: "address"
},
{
internalType: "uint16",
name: "referralCode",
type: "uint16"
}
],
name: "deposit",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "asset",
type: "address"
},
{
internalType: "uint256",
name: "amount",
type: "uint256"
},
{
internalType: "address",
name: "onBehalfOf",
type: "address"
},
{
internalType: "uint16",
name: "referralCode",
type: "uint16"
}
],
name: "supply",
outputs: [],
stateMutability: "nonpayable",
type: "function"
}
];
let dsaWallet0: any;
let masterSigner: Signer;
let instaConnectorsV2: Contract;
let connector: any;
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
before(async () => {
2022-04-02 15:06:31 +00:00
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
//@ts-ignore
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
blockNumber: 26652016
}
}
]
});
2022-04-02 09:53:19 +00:00
masterSigner = await getMasterSigner();
2022-04-02 15:06:31 +00:00
[wallet0] = await ethers.getSigners();
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);
2022-04-02 09:53:19 +00:00
2022-04-03 12:01:16 +00:00
await token.connect(signer).transfer(wallet0.address, ethers.utils.parseEther("8"));
2022-04-02 09:53:19 +00:00
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
connector = await deployAndEnableConnector({
connectorName,
contractArtifact: ConnectV2AaveV3ImportPermitPolygon__factory,
signer: masterSigner,
connectors: instaConnectorsV2
});
2022-04-02 15:06:31 +00:00
const aave = new ethers.Contract(aaveAddress, aaveAbi);
2022-04-02 09:53:19 +00:00
2022-04-02 15:06:31 +00:00
// approve DAI to aavePool
2022-04-03 12:01:16 +00:00
await token.connect(wallet0).approve(aaveAddress, parseEther("8"));
2022-04-02 09:53:19 +00:00
2022-04-02 15:06:31 +00:00
//deposit DAI in aave
2022-04-03 12:01:16 +00:00
await aave.connect(wallet0).supply(DAI, parseEther("8"), wallet.address, 3228);
2022-04-02 15:06:31 +00:00
console.log("Supplied DAI on aave");
2022-04-02 09:53:19 +00:00
2022-04-02 15:06:31 +00:00
//borrow USDC from aave
2022-04-03 12:01:16 +00:00
await aave.connect(wallet0).borrow(USDC, parseUnits("5", 6), 2, 3228, wallet.address);
2022-04-02 15:06:31 +00:00
console.log("Borrowed USDC from aave");
2022-04-02 09:53:19 +00:00
});
describe("Deployment", async () => {
it("Should set correct name", async () => {
expect(await connector.name()).to.eq("Aave-v3-import-permit-v1");
});
});
2022-04-02 15:06:31 +00:00
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,
2022-04-03 12:01:16 +00:00
value: ethers.utils.parseEther("5")
2022-04-02 15:06:31 +00:00
});
2022-04-03 12:01:16 +00:00
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("5"));
2022-04-02 15:06:31 +00:00
});
});
2022-04-02 09:53:19 +00:00
describe("check user AAVE position", async () => {
it("Should check position of user", async () => {
2022-04-02 15:06:31 +00:00
expect(await aDai.connect(wallet0).balanceOf(wallet.address)).to.be.gte(
2022-04-03 12:01:16 +00:00
new BigNumber(8).multipliedBy(1e18).toString()
2022-04-02 15:06:31 +00:00
);
expect(await usdcToken.connect(wallet0).balanceOf(wallet.address)).to.be.gte(
2022-04-03 12:01:16 +00:00
new BigNumber(5).multipliedBy(1e6).toString()
2022-04-02 09:53:19 +00:00
);
});
});
describe("Aave position migration", async () => {
it("Should migrate Aave position", async () => {
2022-04-02 15:06:31 +00:00
const DOMAIN_SEPARATOR = await aDai.connect(wallet0).DOMAIN_SEPARATOR();
2022-04-02 09:53:19 +00:00
const PERMIT_TYPEHASH = "0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9";
2022-04-03 20:31:04 +00:00
2022-04-03 12:01:16 +00:00
let nonce = (await aDai.connect(wallet0).nonces(wallet.address)).toNumber();
2022-04-03 20:11:52 +00:00
//Approving max amount
const amount = ethers.constants.MaxUint256;
2022-04-02 09:53:19 +00:00
const expiry = Date.now() + 20 * 60;
2022-04-02 15:06:31 +00:00
2022-04-02 09:53:19 +00:00
const digest = keccak256(
ethers.utils.solidityPack(
["bytes1", "bytes1", "bytes32", "bytes32"],
[
"0x19",
"0x01",
DOMAIN_SEPARATOR,
keccak256(
defaultAbiCoder.encode(
["bytes32", "address", "address", "uint256", "uint256", "uint256"],
2022-04-02 15:06:31 +00:00
[PERMIT_TYPEHASH, wallet.address, dsaWallet0.address, amount, nonce, expiry]
2022-04-02 09:53:19 +00:00
)
)
]
)
);
const { v, r, s } = ecsign(Buffer.from(digest.slice(2), "hex"), Buffer.from(wallet.privateKey.slice(2), "hex"));
2022-04-02 15:06:31 +00:00
const amount0 = new BigNumber(await usdcToken.connect(wallet0).balanceOf(wallet.address));
2022-04-02 17:09:19 +00:00
const amountB = new BigNumber(amount0.toString()).multipliedBy(5).dividedBy(1e4);
2022-04-02 09:53:19 +00:00
const amountWithFee = amount0.plus(amountB);
const flashSpells = [
{
connector: "AAVE-V3-IMPORT-PERMIT-X",
method: "importAave",
2022-04-03 20:31:04 +00:00
args: [
wallet.address,
[[DAI], [USDC], false, [amountB.toFixed(0)]],
[[ethers.utils.hexlify(v)], [ethers.utils.hexlify(r)], [ethers.utils.hexlify(s)], [expiry]]
]
2022-04-02 09:53:19 +00:00
},
{
connector: "INSTAPOOL-C",
2022-04-02 17:09:19 +00:00
method: "flashPayback",
2022-04-02 15:06:31 +00:00
args: [USDC, amountWithFee.toFixed(0), 0, 0]
2022-04-02 09:53:19 +00:00
}
];
const spells = [
{
connector: "INSTAPOOL-C",
method: "flashBorrowAndCast",
2022-04-02 17:09:19 +00:00
args: [USDC, amount0.toString(), 5, encodeFlashcastData(flashSpells), "0x"]
2022-04-02 09:53:19 +00:00
}
];
2022-04-02 15:06:31 +00:00
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet.address);
2022-04-02 09:53:19 +00:00
const receipt = await tx.wait();
});
2022-04-02 15:06:31 +00:00
it("Should check DSA AAVE position", async () => {
expect(await aDai.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(
2022-04-03 12:01:16 +00:00
new BigNumber(8).multipliedBy(1e18).toString()
2022-04-02 09:53:19 +00:00
);
});
});
});