2020-10-19 23:16:23 +00:00
|
|
|
const {expect} = require("chai");
|
2020-10-19 16:17:27 +00:00
|
|
|
const hre = require("hardhat");
|
|
|
|
const {ethers} = hre;
|
2020-10-19 23:16:23 +00:00
|
|
|
|
2020-11-03 18:18:06 +00:00
|
|
|
const GelatoCoreLib = require("@gelatonetwork/core");
|
|
|
|
|
2020-10-19 23:16:23 +00:00
|
|
|
// #region Contracts ABI
|
|
|
|
|
2020-10-23 19:35:20 +00:00
|
|
|
const ConnectMaker = require("../../pre-compiles/ConnectMaker.json");
|
|
|
|
const GetCdps = require("../../pre-compiles/GetCdps.json");
|
|
|
|
const DssCdpManager = require("../../pre-compiles/DssCdpManager.json");
|
|
|
|
const ConnectBasic = require("../../pre-compiles/ConnectBasic.json");
|
|
|
|
const InstaList = require("../../pre-compiles/InstaList.json");
|
|
|
|
const InstaAccount = require("../../pre-compiles/InstaAccount.json");
|
|
|
|
const InstaIndex = require("../../pre-compiles/InstaIndex.json");
|
|
|
|
const IERC20 = require("../../pre-compiles/IERC20.json");
|
|
|
|
const InstaConnector = require("../../pre-compiles/InstaConnectors.json");
|
2020-10-19 23:16:23 +00:00
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
2020-10-19 16:17:27 +00:00
|
|
|
describe("ConnectGelatoProviderPayment Unit Test", function () {
|
2020-10-19 23:16:23 +00:00
|
|
|
this.timeout(0);
|
2020-10-19 16:17:27 +00:00
|
|
|
if (hre.network.name !== "hardhat") {
|
|
|
|
console.error("Test Suite is meant to be run on hardhat only");
|
2020-10-19 23:16:23 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let userWallet;
|
|
|
|
let userAddress;
|
|
|
|
let providerWallet;
|
|
|
|
let providerAddress;
|
|
|
|
|
2020-11-03 18:18:06 +00:00
|
|
|
let gelatoCore;
|
|
|
|
|
2020-10-19 23:16:23 +00:00
|
|
|
let instaList;
|
|
|
|
let instaIndex;
|
2020-11-02 10:51:49 +00:00
|
|
|
let DAI;
|
2020-10-19 23:16:23 +00:00
|
|
|
let instaConnectors;
|
|
|
|
let instaMaster;
|
|
|
|
let connectBasic;
|
|
|
|
let getCdps;
|
|
|
|
let dssCdpManager;
|
|
|
|
|
|
|
|
let connectGelatoProviderPayment;
|
|
|
|
|
|
|
|
let dsa;
|
|
|
|
let cdpId;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
// Get Test Wallet for local testnet
|
|
|
|
[userWallet] = await ethers.getSigners();
|
|
|
|
userAddress = await userWallet.getAddress();
|
|
|
|
|
|
|
|
[, providerWallet] = await ethers.getSigners();
|
|
|
|
providerAddress = await providerWallet.getAddress();
|
|
|
|
|
|
|
|
instaMaster = await ethers.provider.getSigner(
|
2020-10-19 16:17:27 +00:00
|
|
|
hre.network.config.InstaMaster
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
|
2020-11-03 18:18:06 +00:00
|
|
|
gelatoCore = await ethers.getContractAt(
|
|
|
|
GelatoCoreLib.GelatoCore.abi,
|
|
|
|
hre.network.config.GelatoCore
|
|
|
|
);
|
|
|
|
|
2020-10-19 16:17:27 +00:00
|
|
|
// Hardhat default accounts prefilled with 100 ETH
|
2020-10-19 23:16:23 +00:00
|
|
|
expect(await userWallet.getBalance()).to.be.gt(
|
|
|
|
ethers.utils.parseEther("10")
|
|
|
|
);
|
|
|
|
|
|
|
|
instaIndex = await ethers.getContractAt(
|
|
|
|
InstaIndex.abi,
|
2020-10-19 16:17:27 +00:00
|
|
|
hre.network.config.InstaIndex
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
instaList = await ethers.getContractAt(
|
|
|
|
InstaList.abi,
|
2020-10-19 16:17:27 +00:00
|
|
|
hre.network.config.InstaList
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
connectBasic = await ethers.getContractAt(
|
|
|
|
ConnectBasic.abi,
|
2020-10-19 16:17:27 +00:00
|
|
|
hre.network.config.ConnectBasic
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
instaConnectors = await ethers.getContractAt(
|
|
|
|
InstaConnector.abi,
|
2020-10-19 16:17:27 +00:00
|
|
|
hre.network.config.InstaConnectors
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
getCdps = await ethers.getContractAt(
|
|
|
|
GetCdps.abi,
|
2020-10-19 16:17:27 +00:00
|
|
|
hre.network.config.GetCdps
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
dssCdpManager = await ethers.getContractAt(
|
|
|
|
DssCdpManager.abi,
|
2020-10-19 16:17:27 +00:00
|
|
|
hre.network.config.DssCdpManager
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
2020-11-02 10:51:49 +00:00
|
|
|
DAI = await ethers.getContractAt(IERC20.abi, hre.network.config.DAI);
|
2020-10-19 23:16:23 +00:00
|
|
|
|
|
|
|
// ========== Test Setup ============
|
|
|
|
|
|
|
|
const connectorLength = await instaConnectors.connectorLength();
|
|
|
|
const connectorId = connectorLength.add(1);
|
|
|
|
|
|
|
|
const ConnectGelatoProviderPayment = await ethers.getContractFactory(
|
|
|
|
"ConnectGelatoProviderPayment"
|
|
|
|
);
|
|
|
|
connectGelatoProviderPayment = await ConnectGelatoProviderPayment.deploy(
|
2020-11-03 11:48:00 +00:00
|
|
|
connectorId,
|
|
|
|
ethers.constants.AddressZero
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
connectGelatoProviderPayment.deployed();
|
|
|
|
|
2020-10-19 16:17:27 +00:00
|
|
|
await userWallet.sendTransaction({
|
|
|
|
to: hre.network.config.InstaMaster,
|
|
|
|
value: ethers.utils.parseEther("0.1"),
|
|
|
|
});
|
|
|
|
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_impersonateAccount",
|
|
|
|
params: [await instaMaster.getAddress()],
|
|
|
|
});
|
|
|
|
|
2020-10-19 23:16:23 +00:00
|
|
|
await instaConnectors
|
|
|
|
.connect(instaMaster)
|
|
|
|
.enable(connectGelatoProviderPayment.address);
|
|
|
|
|
2020-10-19 16:17:27 +00:00
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_stopImpersonatingAccount",
|
|
|
|
params: [await instaMaster.getAddress()],
|
2020-10-19 23:16:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
await instaConnectors.isConnector([connectGelatoProviderPayment.address])
|
|
|
|
).to.be.true;
|
|
|
|
|
|
|
|
// ========== Create DeFi Smart Account for User account ============
|
|
|
|
|
|
|
|
const dsaAccountCount = await instaList.accounts();
|
|
|
|
|
|
|
|
await expect(instaIndex.build(userAddress, 1, userAddress)).to.emit(
|
|
|
|
instaIndex,
|
|
|
|
"LogAccountCreated"
|
|
|
|
);
|
|
|
|
const dsaID = dsaAccountCount.add(1);
|
|
|
|
await expect(await instaList.accounts()).to.be.equal(dsaID);
|
|
|
|
|
|
|
|
// ========== Instantiate the DSA ============
|
|
|
|
dsa = await ethers.getContractAt(
|
|
|
|
InstaAccount.abi,
|
|
|
|
await instaList.accountAddr(dsaID)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-11-03 11:48:00 +00:00
|
|
|
it("#1: payProvider should return error message ConnectGelatoProviderPayment.payProvider:INVALIDADDESS when provider is Zero Address", async function () {
|
|
|
|
await expect(
|
|
|
|
dsa.cast(
|
|
|
|
[connectBasic.address, connectGelatoProviderPayment.address],
|
|
|
|
[
|
|
|
|
await hre.run("abi-encode-withselector", {
|
|
|
|
abi: ConnectBasic.abi,
|
|
|
|
functionname: "deposit",
|
|
|
|
inputs: [
|
|
|
|
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
|
|
|
|
ethers.utils.parseEther("1"),
|
|
|
|
0,
|
|
|
|
"105",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
await hre.run("abi-encode-withselector", {
|
|
|
|
abi: (
|
|
|
|
await hre.artifacts.readArtifact("ConnectGelatoProviderPayment")
|
|
|
|
).abi,
|
|
|
|
functionname: "payProvider",
|
|
|
|
inputs: ["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", 0, "105", 0],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
userAddress,
|
|
|
|
{
|
|
|
|
value: ethers.utils.parseEther("1"),
|
|
|
|
}
|
|
|
|
)
|
2020-11-03 13:56:56 +00:00
|
|
|
).to.be.revertedWith("ConnectGelatoProviderPayment.payProvider:!provider");
|
2020-11-03 11:48:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("#2: setProvider should change the provider address", async function () {
|
2020-11-03 13:25:36 +00:00
|
|
|
expect(await connectGelatoProviderPayment.gelatoProvider()).to.be.equal(
|
2020-11-03 11:48:00 +00:00
|
|
|
ethers.constants.AddressZero
|
|
|
|
);
|
|
|
|
|
|
|
|
await connectGelatoProviderPayment.setProvider(providerAddress);
|
|
|
|
|
2020-11-03 13:25:36 +00:00
|
|
|
expect(await connectGelatoProviderPayment.gelatoProvider()).to.be.equal(
|
2020-11-03 11:48:00 +00:00
|
|
|
providerAddress
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("#3: payProvider should pay to Provider 300 Dai", async function () {
|
2020-11-02 10:51:49 +00:00
|
|
|
const providerDAIBalanceBefore = await DAI.balanceOf(providerAddress);
|
2020-10-19 23:16:23 +00:00
|
|
|
|
|
|
|
await dsa.cast(
|
2020-10-19 16:17:27 +00:00
|
|
|
[hre.network.config.ConnectMaker],
|
2020-10-19 23:16:23 +00:00
|
|
|
[
|
2020-10-19 16:17:27 +00:00
|
|
|
await hre.run("abi-encode-withselector", {
|
2020-10-19 23:16:23 +00:00
|
|
|
abi: ConnectMaker.abi,
|
|
|
|
functionname: "open",
|
|
|
|
inputs: ["ETH-A"],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
userAddress
|
|
|
|
);
|
|
|
|
|
2020-10-19 16:17:27 +00:00
|
|
|
const cdps = await getCdps.getCdpsAsc(dssCdpManager.address, dsa.address);
|
2020-10-19 23:16:23 +00:00
|
|
|
cdpId = String(cdps.ids[0]);
|
|
|
|
|
|
|
|
expect(cdps.ids[0].isZero()).to.be.false;
|
|
|
|
|
|
|
|
await dsa.cast(
|
2020-10-19 16:17:27 +00:00
|
|
|
[hre.network.config.ConnectMaker],
|
2020-10-19 23:16:23 +00:00
|
|
|
[
|
2020-10-19 16:17:27 +00:00
|
|
|
await hre.run("abi-encode-withselector", {
|
2020-10-19 23:16:23 +00:00
|
|
|
abi: ConnectMaker.abi,
|
|
|
|
functionname: "deposit",
|
|
|
|
inputs: [cdpId, ethers.utils.parseEther("10"), 0, 0],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
userAddress,
|
|
|
|
{
|
|
|
|
value: ethers.utils.parseEther("10"),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await dsa.cast(
|
2020-10-19 16:17:27 +00:00
|
|
|
[hre.network.config.ConnectMaker],
|
2020-10-19 23:16:23 +00:00
|
|
|
[
|
2020-10-19 16:17:27 +00:00
|
|
|
await hre.run("abi-encode-withselector", {
|
2020-10-19 23:16:23 +00:00
|
|
|
abi: ConnectMaker.abi,
|
|
|
|
functionname: "borrow",
|
|
|
|
inputs: [cdpId, ethers.utils.parseUnits("1000", 18), 0, 0],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
userAddress
|
|
|
|
);
|
|
|
|
|
2020-11-02 10:51:49 +00:00
|
|
|
expect(await DAI.balanceOf(dsa.address)).to.be.equal(
|
2020-10-19 23:16:23 +00:00
|
|
|
ethers.utils.parseEther("1000")
|
|
|
|
);
|
|
|
|
|
|
|
|
await dsa.cast(
|
|
|
|
[connectGelatoProviderPayment.address],
|
|
|
|
[
|
2020-10-19 16:17:27 +00:00
|
|
|
await hre.run("abi-encode-withselector", {
|
|
|
|
abi: (
|
|
|
|
await hre.artifacts.readArtifact("ConnectGelatoProviderPayment")
|
|
|
|
).abi,
|
2020-10-19 23:16:23 +00:00
|
|
|
functionname: "payProvider",
|
2020-11-03 11:48:00 +00:00
|
|
|
inputs: [DAI.address, ethers.utils.parseUnits("300", 18), 0, 0],
|
2020-10-19 23:16:23 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
userAddress
|
|
|
|
);
|
|
|
|
|
2020-11-02 10:51:49 +00:00
|
|
|
expect(await DAI.balanceOf(providerAddress)).to.be.equal(
|
2020-10-19 23:16:23 +00:00
|
|
|
providerDAIBalanceBefore.add(ethers.utils.parseUnits("300", 18))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-11-03 11:48:00 +00:00
|
|
|
it("#4: payProvider should pay to Provider 1 ether", async function () {
|
2020-11-03 18:18:06 +00:00
|
|
|
const providerBalanceOnGelatoCoreBefore = await gelatoCore.providerFunds(
|
|
|
|
providerAddress
|
|
|
|
);
|
2020-10-19 23:16:23 +00:00
|
|
|
|
|
|
|
await dsa.cast(
|
|
|
|
[connectBasic.address, connectGelatoProviderPayment.address],
|
|
|
|
[
|
2020-10-19 16:17:27 +00:00
|
|
|
await hre.run("abi-encode-withselector", {
|
2020-10-19 23:16:23 +00:00
|
|
|
abi: ConnectBasic.abi,
|
|
|
|
functionname: "deposit",
|
|
|
|
inputs: [
|
|
|
|
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
|
|
|
|
ethers.utils.parseEther("1"),
|
|
|
|
0,
|
|
|
|
"105",
|
|
|
|
],
|
|
|
|
}),
|
2020-10-19 16:17:27 +00:00
|
|
|
await hre.run("abi-encode-withselector", {
|
|
|
|
abi: (
|
|
|
|
await hre.artifacts.readArtifact("ConnectGelatoProviderPayment")
|
|
|
|
).abi,
|
2020-10-19 23:16:23 +00:00
|
|
|
functionname: "payProvider",
|
2020-11-03 11:48:00 +00:00
|
|
|
inputs: ["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", 0, "105", 0],
|
2020-10-19 23:16:23 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
userAddress,
|
|
|
|
{
|
|
|
|
value: ethers.utils.parseEther("1"),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-11-03 18:18:06 +00:00
|
|
|
expect(await gelatoCore.providerFunds(providerAddress)).to.be.equal(
|
|
|
|
providerBalanceOnGelatoCoreBefore.add(ethers.utils.parseEther("1"))
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|