Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardhat tests: use loadFixture() #89

Merged
merged 29 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7723169
TroveManagerTest: make use of hardhat’s loadFixture()
bpierre Apr 3, 2024
9ceffaa
Fixtures: add BorrowerOperationsTest, TroveManager_LiquidationRewards…
bpierre Apr 4, 2024
717051b
Fixtures: add FeeArithmeticTest
bpierre Apr 4, 2024
d89326f
Merge main
bpierre Apr 4, 2024
0018bde
Merge main
bpierre Apr 4, 2024
4a67996
Fixtures: StabilityPoolTest, PoolsTest
bpierre Apr 5, 2024
6fc3f62
Merge branch 'main' into test-fixtures
bpierre Apr 5, 2024
8853cc5
Merge branch 'main' into test-fixtures
bpierre Apr 7, 2024
57b719a
Fix failing tests by forcing a block to be mined after funding accounts
bpierre Apr 8, 2024
021eec5
createDeployAndFundFixture(): allow to pass mock contracts
bpierre Apr 8, 2024
13fc17f
StabilityPoolTest: move back to forEach()
bpierre Apr 8, 2024
21a899f
fundAccounts(): make sure its promises are awaited
bpierre Apr 8, 2024
76a49c0
Move more tests to the beforeEach() + loadFixture() approach
bpierre Apr 8, 2024
889d5b7
Remove cleanup changes (reduce the PR diff size)
bpierre Apr 8, 2024
262dd70
Run tests in parallel
bpierre Apr 9, 2024
5c1967d
Merge branch 'main' into test-fixtures
bpierre Apr 9, 2024
cb1f4e8
Remove more log from output
bpierre Apr 9, 2024
db1d666
fundAccounts: remove unused code
bpierre Apr 9, 2024
545098f
Align coding style
bpierre Apr 9, 2024
2b9b332
Merge main
bpierre Apr 10, 2024
1091e3b
Minor tweak
bpierre Apr 10, 2024
c5e5c1d
Merge branch 'main' into test-fixtures
bpierre Apr 10, 2024
c434f81
Merge branch 'main' into test-fixtures
bpierre Apr 10, 2024
c093ac2
Merge branch 'main' into test-fixtures
bpierre Apr 10, 2024
95eed5e
Remove debugging code
bpierre Apr 11, 2024
777bd3e
Merge branch 'main' into test-fixtures
bpierre Apr 11, 2024
77b757b
Add missing imports
bpierre Apr 11, 2024
4aac635
Remove output from tests
bpierre Apr 11, 2024
30c1b78
test: add missing import
danielattilasimon Apr 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
131 changes: 58 additions & 73 deletions contracts/test/BorrowerOperationsTest.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
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;

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

const [
owner,
alice,
Expand All @@ -36,21 +31,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 +62,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 +4388,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 +4447,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 +4506,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 +4564,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 +4623,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 +4682,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 +4741,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 +4800,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 +4859,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 +4891,7 @@ contract("BorrowerOperations", async (accounts) => {
});
};

describe("Test", async () => {
testCorpus();
});
testCorpus();
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the describe("test") but kept the testCorpus() call to not mess up with the indentation in this PR.


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