Skip to content

Commit

Permalink
feat: adds hardhat deploy with its utils (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
404skillz committed Sep 14, 2021
1 parent 52af857 commit 0a30e58
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ coverage
cache
artifacts
typechained
deployments/hardhat
deployments/localhost

# Config files
.env

# Not ignore gitkeep
!/**/.gitkeep
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ coverage.json
artifacts
cache
typechained
deployments

# JS
node_modules
Expand Down
31 changes: 31 additions & 0 deletions deploy/001_deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { DeployFunction } from 'hardhat-deploy/types';
import { getChainId, shouldVerifyContract } from '../utils/deploy';

export const INITIAL_GREET: { [chainId: string]: string } = {
'1': 'Halo!',
'137': 'Halo to polygon network!',
};

const deployFunction: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployer } = await hre.getNamedAccounts();

const chainId = await getChainId(hre);

const deploy = await hre.deployments.deploy('Greeter', {
contract: 'contracts/Greeter.sol:Greeter',
from: deployer,
args: [INITIAL_GREET[chainId]],
log: true,
});

if (await shouldVerifyContract(deploy)) {
await hre.run('verify:verify', {
address: deploy.address,
constructorArguments: [INITIAL_GREET[chainId]],
});
}
};
deployFunction.dependencies = [];
deployFunction.tags = ['Greeter'];
export default deployFunction;
Empty file added deployments/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '@typechain/hardhat';
import '@typechain/hardhat/dist/type-extensions';
import { removeConsoleLog } from 'hardhat-preprocessor';
import 'hardhat-gas-reporter';
import 'hardhat-deploy';
import 'solidity-coverage';
import { HardhatUserConfig, MultiSolcUserConfig, NetworksUserConfig } from 'hardhat/types';
import { getNodeUrl, accounts } from './utils/network';
Expand Down Expand Up @@ -44,6 +45,11 @@ const networks: NetworksUserConfig = process.env.TEST

const config: HardhatUserConfig = {
defaultNetwork: 'hardhat',
namedAccounts: {
deployer: {
default: 0,
},
},
mocha: {
timeout: process.env.MOCHA_TIMEOUT || 300000,
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"compile:test": "cross-env TEST=true hardhat compile",
"coverage": "hardhat coverage",
"docs": "solidity-docgen --solc-module solc-0.8",
"deploy": "npx hardhat deploy",
"fork:node": "cross-env FORK=true hardhat node",
"fork:script": "cross-env FORK=true hardhat run",
"postinstall": "husky install && yarn compile:test",
Expand Down Expand Up @@ -80,6 +81,7 @@
"ethereum-waffle": "3.4.0",
"ethers": "5.4.6",
"hardhat": "2.6.4",
"hardhat-deploy": "^0.9.1",
"hardhat-gas-reporter": "1.0.4",
"hardhat-preprocessor": "0.1.4",
"husky": "7.0.2",
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2018",
"target": "es2019",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
Expand All @@ -15,6 +15,6 @@
"@unit/*": ["test/unit/*"]
}
},
"include": ["./scripts", "./test"],
"include": ["./scripts", "./deploy", "./test"],
"files": ["./hardhat.config.ts"]
}
34 changes: 34 additions & 0 deletions utils/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ethers } from 'hardhat';
import { DeployResult } from 'hardhat-deploy/dist/types';
import { HardhatNetworkUserConfig, HardhatRuntimeEnvironment } from 'hardhat/types';

let testChainId: number;

export const setTestChainId = (chainId: number): void => {
testChainId = chainId;
};

export const getChainId = async (hre: HardhatRuntimeEnvironment): Promise<number> => {
if (!!process.env.TEST) {
if (!testChainId) throw new Error('Should specify chain id of test');
return testChainId;
}
if (!!process.env.FORK) return getRealChainIdOfFork(hre);
return await (hre as any).getChainId();
};

export const getRealChainIdOfFork = (hre: HardhatRuntimeEnvironment): number => {
const config = hre.network.config as HardhatNetworkUserConfig;
if (config.forking?.url.includes('mainnet')) return 1;
if (config.forking?.url.includes('ftm') || config.forking?.url.includes('fantom')) return 250;
if (config.forking?.url.includes('polygon')) return 137;
throw new Error('Should specify chain id of fork');
};

export const shouldVerifyContract = async (deploy: DeployResult): Promise<boolean> => {
if (process.env.FORK || process.env.TEST) return false;
if (!deploy.newlyDeployed) return false;
const txReceipt = await ethers.provider.getTransaction(deploy.receipt!.transactionHash);
await txReceipt.wait(10);
return true;
};
67 changes: 54 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
"@ethersproject/transactions" "^5.4.0"
"@ethersproject/web" "^5.4.0"

