diff --git a/libs/remix-simulator/src/genesis.ts b/libs/remix-simulator/src/genesis.ts index b9674c695e..84b97526ff 100644 --- a/libs/remix-simulator/src/genesis.ts +++ b/libs/remix-simulator/src/genesis.ts @@ -1,6 +1,5 @@ -const EthJSBlock = require('ethereumjs-block') -const ethJSUtil = require('ethereumjs-util') -const BN = ethJSUtil.BN +import EthJSBlock from 'ethereumjs-block' +import { BN } from 'ethereumjs-util' function generateBlock (executionContext) { const block = new EthJSBlock({ diff --git a/libs/remix-simulator/src/methods/misc.ts b/libs/remix-simulator/src/methods/misc.ts index 045082702f..241f0d3947 100644 --- a/libs/remix-simulator/src/methods/misc.ts +++ b/libs/remix-simulator/src/methods/misc.ts @@ -1,64 +1,62 @@ const version = require('../../package.json').version -const web3 = require('web3') - -const Misc = function () { -} - -Misc.prototype.methods = function () { - return { - web3_clientVersion: this.web3_clientVersion.bind(this), - eth_protocolVersion: this.eth_protocolVersion.bind(this), - eth_syncing: this.eth_syncing.bind(this), - eth_mining: this.eth_mining.bind(this), - eth_hashrate: this.eth_hashrate.bind(this), - web3_sha3: this.web3_sha3.bind(this), - eth_getCompilers: this.eth_getCompilers.bind(this), - eth_compileSolidity: this.eth_compileSolidity.bind(this), - eth_compileLLL: this.eth_compileLLL.bind(this), - eth_compileSerpent: this.eth_compileSerpent.bind(this) +import web3 from 'web3' + +export class Misc { + + methods () { + return { + web3_clientVersion: this.web3_clientVersion.bind(this), + eth_protocolVersion: this.eth_protocolVersion.bind(this), + eth_syncing: this.eth_syncing.bind(this), + eth_mining: this.eth_mining.bind(this), + eth_hashrate: this.eth_hashrate.bind(this), + web3_sha3: this.web3_sha3.bind(this), + eth_getCompilers: this.eth_getCompilers.bind(this), + eth_compileSolidity: this.eth_compileSolidity.bind(this), + eth_compileLLL: this.eth_compileLLL.bind(this), + eth_compileSerpent: this.eth_compileSerpent.bind(this) + } } -} -Misc.prototype.web3_clientVersion = function (payload, cb) { - cb(null, 'Remix Simulator/' + version) -} + web3_clientVersion (payload, cb) { + cb(null, 'Remix Simulator/' + version) + } -Misc.prototype.eth_protocolVersion = function (payload, cb) { - cb(null, '0x3f') -} + eth_protocolVersion (payload, cb) { + cb(null, '0x3f') + } -Misc.prototype.eth_syncing = function (payload, cb) { - cb(null, false) -} + eth_syncing (payload, cb) { + cb(null, false) + } -Misc.prototype.eth_mining = function (payload, cb) { - // TODO: should depend on the state - cb(null, false) -} + eth_mining (payload, cb) { + // TODO: should depend on the state + cb(null, false) + } -Misc.prototype.eth_hashrate = function (payload, cb) { - cb(null, '0x0') -} + eth_hashrate (payload, cb) { + cb(null, '0x0') + } -Misc.prototype.web3_sha3 = function (payload, cb) { - const str = payload.params[0] - cb(null, web3.utils.sha3(str)) -} + web3_sha3 (payload, cb) { + const str = payload.params[0] + cb(null, web3.utils.sha3(str)) + } -Misc.prototype.eth_getCompilers = function (payload, cb) { - cb(null, []) -} + eth_getCompilers (payload, cb) { + cb(null, []) + } -Misc.prototype.eth_compileSolidity = function (payload, cb) { - cb(null, 'unsupported') -} + eth_compileSolidity (payload, cb) { + cb(null, 'unsupported') + } -Misc.prototype.eth_compileLLL = function (payload, cb) { - cb(null, 'unsupported') -} + eth_compileLLL (payload, cb) { + cb(null, 'unsupported') + } -Misc.prototype.eth_compileSerpent = function (payload, cb) { - cb(null, 'unsupported') + eth_compileSerpent (payload, cb) { + cb(null, 'unsupported') + } } - -module.exports = Misc diff --git a/libs/remix-simulator/src/methods/net.ts b/libs/remix-simulator/src/methods/net.ts index 1c4819c19a..ef1cb34322 100644 --- a/libs/remix-simulator/src/methods/net.ts +++ b/libs/remix-simulator/src/methods/net.ts @@ -1,26 +1,24 @@ -const Net = function () { -} +export class Net { -Net.prototype.methods = function () { - return { - net_version: this.net_version, - net_listening: this.net_listening, - net_peerCount: this.net_peerCount + methods () { + return { + net_version: this.net_version, + net_listening: this.net_listening, + net_peerCount: this.net_peerCount + } } -} -Net.prototype.net_version = function (payload, cb) { - // should be configured networkId - cb(null, 1337) -} + net_version (payload, cb) { + // should be configured networkId + cb(null, 1337) + } -Net.prototype.net_listening = function (payload, cb) { - cb(null, true) -} + net_listening (payload, cb) { + cb(null, true) + } -Net.prototype.net_peerCount = function (payload, cb) { - cb(null, 0) + net_peerCount (payload, cb) { + cb(null, 0) + } } - -module.exports = Net diff --git a/libs/remix-simulator/src/provider.ts b/libs/remix-simulator/src/provider.ts index d40c672bd2..c735a66674 100644 --- a/libs/remix-simulator/src/provider.ts +++ b/libs/remix-simulator/src/provider.ts @@ -1,16 +1,17 @@ import { Blocks } from './methods/blocks' -const RemixLib = require('@remix-project/remix-lib') -const executionContext = RemixLib.execution.executionContext +import { execution } from '@remix-project/remix-lib' +const { executionContext } = execution -const log = require('./utils/logs.js') -const merge = require('merge') +import { Logger } from './utils/logs' +const logger = new Logger() +import merge from 'merge' -const Accounts = require('./methods/accounts.js') -const Filters = require('./methods/filters.js') -const Misc = require('./methods/misc.js') -const Net = require('./methods/net.js') -const Transactions = require('./methods/transactions.js') -const Debug = require('./methods/debug.js') +import { Accounts } from './methods/accounts' +import { Filters } from './methods/filters' +import { Misc } from './methods/misc' +import { Net } from './methods/net.js' +import { Transactions } from './methods/transactions.js' +import { Debug } from './methods/debug.js' const generateBlock = require('./genesis.js') @@ -51,13 +52,13 @@ export class Provider { const method = this.methods[payload.method] if (this.options.logDetails) { - log.info(payload) + logger.info(payload) } if (method) { return method.call(method, payload, (err, result) => { if (this.options.logDetails) { - log.info(err) - log.info(result) + logger.info(err) + logger.info(result) } if (err) { return callback(err) diff --git a/libs/remix-simulator/src/server.ts b/libs/remix-simulator/src/server.ts index b2eac34902..31167f0471 100644 --- a/libs/remix-simulator/src/server.ts +++ b/libs/remix-simulator/src/server.ts @@ -1,18 +1,23 @@ -const express = require('express') -const cors = require('cors') -const bodyParser = require('body-parser') +import express from 'express' +import cors from 'cors' +import bodyParser from 'body-parser' const app = express() -const expressWs = require('express-ws') -const Provider = require('./provider') -const log = require('./utils/logs.js') +import expressWs from 'express-ws' +import { Provider } from './provider' +import { Logger } from './utils/logs' +const logger = new Logger() class Server { + + provider + rpcOnly + constructor (options) { this.provider = new Provider(options) this.provider.init().then(() => { - log('Provider initiated') + logger.log('Provider initiated') }).catch((error) => { - log(error) + logger.log(error) }) this.rpcOnly = options.rpc } @@ -55,9 +60,9 @@ class Server { } app.listen(port, host, () => { - log('Remix Simulator listening on ws://' + host + ':' + port) + logger.log('Remix Simulator listening on ws://' + host + ':' + port) if (!this.rpcOnly) { - log('http json-rpc is deprecated and disabled by default. To enable it use --rpc') + logger.log('http json-rpc is deprecated and disabled by default. To enable it use --rpc') } }) } diff --git a/libs/remix-simulator/src/utils/logs.ts b/libs/remix-simulator/src/utils/logs.ts index d6a22b651a..45099fd91f 100644 --- a/libs/remix-simulator/src/utils/logs.ts +++ b/libs/remix-simulator/src/utils/logs.ts @@ -1,82 +1,78 @@ 'use strict' -const gray = require('ansi-gray') -const timestamp = require('time-stamp') -const supportsColor = require('color-support') +import gray from 'ansi-gray' +import timestamp from 'time-stamp' +import supportsColor from 'color-support' -function hasFlag (flag) { - return ((typeof (process) !== 'undefined') && (process.argv.indexOf('--' + flag) !== -1)) -} +export class Logger { -function addColor (str) { - if (hasFlag('no-color')) { - return str + private hasFlag(flag) { + return ((typeof (process) !== 'undefined') && (process.argv.indexOf('--' + flag) !== -1)) } - if (hasFlag('color')) { - return gray(str) - } + private addColor(str) { + if (this.hasFlag('no-color')) { + return str + } - if (supportsColor()) { - return gray(str) - } + if (this.hasFlag('color')) { + return gray(str) + } - return str -} + if (supportsColor()) { + return gray(str) + } + + return str + } -const logger = { - stdout: function (arg) { + private stdout(arg) { if (typeof (process) === 'undefined' || !process.stdout) return process.stdout.write(arg) - }, - stderr: function (arg) { + } + + private stderr(arg) { if (typeof (process) === 'undefined' || process.stderr) return process.stderr.write(arg) } -} -function getTimestamp () { - const coloredTimestamp = addColor(timestamp('HH:mm:ss')) - return '[' + coloredTimestamp + ']' -} + private getTimestamp() { + const coloredTimestamp = this.addColor(timestamp('HH:mm:ss')) + return '[' + coloredTimestamp + ']' + } -function log () { - const time = getTimestamp() - logger.stdout(time + ' ') - console.log.apply(console, arguments) - return this -} + log(...args: any[]) { + const time = this.getTimestamp() + this.stdout(time + ' ') + console.log(args) + return this + } -function info () { - const time = getTimestamp() - logger.stdout(time + ' ') - console.info.apply(console, arguments) - return this -} + info(...args: any[]) { + const time = this.getTimestamp() + this.stdout(time + ' ') + console.info(args) + return this + } -function dir () { - const time = getTimestamp() - logger.stdout(time + ' ') - console.dir.apply(console, arguments) - return this -} + dir(...args: any[]) { + const time = this.getTimestamp() + this.stdout(time + ' ') + console.dir(args) + return this + } -function warn () { - const time = getTimestamp() - logger.stderr(time + ' ') - console.warn.apply(console, arguments) - return this -} + warn(...args: any[]) { + const time = this.getTimestamp() + this.stderr(time + ' ') + console.warn(args) + return this + } -function error () { - const time = getTimestamp() - logger.stderr(time + ' ') - console.error.apply(console, arguments) - return this + error(...args: any[]) { + const time = this.getTimestamp() + this.stderr(time + ' ') + console.error(args) + return this + } } - -module.exports = log -module.exports.info = info -module.exports.dir = dir -module.exports.warn = warn -module.exports.error = error diff --git a/libs/remix-simulator/tsconfig.lib.json b/libs/remix-simulator/tsconfig.lib.json index 463da90b45..f8c817af2a 100644 --- a/libs/remix-simulator/tsconfig.lib.json +++ b/libs/remix-simulator/tsconfig.lib.json @@ -8,7 +8,8 @@ "types": ["node"] }, "exclude": [ - "**/*.spec.ts" + "**/*.spec.ts", + "test/" ], "include": ["**/*.ts"] } diff --git a/package-lock.json b/package-lock.json index bad6de44fc..f78d190743 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9013,8 +9013,7 @@ "async-limiter": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", - "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==", - "dev": true + "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" }, "async-settle": { "version": "1.0.0", @@ -16901,6 +16900,24 @@ } } }, + "express-ws": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/express-ws/-/express-ws-4.0.0.tgz", + "integrity": "sha512-KEyUw8AwRET2iFjFsI1EJQrJ/fHeGiJtgpYgEWG3yDv4l/To/m3a2GaYfeGyB3lsWdvbesjF5XCMx+SVBgAAYw==", + "requires": { + "ws": "^5.2.0" + }, + "dependencies": { + "ws": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-5.2.2.tgz", + "integrity": "sha512-jaHFD6PFv6UgoIVda6qZllptQsMlDEJkTQcybzzXDYM1XO9Y8em691FGMPmM46WGyLU4z9KMgQN+qrux/nhlHA==", + "requires": { + "async-limiter": "~1.0.0" + } + } + } + }, "ext": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/ext/-/ext-1.4.0.tgz", @@ -28265,7 +28282,6 @@ }, "dezalgo": { "version": "1.0.3", - "resolved": false, "integrity": "sha1-f3Qt4Gb8dIvI24IFad3c5Jvw1FY=", "requires": { "asap": "^2.0.0", @@ -28894,7 +28910,6 @@ }, "normalize-git-url": { "version": "3.0.2", - "resolved": false, "integrity": "sha1-jl8Uvgva7bc+ByADEKpBbCc1D8Q=" }, "normalize-package-data": { @@ -28932,7 +28947,6 @@ }, "npm-install-checks": { "version": "3.0.0", - "resolved": false, "integrity": "sha1-1K7N/VGlPjcjt7L5Oy7ijjB7wNc=", "requires": { "semver": "^2.3.0 || 3.x || 4 || 5" @@ -29279,7 +29293,6 @@ }, "realize-package-specifier": { "version": "3.0.3", - "resolved": false, "integrity": "sha1-0N74gpUrjeP2frpekRmWYScfQfQ=", "requires": { "dezalgo": "^1.0.1", @@ -29816,7 +29829,6 @@ "dependencies": { "unique-slug": { "version": "2.0.0", - "resolved": false, "integrity": "sha1-22Z258fMBimHj/GWCXx4hVrp9Ks=", "requires": { "imurmurhash": "^0.1.4" diff --git a/package.json b/package.json index 21767dfd66..1d0e558e99 100644 --- a/package.json +++ b/package.json @@ -145,6 +145,7 @@ "ethereumjs-block": "^2.2.2", "ethereumjs-tx": "^2.1.2", "ethereumjs-vm": "4.1.3", + "express-ws": "^4.0.0", "fs-extra": "^3.0.1", "http-server": "^0.11.1", "isbinaryfile": "^3.0.2", diff --git a/workspace.json b/workspace.json index 4eeb4a9d9a..ea27de7ffd 100644 --- a/workspace.json +++ b/workspace.json @@ -292,7 +292,7 @@ }, "remix-simulator": { "root": "libs/remix-simulator", - "sourceRoot": "libs/remix-simulator/", + "sourceRoot": "libs/remix-simulator/src", "projectType": "library", "schematics": {}, "architect": { @@ -322,7 +322,7 @@ "outputPath": "dist/libs/remix-simulator", "tsConfig": "libs/remix-simulator/tsconfig.lib.json", "packageJson": "libs/remix-simulator/package.json", - "main": "libs/remix-simulator/index.js", + "main": "libs/remix-simulator/src/index.ts", "assets": [ { "glob": "ethsim",