-
Notifications
You must be signed in to change notification settings - Fork 10
/
NetworkConfiguration.ts
90 lines (82 loc) · 2.4 KB
/
NetworkConfiguration.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
require("dotenv").config();
import BN = require("bn.js");
type NetworkOptions = {
isProduction: boolean;
http: string;
privateKey: string;
gasPrice: BN;
};
type Networks = {
[networkName: string]: NetworkOptions;
};
const networks: Networks = {
kovan: {
isProduction: false,
http: process.env.KOVAN_ETHEREUM_HTTP!,
privateKey: process.env.KOVAN_PRIVATE_KEY!,
gasPrice: new BN(1)
},
rinkeby: {
isProduction: false,
http: process.env.RINKEBY_ETHEREUM_HTTP!,
privateKey: process.env.RINKEBY_PRIVATE_KEY!,
gasPrice: new BN(31 * 1000000000)
},
ropsten: {
isProduction: false,
http: process.env.ROPSTEN_ETHEREUM_HTTP!,
privateKey: process.env.ROPSTEN_PRIVATE_KEY!,
gasPrice: new BN(20 * 1000000000)
},
mainnet: {
isProduction: true,
http: process.env.MAINNET_ETHEREUM_HTTP!,
privateKey: process.env.MAINNET_PRIVATE_KEY!,
gasPrice: (typeof process.env.ETHEREUM_GAS_PRICE_IN_NANOETH === "undefined"
? new BN(20)
: new BN(process.env.ETHEREUM_GAS_PRICE_IN_NANOETH!)
).mul(new BN(1000000000))
},
testrpc: {
isProduction: false,
http: "http://localhost:8545",
gasPrice: new BN(1),
privateKey: process.env.TESTRPC_PRIVATE_KEY!
}
};
export class NetworkConfiguration {
public readonly networkName: string;
public readonly http: string;
public readonly privateKey: string;
public readonly gasPrice: BN;
public readonly isProduction: boolean;
public constructor(
networkName: string,
http: string,
gasPrice: BN,
privateKey: string,
isProduction: boolean
) {
this.networkName = networkName;
this.http = http;
this.gasPrice = gasPrice;
this.privateKey = privateKey;
this.isProduction = isProduction;
}
public static create(networkName: string = "kovan"): NetworkConfiguration {
const network = networks[networkName];
if (network === undefined || network === null)
throw new Error(`Network configuration ${networkName} not found`);
if (network.privateKey === undefined || network.privateKey === null)
throw new Error(
`Network configuration for ${networkName} has no private key available. Check that this key is in the environment ${networkName.toUpperCase()}_PRIVATE_KEY`
);
return new NetworkConfiguration(
networkName,
network.http,
network.gasPrice,
network.privateKey,
network.isProduction
);
}
}