-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathhardhat.config.ts
141 lines (117 loc) · 4.93 KB
/
hardhat.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fs from 'fs';
import * as dotenv from 'dotenv';
import { HardhatUserConfig, task } from 'hardhat/config';
import { MerkleTree } from 'merkletreejs';
import keccak256 from 'keccak256';
import '@nomiclabs/hardhat-etherscan';
import '@nomiclabs/hardhat-waffle';
import '@typechain/hardhat';
import 'hardhat-gas-reporter';
import 'solidity-coverage';
import CollectionConfig from './config/CollectionConfig';
dotenv.config();
// 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 (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
task('generate-root-hash', 'Generates and prints out the root hash for the current whitelist', async () => {
// Check configuration
if (CollectionConfig.whitelistAddresses.length < 1) {
throw 'The whitelist is empty, please add some addresses to the configuration.';
}
// Build the Merkle Tree
const leafNodes = CollectionConfig.whitelistAddresses.map(addr => keccak256(addr));
const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
const rootHash = '0x' + merkleTree.getRoot().toString('hex');
console.log('The Merkle Tree root hash for the current whitelist is: ' + rootHash);
});
task('generate-proof', 'Generates and prints out the whitelist proof for the given address (compatible with Etherscan)', async (taskArgs: {address: string}) => {
// Check configuration
if (CollectionConfig.whitelistAddresses.length < 1) {
throw 'The whitelist is empty, please add some addresses to the configuration.';
}
// Build the Merkle Tree
const leafNodes = CollectionConfig.whitelistAddresses.map(addr => keccak256(addr));
const merkleTree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
const proof = merkleTree.getHexProof(keccak256(taskArgs.address)).toString().replace(/'/g, '').replace(/ /g, '');
console.log('The whitelist proof for the given address is: ' + proof);
})
.addPositionalParam('address', 'The public address');
task('rename-contract', 'Renames the smart contract replacing all occurrences in source files', async (taskArgs: {newName: string}, hre) => {
// Validate new name
if (!/^([A-Z][A-Za-z0-9]+)$/.test(taskArgs.newName)) {
throw 'The contract name must be in PascalCase: https://en.wikipedia.org/wiki/Camel_case#Variations_and_synonyms';
}
const oldContractFile = `${__dirname}/contracts/${CollectionConfig.contractName}.sol`;
const newContractFile = `${__dirname}/contracts/${taskArgs.newName}.sol`;
if (!fs.existsSync(oldContractFile)) {
throw `Contract file not found: "${oldContractFile}" (did you change the configuration manually?)`;
}
if (fs.existsSync(newContractFile)) {
throw `A file with that name already exists: "${oldContractFile}"`;
}
// Replace names in source files
replaceInFile(__dirname + '/../minting-dapp/src/scripts/lib/NftContractType.ts', CollectionConfig.contractName, taskArgs.newName);
replaceInFile(__dirname + '/config/CollectionConfig.ts', CollectionConfig.contractName, taskArgs.newName);
replaceInFile(__dirname + '/lib/NftContractProvider.ts', CollectionConfig.contractName, taskArgs.newName);
replaceInFile(oldContractFile, CollectionConfig.contractName, taskArgs.newName);
// Rename the contract file
fs.renameSync(oldContractFile, newContractFile);
console.log(`Contract renamed successfully from "${CollectionConfig.contractName}" to "${taskArgs.newName}"!`);
// Rebuilding types
await hre.run('typechain');
})
.addPositionalParam('newName', 'The new name');
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
const config: HardhatUserConfig = {
solidity: {
version: '0.8.9',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
networks: {
truffle: {
url: 'http://localhost:24012/rpc',
},
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: 'USD',
coinmarketcap: process.env.GAS_REPORTER_COIN_MARKET_CAP_API_KEY,
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY,
},
};
// Setup Rinkeby network
if (process.env.NETWORK_RINKEBY_URL !== undefined) {
config.networks!.rinkeby = {
url: process.env.NETWORK_RINKEBY_URL,
accounts: [process.env.NETWORK_RINKEBY_PRIVATE_KEY!],
};
}
// Setup Ethereum network
if (process.env.NETWORK_MAINNET_URL !== undefined) {
config.networks!.mainnet = {
url: process.env.NETWORK_MAINNET_URL,
accounts: [process.env.NETWORK_MAINNET_PRIVATE_KEY!],
};
}
export default config;
/**
* Replaces all occurrences of a string in the given file.
*/
function replaceInFile(file: string, search: string, replace: string): void
{
const fileContent = fs.readFileSync(file, 'utf8').replace(new RegExp(search, 'g'), replace);
fs.writeFileSync(file, fileContent, 'utf8');
}