Skip to content

Commit

Permalink
Merge pull request #89 from liquity/test-fixtures
Browse files Browse the repository at this point in the history
Hardhat tests: use loadFixture()
  • Loading branch information
danielattilasimon authored Apr 12, 2024
2 parents 4e391cb + 30c1b78 commit 7f3e73f
Show file tree
Hide file tree
Showing 21 changed files with 569 additions and 851 deletions.
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"lib": "lib"
},
"scripts": {
"test": "hardhat test",
"test": "hardhat test --parallel",
"coverage": "hardhat coverage"
},
"repository": {
Expand Down
71 changes: 32 additions & 39 deletions contracts/test/AccessControlTest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const deploymentHelper = require("../utils/deploymentHelpers.js");
const testHelpers = require("../utils/testHelpers.js");
const { fundAccounts } = require("../utils/fundAccounts.js");
const { TestHelper: th } = require("../utils/testHelpers.js");
const { createDeployAndFundFixture } = require("../utils/testFixtures.js");
const TroveManagerTester = artifacts.require("TroveManagerTester");

const th = testHelpers.TestHelper;
const timeValues = testHelpers.TimeValues;

const dec = th.dec;
const toBN = th.toBN;
const { dec, toBN } = th;

/* The majority of access control tests are contained in this file. However, tests for restrictions
on the Liquity admin address's capabilities during the first year are found in:
Expand All @@ -17,55 +12,53 @@ test/launchSequenceTest/DuringLockupPeriodTest.js */
contract(
"Access Control: Liquity functions with the caller restricted to Liquity contract(s)",
async (accounts) => {
const [owner, alice, bob, carol] = accounts;
const fundedAccounts = accounts.slice(0, 10);

const [owner, alice, bob, carol] = fundedAccounts;
const [bountyAddress, lpRewardsAddress, multisig] = accounts.slice(
997,
1000
);

let coreContracts;
let contracts;

let priceFeed;
let boldToken;
let sortedTroves;
let troveManager;
let nameRegistry;
let activePool;
let stabilityPool;
let defaultPool;
let functionCaller;
let borrowerOperations;

before(async () => {
coreContracts = await deploymentHelper.deployLiquityCore();
coreContracts.troveManager = await TroveManagerTester.new();
// TODO: coreContracts = await deploymentHelper.deployBoldTokenTester(coreContracts)

priceFeed = coreContracts.priceFeed;
boldToken = coreContracts.boldToken;
sortedTroves = coreContracts.sortedTroves;
troveManager = coreContracts.troveManager;
nameRegistry = coreContracts.nameRegistry;
activePool = coreContracts.activePool;
stabilityPool = coreContracts.stabilityPool;
defaultPool = coreContracts.defaultPool;
functionCaller = coreContracts.functionCaller;
borrowerOperations = coreContracts.borrowerOperations;

await deploymentHelper.connectCoreContracts(coreContracts);


await fundAccounts(accounts.slice(0, 10), coreContracts.WETH);

for (const account of accounts.slice(0, 10)) {
await th.openTrove(coreContracts, {
extraBoldAmount: toBN(dec(20000, 18)),
ICR: toBN(dec(2, 18)),
extraParams: { from: account },
});
const deployFixture = createDeployAndFundFixture({
accounts: fundedAccounts,
mocks: { TroveManager: TroveManagerTester },
callback: async (contracts) => {
await Promise.all(fundedAccounts.map(
(account) => th.openTrove(contracts, {
extraBoldAmount: toBN(dec(20000, 18)),
ICR: toBN(dec(2, 18)),
extraParams: { from: account },
})
))
}
});

beforeEach(async () => {
const result = await deployFixture();

contracts = result.contracts;
priceFeed = contracts.priceFeed;
boldToken = contracts.boldToken;
sortedTroves = contracts.sortedTroves;
troveManager = contracts.troveManager;
activePool = contracts.activePool;
stabilityPool = contracts.stabilityPool;
defaultPool = contracts.defaultPool;
borrowerOperations = contracts.borrowerOperations;
});

describe("BorrowerOperations", async (accounts) => {
it("moveETHGainToTrove(): reverts when called by an account that is not StabilityPool", async () => {
// Attempt call from alice
Expand Down
135 changes: 59 additions & 76 deletions contracts/test/BorrowerOperationsTest.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
const deploymentHelper = require("../utils/deploymentHelpers.js");
const { fundAccounts } = require("../utils/fundAccounts.js");
const testHelpers = require("../utils/testHelpers.js");
const { TestHelper: th } = require("../utils/testHelpers.js");
const { createDeployAndFundFixture } = require("../utils/testFixtures.js");

const BorrowerOperationsTester = artifacts.require(
"./BorrowerOperationsTester.sol"
);
const TroveManagerTester = artifacts.require("TroveManagerTester");

const th = testHelpers.TestHelper;

const dec = th.dec;
const toBN = th.toBN;
const mv = testHelpers.MoneyValues;
const timeValues = testHelpers.TimeValues;

const ZERO_ADDRESS = th.ZERO_ADDRESS;
const assertRevert = th.assertRevert;
const { dec, toBN, assertRevert } = th;

contract("BorrowerOperations", async (accounts) => {
const accountsToFund = accounts.slice(0, 17);

const [
owner,
alice,
Expand All @@ -36,21 +29,23 @@ contract("BorrowerOperations", async (accounts) => {
frontEnd_1,
frontEnd_2,
frontEnd_3,
] = accounts;
] = accountsToFund;

const [bountyAddress, lpRewardsAddress, multisig] = accounts.slice(997, 1000);

let contracts

let priceFeed;
let boldToken;
let sortedTroves;
let troveManager;
let activePool;
let stabilityPool;
let defaultPool;
let borrowerOperations;
let lqtyToken;

let contracts;
let BOLD_GAS_COMPENSATION;
let MIN_NET_DEBT;
let BORROWING_FEE_FLOOR;

const getOpenTroveBoldAmount = async (totalDebt) =>
th.getOpenTroveBoldAmount(contracts, totalDebt);
Expand All @@ -65,57 +60,47 @@ contract("BorrowerOperations", async (accounts) => {
th.getTroveEntireDebt(contracts, trove);
const getTroveStake = async (trove) => th.getTroveStake(contracts, trove);

let BOLD_GAS_COMPENSATION;
let MIN_NET_DEBT;
let BORROWING_FEE_FLOOR;

before(async () => {});
const deployFixture = createDeployAndFundFixture({
accounts: accountsToFund,
mocks: {
BorrowerOperations: BorrowerOperationsTester,
TroveManager: TroveManagerTester,
},
callback: async (contracts) => {
const { borrowerOperations } = contracts;
const [
BOLD_GAS_COMPENSATION,
MIN_NET_DEBT,
BORROWING_FEE_FLOOR,
] = await Promise.all([
borrowerOperations.BOLD_GAS_COMPENSATION(),
borrowerOperations.MIN_NET_DEBT(),
borrowerOperations.BORROWING_FEE_FLOOR(),
]);
return {
BOLD_GAS_COMPENSATION,
MIN_NET_DEBT,
BORROWING_FEE_FLOOR,
}
},
});

const testCorpus = () => {
beforeEach(async () => {
contracts = await deploymentHelper.deployLiquityCore();
contracts.borrowerOperations = await BorrowerOperationsTester.new(contracts.WETH.address);
contracts.troveManager = await TroveManagerTester.new();
contracts = await deploymentHelper.deployBoldToken(contracts);

await deploymentHelper.connectCoreContracts(contracts);

priceFeed = contracts.priceFeedTestnet;
boldToken = contracts.boldToken;
sortedTroves = contracts.sortedTroves;
troveManager = contracts.troveManager;
activePool = contracts.activePool;
stabilityPool = contracts.stabilityPool;
defaultPool = contracts.defaultPool;
borrowerOperations = contracts.borrowerOperations;
hintHelpers = contracts.hintHelpers;

BOLD_GAS_COMPENSATION = await borrowerOperations.BOLD_GAS_COMPENSATION();
MIN_NET_DEBT = await borrowerOperations.MIN_NET_DEBT();
BORROWING_FEE_FLOOR = await borrowerOperations.BORROWING_FEE_FLOOR();

await fundAccounts([
owner,
alice,
bob,
carol,
dennis,
whale,
A,
B,
C,
D,
E,
F,
G,
H,
frontEnd_1,
frontEnd_2,
frontEnd_3,
bountyAddress,
lpRewardsAddress,
multisig,
], contracts.WETH);
const result = await deployFixture()

contracts = result.contracts
priceFeed = contracts.priceFeedTestnet
boldToken = contracts.boldToken
sortedTroves = contracts.sortedTroves
troveManager = contracts.troveManager
activePool = contracts.activePool
defaultPool = contracts.defaultPool
borrowerOperations = contracts.borrowerOperations

BOLD_GAS_COMPENSATION = result.BOLD_GAS_COMPENSATION
MIN_NET_DEBT = result.MIN_NET_DEBT
BORROWING_FEE_FLOOR = result.BORROWING_FEE_FLOOR
});

it("addColl(): reverts when top-up would leave trove with ICR < MCR", async () => {
Expand Down Expand Up @@ -4401,7 +4386,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4460,7 +4445,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4519,7 +4504,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4577,7 +4562,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4636,7 +4621,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4695,7 +4680,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4754,7 +4739,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4813,7 +4798,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4872,7 +4857,7 @@ contract("BorrowerOperations", async (accounts) => {
);

const liqPrice = th.toBN(dec(100,18))
th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
// th.logBN("Bob ICR before liq", await troveManager.getCurrentICR(bob, liqPrice))
await priceFeed.setPrice(liqPrice);
// Confirm we are in Normal Mode
assert.isFalse(await troveManager.checkRecoveryMode(liqPrice))
Expand Down Expand Up @@ -4904,9 +4889,7 @@ contract("BorrowerOperations", async (accounts) => {
});
};

describe("Test", async () => {
testCorpus();
});
testCorpus();
});

contract("Reset chain state", async (accounts) => {});
Expand Down
Loading

0 comments on commit 7f3e73f

Please sign in to comment.