Skip to content

Commit

Permalink
Fix compoundManager invest test
Browse files Browse the repository at this point in the history
  • Loading branch information
elenadimitrova committed Oct 20, 2020
1 parent ea70633 commit 26d129c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"compile:test": "npx truffle compile --config truffle-config-contracts-test.js",
"compile": "npm run compile:infrastructure && npm run compile:modules && npm run compile:wallet",
"cc": "rm -rf build && rm -rf build-legacy && npm run compile:lib && npm run compile && npm run compile:legacy && npm run compile:test",
"ganache": "npx ganache-cli --chainId 1895 --networkId 1597649375983 --gasLimit=20700000 -e 10000 --acctKeys=\"./ganache-accounts.json\" --account=\"0x7ab741b57e8d94dd7e1a29055646bafde7010f38a900f55bbd7647880faa6ee8, 100000000000000000000\" --account=\"0x2030b463177db2da82908ef90fa55ddfcef56e8183caf60db464bc398e736e6f, 100000000000000000000\" --account=\"0x62ecd49c4ccb41a70ad46532aed63cf815de15864bc415c87d507afd6a5e8da2, 100000000000000000000\" --account=\"0xf473040b1a83739a9c7cc1f5719fab0f5bf178f83314d98557c58aae1910e03a, 100000000000000000000\" --account=\"0x823d590ed2cb5e8493bb0efc834771c1cde36f9fc49b9fe3620ebd0754ad6ea2, 100000000000000000000\" --account=\"0xd6d710943471e4c37ceb787857e7a2b41ca57f9cb4307ee9a9b21436a8e709c3, 100000000000000000000\" --account=\"0x187bb12e927c1652377405f81d93ce948a593f7d66cfba383ee761858b05921a, 100000000000000000000\" --account=\"0xf41486fdb04505e7966c8720a353ed92ce0d6830f8a5e915fbde735106a06d25, 100000000000000000000\" --account=\"0x6ca40ba4cca775643398385022264c0c414da1abd21d08d9e7136796a520a543, 100000000000000000000\" --account=\"0xfac0bc9325ad342033afe956e83f0bf8f1e863c1c3e956bc75d66961fe4cd186, 100000000000000000000\"",
"ganache": "npx ganache-cli --chainId 1895 --networkId 1597649375983 --gasLimit=20700000 -e 10000 --acctKeys=\"./ganache-accounts.json\" --account=\"0x7ab741b57e8d94dd7e1a29055646bafde7010f38a900f55bbd7647880faa6ee8, 100000000000000000000000000000000000000\" --account=\"0x2030b463177db2da82908ef90fa55ddfcef56e8183caf60db464bc398e736e6f, 100000000000000000000000000000000000000\" --account=\"0x62ecd49c4ccb41a70ad46532aed63cf815de15864bc415c87d507afd6a5e8da2, 100000000000000000000000000000000000000\" --account=\"0xf473040b1a83739a9c7cc1f5719fab0f5bf178f83314d98557c58aae1910e03a, 100000000000000000000000000000000000000\" --account=\"0x823d590ed2cb5e8493bb0efc834771c1cde36f9fc49b9fe3620ebd0754ad6ea2, 100000000000000000000000000000000000000\" --account=\"0xd6d710943471e4c37ceb787857e7a2b41ca57f9cb4307ee9a9b21436a8e709c3, 100000000000000000000000000000000000000\" --account=\"0x187bb12e927c1652377405f81d93ce948a593f7d66cfba383ee761858b05921a, 100000000000000000000000000000000000000\" --account=\"0xf41486fdb04505e7966c8720a353ed92ce0d6830f8a5e915fbde735106a06d25, 100000000000000000000000000000000000000\" --account=\"0x6ca40ba4cca775643398385022264c0c414da1abd21d08d9e7136796a520a543, 100000000000000000000000000000000000000\" --account=\"0xfac0bc9325ad342033afe956e83f0bf8f1e863c1c3e956bc75d66961fe4cd186, 100000000000000000000000000000000000000\"",
"kovan-fork": "npx ganache-cli -i 42 -l 20700000 -f https://kovan.infura.io/v3/$(cat .env | sed -En 's/INFURA_KEY=''\"''([^''\"'']+)''\"''/\\1/p')@16988040",
"kovan-fork-latest": "npx ganache-cli -i 42 -l 20700000 -f https://kovan.infura.io/v3/$(cat .env | sed -En 's/INFURA_KEY=''\"''([^''\"'']+)''\"''/\\1/p')",
"test": "npx truffle test",
Expand Down
33 changes: 23 additions & 10 deletions test/compoundManager_invest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* global artifacts */
const { formatBytes32String } = require("ethers").utils;
const ethers = require("ethers");
const { BN } = require("bn.js");
const chai = require("chai");
const BN = require("bn.js");
const bnChai = require("bn-chai");