"@ethersproject/abstract-signer@5.4.1", "@ethersproject/abstract-signer@^5.4.0":
"@ethersproject/abstract-signer@5.4.1", "@ethersproject/abstract-signer@^5.4.0", "@ethersproject/abstract-signer@^5.4.1":
version "5.4.1"
resolved "https://registry.yarnpkg.com/@ethersproject/abstract-signer/-/abstract-signer-5.4.1.tgz#e4e9abcf4dd4f1ba0db7dff9746a5f78f355ea81"
integrity sha512-SkkFL5HVq1k4/25dM+NWP9MILgohJCgGv5xT5AcRruGz4ILpfHeBtO/y6j+Z3UN/PAjDeb4P7E51Yh8wcGNLGA==
Expand Down Expand Up @@ -433,7 +433,7 @@
"@ethersproject/bytes" "^5.4.0"
"@ethersproject/properties" "^5.4.0"

"@ethersproject/bignumber@5.4.1", "@ethersproject/bignumber@>=5.0.0-beta.130", "@ethersproject/bignumber@^5.0.7", "@ethersproject/bignumber@^5.4.0":
"@ethersproject/bignumber@5.4.1", "@ethersproject/bignumber@>=5.0.0-beta.130", "@ethersproject/bignumber@^5.0.7", "@ethersproject/bignumber@^5.4.0", "@ethersproject/bignumber@^5.4.1":
version "5.4.1"
resolved "https://registry.yarnpkg.com/@ethersproject/bignumber/-/bignumber-5.4.1.tgz#64399d3b9ae80aa83d483e550ba57ea062c1042d"
integrity sha512-fJhdxqoQNuDOk6epfM7yD6J8Pol4NUCy1vkaGAkuujZm0+lNow//MKu1hLhRiYV4BsOHyBv5/lsTjF+7hWwhJg==
Expand All @@ -456,7 +456,7 @@
dependencies:
"@ethersproject/bignumber" "^5.4.0"

"@ethersproject/contracts@5.4.1":
"@ethersproject/contracts@5.4.1", "@ethersproject/contracts@^5.4.1":
version "5.4.1"
resolved "https://registry.yarnpkg.com/@ethersproject/contracts/-/contracts-5.4.1.tgz#3eb4f35b7fe60a962a75804ada2746494df3e470"
integrity sha512-m+z2ZgPy4pyR15Je//dUaymRUZq5MtDajF6GwFbGAVmKz/RF+DNIPwF0k5qEcL3wPGVqUjFg2/krlCRVTU4T5w==
Expand Down Expand Up @@ -558,7 +558,7 @@
dependencies:
"@ethersproject/logger" "^5.4.0"

"@ethersproject/providers@5.4.5":
"@ethersproject/providers@5.4.5", "@ethersproject/providers@^5.4.4":
version "5.4.5"
resolved "https://registry.yarnpkg.com/@ethersproject/providers/-/providers-5.4.5.tgz#eb2ea2a743a8115f79604a8157233a3a2c832928"
integrity sha512-1GkrvkiAw3Fj28cwi1Sqm8ED1RtERtpdXmRfwIBGmqBSN5MoeRUHuwHPppMtbPayPgpFcvD7/Gdc9doO5fGYgw==
Expand Down Expand Up @@ -620,7 +620,7 @@
elliptic "6.5.4"
hash.js "1.1.7"

"@ethersproject/solidity@5.4.0":
"@ethersproject/solidity@5.4.0", "@ethersproject/solidity@^5.4.0":
version "5.4.0"
resolved "https://registry.yarnpkg.com/@ethersproject/solidity/-/solidity-5.4.0.tgz#1305e058ea02dc4891df18b33232b11a14ece9ec"
integrity sha512-XFQTZ7wFSHOhHcV1DpcWj7VXECEiSrBuv7JErJvB9Uo+KfCdc3QtUZV+Vjh/AAaYgezUEKbCtE6Khjm44seevQ==
Expand Down Expand Up @@ -664,7 +664,7 @@
"@ethersproject/constants" "^5.4.0"
"@ethersproject/logger" "^5.4.0"

"@ethersproject/wallet@5.4.0":
"@ethersproject/wallet@5.4.0", "@ethersproject/wallet@^5.4.0":
version "5.4.0"
resolved "https://registry.yarnpkg.com/@ethersproject/wallet/-/wallet-5.4.0.tgz#fa5b59830b42e9be56eadd45a16a2e0933ad9353"
integrity sha512-wU29majLjM6AjCjpat21mPPviG+EpK7wY1+jzKD0fg3ui5fgedf2zEu1RDgpfIMsfn8fJHJuzM4zXZ2+hSHaSQ==
Expand Down Expand Up @@ -1194,7 +1194,7 @@
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.3.2.tgz#fc8c2825e4ed2142473b4a81064e6e081463d1b3"
integrity sha512-eI5Yrz3Qv4KPUa/nSIAi0h+qX0XyewOliug5F2QAtuRg6Kjg6jfmxe1GIwoIRhZspD1A0RP8ANrPwvEXXtRFog==

"@types/qs@^6.2.31":
"@types/qs@^6.2.31", "@types/qs@^6.9.7":
version "6.9.7"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.7.tgz#63bb7d067db107cc1e457c303bc25d511febf6cb"
integrity sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==
Expand Down Expand Up @@ -1702,7 +1702,7 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==

