This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
environment.js
153 lines (128 loc) · 4.5 KB
/
environment.js
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
142
143
144
145
146
147
148
149
150
151
152
153
const Web3 = require("web3");
const Web3Shim = require("truffle-interface-adapter").Web3Shim;
const TruffleError = require("truffle-error");
const expect = require("truffle-expect");
const Resolver = require("truffle-resolver");
const Artifactor = require("truffle-artifactor");
const Ganache = require("ganache-core");
const Environment = {
// It's important config is a Config object and not a vanilla object
detect: async function(config) {
expect.options(config, ["networks"]);
helpers.setUpConfig(config);
helpers.validateNetworkConfig(config);
const web3 = new Web3Shim({
provider: config.provider,
networkType: config.networks[config.network].type
});
await helpers.detectAndSetNetworkId(config, web3);
await helpers.setFromOnConfig(config, web3);
},
// Ensure you call Environment.detect() first.
fork: async function(config) {
expect.options(config, ["from", "provider", "networks", "network"]);
const web3 = new Web3Shim({
provider: config.provider,
networkType: config.networks[config.network].type
});
const accounts = await web3.eth.getAccounts();
const block = await web3.eth.getBlock("latest");
const upstreamNetwork = config.network;
const upstreamConfig = config.networks[upstreamNetwork];
const forkedNetwork = config.network + "-fork";
config.networks[forkedNetwork] = {
network_id: config.network_id,
provider: Ganache.provider({
fork: config.provider,
unlocked_accounts: accounts,
gasLimit: block.gasLimit
}),
from: config.from,
gas: upstreamConfig.gas,
gasPrice: upstreamConfig.gasPrice
};
config.network = forkedNetwork;
},
develop: function(config, ganacheOptions, callback) {
expect.options(config, ["networks"]);
var network = config.network || "develop";
var url = `http://${ganacheOptions.host}:${ganacheOptions.port}/`;
config.networks[network] = {
network_id: ganacheOptions.network_id,
provider: function() {
return new Web3.providers.HttpProvider(url, { keepAlive: false });
}
};
config.network = network;
Environment.detect(config)
.then(() => {
callback();
})
.catch(error => {
callback(error);
});
}
};
const helpers = {
setFromOnConfig: async (config, web3) => {
if (config.from) return;
const accounts = await web3.eth.getAccounts();
config.networks[config.network].from = accounts[0];
},
detectAndSetNetworkId: async (config, web3) => {
const configNetworkId = config.networks[config.network].network_id;
const providerNetworkId = await web3.eth.net.getId();
if (configNetworkId !== "*") {
// Ensure the network id matches the one in the config for safety
if (providerNetworkId.toString() !== configNetworkId.toString()) {
const error =
`The network id specified in the truffle config ` +
`(${configNetworkId}) does not match the one returned by the network ` +
`(${providerNetworkId}). Ensure that both the network and the ` +
`provider are properly configured.`;
throw new Error(error);
}
} else {
// We have a "*" network. Get the current network and replace it with the real one.
// TODO: Should we replace this with the blockchain uri?
config.networks[config.network].network_id = providerNetworkId;
}
},
validateNetworkConfig: config => {
const networkConfig = config.networks[config.network];
if (!networkConfig) {
throw new TruffleError(
`Unknown network "${config.network}` +
`". See your Truffle configuration file for available networks.`
);
}
const configNetworkId = config.networks[config.network].network_id;
if (configNetworkId == null) {
throw new Error(
`You must specify a network_id in your '` +
`${config.network}' configuration in order to use this network.`
);
}
},
setUpConfig: config => {
if (!config.resolver) {
config.resolver = new Resolver(config);
}
if (!config.artifactor) {
config.artifactor = new Artifactor(config.contracts_build_directory);
}
if (!config.network) {
if (config.networks["development"]) {
config.network = "development";
} else {
config.network = "ganache";
config.networks[config.network] = {
host: "127.0.0.1",
port: 7545,
network_id: 5777
};
}
}
}
};
module.exports = Environment;