forked from wighawag/template-ethereum-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork.ts
94 lines (86 loc) · 2.81 KB
/
network.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
import 'dotenv/config';
import {HDAccountsUserConfig, HttpNetworkUserConfig, NetworksUserConfig} from 'hardhat/types';
export function node_url(networkName: string): string {
if (networkName) {
const uri = process.env['ETH_NODE_URI_' + networkName.toUpperCase()];
if (uri && uri !== '') {
return uri;
}
}
if (networkName === 'localhost') {
// do not use ETH_NODE_URI
return 'http://localhost:8545';
}
let uri = process.env.ETH_NODE_URI;
if (uri) {
uri = uri.replace('{{networkName}}', networkName);
}
if (!uri || uri === '') {
// throw new Error(`environment variable "ETH_NODE_URI" not configured `);
return '';
}
if (uri.indexOf('{{') >= 0) {
throw new Error(`invalid uri or network not supported by node provider : ${uri}`);
}
return uri;
}
export function getMnemonic(networkName?: string): string {
if (networkName) {
const mnemonic = process.env['MNEMONIC_' + networkName.toUpperCase()];
if (mnemonic && mnemonic !== '') {
return mnemonic;
}
}
const mnemonic = process.env.MNEMONIC;
if (!mnemonic || mnemonic === '') {
return 'test test test test test test test test test test test junk';
}
return mnemonic;
}
export function accounts(networkName?: string): {mnemonic: string} {
return {mnemonic: getMnemonic(networkName)};
}
export function addForkConfiguration(networks: NetworksUserConfig): NetworksUserConfig {
// While waiting for hardhat PR: https://github.com/nomiclabs/hardhat/pull/1542
if (process.env.HARDHAT_FORK) {
process.env['HARDHAT_DEPLOY_FORK'] = process.env.HARDHAT_FORK;
}
const currentNetworkName = process.env.HARDHAT_FORK;
let forkURL: string | undefined = currentNetworkName && node_url(currentNetworkName);
let hardhatAccounts: HDAccountsUserConfig | undefined;
if (currentNetworkName && currentNetworkName !== 'hardhat') {
const currentNetwork = networks[currentNetworkName] as HttpNetworkUserConfig;
if (currentNetwork) {
forkURL = currentNetwork.url;
if (
currentNetwork.accounts &&
typeof currentNetwork.accounts === 'object' &&
'mnemonic' in currentNetwork.accounts
) {
hardhatAccounts = currentNetwork.accounts;
}
}
}
const newNetworks = {
...networks,
hardhat: {
...networks.hardhat,
...{
accounts: hardhatAccounts,
forking: forkURL
? {
url: forkURL,
blockNumber: process.env.HARDHAT_FORK_NUMBER ? parseInt(process.env.HARDHAT_FORK_NUMBER) : undefined,
}
: undefined,
mining: process.env.MINING_INTERVAL
? {
auto: false,
interval: process.env.MINING_INTERVAL.split(',').map((v) => parseInt(v)) as [number, number],
}
: undefined,
},
},
};
return newNetworks;
}