1 | import assignIn from "lodash.assignin";
|
2 | import * as path from "path";
|
3 | import Provider from "@truffle/provider";
|
4 | import TruffleConfig from "./";
|
5 |
|
6 | export const getInitialConfig = ({
|
7 | truffleDirectory,
|
8 | workingDirectory,
|
9 | network
|
10 | }: {
|
11 | truffleDirectory?: string;
|
12 | workingDirectory?: string;
|
13 | network?: string;
|
14 | }) => {
|
15 | const truffle_directory =
|
16 | truffleDirectory || path.resolve(path.join(__dirname, "../"));
|
17 | const working_directory = workingDirectory || process.cwd();
|
18 |
|
19 | return {
|
20 | truffle_directory,
|
21 | working_directory,
|
22 | network,
|
23 | networks: {},
|
24 | verboseRpc: false,
|
25 | gas: null,
|
26 | gasPrice: null,
|
27 | from: null,
|
28 | confirmations: 0,
|
29 | timeoutBlocks: 0,
|
30 | production: false,
|
31 | skipDryRun: false,
|
32 | build: null,
|
33 | resolver: null,
|
34 | artifactor: null,
|
35 | ethpm: {
|
36 | ipfs_host: "ipfs.infura.io",
|
37 | ipfs_protocol: "https",
|
38 | registry: "0x8011df4830b4f696cd81393997e5371b93338878",
|
39 | install_provider_uri:
|
40 | "https://ropsten.infura.io/v3/26e88e46be924823983710becd929f36"
|
41 | },
|
42 | ens: {
|
43 | enabled: false,
|
44 | registryAddress: null
|
45 | },
|
46 | compilers: {
|
47 | solc: {
|
48 | settings: {
|
49 | optimizer: {
|
50 | enabled: false,
|
51 | runs: 200
|
52 | },
|
53 | remappings: []
|
54 | }
|
55 | },
|
56 | vyper: {}
|
57 | },
|
58 | logger: console
|
59 | };
|
60 | };
|
61 |
|
62 | export const configProps = ({
|
63 | configObject
|
64 | }: {
|
65 | configObject: TruffleConfig;
|
66 | }) => {
|
67 | const resolveDirectory = (value: string): string =>
|
68 | path.resolve(configObject.working_directory, value);
|
69 |
|
70 | const defaultTXValues = {
|
71 | gas: 6721975,
|
72 | gasPrice: 20000000000,
|
73 | from: null
|
74 | };
|
75 |
|
76 | return {
|
77 |
|
78 | truffle_directory() {},
|
79 | working_directory() {},
|
80 | network() {},
|
81 | networks() {},
|
82 | verboseRpc() {},
|
83 | build() {},
|
84 | resolver() {},
|
85 | artifactor() {},
|
86 | ethpm() {},
|
87 | logger() {},
|
88 | compilers() {},
|
89 | ens() {},
|
90 |
|
91 | build_directory: {
|
92 | default: () => path.join(configObject.working_directory, "build"),
|
93 | transform: resolveDirectory
|
94 | },
|
95 | contracts_directory: {
|
96 | default: () => path.join(configObject.working_directory, "contracts"),
|
97 | transform: resolveDirectory
|
98 | },
|
99 | contracts_build_directory: {
|
100 | default: () => path.join(configObject.build_directory, "contracts"),
|
101 | transform: resolveDirectory
|
102 | },
|
103 | migrations_directory: {
|
104 | default: () => path.join(configObject.working_directory, "migrations"),
|
105 | transform: resolveDirectory
|
106 | },
|
107 | migrations_file_extension_regexp() {
|
108 | return /^\.(js|es6?)$/;
|
109 | },
|
110 | test_directory: {
|
111 | default: () => path.join(configObject.working_directory, "test"),
|
112 | transform: resolveDirectory
|
113 | },
|
114 | test_file_extension_regexp() {
|
115 | return /.*\.(js|ts|es|es6|jsx|sol)$/;
|
116 | },
|
117 | example_project_directory: {
|
118 | default: () => path.join(configObject.truffle_directory, "example"),
|
119 | transform: resolveDirectory
|
120 | },
|
121 | network_id: {
|
122 | get() {
|
123 | try {
|
124 | return configObject.network_config.network_id;
|
125 | } catch (e) {
|
126 | return null;
|
127 | }
|
128 | },
|
129 | set() {
|
130 | throw new Error(
|
131 | "Do not set config.network_id. Instead, set config.networks and then config.networks[<network name>].network_id"
|
132 | );
|
133 | }
|
134 | },
|
135 | network_config: {
|
136 | get() {
|
137 | const network = configObject.network;
|
138 |
|
139 | if (network === null || network === undefined) {
|
140 | throw new Error("Network not set. Cannot determine network to use.");
|
141 | }
|
142 |
|
143 | let config = configObject.networks[network];
|
144 |
|
145 | if (config === null || config === undefined) {
|
146 | config = {};
|
147 | }
|
148 |
|
149 | config = assignIn({}, defaultTXValues, config);
|
150 |
|
151 | return config;
|
152 | },
|
153 | set() {
|
154 | throw new Error(
|
155 | "Don't set config.network_config. Instead, set config.networks with the desired values."
|
156 | );
|
157 | }
|
158 | },
|
159 | from: {
|
160 | get() {
|
161 | try {
|
162 | return configObject.network_config.from;
|
163 | } catch (e) {
|
164 | return defaultTXValues.from;
|
165 | }
|
166 | },
|
167 | set() {
|
168 | throw new Error(
|
169 | "Don't set config.from directly. Instead, set config.networks and then config.networks[<network name>].from"
|
170 | );
|
171 | }
|
172 | },
|
173 | gas: {
|
174 | get() {
|
175 | try {
|
176 | return configObject.network_config.gas;
|
177 | } catch (e) {
|
178 | return defaultTXValues.gas;
|
179 | }
|
180 | },
|
181 | set() {
|
182 | throw new Error(
|
183 | "Don't set config.gas directly. Instead, set config.networks and then config.networks[<network name>].gas"
|
184 | );
|
185 | }
|
186 | },
|
187 | gasPrice: {
|
188 | get() {
|
189 | try {
|
190 | return configObject.network_config.gasPrice;
|
191 | } catch (e) {
|
192 | return defaultTXValues.gasPrice;
|
193 | }
|
194 | },
|
195 | set() {
|
196 | throw new Error(
|
197 | "Don't set config.gasPrice directly. Instead, set config.networks and then config.networks[<network name>].gasPrice"
|
198 | );
|
199 | }
|
200 | },
|
201 | provider: {
|
202 | get() {
|
203 | if (!configObject.network) {
|
204 | return null;
|
205 | }
|
206 |
|
207 | const options = configObject.network_config;
|
208 | options.verboseRpc = configObject.verboseRpc;
|
209 |
|
210 | return Provider.create(options);
|
211 | },
|
212 | set() {
|
213 | throw new Error(
|
214 | "Don't set config.provider directly. Instead, set config.networks and then set config.networks[<network name>].provider"
|
215 | );
|
216 | }
|
217 | },
|
218 | confirmations: {
|
219 | get() {
|
220 | try {
|
221 | return configObject.network_config.confirmations;
|
222 | } catch (e) {
|
223 | return 0;
|
224 | }
|
225 | },
|
226 | set() {
|
227 | throw new Error(
|
228 | "Don't set config.confirmations directly. Instead, set config.networks and then config.networks[<network name>].confirmations"
|
229 | );
|
230 | }
|
231 | },
|
232 | production: {
|
233 | get() {
|
234 | try {
|
235 | return configObject.network_config.production;
|
236 | } catch (e) {
|
237 | return false;
|
238 | }
|
239 | },
|
240 | set() {
|
241 | throw new Error(
|
242 | "Don't set config.production directly. Instead, set config.networks and then config.networks[<network name>].production"
|
243 | );
|
244 | }
|
245 | },
|
246 | timeoutBlocks: {
|
247 | get() {
|
248 | try {
|
249 | return configObject.network_config.timeoutBlocks;
|
250 | } catch (e) {
|
251 | return 0;
|
252 | }
|
253 | },
|
254 | set() {
|
255 | throw new Error(
|
256 | "Don't set config.timeoutBlocks directly. Instead, set config.networks and then config.networks[<network name>].timeoutBlocks"
|
257 | );
|
258 | }
|
259 | }
|
260 | };
|
261 | };
|