Skip to content

Commit

Permalink
Fixing tests including:
Browse files Browse the repository at this point in the history
- Calling overloaded functions with different syntax due to an issue with truffle
- Changing .mul to BigNumber.times
- Change ethers.utils.parseEther to web3.utils.toWei
- Fix an issue finding events from the Relayer
  • Loading branch information
elenadimitrova committed Oct 16, 2020
1 parent f9aca80 commit 5031307
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 159 deletions.
10 changes: 5 additions & 5 deletions scripts/deploy_defi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const DSValue = artifacts.require("DSValue");
const RAY = new BigNumber("1000000000000000000000000000"); // 10**27
const WAD = new BigNumber("1000000000000000000"); // 10**18
const USD_PER_DAI = RAY; // 1 DAI = 1 USD
const USD_PER_ETH = WAD.mul(250); // 1 ETH = 250 USD
const USD_PER_MKR = WAD.mul(700); // 1 MKR = 700 USD
const USD_PER_ETH = WAD.times(250); // 1 ETH = 250 USD
const USD_PER_MKR = WAD.times(700); // 1 MKR = 700 USD

const ETH_PER_MKR = WAD.mul(USD_PER_MKR).div(USD_PER_ETH); // 1 MKR = 2.8 ETH
const ETH_PER_MKR = WAD.times(USD_PER_MKR).div(USD_PER_ETH); // 1 MKR = 2.8 ETH

