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

Umbra js hardhat tests & coverage #157

Merged
merged 12 commits into from
Apr 14, 2021
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ Umbra is a monorepo consisting of 3 packages: @umbra/frontend, @umbra/contracts,

```sh
# run these commands from workspace root!
cp contracts/.env.example contracts/.env # please edit the .env with your own environment variable values
cp frontend/.env.example frontend/.env # please edit the .env with your own environment variable values
cp umbra-js/.env.example umbra-js/.env # please edit the .env with your own environment variable values
yarn install # installs dependencies for each of the 3 packages. Also builds umbra-js.
yarn build # builds each of the 3 packages
yarn clean # removes build artifacts for each of the 3 packages
Expand Down
2 changes: 1 addition & 1 deletion contracts/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
INFURA_API_KEY=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
INFURA_ID=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
MNEMONIC=here is where your twelve words mnemonic should be put my friend
13 changes: 7 additions & 6 deletions contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'hardhat-typechain';
import 'solidity-coverage';
import 'hardhat-gas-reporter';


const chainIds = {
ganache: 1337,
goerli: 5,
Expand All @@ -27,6 +26,8 @@ const chainIds = {
};

// Ensure that we have all the environment variables we need.
// Note: So that the monorepo can be imported to other projects, we make these env variables
// optional so that typechain can still build its types without hard failing on this.
let mnemonic = '';
if (!process.env.MNEMONIC) {
console.warn('Please set your MNEMONIC in a .env file');
Expand All @@ -35,16 +36,16 @@ if (!process.env.MNEMONIC) {
}

let infuraApiKey = '';
if (!process.env.INFURA_API_KEY) {
console.warn('Please set your INFURA_API_KEY in a .env file');
if (!process.env.INFURA_ID) {
console.warn('Please set your INFURA_ID in a .env file');
} else {
infuraApiKey = process.env.INFURA_API_KEY;
infuraApiKey = process.env.INFURA_ID;
}

const shouldReportGas = process.env.REPORT_GAS === 'true';

function createTestnetConfig(network: keyof typeof chainIds): NetworkUserConfig {
const url: string = 'https://' + network + '.infura.io/v3/' + infuraApiKey;
const url = `https://${network}.infura.io/v3/${infuraApiKey}`;
return {
accounts: {
count: 10,
Expand All @@ -62,7 +63,7 @@ const config: HardhatUserConfig = {
networks: {
hardhat: {
forking: {
url: 'https://rinkeby' + '.infura.io/v3/' + infuraApiKey,
url: `https://rinkeby.infura.io/v3/${infuraApiKey}`,
},
chainId: chainIds.hardhat,
accounts: {
Expand Down
4 changes: 3 additions & 1 deletion contracts/test/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { web3 } = require('@openzeppelin/test-environment');
const { web3 } = require('hardhat');
const ethers = require('ethers');

const { BN } = web3.utils;
Expand All @@ -11,9 +11,11 @@ const { AddressZero } = ethers.constants;
* @return {string} Sum of amounts as string
*/
const sumTokenAmounts = (amounts) => {
/* eslint-disable */
const sum = amounts.map((amount) => new BN(amount)).reduce((acc, val) => acc.add(val), new BN('0'));

return sum.toString();
/* eslint-enable */
};

/**
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions umbra-js/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INFURA_ID=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4 changes: 2 additions & 2 deletions umbra-js/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build
test-keys.json
cache

# autogenerated typechain artifacts
types/contracts
types/contracts
31 changes: 31 additions & 0 deletions umbra-js/.nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"cache": false,
"check-coverage": false,
"extension": [
".ts"
],
"include": [
"**/*.js",
"**/*.ts"
],
"exclude": [
"coverage/**",
"node_modules/**",
"**/*.d.ts",
"**/*.test.ts",
"src/index.ts",
"types/contracts/**",
".eslintrc.js",
".lintstagedrc.js",
".prettierrc.js",
"hardhat.config.ts",
],
"sourceMap": true,
"reporter": [
"html",
"text",
"text-summary"
],
"all": true,
"instrument": true
}
3 changes: 3 additions & 0 deletions umbra-js/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/build
/node_modules
/coverage
/cache
/.nyc_output
1 change: 0 additions & 1 deletion umbra-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ await tx3.wait();
1. Create a file in this directory called `.env` that looks like the one below.
```bash
INFURA_ID=yourInfuraId
TEST_ADDRESS=0x60A5dcB2fC804874883b797f37CbF1b0582ac2dD
```
2. Run `yarn` to install packages
3. Run `yarn test` to run all tests.
Expand Down
91 changes: 91 additions & 0 deletions umbra-js/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { config as dotenvConfig } from 'dotenv';
import { resolve } from 'path';
dotenvConfig({ path: resolve(__dirname, './.env') });
wildmolasses marked this conversation as resolved.
Show resolved Hide resolved

import { HardhatUserConfig } from 'hardhat/config';
import { NetworkUserConfig } from 'hardhat/types';

import '@nomiclabs/hardhat-ethers';
import '@nomiclabs/hardhat-waffle';

const chainIds = {
ganache: 1337,
goerli: 5,
hardhat: 1337,
kovan: 42,
mainnet: 1,
rinkeby: 4,
ropsten: 3,
};

// Ensure that we have all the environment variables we need.
const mnemonic = 'test test test test test test test test test test test junk';

const infuraApiKey = process.env.INFURA_ID;
if (!infuraApiKey) throw new Error('Please set your INFURA_ID in a .env file');

function createTestnetConfig(network: keyof typeof chainIds): NetworkUserConfig {
const url = `https://${network}.infura.io/v3/${infuraApiKey as string}`;
return {
accounts: {
count: 10,
initialIndex: 0,
mnemonic,
path: "m/44'/60'/0'/0",
},
chainId: chainIds[network],
url,
};
}

const config: HardhatUserConfig = {
defaultNetwork: 'hardhat',
networks: {
hardhat: {
forking: {
url: `https://rinkeby.infura.io/v3/${infuraApiKey}`,
},
chainId: chainIds.hardhat,
accounts: {
count: 10,
initialIndex: 0,
mnemonic,
path: "m/44'/60'/1'/0",
},
},
goerli: createTestnetConfig('goerli'),
kovan: createTestnetConfig('kovan'),
rinkeby: createTestnetConfig('rinkeby'),
ropsten: createTestnetConfig('ropsten'),
},
paths: {
cache: './cache',
tests: './test',
},
solidity: {
compilers: [
{
version: '0.7.6',
settings: {
// https://hardhat.org/hardhat-network/#solidity-optimizer-support
optimizer: {
enabled: true,
runs: 999999,
},
},
},
{
version: '0.6.12',
settings: {
// https://hardhat.org/hardhat-network/#solidity-optimizer-support
optimizer: {
enabled: true,
runs: 999999,
},
},
},
],
},
};

export default config;
9 changes: 6 additions & 3 deletions umbra-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"build"
],
"scripts": {
"test": "yarn build && mocha -r ts-node/register ./test/*.test.ts --exit --timeout 0",
"test": "yarn build && yarn hardhat test",
"coverage": "yarn build && nyc yarn hardhat test",
"lint": "eslint --ext .js,.ts ./",
"prettier": "prettier --write .",
"watch": "tsc --watch",
"build": "tsc --build",
"clean": "rimraf build",
"clean": "rimraf build coverage .nyc_output cache",
"prepare": "yarn build",
"prepublishOnly": "yarn lint && yarn test",
"precommit": "lint-staged"
Expand All @@ -26,7 +27,8 @@
"noble-secp256k1": "^1.1.2"
},
"devDependencies": {
"@openzeppelin/test-environment": "^0.1.6",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-waffle": "^2.0.0",
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.0.4",
Expand All @@ -37,6 +39,7 @@
"eslint-plugin-chai-friendly": "^0.6.0",
"eslint-plugin-import": "^2.20.2",
"mocha": "^7.1.2",
"nyc": "^15.1.0",
"ts-node": "^9.0.0"
},
"keywords": [
Expand Down
8 changes: 0 additions & 8 deletions umbra-js/test-environment.config.js

This file was deleted.

8 changes: 2 additions & 6 deletions umbra-js/test/DomainService.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import * as chai from 'chai';
import { ethers } from 'ethers';
import { provider } from '@openzeppelin/test-environment';

import { ethers } from 'hardhat';
import { DomainService } from '../src/classes/DomainService';
import type { ExternalProvider } from '../src/types';

const { expect } = chai;
const web3Provider = (provider as unknown) as ExternalProvider;
const ethersProvider = new ethers.providers.Web3Provider(web3Provider);
const ethersProvider = ethers.provider;

// Truth parameters to test against (on Rinkeby)
const params = {
Expand Down
7 changes: 2 additions & 5 deletions umbra-js/test/KeyPair.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import * as chai from 'chai';
import { provider } from '@openzeppelin/test-environment';
import { ethers } from 'hardhat';
import { Wallet } from 'ethers';
import { BigNumber } from '@ethersproject/bignumber';
import { hexZeroPad } from '@ethersproject/bytes';
import { Web3Provider } from '@ethersproject/providers';
import { randomBytes } from '@ethersproject/random';
import type { ExternalProvider } from '../src/types';
import { RandomNumber } from '../src/classes/RandomNumber';
import { KeyPair } from '../src/classes/KeyPair';
import { Umbra } from '../src/classes/Umbra';
import * as utils from '../src/utils/utils';

const { expect } = chai;
const web3Provider = (provider as unknown) as ExternalProvider;
const ethersProvider = new Web3Provider(web3Provider);
const ethersProvider = ethers.provider;
const numberOfRuns = 100; // number of runs for tests that execute in a loop
const zeroPrefix = '0x00000000000000000000000000000000'; // 16 bytes of zeros

Expand Down
Loading