Skip to content

Commit

Permalink
template repo added with successful testing
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi3700 committed Aug 20, 2021
1 parent b988d4f commit 1a55d32
Show file tree
Hide file tree
Showing 11 changed files with 36,482 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MNEMONIC=test test test test test test test test test test test test
REPORT_GAS=true
INFURA_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ETHERSCAN_API_KEY=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules

#Hardhat files
cache/
build/
typechain/
artifacts/
coverage*
factories

.env
17 changes: 17 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine":"auto",
"printWidth": 120,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "all",
"overrides": [
{
"files": "*.sol",
"options": {
"tabWidth": 4
}
}
]
}
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Ethereum Solidity Hardhat Template
Ethereum + Solidity + Hardhat + Typechain

Ethereum with Solidity contracts using Hardhat testing framework in TypeScript

## Use
- [Hardhat](https://github.com/nomiclabs/hardhat): compile and run the smart contracts on a local development network
- [TypeChain](https://github.com/ethereum-ts/TypeChain): generate TypeScript types for smart contracts
- [Ethers](https://github.com/ethers-io/ethers.js/): renowned Ethereum library and wallet implementation
- [Waffle](https://github.com/EthWorks/Waffle): tooling for writing comprehensive smart contract tests
- [Solhint](https://github.com/protofire/solhint): linter
- [Prettier Plugin Solidity](https://github.com/prettier-solidity/prettier-plugin-solidity): code formatter

This is a GitHub template, which means you can reuse it as many times as you want. You can do that by clicking the "Use this
template" button at the top of the page.


## Setup
### 1. Environment variables
* Create a `.env` file with its values:
```
DEPLOYER_PRIVATE_KEY=<private_key_without_0x>
INFURA_API_KEY=<SECRET_KEY>
REPORT_GAS=<true_or_false>
```

### 2. Install the dependencies
* Command:
- M-1: `$ npm install` (install all the packages listed inside `package.json`)
- M-2:`$ npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers hardhat-gas-reporter @openzeppelin/contracts typechain @typechain/ethers-v5 dotenv`

### 3. Start writing contracts
* Contracts in the "contracts/" folder.
* Deployment scripts in the "deployment/" folder.
* Testing (locally) scripts in the "scripts/" folder.

## Usage

### Pre Requisites

Before running any command, make sure to install dependencies:

```sh
$ npm install
```

### Compile

Compile the smart contracts with Hardhat:

```sh
$ npx hardhat compile
```

### Test

Run the Mocha tests:

```sh
$ npx hardhat test
```

### Deploy contract to netowrk (requires Mnemonic/deployer_private_key and Infura API key)

```
npx hardhat run --network rinkeby ./deployment/deploy.ts
```

### Validate a contract with etherscan (requires API ke)

```
npx hardhat verify --network <network> <DEPLOYED_CONTRACT_ADDRESS> "Constructor argument 1"
```

### Added plugins

- Gas reporter [hardhat-gas-reporter](https://hardhat.org/plugins/hardhat-gas-reporter.html)
- Etherscan [hardhat-etherscan](https://hardhat.org/plugins/nomiclabs-hardhat-etherscan.html)

## References
* [My notes on Hardhat](https://github.com/abhi3700/ethio_playground/blob/main/libs/hardhat/README.md)

## Thanks

If you like it than you can put a star ⭐ on the repo.
22 changes: 22 additions & 0 deletions contracts/TestToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";

/**
* @notice A mintable ERC20
*/
contract TestToken is ERC20, AccessControl {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

constructor() public ERC20("Test Token", "TST") {
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
_setupRole(MINTER_ROLE, msg.sender);
}

function mint(address to, uint256 amount) external {
require(hasRole(MINTER_ROLE, msg.sender), "Only minter can mint");
_mint(to, amount);
}
}
29 changes: 29 additions & 0 deletions deployment/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 `hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
import { ethers } from 'hardhat';
import { Contract, ContractFactory } from 'ethers';

async function main(): Promise<void> {
// Hardhat always runs the compile task when running scripts through it.
// If this runs in a standalone fashion you may want to call compile manually
// to make sure everything is compiled
// await run("compile");
// We get the contract to deploy
const TestTokenFactory: ContractFactory = await ethers.getContractFactory(
'TestToken',
);
const testToken: Contract = await TestTokenFactory.deploy();
await testToken.deployed();
console.log('TestToken deployed to: ', testToken.address);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch((error: Error) => {
console.error(error);
process.exit(1);
});
116 changes: 116 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { task } from "hardhat/config";

import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
dotenvConfig({ path: resolve(__dirname, "./.env") });

import { HardhatUserConfig } from "hardhat/types";
import { NetworkUserConfig } from "hardhat/types";

import "@nomiclabs/hardhat-waffle";
import "@typechain/hardhat";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-waffle";

import "hardhat-gas-reporter";
import "@nomiclabs/hardhat-etherscan";

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

// const MNEMONIC = process.env.MNEMONIC || "";
// const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || "";
// const ALCHEMY_KEY = process.env.ALCHEMY_KEY || "";
const INFURA_API_KEY = process.env.INFURA_API_KEY || "";
const DEPLOYER_PRIVATE_KEY_RINKEBY = process.env.DEPLOYER_PRIVATE_KEY_RINKEBY || "";

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (args, hre) => {
const accounts = await hre.ethers.getSigners();

for (const account of accounts) {
console.log(await account.getAddress());
}
});

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

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

const config: HardhatUserConfig = {
defaultNetwork: "hardhat",
networks: {
// coverage: {
// url: "http://127.0.0.1:8555",
// },
// mainnet: {
// url: `https://mainnet.infura.io/v3/${INFURA_API_KEY}`,
// chainId: 1,
// accounts: [`0x${DEPLOYER_PRIVATE_KEY_MAINNET}`],
// },
rinkeby: {
url: `https://rinkeby.infura.io/v3/${INFURA_API_KEY}`,
// url: "https://rinkeby.infura.io/v3/24d441e3175047bfb04c60e8221878c9",
chainId: 4,
accounts: [`0x${DEPLOYER_PRIVATE_KEY_RINKEBY}`],
// accounts: ["0xcf2a6872928392175e383fc10f93c13eab0c050bd4dbd6b45201fad5bd9409b7"],
},
// rinkeby: createTestnetConfig("rinkeby"),
},
solidity: {
compilers: [
{
version: "0.6.12",
settings: {
optimizer: {
enabled: true,
runs: 10000,
},
},
},
{
version: "0.6.6",
},
],
},
paths: {
sources: "contracts",
artifacts: "./build/artifacts",
cache: "./build/cache",
},
// etherscan: {
// apiKey: ETHERSCAN_API_KEY,
// },
gasReporter: {
currency: "USD",
gasPrice: 20,
// enabled: process.env.REPORT_GAS ? true : false,
},
typechain: {
outDir: "./build/typechain/",
target: "ethers-v5",
},
};

export default config;
Loading

0 comments on commit 1a55d32

Please sign in to comment.