Skip to content

Commit

Permalink
refactor(test): migrate from truffle to hardhat
Browse files Browse the repository at this point in the history
  • Loading branch information
omerir authored and ppongbitgo committed Feb 7, 2022
1 parent 7f2640b commit ec6d6bc
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 477 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ build
coverage.json

.env

#Hardhat files
artifacts
cache
typechain
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,25 @@ NodeJS 8.14.0 is recommended.
npm install
```

This installs truffle and an Ethereum test RPC client.
This installs hardhat.

## Wallet Solidity Contract

Find it at [contracts/WalletSimple.sol](contracts/WalletSimple.sol)

## Running tests

A test suite is included through the use of the truffle framework, providing coverage for methods in the wallet.

The truffle framework will depend on the Web3 interface to a local Web3 Ethereum JSON-RPC. If you've followed the above steps, run the following to start testrpc.
A test suite is included through the use of the hardhat framework, providing coverage for methods in the wallet.

```shell
npm run truffle-testrpc
npx hardhat test
```

You should verify that you are not already running geth, as this will cause the tests to run against that interface.

In a **separate terminal window**, run the following command to initiate the test suite, which will run against the RPC:
## Running tests in IntelliJ

```shell
npm run truffle-test
You need to add the following to the extra mocha options
```
--require hardhat/register
```

## Notes
Expand Down
60 changes: 60 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as dotenv from "dotenv";

const { HD_WALLET_API_URL, MNEMONIC, ETHERSCAN_API_KEY } = process.env;

import { HardhatUserConfig, task } from "hardhat/config";
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@nomiclabs/hardhat-truffle5";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";

dotenv.config();

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

const config: HardhatUserConfig = {
solidity: '0.8.10',
networks: {
hardhat: {
// If chainId is omitted, then there is no chain id validation
accounts: [
'0xc8209c2200f920b11a460733c91687565c712b40c6f0350e9ad4138bf3193e47',
'0x915334f048736c64127e91a1dc35dad86c91e59081cdc12cd060103050e2f3b1',
'0x80bf357dd53e61db0e68acbb270e16fd42645903b51329c856cf3cb36f180a3e',
'0xdf231d240ce40f844d56cea3a7663b4be8c373fdd6a4fe69cacaaa68c698c590',
'0x71ce3f6c92d687ebbdc9b632744178707f39228ae1001a2de66f8b98de36ca07',
'0xca4e687f97b8c64705cddb53c92454994c83abcb4218c7c62955bac292c3bc9e',
'0x0755057fc0113fdc174e919622f237d30044a4c1c47f3663608b9ee9e8a1a58a',
'0x1a4002a3e2d0c18c058265600838cff40ba24303f6e60cd1c74821e8251f84d5',
'0x6d276292b8f5047b54db5b2179b5f7050636feaccf6c97a2978200d41d9d3374',
'0xace7201611ba195f85fb2e25b53e0f9869e57e2267d1c5eef63144c75dee5142'
].map((key) => ({
privateKey: key,
balance: '200000000000000000000000000',
})),
loggingEnabled: false,
},
ropsten: {
url: process.env.ROPSTEN_URL || '',
// gasPrice: 5800000,
gasPrice: 875000000,
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : []
}
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: 'USD'
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY
},
mocha: {
timeout: 100000,
}
};

export default config;
72 changes: 0 additions & 72 deletions mocha-test/compile.js

This file was deleted.

38 changes: 32 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,46 @@
"q": "^1.5.1",
"should": "^8.3.1",
"solc": "0.8.11",
"solhint": "^3.3.6",
"truffle": "^5.4.25",
"truffle-assertions": "^0.9.2"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.4",
"@nomiclabs/hardhat-etherscan": "^2.1.8",
"@nomiclabs/hardhat-truffle5": "^2.0.3",
"@nomiclabs/hardhat-waffle": "^2.0.2",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@openzeppelin/test-helpers": "^0.5.15",
"@types/mocha": "^9.0.0",
"@typechain/ethers-v5": "^7.2.0",
"@typechain/hardhat": "^2.3.1",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.1.0",
"@types/node": "^12.20.43",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"chai": "^4.3.6",
"dotenv": "^10.0.0",
"prettier": "^2.1.2",
"prettier-plugin-solidity": "^1.0.0-alpha.58",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-promise": "^5.2.0",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.5.4",
"hardhat": "^2.8.3",
"hardhat-gas-reporter": "^1.0.7",
"prettier": "^2.5.1",
"prettier-plugin-solidity": "^1.0.0-beta.13",
"solhint": "^3.3.6",
"solhint-plugin-prettier": "0.0.5",
"solidity-coverage": "^0.7.17",
"solidity-coverage": "^0.7.18",
"truffle-flattener": "^1.5.0",
"truffle-plugin-verify": "^0.5.20",
"typescript": "^4.5.4"
"ts-node": "^10.4.0",
"typechain": "^5.2.0",
"typescript": "^4.5.5",
"web3": "^1.7.0"
}
}
30 changes: 30 additions & 0 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// When running the script with `npx hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
import { ethers } from "hardhat";

