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

chore: removal of hardhat-truffle #810

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ if (dotenvResult.error) {
}

require('@nomiclabs/hardhat-ethers');
require('@nomiclabs/hardhat-truffle5');
require('hardhat-gas-reporter');
require('solidity-coverage');
require('hardhat-abi-exporter');
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@
"@codechecks/client": "^0.1.11",
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^16.2.1",
"@nomiclabs/hardhat-ethers": "2.0.2",
"@nomiclabs/hardhat-ethers": "2.0.3",
"@nomiclabs/hardhat-etherscan": "^2.1.6",
"@nomiclabs/hardhat-truffle5": "^2.0.0",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@tenderly/hardhat-tenderly": "^1.0.12",
"axios": "^0.24.0",
"chai": "^4.3.4",
Expand Down
1 change: 1 addition & 0 deletions scenarios/scenarios.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { assert } = require('chai');
const {
getState, adhocCommit, adhocReveal, getData, adhocPropose,
} = require('../test/helpers/utils');
Expand Down
1 change: 1 addition & 0 deletions test/BlockManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @dev : above is a quick fix for this linting error
// I couldnt understand what it meant, to solve it

const { assert, expect } = require('chai');
const {
assertBNEqual,
assertDeepEqual,
Expand Down
19 changes: 10 additions & 9 deletions test/Initializable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ const {
assertRevert,
} = require('./helpers/testHelpers');

const InitializableMock = artifacts.require('../contracts/mocks/InitializableMock');
let mock;

describe('Initializable', function () {
beforeEach('deploying', async function () {
this.contract = await InitializableMock.new();
const InitializableMock = await ethers.getContractFactory('InitializableMock');
mock = await InitializableMock.deploy();
});

it('initializer has not run', async function () {
assert.isFalse(await this.contract.initializerRan());
assert.isFalse(await mock.initializerRan());
});

it('initializer has run', async function () {
await this.contract.initialize();
assert.isTrue(await this.contract.initializerRan());
await mock.initialize();
assert.isTrue(await mock.initializerRan());
});

it('initializer does not run again', async function () {
await this.contract.initialize();
await assertRevert(this.contract.initialize(), 'contract already initialized');
await mock.initialize();
await assertRevert(mock.initialize(), 'contract already initialized');
});

it('initializer has run after nested initialization', async function () {
await this.contract.initializeNested();
assert.isTrue(await this.contract.initializerRan());
await mock.initializeNested();
assert.isTrue(await mock.initializerRan());
});
});
3 changes: 2 additions & 1 deletion test/RandomNoManager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { assert } = require('chai');
const {
assertBNEqual,
mineToNextEpoch,
Expand Down Expand Up @@ -154,7 +155,7 @@ describe('RandomNoManager', function () {
// Get Random no : Generic From Epoch
const randomNo3 = await randomNoManager.getGenericRandomNumber(epoch);
const seed3 = await randomNoManager.secrets(epoch);
const salt3 = 0;
const salt3 = '0x0000000000000000000000000000000000000000000000000000000000000000';
const locallyCalculatedRandomNo3 = await prngHash(seed3, salt3);
assertBNEqual(randomNo3, toBigNumber(locallyCalculatedRandomNo3));
assertBNNotEqual(randomNo3, randomNo);
Expand Down
42 changes: 10 additions & 32 deletions test/helpers/testHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { assert } = require('chai');
const { BigNumber } = require('ethers');
const { ethers } = require('hardhat');
const { EPOCH_LENGTH, STATE_LENGTH } = require('./constants');
const { getEpoch, toBigNumber } = require('./utils');

Expand Down Expand Up @@ -81,44 +82,27 @@ const assertRevert = async (promise, reason) => {
assert.strictEqual(errorEncountered, true, 'Transaction did not revert as expected');
};

const send = (payload) => {
if (!payload.jsonrpc) payload.jsonrpc = '2.0';
if (!payload.id) payload.id = new Date().getTime();

return new Promise((resolve, reject) => {
web3.currentProvider.send(payload, (error, result) => {
if (error) return reject(error);

return resolve(result);
});
});
};

const waitNBlocks = async (n) => {
await Promise.all(
[...Array(n).keys()].map((i) => send({
jsonrpc: '2.0',
method: 'evm_mine',
id: i,
}))
[...Array(n).keys()].map(() => ethers.provider.send('evm_mine'))
);
};

const mineAdvance = async (n) => {
n = toBigNumber(n);
const blockNumber = toBigNumber(await web3.eth.getBlockNumber());
const blockNumber = toBigNumber(await ethers.provider.getBlockNumber());
if (n.gt(blockNumber)) {
const diff = n.sub(blockNumber);
await waitNBlocks(diff.toNumber());
}
};

const mineBlock = () => send({ method: 'evm_mine' });
const mineBlock = () => ethers.provider.send('evm_mine');

// Mines to the next Epoch from which ever block it is in the current Epoch
const mineToNextEpoch = async () => {
const currentBlockNumber = await web3.eth.getBlockNumber();
const currentBlock = await web3.eth.getBlock(currentBlockNumber);
const currentBlockNumber = await ethers.provider.getBlockNumber();
const currentBlock = await ethers.provider.getBlock(currentBlockNumber);
const currentTimestamp = currentBlock.timestamp;
const currentEpoch = await getEpoch();
const nextEpochBlockTimestamp = (currentEpoch + 1) * EPOCH_LENGTH.toNumber(); // currentBlocktimestamp + epochLength
Expand All @@ -129,8 +113,8 @@ const mineToNextEpoch = async () => {

// Mines to the next state in the current epoch
const mineToNextState = async () => {
const currentBlockNumber = toBigNumber(await web3.eth.getBlockNumber());
const currentBlock = await web3.eth.getBlock(currentBlockNumber);
const currentBlockNumber = await ethers.provider.getBlockNumber();
const currentBlock = await ethers.provider.getBlock(currentBlockNumber);
const currentTimestamp = toBigNumber(currentBlock.timestamp);
const temp = currentTimestamp.div(STATE_LENGTH).add('1');
const nextStateBlockNum = temp.mul(STATE_LENGTH);
Expand All @@ -140,18 +124,12 @@ const mineToNextState = async () => {
};

const restoreSnapshot = async (id) => {
await send({
method: 'evm_revert',
params: [id],
});
await ethers.provider.send('evm_revert', [id]);
await mineBlock();
};

const takeSnapshot = async () => {
const { result } = await send({
jsonrpc: '2.0',
method: 'evm_snapshot',
});
const result = await ethers.provider.send('evm_snapshot');

await mineBlock();

Expand Down
21 changes: 13 additions & 8 deletions test/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { BigNumber, utils } = ethers;
const { ethers } = require('hardhat');

const {
BigNumber, utils, provider,
} = ethers;
const {
ONE_ETHER, EPOCH_LENGTH, NUM_STATES, MATURITIES,
} = require('./constants');
Expand Down Expand Up @@ -57,7 +61,7 @@ const prng = async (max, prngHashes) => {

// pseudo random hash generator based on block hashes.
const prngHash = async (seed, salt) => {
const sum = await web3.utils.soliditySha3(seed, salt);
const sum = await utils.solidityKeccak256(['bytes32', 'bytes32'], [seed, salt]);
return (sum);
};

Expand All @@ -68,12 +72,13 @@ const maturity = async (age) => {

const isElectedProposer = async (iteration, biggestStake, stake, stakerId, numStakers, salt) => {
// add +1 since prng returns 0 to max-1 and staker start from 1
const salt1 = await web3.utils.soliditySha3(iteration);
const abiCoder = ethers.utils.defaultAbiCoder;
const salt1 = await utils.solidityKeccak256(['bytes'], [await abiCoder.encode(['uint256'], [iteration])]);
const seed1 = await prngHash(salt, salt1);
const rand1 = await prng(numStakers, seed1);
if (!(toBigNumber(rand1).add(1).eq(stakerId))) return false;

const salt2 = await web3.utils.soliditySha3(stakerId, iteration);
const salt2 = await utils.solidityKeccak256(['bytes'], [await abiCoder.encode(['uint32', 'uint256'], [stakerId, iteration])]);
const seed2 = await prngHash(salt, salt2);
const rand2 = await prng(toBigNumber(2).pow(toBigNumber(32)), toBigNumber(seed2));
if ((rand2.mul(biggestStake)).lt(stake.mul(toBigNumber(2).pow(32)))) return true;
Expand All @@ -82,8 +87,8 @@ const isElectedProposer = async (iteration, biggestStake, stake, stakerId, numSt
};

const getEpoch = async () => {
const blockNumber = toBigNumber(await web3.eth.getBlockNumber());
const getCurrentBlock = await web3.eth.getBlock(blockNumber.toNumber());
const blockNumber = toBigNumber(await provider.getBlockNumber());
const getCurrentBlock = await provider.getBlock(blockNumber.toNumber());
const timestamp = toBigNumber(getCurrentBlock.timestamp);
return timestamp.div(EPOCH_LENGTH).toNumber();
};
Expand Down Expand Up @@ -159,8 +164,8 @@ const getFalseIteration = async (voteManager, stakeManager, staker) => {
};

const getState = async () => {
const blockNumber = toBigNumber(await web3.eth.getBlockNumber());
const getCurrentBlock = await web3.eth.getBlock(blockNumber.toNumber());
const blockNumber = toBigNumber(await provider.getBlockNumber());
const getCurrentBlock = await provider.getBlock(blockNumber.toNumber());
const timestamp = toBigNumber(getCurrentBlock.timestamp);
const state = timestamp.div(EPOCH_LENGTH.div(NUM_STATES));
const lowerLimit = 5;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@nomiclabs/hardhat-ethers@2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@nomiclabs/hardhat-ethers/-/hardhat-ethers-2.0.2.tgz#c472abcba0c5185aaa4ad4070146e95213c68511"
integrity sha512-6quxWe8wwS4X5v3Au8q1jOvXYEPkS1Fh+cME5u6AwNdnI4uERvPlVjlgRWzpnb+Rrt1l/cEqiNRH9GlsBMSDQg==
"@nomiclabs/hardhat-ethers@2.0.3":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@nomiclabs/hardhat-ethers/-/hardhat-ethers-2.0.3.tgz#06e20a57274f6ce3148132910e723948a711edf1"
integrity sha512-IJ0gBotVtO7YyLZyHNgbxzskUtFok+JkRlKPo8YELqj1ms9XL6Qm3vsfsGdZr22wnJeVEF5TQPotKuwQk21Dag==

"@nomiclabs/hardhat-etherscan@^2.1.6":
version "2.1.8"
Expand Down