@dydxfoundation/governance
Version:
dYdX governance smart contracts
144 lines (143 loc) • 6.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.describeContractHardhatRevertBeforeEach = exports.describeContract = exports.describeContractHardhatRevertBefore = exports.describeContractForNetwork = void 0;
const chai_1 = __importDefault(require("chai"));
const dirty_chai_1 = __importDefault(require("dirty-chai"));
const ethereum_waffle_1 = require("ethereum-waffle");
const config_1 = __importDefault(require("../../src/config"));
const deploy_config_1 = require("../../src/deploy-config");
const get_deployer_address_1 = require("../../src/deploy-config/get-deployer-address");
const hre_1 = require("../../src/hre");
const hre_2 = __importDefault(require("../hre"));
const evm_1 = require("./evm");
const get_deployed_contracts_for_test_1 = require("./get-deployed-contracts-for-test");
// Global chai setup.
chai_1.default.use(dirty_chai_1.default);
chai_1.default.use(ethereum_waffle_1.solidity);
function describeContractForNetwork(name, ctx, networkForTest, mainnetForkTest, tests) {
if (networkForTest === (0, hre_1.getNetworkName)() && mainnetForkTest === config_1.default.FORK_MAINNET) {
describe(`Running test on ${networkForTest} ${mainnetForkTest ? 'fork' : ''}: ${name}`, () => {
tests(ctx);
});
}
else {
describe.skip(`Skipping test on ${networkForTest} ${mainnetForkTest ? 'fork' : ''}: ${name}`, () => { });
}
}
exports.describeContractForNetwork = describeContractForNetwork;
/**
* @notice Used for running tests on the Hardhat network (excluding mainnet forks). The tests
* will be skipped if on a non-Hardhat network or mainnet fork.
*
* IMPORTANT: This method is different than `describeContractHardhatRevertBeforeEach` because it does _not_
* revert snapshots after each test (only after all tests have run). This is useful if you want the contracts
* to persist state into nested `describe` blocks.
*
* @param name The name of the test.
* @param init The function to run before running the tests.
* @param tests The tests to run.
*/
function describeContractHardhatRevertBefore(name, init, tests) {
if (!config_1.default.isHardhat() || config_1.default.FORK_MAINNET) {
// These tests only run on Hardhat (not including mainnet forks)
describe.skip(name, () => { });
}
else {
// Note that the function passed into describe() should not be async.
describe(name, () => {
const ctx = {
config: (0, deploy_config_1.getDeployConfig)(),
};
let preInitSnapshotId;
// Runs before any before() calls made within the describeContract() call.
before(async () => {
const accounts = await hre_2.default.ethers.getSigners();
ctx.deployer = await (0, get_deployer_address_1.getDeployerSigner)();
ctx.users = accounts.slice(1);
// Deploy contracts before taking the pre-init snapshot.
const deployedContracts = await (0, get_deployed_contracts_for_test_1.getDeployedContractsOnceForTest)();
Object.assign(ctx, deployedContracts);
preInitSnapshotId = await (0, evm_1.evmSnapshot)();
await init(ctx);
});
// Runs before any after() calls made within the describeContract() call.
after(async () => {
await (0, evm_1.evmReset)(preInitSnapshotId);
});
tests(ctx);
});
}
}
exports.describeContractHardhatRevertBefore = describeContractHardhatRevertBefore;
function describeContract(name, init, tests) {
// Note that the function passed into describe() should not be async.
describe(name, () => {
const ctx = {
config: (0, deploy_config_1.getDeployConfig)(),
};
let preInitSnapshotId;
let postInitSnapshotId;
// Runs before any before() calls made within the describeContract() call.
before(async () => {
const accounts = await hre_2.default.ethers.getSigners();
ctx.deployer = await (0, get_deployer_address_1.getDeployerSigner)();
ctx.users = accounts.slice(1);
// Deploy contracts before taking the pre-init snapshot.
const deployedContracts = await (0, get_deployed_contracts_for_test_1.getDeployedContractsOnceForTest)();
Object.assign(ctx, deployedContracts);
preInitSnapshotId = await maybeEvmSnapshot();
await init(ctx);
postInitSnapshotId = await maybeEvmSnapshot();
});
// Runs before any beforeEach() calls made within the describeContract() call.
beforeEach(async () => {
await maybeEvmReset(postInitSnapshotId);
postInitSnapshotId = await maybeEvmSnapshot();
});
// Runs before any after() calls made within the describeContract() call.
after(async () => {
if (typeof preInitSnapshotId !== 'undefined') {
await maybeEvmReset(preInitSnapshotId);
preInitSnapshotId = await maybeEvmSnapshot();
}
});
tests(ctx);
});
}
exports.describeContract = describeContract;
async function maybeEvmSnapshot() {
if (config_1.default.isHardhat()) {
return (0, evm_1.evmSnapshot)();
}
return '';
}
async function maybeEvmReset(id) {
if (config_1.default.isHardhat()) {
await (0, evm_1.evmReset)(id);
}
}
/**
* @notice Used for running tests on the Hardhat network (excluding mainnet forks). The tests
* will be skipped if on a non-Hardhat network or mainnet fork.
*
* IMPORTANT: This method is different than `describeContractHardhatRevertBefore` because it _does_
* revert snapshots after each test. This is useful if you don't want the contracts to persist state
* into nested `describe` blocks.
*
* @param name The name of the test.
* @param init The function to run before running the tests.
* @param tests The tests to run.
*/
function describeContractHardhatRevertBeforeEach(name, init, tests) {
if (config_1.default.FORK_MAINNET) {
// Skip tests that do not run on mainnet forks when forking mainnet.
describe.skip(name, () => { });
}
else {
describeContract(name, init, tests);
}
}
exports.describeContractHardhatRevertBeforeEach = describeContractHardhatRevertBeforeEach;