async function main() {
// Hardhat always runs the compile task when running scripts with its command
// line interface.
//
// If this script is run directly using `node` you may want to call compile
// manually to make sure everything is compiled
// await hre.run('compile');

// We get the contract to deploy
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");

await greeter.deployed();

console.log("Greeter deployed to:", greeter.address);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
36 changes: 22 additions & 14 deletions test/batcher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const hre = require('hardhat');

const Batcher = artifacts.require('./Batcher.sol');
const TestBatcherDriver = artifacts.require('./TestBatcherDriver.sol');
const Reentry = artifacts.require('./Reentry.sol');
Expand Down Expand Up @@ -44,15 +46,10 @@ const assertVMException = async (promise, expectedExceptionMsg) => {
await promise;
badSucceed = true;
} catch (err) {
assert.notStrictEqual(
err.message.search('VM Exception'),
-1,
'Received non-VM exception'
);
assert.strictEqual(
err.reason || '',
expectedExceptionMsg,
"Didn't receive expected VM exception"
err.message,
`VM Exception while processing transaction: reverted with reason string '${expectedExceptionMsg}'`,
"Invalid exception"
);
}
if (badSucceed) {
Expand All @@ -65,19 +62,23 @@ const assertBalanceDiff = (start, end, diff, errMsg) => {
assert.strictEqual(startWithDiff.toString(), end.toString(), errMsg);
};

contract('Batcher', (accounts) => {
describe('Batcher', () => {
let batcherInstance;
let reentryInstance;
let failInstance;
let gasGuzzlerInstance;
let gasHeavyInstance;
let testBatcherDriverInstance;

const sender = accounts[0];
const batcherOwner = accounts[8];
let sender;
let batcherOwner;
const zeroAddr = '0x0000000000000000000000000000000000000000';

before(async () => {
await hre.network.provider.send("hardhat_reset");
accounts = await web3.eth.getAccounts();
sender = accounts[0];
batcherOwner = accounts[8];

batcherInstance = await Batcher.new({ from: batcherOwner });
reentryInstance = await Reentry.new(batcherInstance.address);
failInstance = await Fail.new();
Expand Down Expand Up @@ -607,10 +608,14 @@ contract('Batcher', (accounts) => {
});

describe('Transferring ownership and setting gas transfer limit', () => {

// note: at the start of every test, the Batcher owner should be `batcherOwner`
// and the transfer gas limit should be the default
const otherBatcherOwner = accounts[7];
const defaultTransferGasLimit = 1e4;
let otherBatcherOwner;
beforeEach(async () => {
otherBatcherOwner = accounts[7];
});

const setBatcherOwner = async (oldBatcherOwner, newBatcherOwner) => {
const tx = await batcherInstance.transferOwnership(newBatcherOwner, {
Expand Down Expand Up @@ -720,7 +725,10 @@ contract('Batcher', (accounts) => {
describe('Using recover for ERC20 Tokens', () => {
let tokenContract;
let totalSupply;
const tokenContractOwner = accounts[9];
let tokenContractOwner;
beforeEach(async () => {
tokenContractOwner = accounts[9];
});

const checkBalance = async (address, expectedAmt) => {
const balance = await tokenContract.balanceOf(address);
Expand Down
10 changes: 9 additions & 1 deletion test/forwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const AlwaysFalseERC165 = artifacts.require('./AlwaysFalseERC165.sol');
const ReentryForwarder = artifacts.require('./ReentryForwarder');
const util = require('ethereumjs-util');
const abi = require('ethereumjs-abi');
const hre = require('hardhat');

const createForwarder = async (creator, parent) => {
const forwarderContract = await Forwarder.new([], { from: creator });
Expand Down Expand Up @@ -40,7 +41,14 @@ const getMethodData = async function (types, values, methodName) {

const FORWARDER_DEPOSITED_EVENT = 'ForwarderDeposited';

contract('Forwarder', function (accounts) {
describe('Forwarder', function () {

let accounts;
before(async () => {
await hre.network.provider.send("hardhat_reset");
accounts = await web3.eth.getAccounts();
})

it('Basic forwarding test', async function () {
const forwarder = await createForwarder(accounts[0], accounts[0]);
const startBalance = await getBalanceInWei(accounts[0]);
Expand Down
10 changes: 9 additions & 1 deletion test/forwarderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Forwarder = artifacts.require('./Forwarder.sol');
const ForwarderFactory = artifacts.require('./ForwarderFactory.sol');

const ForwarderABI = require('../ABIs/Forwarder.json');
const hre = require('hardhat');

const createForwarderFactory = async () => {
const forwarderContract = await Forwarder.new([], {});
Expand Down Expand Up @@ -65,7 +66,14 @@ const createForwarder = async (
return forwarderAddress;
};

contract('ForwarderFactory', function (accounts) {
describe('ForwarderFactory', function () {

let accounts;
before(async () => {
await hre.network.provider.send("hardhat_reset");
accounts = await web3.eth.getAccounts();
})

it('Should create a functional forwarder using the factory', async function () {
const { factory, implementationAddress } = await createForwarderFactory();

Expand Down
Loading

0 comments on commit ec6d6bc

Please sign in to comment.