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
167 lines (141 loc) · 5.25 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const Web3 = require("web3");
const { Web3Shim, InterfaceAdapter } = require("@truffle/interface-adapter");
const expect = require("@truffle/expect");
const TruffleError = require("@truffle/error");
const Resolver = require("@truffle/resolver");
const Artifactor = require("@truffle/artifactor");
const Ganache = require("ganache-core/public-exports");
const Provider = require("@truffle/provider");
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 interfaceAdapter = new InterfaceAdapter({
provider: config.provider,
networkType: config.networks[config.network].type
});
const web3 = new Web3Shim({
provider: config.provider,
networkType: config.networks[config.network].type
});
await Provider.testConnection(config);
await helpers.detectAndSetNetworkId(config, web3, interfaceAdapter);
await helpers.setFromOnConfig(config, web3, interfaceAdapter);
},
// Ensure you call Environment.detect() first.
fork: async function(config) {
expect.options(config, ["from", "provider", "networks", "network"]);
const interfaceAdapter = new InterfaceAdapter({
provider: config.provider,
networkType: config.networks[config.network].type
});
const web3 = new Web3Shim({
provider: config.provider,
networkType: config.networks[config.network].type
});
const accounts = await web3.eth.getAccounts();
const block = await interfaceAdapter.getBlock("latest");
const upstreamNetwork = config.network;
const upstreamConfig = config.networks[upstreamNetwork];
const forkedNetwork = config.network + "-fork";
const ganacheOptions = {
fork: config.provider,
gasLimit: block.gasLimit
};
if (accounts.length > 0) ganacheOptions.unlocked_accounts = accounts;
config.networks[forkedNetwork] = {
network_id: config.network_id,
provider: Ganache.provider(ganacheOptions),
from: config.from,
gas: upstreamConfig.gas,
gasPrice: upstreamConfig.gasPrice
};
config.network = forkedNetwork;
},
develop: async (config, ganacheOptions) => {
expect.options(config, ["networks"]);
const network = config.network || "develop";
const 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;
return await Environment.detect(config);
}
};
const helpers = {
setFromOnConfig: async (config, web3, interfaceAdapter) => {
if (config.from) return;
const accounts = await web3.eth.getAccounts();
config.networks[config.network].from = accounts[0];
},
detectAndSetNetworkId: async (config, web3, interfaceAdapter) => {
const configNetworkId = config.networks[config.network].network_id;
const providerNetworkId = await interfaceAdapter.getNetworkId();
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
};
}
}
const currentNetworkSettings = config.networks[config.network];
if (
currentNetworkSettings &&
currentNetworkSettings.ens &&
currentNetworkSettings.ens.registry
) {
config.ens.registryAddress = currentNetworkSettings.ens.registry.address;
}
}
};
module.exports = Environment;