1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const chalk_1 = __importDefault(require("chalk"));
|
7 | const debug_1 = __importDefault(require("debug"));
|
8 | const ethereumjs_util_1 = require("ethereumjs-util");
|
9 | const server_1 = require("../internal/buidler-evm/jsonrpc/server");
|
10 | const constants_1 = require("../internal/constants");
|
11 | const config_env_1 = require("../internal/core/config/config-env");
|
12 | const errors_1 = require("../internal/core/errors");
|
13 | const errors_list_1 = require("../internal/core/errors-list");
|
14 | const construction_1 = require("../internal/core/providers/construction");
|
15 | const reporter_1 = require("../internal/sentry/reporter");
|
16 | const lazy_1 = require("../internal/util/lazy");
|
17 | const task_names_1 = require("./task-names");
|
18 | const watch_1 = require("./utils/watch");
|
19 | const log = debug_1.default("buidler:core:tasks:node");
|
20 | function _createBuidlerEVMProvider(config) {
|
21 | log("Creating BuidlerEVM Provider");
|
22 | const networkName = constants_1.BUIDLEREVM_NETWORK_NAME;
|
23 | const networkConfig = config.networks[networkName];
|
24 | return lazy_1.lazyObject(() => {
|
25 | log(`Creating buidlerevm provider for JSON-RPC sever`);
|
26 | return construction_1.createProvider(networkName, Object.assign({ loggingEnabled: true }, networkConfig), config.solc.version, config.paths);
|
27 | });
|
28 | }
|
29 | function logBuidlerEvmAccounts(networkConfig) {
|
30 | if (networkConfig.accounts === undefined) {
|
31 | return;
|
32 | }
|
33 | console.log("Accounts");
|
34 | console.log("========");
|
35 | for (const [index, account] of networkConfig.accounts.entries()) {
|
36 | const address = ethereumjs_util_1.bufferToHex(ethereumjs_util_1.privateToAddress(ethereumjs_util_1.toBuffer(account.privateKey)));
|
37 | const privateKey = ethereumjs_util_1.bufferToHex(ethereumjs_util_1.toBuffer(account.privateKey));
|
38 | const balance = new ethereumjs_util_1.BN(account.balance)
|
39 | .div(new ethereumjs_util_1.BN(10).pow(new ethereumjs_util_1.BN(18)))
|
40 | .toString(10);
|
41 | console.log(`Account #${index}: ${address} (${balance} ETH)
|
42 | Private Key: ${privateKey}
|
43 | `);
|
44 | }
|
45 | }
|
46 | function default_1() {
|
47 | config_env_1.task(task_names_1.TASK_NODE, "Starts a JSON-RPC server on top of Buidler EVM")
|
48 | .addOptionalParam("hostname", "The host to which to bind to for new connections", "localhost", config_env_1.types.string)
|
49 | .addOptionalParam("port", "The port on which to listen for new connections", 8545, config_env_1.types.int)
|
50 | .setAction(async ({ hostname, port }, { network, buidlerArguments, config }) => {
|
51 | if (network.name !== constants_1.BUIDLEREVM_NETWORK_NAME &&
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | buidlerArguments.network !== undefined &&
|
57 | buidlerArguments.network !== config.defaultNetwork) {
|
58 | throw new errors_1.BuidlerError(errors_list_1.ERRORS.BUILTIN_TASKS.JSONRPC_UNSUPPORTED_NETWORK);
|
59 | }
|
60 | try {
|
61 | const serverConfig = {
|
62 | hostname,
|
63 | port,
|
64 | provider: _createBuidlerEVMProvider(config),
|
65 | };
|
66 | const server = new server_1.JsonRpcServer(serverConfig);
|
67 | const { port: actualPort, address } = await server.listen();
|
68 | console.log(chalk_1.default.green(`Started HTTP and WebSocket JSON-RPC server at http://${address}:${actualPort}/`));
|
69 | console.log();
|
70 | try {
|
71 | await watch_1.watchCompilerOutput(server.getProvider(), config.solc, config.paths);
|
72 | }
|
73 | catch (error) {
|
74 | console.warn(chalk_1.default.yellow("There was a problem watching the compiler output, changes in the contracts won't be reflected in the Buidler EVM. Run Buidler with --verbose to learn more."));
|
75 | log("Compilation output can't be watched. Please report this to help us improve Buidler.\n", error);
|
76 | reporter_1.Reporter.reportError(error);
|
77 | }
|
78 | const networkConfig = config.networks[constants_1.BUIDLEREVM_NETWORK_NAME];
|
79 | logBuidlerEvmAccounts(networkConfig);
|
80 | await server.waitUntilClosed();
|
81 | }
|
82 | catch (error) {
|
83 | if (errors_1.BuidlerError.isBuidlerError(error)) {
|
84 | throw error;
|
85 | }
|
86 | throw new errors_1.BuidlerError(errors_list_1.ERRORS.BUILTIN_TASKS.JSONRPC_SERVER_ERROR, {
|
87 | error: error.message,
|
88 | }, error);
|
89 | }
|
90 | });
|
91 | }
|
92 | exports.default = default_1;
|
93 |
|
\ | No newline at end of file |