const { expect } = chai;
chai.use(bnChai(BN));
const utils = require("../utils/utilities.js");

const GuardianStorage = artifacts.require("GuardianStorage");
Expand Down Expand Up @@ -61,10 +66,10 @@ contract("Invest Manager with Compound", (accounts) => {
const comptrollerProxy = await Unitroller.new();
const comptrollerImpl = await Comptroller.new();
await comptrollerProxy._setPendingImplementation(comptrollerImpl.address);
await comptrollerImpl._become(comptrollerProxy.address, oracle.address, WAD.div(10), 5, false);
await comptrollerImpl._become(comptrollerProxy.address, oracle.address, WAD.divn(10), 5, false);
comptroller = await Comptroller.at(comptrollerProxy.address);
// deploy Interest rate model
const interestModel = await InterestModel.new(WAD.times(250).div(10000), WAD.times(2000).div(10000));
const interestModel = await InterestModel.new(WAD.muln(250).divn(10000), WAD.muln(2000).divn(10000));
// deploy CEther
cEther = await CEther.new(
comptrollerProxy.address,
Expand All @@ -88,16 +93,16 @@ contract("Invest Manager with Compound", (accounts) => {
18,
);
// add price to Oracle
await oracle.setUnderlyingPrice(cToken.address, WAD.div(10));
await oracle.setUnderlyingPrice(cToken.address, WAD.divn(10));
// list cToken in Comptroller
await comptroller._supportMarket(cEther.address);
await comptroller._supportMarket(cToken.address);
// deploy Price Oracle proxy
oracleProxy = await PriceOracleProxy.new(comptroller.address, oracle.address, cEther.address);
await comptroller._setPriceOracle(oracleProxy.address);
// set collateral factor
await comptroller._setCollateralFactor(cToken.address, WAD.div(10));
await comptroller._setCollateralFactor(cEther.address, WAD.div(10));
await comptroller._setCollateralFactor(cToken.address, WAD.divn(10));
await comptroller._setCollateralFactor(cEther.address, WAD.divn(10));

// add liquidity to tokens
const tenEther = await web3.utils.toWei("10", "ether");
Expand Down Expand Up @@ -159,10 +164,9 @@ 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");
expect(cTokenPrice).to.eq.BN(WAD.divn(10));
const cEtherPrice = await oracleProxy.getUnderlyingPrice(cEther.address);
assert.isTrue(cEtherPrice.eq(WAD), "cEther price should be 1e18");
expect(cEtherPrice).to.eq.BN(WAD);
});
});

Expand Down Expand Up @@ -232,8 +236,17 @@ contract("Invest Manager with Compound", (accounts) => {
}
await utils.hasEvent(txReceipt, "InvestmentRemoved");

// TODO: Manual division result rounding up until https://github.com/indutny/bn.js/issues/79 is added to BN.js
const result = before.muln(10000 - fraction);
const divisionRemainder = new BN(result.modn(10000));

let divisionResult = result.divn(10000);
if (!divisionRemainder.isZero()) {
divisionResult = divisionResult.iaddn(1);
}

const after = investInEth ? await cEther.balanceOf(wallet.address) : await cToken.balanceOf(wallet.address);
assert.isTrue(after.eq(Math.ceil((before * (10000 - fraction)) / 10000)), "should have removed the correct fraction");
expect(after).to.eq.BN(divisionResult);
}

describe("Add Investment", () => {
Expand Down

0 comments on commit 26d129c

Please sign in to comment.