axios@0.21.4:
axios@0.21.4, axios@^0.21.1:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
Expand Down Expand Up @@ -2695,7 +2695,7 @@ chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.1, chalk@^2.4.2:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^4.0.0, chalk@^4.1.0:
chalk@^4.0.0, chalk@^4.1.0, chalk@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
Expand Down Expand Up @@ -2740,7 +2740,7 @@ chokidar@3.3.0:
optionalDependencies:
fsevents "~2.1.1"

chokidar@3.5.2, chokidar@^3.4.0:
chokidar@3.5.2, chokidar@^3.4.0, chokidar@^3.5.2:
version "3.5.2"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.2.tgz#dba3976fcadb016f66fd365021d91600d01c1e75"
integrity sha512-ekGhOnNVPgT77r4K/U3GDhu+FQ2S8TnK/s2KbIGXi0SZWuwkZ2QNyfWdZW+TVfn84DpEP7rLeCt2UI6bJ8GwbQ==
Expand Down Expand Up @@ -3414,7 +3414,7 @@ debug@3.2.6:
dependencies:
ms "^2.1.1"

debug@4, debug@^4.0.1, debug@^4.1.1:
debug@4, debug@^4.0.1, debug@^4.1.1, debug@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.2.tgz#f0a49c18ac8779e31d4a0c6029dfb76873c7428b"
integrity sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==
Expand Down Expand Up @@ -3774,7 +3774,7 @@ end-of-stream@^1.1.0:
dependencies:
once "^1.4.0"

enquirer@^2.3.0:
enquirer@^2.3.0, enquirer@^2.3.6:
version "2.3.6"
resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.3.6.tgz#2a7fe5dd634a1e4125a975ec994ff5456dc3734d"
integrity sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==
Expand Down Expand Up @@ -4925,6 +4925,15 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down Expand Up @@ -5443,6 +5452,33 @@ hard-rejection@^2.1.0:
resolved "https://registry.yarnpkg.com/hard-rejection/-/hard-rejection-2.1.0.tgz#1c6eda5c1685c63942766d79bb40ae773cecd883"
integrity sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==

hardhat-deploy@^0.9.1:
version "0.9.1"
resolved "https://registry.yarnpkg.com/hardhat-deploy/-/hardhat-deploy-0.9.1.tgz#878bb10ef1bfcfee892477c2133295a250721672"
integrity sha512-GUrLlsBKZqWJ2isFDnpnlnyLuIDqES7mEFGm4P7GutG2jeBebvTg2wCNh3RTW/rpHUtU66yCZXfy+LeD0QXFbg==
dependencies:
"@ethersproject/abi" "^5.4.0"
"@ethersproject/abstract-signer" "^5.4.1"
"@ethersproject/address" "^5.4.0"
"@ethersproject/bignumber" "^5.4.1"
"@ethersproject/bytes" "^5.4.0"
"@ethersproject/contracts" "^5.4.1"
"@ethersproject/providers" "^5.4.4"
"@ethersproject/solidity" "^5.4.0"
"@ethersproject/transactions" "^5.4.0"
"@ethersproject/wallet" "^5.4.0"
"@types/qs" "^6.9.7"
axios "^0.21.1"
chalk "^4.1.2"
chokidar "^3.5.2"
debug "^4.3.2"
enquirer "^2.3.6"
form-data "^4.0.0"
fs-extra "^10.0.0"
match-all "^1.2.6"
murmur-128 "^0.2.1"
qs "^6.9.4"

hardhat-gas-reporter@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/hardhat-gas-reporter/-/hardhat-gas-reporter-1.0.4.tgz#59e3137e38e0dfeac2e4f90d5c74160b50ad4829"
Expand Down Expand Up @@ -7007,6 +7043,11 @@ marked@^0.7.0:
resolved "https://registry.yarnpkg.com/marked/-/marked-0.7.0.tgz#b64201f051d271b1edc10a04d1ae9b74bb8e5c0e"
integrity sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg==

match-all@^1.2.6:
version "1.2.6"
resolved "https://registry.yarnpkg.com/match-all/-/match-all-1.2.6.tgz#66d276ad6b49655551e63d3a6ee53e8be0566f8d"
integrity sha512-0EESkXiTkWzrQQntBu2uzKvLu6vVkUGz40nGPbSZuegcfE5UuSzNjLaIu76zJWuaT/2I3Z/8M06OlUOZLGwLlQ==

mcl-wasm@^0.7.1:
version "0.7.9"
resolved "https://registry.yarnpkg.com/mcl-wasm/-/mcl-wasm-0.7.9.tgz#c1588ce90042a8700c3b60e40efb339fc07ab87f"
Expand Down Expand Up @@ -8353,7 +8394,7 @@ qs@6.7.0:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==

qs@^6.4.0, qs@^6.7.0:
qs@^6.4.0, qs@^6.7.0, qs@^6.9.4:
version "6.10.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.10.1.tgz#4931482fa8d647a5aab799c5271d2133b981fb6a"
integrity sha512-M528Hph6wsSVOBiYUnGf+K/7w0hNshs/duGsNXPUCLH5XAqjEtiPGwNONLV0tBH8NoGb0mvD5JubnUTrujKDTg==
Expand Down

0 comments on commit 0a30e58

Please sign in to comment.