async function getTimestamp(deployer) {
const block = await deployer.provider.getBlock("latest");
Expand Down Expand Up @@ -71,7 +71,7 @@ async function deploy() {
// set the total DAI debt ceiling to 50,000 DAI
await tub.mold(formatBytes32String("cap"), parseEther("50000"));
// set the liquidity ratio to 150%
await tub.mold(formatBytes32String("mat"), RAY.mul(3).div(2));
await tub.mold(formatBytes32String("mat"), RAY.times(3).div(2));
// set the governance fee to 7.5% APR
await tub.mold(formatBytes32String("fee"), "1000000002293273137447730714", { gasLimit: 150000 });
// set the liquidation penalty to 13%
Expand All @@ -86,7 +86,7 @@ async function deploy() {
/* *************** create MKR exchange ***************** */

const ethLiquidity = parseEther("1");
const mkrLiquidity = ethLiquidity.mul(WAD).div(ETH_PER_MKR);
const mkrLiquidity = ethLiquidity.times(WAD).div(ETH_PER_MKR);
await gov["mint(address,uint256)"](manager, mkrLiquidity);

await uniswapFactory.createExchange(gov.address, { gasLimit: 450000 });
Expand Down
29 changes: 15 additions & 14 deletions test/baseWallet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global artifacts */
const ethers = require("ethers");
const { BigNumber } = require("bignumber.js");

const Proxy = artifacts.require("Proxy");
const BaseWallet = artifacts.require("BaseWallet");
Expand Down Expand Up @@ -29,13 +30,13 @@ contract("BaseWallet", (accounts) => {
let lockStorage;

async function deployTestModule() {
const module = await deployer.deploy(VersionManager, {},
const module = await VersionManager.new(
registry.address,
lockStorage.address,
guardianStorage.address,
ethers.constants.AddressZero,
ethers.constants.AddressZero);
const feature = await deployer.deploy(TestFeature, {},
const feature = await TestFeature.new(
lockStorage.address,
module.address,
true,
Expand Down Expand Up @@ -64,7 +65,7 @@ contract("BaseWallet", (accounts) => {
it("should register a module with the correct info", async () => {
const name = ethers.utils.formatBytes32String("module1");
await registry.registerModule(module1.address, name);
const isRegistered = await registry.isRegisteredModule(module1.address);
const isRegistered = await registry.contract.methods['isRegisteredModule(address)'](module1.address).call();
assert.isTrue(isRegistered, "module should be registered");
const info = await registry.moduleInfo(module1.address);
assert.equal(name, info, "name should be correct");
Expand All @@ -73,10 +74,10 @@ contract("BaseWallet", (accounts) => {
it("should deregister a module", async () => {
const name = ethers.utils.formatBytes32String("module2");
await registry.registerModule(module2.address, name);
let isRegistered = await registry.isRegisteredModule(module2.address);
let isRegistered = await registry.contract.methods['isRegisteredModule(address)'](module2.address).call();
assert.isTrue(isRegistered, "module should be registered");
await registry.deregisterModule(module2.address);
isRegistered = await registry.isRegisteredModule(module2.address);
isRegistered = await registry.contract.methods['isRegisteredModule(address)'](module2.address).call();
assert.isFalse(isRegistered, "module should be deregistered");
});

Expand Down Expand Up @@ -148,7 +149,7 @@ contract("BaseWallet", (accounts) => {

it("should accept ETH with data", async () => {
const before = await getBalance(wallet.address);
await wallet.send(50000000, { data: 0x1234 });
await web3.eth.sendTransaction({ from: accounts[0], to: wallet.address, data: "0x1234", value: 50000000 });
const after = await getBalance(wallet.address);
assert.equal(after.sub(before).toNumber(), 50000000, "should have received ETH");
});
Expand All @@ -174,10 +175,10 @@ contract("BaseWallet", (accounts) => {
await module1.upgradeWallet(wallet.address, await module1.lastVersion(), { from: owner });
const module1IsAuthorised = await wallet.authorised(module1.address);
assert.equal(module1IsAuthorised, true, "module1 should be authorised");
const walletAsFeature = deployer.wrapDeployedContract(TestFeature, wallet.address);
const boolVal = await walletAsFeature.contract.getBoolean();
const uintVal = await walletAsFeature.contract.getUint();
const addressVal = await walletAsFeature.contract.getAddress(nonowner);
const walletAsFeature = await TestFeature.at(wallet.address);
const boolVal = await walletAsFeature.getBoolean();
const uintVal = await walletAsFeature.getUint();
const addressVal = await walletAsFeature.getAddress(nonowner);
assert.equal(boolVal, true, "should have the correct bool");
assert.equal(uintVal, 42, "should have the correct uint");
assert.equal(addressVal, nonowner, "should have the address");
Expand All @@ -190,7 +191,7 @@ contract("BaseWallet", (accounts) => {
assert.equal(module1IsAuthorised, true, "module1 should be authorised");

// removing module 1
const upgrader = await deployer.deploy(SimpleUpgrader, {},
const upgrader = await SimpleUpgrader.new(
registry.address, lockStorage.address, [module1.address], []);
await registry.registerModule(upgrader.address, ethers.utils.formatBytes32String("Removing module1"));
await module1.addModule(wallet.address, upgrader.address, { from: owner });
Expand All @@ -199,14 +200,14 @@ contract("BaseWallet", (accounts) => {

// trying to execute static call delegated to module1 (it should fail)
const walletAsModule = await TestFeature.at(wallet.address);
await assertRevert(walletAsModule.getBoolean(), "BW: must be an authorised module for static call");
await assertRevert(walletAsModule.contract.methods.getBoolean(), "BW: must be an authorised module for static call");
});
});
});

describe("Old BaseWallet V1.3", () => {
it("should work with new modules", async () => {
const oldWallet = await deployer.deploy(OldWalletV13);
const oldWallet = await OldWalletV13.new();
await oldWallet.init(owner, [module1.address]);
await module1.upgradeWallet(oldWallet.address, await module1.lastVersion(), { from: owner });
await feature1.callDapp(oldWallet.address);
Expand All @@ -217,7 +218,7 @@ contract("BaseWallet", (accounts) => {

describe("Old BaseWallet V1.6", () => {
it("should work with new modules", async () => {
const oldWallet = await deployer.deploy(OldWalletV16);
const oldWallet = await OldWalletV16.new();
await oldWallet.init(owner, [module1.address]);
await module1.upgradeWallet(oldWallet.address, await module1.lastVersion(), { from: owner });
await feature1.callDapp(oldWallet.address);
Expand Down
50 changes: 26 additions & 24 deletions test/compoundManager_invest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global artifacts */
const { parseEther, formatBytes32String } = require("ethers").utils;
const { formatBytes32String } = require("ethers").utils;
const ethers = require("ethers");
const { BigNumber } = require("bignumber.js");
const { BN } = require("bn.js");
const utils = require("../utils/utilities.js");

const GuardianStorage = artifacts.require("GuardianStorage");
Expand All @@ -23,8 +23,8 @@ const CEther = artifacts.require("CEther");
const CErc20 = artifacts.require("CErc20");
const CompoundRegistry = artifacts.require("CompoundRegistry");

const WAD = new BigNumber("1000000000000000000"); // 10**18
const ETH_EXCHANGE_RATE = new BigNumber("200000000000000000000000000");
const WAD = new BN("1000000000000000000"); // 10**18
const ETH_EXCHANGE_RATE = new BN("200000000000000000000000000");

const ERC20 = artifacts.require("TestERC20");

Expand Down Expand Up @@ -62,9 +62,9 @@ contract("Invest Manager with Compound", (accounts) => {
const comptrollerImpl = await Comptroller.new();
await comptrollerProxy._setPendingImplementation(comptrollerImpl.address);
await comptrollerImpl._become(comptrollerProxy.address, oracle.address, WAD.div(10), 5, false);
comptroller = Comptroller.at(comptrollerProxy.address);
comptroller = await Comptroller.at(comptrollerProxy.address);
// deploy Interest rate model
const interestModel = await InterestModel.new(WAD.mul(250).div(10000), WAD.mul(2000).div(10000));
const interestModel = await InterestModel.new(WAD.times(250).div(10000), WAD.times(2000).div(10000));
// deploy CEther
cEther = await CEther.new(
comptrollerProxy.address,
Expand Down Expand Up @@ -100,9 +100,10 @@ contract("Invest Manager with Compound", (accounts) => {
await comptroller._setCollateralFactor(cEther.address, WAD.div(10));

// add liquidity to tokens
await cEther.mint({ value: parseEther("100"), from: liquidityProvider });
await token.approve(cToken.address, parseEther("100"), { from: liquidityProvider });
await cToken.mint(parseEther("10"), { from: liquidityProvider });
const tenEther = await web3.utils.toWei("10", "ether");
await cEther.mint({ value: tenEther, from: liquidityProvider });
await token.approve(cToken.address, tenEther, { from: liquidityProvider });
await cToken.mint(web3.utils.toWei("1", "ether"), { from: liquidityProvider });

/* Deploy Argent Architecture */

Expand Down Expand Up @@ -158,6 +159,7 @@ contract("Invest Manager with Compound", (accounts) => {
const cOracle = await comptroller.oracle();
assert.isTrue(cOracle === oracleProxy.address, "oracle should be registered");
const cTokenPrice = await oracleProxy.getUnderlyingPrice(cToken.address);
console.log("cTokenPrice", cTokenPrice)
assert.isTrue(cTokenPrice.eq(WAD.div(10)), "cToken price should be 1e17");
const cEtherPrice = await oracleProxy.getUnderlyingPrice(cEther.address);
assert.isTrue(cEtherPrice.eq(WAD), "cEther price should be 1e18");
Expand All @@ -170,13 +172,13 @@ contract("Invest Manager with Compound", (accounts) => {
// generate borrows to create interests
await comptroller.enterMarkets([cEther.address, cToken.address], { from: borrower });
if (investInEth) {
await token.approve(cToken.address, parseEther("20"), { from: borrower });
await cToken.mint(parseEther("20"), { from: borrower });
tx = await cEther.borrow(parseEther("0.1"), { from: borrower });
await token.approve(cToken.address, web3.utils.toWei("20", "ether"), { from: borrower });
await cToken.mint(web3.utils.toWei("20", "ether"), { from: borrower });
tx = await cEther.borrow(web3.utils.toWei("0.1", "ether"), { from: borrower });
await utils.hasEvent(tx.receipt, "Borrow");
} else {
await cEther.mint({ value: parseEther("2"), from: borrower });
tx = await cToken.borrow(parseEther("0.1"), { from: borrower });
await cEther.mint({ value: web3.utils.toWei("2", "ether"), from: borrower });
tx = await cToken.borrow(web3.utils.toWei("0.1", "ether"), { from: borrower });
await utils.hasEvent(tx.receipt, "Borrow");
}
// increase time to accumulate interests
Expand All @@ -203,7 +205,7 @@ contract("Invest Manager with Compound", (accounts) => {
txReceipt = tx.receipt;
}

await utils.hasEvent(txReceipt, "InvestmentAdded");
await utils.hasEvent(txReceipt, "CompoundManager.InvestmentAdded");

await accrueInterests(days, investInEth);

Expand All @@ -218,7 +220,7 @@ contract("Invest Manager with Compound", (accounts) => {
txReceipt;
const investInEth = (tokenAddress === ETH_TOKEN);

await addInvestment(tokenAddress, parseEther("0.1"), 365, false);
await addInvestment(tokenAddress, web3.utils.toWei("0.1", "ether"), 365, false);
const before = investInEth ? await cEther.balanceOf(wallet.address) : await cToken.balanceOf(wallet.address);

const params = [wallet.address, tokenAddress, fraction];
Expand All @@ -238,25 +240,25 @@ contract("Invest Manager with Compound", (accounts) => {
// Successes

it("should invest in ERC20 for 1 year and gain interests (blockchain tx)", async () => {
await addInvestment(token.address, parseEther("1"), 365, false);
await addInvestment(token.address, web3.utils.toWei("1", "ether"), 365, false);
});

it("should invest in ERC20 for 1 year and gain interests (relay tx)", async () => {
await addInvestment(token.address, parseEther("1"), 365, true);
await addInvestment(token.address, web3.utils.toWei("1", "ether"), 365, true);
});

it("should invest in ETH for 1 year and gain interests (blockchain tx)", async () => {
await addInvestment(ETH_TOKEN, parseEther("1"), 365, false);
await addInvestment(ETH_TOKEN, web3.utils.toWei("1", "ether"), 365, false);
});

it("should invest in ETH for 1 year and gain interests (relay tx)", async () => {
await addInvestment(ETH_TOKEN, parseEther("1"), 365, true);
await addInvestment(ETH_TOKEN, web3.utils.toWei("1", "ether"), 365, true);
});

// Reverts

it("should fail to invest in ERC20 with an unknown token", async () => {
const params = [wallet.address, ethers.constants.AddressZero, parseEther("1"), 0];
const params = [wallet.address, ethers.constants.AddressZero, web3.utils.toWei("1", "ether"), 0];
await utils.assertRevert(investManager.addInvestment(...params, { from: owner }), "CM: No market for target token");
});

Expand All @@ -266,7 +268,7 @@ contract("Invest Manager with Compound", (accounts) => {
});

it("should fail to invest in ERC20 when not holding any ERC20", async () => {
const params = [wallet.address, token.address, parseEther("1"), 0];
const params = [wallet.address, token.address, web3.utils.toWei("1", "ether"), 0];
await utils.assertRevert(investManager.addInvestment(...params, { from: owner }), "CM: mint failed");
});
});
Expand Down Expand Up @@ -305,8 +307,8 @@ contract("Invest Manager with Compound", (accounts) => {
});

it("should fail to remove all of an ERC20 investment when it collateralizes a loan", async () => {
const collateralAmount = parseEther("1");
const debtAmount = parseEther("0.001");
const collateralAmount = await web3.utils.toWei("1", "ether");
const debtAmount = await web3.utils.toWei("0.001", "ether");
await token.transfer(wallet.address, collateralAmount);
const openLoanParams = [
wallet.address,
Expand Down
Loading

0 comments on commit 5031307

Please sign in to comment.