class and type updates

revert-603-optF
aniket-engg 4 years ago committed by Aniket
parent febab2efc2
commit 0c4348a6ee
  1. 2
      libs/remix-simulator/src/genesis.ts
  2. 2
      libs/remix-simulator/src/index.ts
  3. 52
      libs/remix-simulator/src/methods/misc.ts
  4. 18
      libs/remix-simulator/src/methods/net.ts
  5. 17
      libs/remix-simulator/src/provider.ts
  6. 41
      libs/remix-simulator/src/utils/logs.ts
  7. 14
      libs/remix-simulator/test/accounts.ts

@ -2,7 +2,7 @@ import EthJSBlock from 'ethereumjs-block'
import { BN } from 'ethereumjs-util' import { BN } from 'ethereumjs-util'
export function generateBlock (executionContext) { export function generateBlock (executionContext) {
const block = new EthJSBlock({ const block: EthJSBlock = new EthJSBlock({
header: { header: {
timestamp: (new Date().getTime() / 1000 | 0), timestamp: (new Date().getTime() / 1000 | 0),
number: 0, number: 0,

@ -1,3 +1 @@
export { Provider } from './provider' export { Provider } from './provider'
// export { Provider }

@ -1,9 +1,7 @@
const version = require('../../package.json').version const version = require('../../package.json').version
import web3 from 'web3' import Web3 from 'web3'
export class Misc { export function methods () {
methods () {
return { return {
web3_clientVersion: this.web3_clientVersion.bind(this), web3_clientVersion: this.web3_clientVersion.bind(this),
eth_protocolVersion: this.eth_protocolVersion.bind(this), eth_protocolVersion: this.eth_protocolVersion.bind(this),
@ -16,47 +14,47 @@ export class Misc {
eth_compileLLL: this.eth_compileLLL.bind(this), eth_compileLLL: this.eth_compileLLL.bind(this),
eth_compileSerpent: this.eth_compileSerpent.bind(this) eth_compileSerpent: this.eth_compileSerpent.bind(this)
} }
} }
web3_clientVersion (payload, cb) { export function web3_clientVersion (payload, cb) {
cb(null, 'Remix Simulator/' + version) cb(null, 'Remix Simulator/' + version)
} }
eth_protocolVersion (payload, cb) { export function eth_protocolVersion (payload, cb) {
cb(null, '0x3f') cb(null, '0x3f')
} }
eth_syncing (payload, cb) { export function eth_syncing (payload, cb) {
cb(null, false) cb(null, false)
} }
eth_mining (payload, cb) { export function eth_mining (payload, cb) {
// TODO: should depend on the state // TODO: should depend on the state
cb(null, false) cb(null, false)
} }
eth_hashrate (payload, cb) { export function eth_hashrate (payload, cb) {
cb(null, '0x0') cb(null, '0x0')
} }
web3_sha3 (payload, cb) { export function web3_sha3 (payload, cb) {
const str = payload.params[0] const str: string = payload.params[0]
cb(null, web3.utils.sha3(str)) cb(null, Web3.utils.sha3(str))
} }
eth_getCompilers (payload, cb) { export function eth_getCompilers (payload, cb) {
cb(null, []) cb(null, [])
} }
eth_compileSolidity (payload, cb) { export function eth_compileSolidity (payload, cb) {
cb(null, 'unsupported') cb(null, 'unsupported')
} }
eth_compileLLL (payload, cb) { export function eth_compileLLL (payload, cb) {
cb(null, 'unsupported') cb(null, 'unsupported')
} }
eth_compileSerpent (payload, cb) { export function eth_compileSerpent (payload, cb) {
cb(null, 'unsupported') cb(null, 'unsupported')
}
} }

@ -1,24 +1,20 @@
export function methods (): Record<string, unknown> {
export class Net {
methods () {
return { return {
net_version: this.net_version, net_version: this.net_version,
net_listening: this.net_listening, net_listening: this.net_listening,
net_peerCount: this.net_peerCount net_peerCount: this.net_peerCount
} }
} }
net_version (payload, cb) { export function net_version (payload, cb): void {
// should be configured networkId // should be configured networkId
cb(null, 1337) cb(null, 1337)
} }
net_listening (payload, cb) { export function net_listening (payload, cb): void {
cb(null, true) cb(null, true)
} }
net_peerCount (payload, cb) { export function net_peerCount (payload, cb): void {
cb(null, 0) cb(null, 0)
}
} }

@ -2,14 +2,13 @@ import { Blocks } from './methods/blocks'
import { execution } from '@remix-project/remix-lib' import { execution } from '@remix-project/remix-lib'
const { executionContext } = execution const { executionContext } = execution
import { Logger } from './utils/logs' import { info } from './utils/logs'
const logger = new Logger()
import merge from 'merge' import merge from 'merge'
import { Accounts } from './methods/accounts' import { Accounts } from './methods/accounts'
import { Filters } from './methods/filters' import { Filters } from './methods/filters'
import { Misc } from './methods/misc' import { methods as miscMethods } from './methods/misc'
import { Net } from './methods/net' import { methods as netMethods } from './methods/net'
import { Transactions } from './methods/transactions' import { Transactions } from './methods/transactions'
import { Debug } from './methods/debug' import { Debug } from './methods/debug'
import { generateBlock } from './genesis' import { generateBlock } from './genesis'
@ -31,9 +30,9 @@ export class Provider {
this.methods = {} this.methods = {}
this.methods = merge(this.methods, this.Accounts.methods()) this.methods = merge(this.methods, this.Accounts.methods())
this.methods = merge(this.methods, (new Blocks(this.executionContext, options)).methods()) this.methods = merge(this.methods, (new Blocks(this.executionContext, options)).methods())
this.methods = merge(this.methods, (new Misc()).methods()) this.methods = merge(this.methods, miscMethods())
this.methods = merge(this.methods, (new Filters(this.executionContext)).methods()) this.methods = merge(this.methods, (new Filters(this.executionContext)).methods())
this.methods = merge(this.methods, (new Net()).methods()) this.methods = merge(this.methods, netMethods())
this.methods = merge(this.methods, this.Transactions.methods()) this.methods = merge(this.methods, this.Transactions.methods())
this.methods = merge(this.methods, (new Debug(this.executionContext)).methods()) this.methods = merge(this.methods, (new Debug(this.executionContext)).methods())
@ -51,13 +50,13 @@ export class Provider {
const method = this.methods[payload.method] const method = this.methods[payload.method]
if (this.options.logDetails) { if (this.options.logDetails) {
logger.info(payload) info(payload)
} }
if (method) { if (method) {
return method.call(method, payload, (err, result) => { return method.call(method, payload, (err, result) => {
if (this.options.logDetails) { if (this.options.logDetails) {
logger.info(err) info(err)
logger.info(result) info(result)
} }
if (err) { if (err) {
return callback(err) return callback(err)

@ -4,13 +4,11 @@ import gray from 'ansi-gray'
import timestamp from 'time-stamp' import timestamp from 'time-stamp'
import supportsColor from 'color-support' import supportsColor from 'color-support'
export class Logger { function hasFlag(flag) {
private hasFlag(flag) {
return ((typeof (process) !== 'undefined') && (process.argv.indexOf('--' + flag) !== -1)) return ((typeof (process) !== 'undefined') && (process.argv.indexOf('--' + flag) !== -1))
} }
private addColor(str) { function addColor(str) {
if (this.hasFlag('no-color')) { if (this.hasFlag('no-color')) {
return str return str
} }
@ -24,55 +22,54 @@ export class Logger {
} }
return str return str
} }
private stdout(arg) { function stdout(arg) {
if (typeof (process) === 'undefined' || !process.stdout) return if (typeof (process) === 'undefined' || !process.stdout) return
process.stdout.write(arg) process.stdout.write(arg)
} }
private stderr(arg) { function stderr(arg) {
if (typeof (process) === 'undefined' || process.stderr) return if (typeof (process) === 'undefined' || process.stderr) return
process.stderr.write(arg) process.stderr.write(arg)
} }
private getTimestamp() { function getTimestamp() {
const coloredTimestamp = this.addColor(timestamp('HH:mm:ss')) const coloredTimestamp = this.addColor(timestamp('HH:mm:ss'))
return '[' + coloredTimestamp + ']' return '[' + coloredTimestamp + ']'
} }
log(...args: any[]) { export function log(...args: any[]) {
const time = this.getTimestamp() const time = this.getTimestamp()
this.stdout(time + ' ') this.stdout(time + ' ')
console.log(args) console.log(args)
return this return this
} }
info(...args: any[]) { export function info(...args: any[]) {
const time = this.getTimestamp() const time = this.getTimestamp()
this.stdout(time + ' ') this.stdout(time + ' ')
console.info(args) console.info(args)
return this return this
} }
dir(...args: any[]) { export function dir(...args: any[]) {
const time = this.getTimestamp() const time = this.getTimestamp()
this.stdout(time + ' ') this.stdout(time + ' ')
console.dir(args) console.dir(args)
return this return this
} }
warn(...args: any[]) { export function warn(...args: any[]) {
const time = this.getTimestamp() const time = this.getTimestamp()
this.stderr(time + ' ') this.stderr(time + ' ')
console.warn(args) console.warn(args)
return this return this
} }
error(...args: any[]) { export function error(...args: any[]) {
const time = this.getTimestamp() const time = this.getTimestamp()
this.stderr(time + ' ') this.stderr(time + ' ')
console.error(args) console.error(args)
return this return this
}
} }

@ -12,17 +12,17 @@ describe('Accounts', () => {
describe('eth_getAccounts', () => { describe('eth_getAccounts', () => {
it('should get a list of accounts', async function () { it('should get a list of accounts', async function () {
const accounts = await web3.eth.getAccounts() const accounts: string[] = await web3.eth.getAccounts()
assert.notEqual(accounts.length, 0) assert.notEqual(accounts.length, 0)
}) })
}) })
describe('eth_getBalance', () => { describe('eth_getBalance', () => {
it('should get a account balance', async () => { it('should get a account balance', async () => {
const accounts = await web3.eth.getAccounts() const accounts: string[] = await web3.eth.getAccounts()
const balance0 = await web3.eth.getBalance(accounts[0]) const balance0: string = await web3.eth.getBalance(accounts[0])
const balance1 = await web3.eth.getBalance(accounts[1]) const balance1: string = await web3.eth.getBalance(accounts[1])
const balance2 = await web3.eth.getBalance(accounts[2]) const balance2: string = await web3.eth.getBalance(accounts[2])
assert.deepEqual(balance0, '100000000000000000000') assert.deepEqual(balance0, '100000000000000000000')
assert.deepEqual(balance1, '100000000000000000000') assert.deepEqual(balance1, '100000000000000000000')
@ -32,8 +32,8 @@ describe('Accounts', () => {
describe('eth_sign', () => { describe('eth_sign', () => {
it('should sign payloads', async () => { it('should sign payloads', async () => {
const accounts = await web3.eth.getAccounts() const accounts: string[] = await web3.eth.getAccounts()
const signature = await web3.eth.sign('Hello world', accounts[0]) const signature: string = await web3.eth.sign('Hello world', accounts[0])
assert.deepEqual(signature.length, 132) assert.deepEqual(signature.length, 132)
}) })

Loading…
Cancel
Save