Skip to content

Commit

Permalink
Fix failing tests by forcing a block to be mined after funding accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre committed Apr 8, 2024
1 parent 8853cc5 commit 57b719a
Showing 1 changed file with 43 additions and 40 deletions.
83 changes: 43 additions & 40 deletions contracts/utils/testFixtures.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers");
const { loadFixture, mine } = require("@nomicfoundation/hardhat-network-helpers");
const deploymentHelper = require("./deploymentHelpers.js");
const { fundAccounts } = require("./fundAccounts.js");
const { TestHelper: th } = require("./testHelpers.js");
Expand All @@ -9,56 +9,59 @@ const { TestHelper: th } = require("./testHelpers.js");
// and after the connection. They take the contracts as a parameter,
// which can be modified as needed. The returned object will be
// merged with the object returned by the fixture loader utility.
function createDeployAndFundFixture(accounts, {
afterDeploy = async () => null,
afterConnect = async () => null,
function createDeployAndFundFixture({
accounts = [],
callback = async () => null,
mocks = {}, // e.g. { Contract: MockContract }
} = {}) {
const fixture = async () => {
const contracts = await deploymentHelper.deployLiquityCore();

const afterDeployResult = await afterDeploy(contracts);

await deploymentHelper.deployBoldToken(contracts);
const contracts = await deploymentHelper.deployLiquityCore(mocks);
await deploymentHelper.connectCoreContracts(contracts);
contracts.priceFeed = contracts.priceFeedTestnet;

const afterConnectResult = await afterConnect(contracts);
contracts.priceFeed = contracts.priceFeedTestnet;

if (!accounts) {
throw new Error("No accounts provided to the fixture");
}
await fundAccounts(accounts, contracts.WETH);

// Without forcing a block to be mined, the tests were sometimes
// failing due to the accounts not being funded.
await mine();

const helpers = {
getOpenTroveBoldAmount: async (totalDebt) => (
th.getOpenTroveBoldAmount(contracts, totalDebt)
),
getNetBorrowingAmount: async (debtWithFee) => (
th.getNetBorrowingAmount(contracts, debtWithFee)
),
openTrove: async (params) => (
th.openTrove(contracts, params)
),
withdrawBold: async (params) => (
th.withdrawBold(contracts, params)
),
getActualDebtFromComposite: async (compositeDebt) => (
th.getActualDebtFromComposite(compositeDebt, contracts)
),
getTroveEntireColl: async (trove) => (
th.getTroveEntireColl(contracts, trove)
),
getTroveEntireDebt: async (trove) => (
th.getTroveEntireDebt(contracts, trove)
),
getTroveStake: async (trove) => (
th.getTroveStake(contracts, trove)
),
};

const callbackResult = await callback(contracts);

return {
contracts,
getOpenTroveBoldAmount: async (totalDebt) => {
return th.getOpenTroveBoldAmount(contracts, totalDebt);
},
getNetBorrowingAmount: async (debtWithFee) => {
return th.getNetBorrowingAmount(contracts, debtWithFee);
},
openTrove: async (params) => {
return th.openTrove(contracts, params);
},
withdrawBold: async (params) => {
return th.withdrawBold(contracts, params);
},
getActualDebtFromComposite: async (compositeDebt) => {
return th.getActualDebtFromComposite(compositeDebt, contracts);
},
getTroveEntireColl: async (trove) => {
return th.getTroveEntireColl(contracts, trove);
},
getTroveEntireDebt: async (trove) => {
return th.getTroveEntireDebt(contracts, trove);
},
getTroveStake: async (trove) => {
return th.getTroveStake(contracts, trove);
},
...afterDeployResult,
...afterConnectResult,
...helpers,
...callbackResult,
};
};

return () => loadFixture(fixture);
}

Expand Down

0 comments on commit 57b719a

Please sign in to comment.