From 5dfd594abd59dc333b036a91a30dd33e2da81512 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 20 Apr 2018 08:53:33 -0400 Subject: [PATCH] move other methods to their own modules --- remix-simulator/src/methods/accounts.js | 22 +++++++++++++++ remix-simulator/src/methods/blocks.js | 4 +++ remix-simulator/src/methods/misc.js | 16 +++++++++++ remix-simulator/src/methods/transactions.js | 7 ++++- remix-simulator/src/provider.js | 30 ++++++--------------- 5 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 remix-simulator/src/methods/accounts.js create mode 100644 remix-simulator/src/methods/misc.js diff --git a/remix-simulator/src/methods/accounts.js b/remix-simulator/src/methods/accounts.js new file mode 100644 index 0000000000..406069e2e2 --- /dev/null +++ b/remix-simulator/src/methods/accounts.js @@ -0,0 +1,22 @@ +var Web3 = require('web3') + +var Accounts = function(web3) { + this.web3 = new Web3() + // TODO: make it random and/or use remix-libs + this.accounts = [this.web3.eth.accounts.create(['abcd'])] + + this.accounts[this.accounts[0].address.toLowerCase()] = this.accounts[0] + this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex') +} + +Accounts.prototype.methods = function () { + return { + eth_accounts: this.eth_accounts.bind(this) + } +} + +Accounts.prototype.eth_accounts = function(payload, cb) { + return cb(null, this.accounts.map((x) => x.address)) +} + +module.exports = Accounts; diff --git a/remix-simulator/src/methods/blocks.js b/remix-simulator/src/methods/blocks.js index cb2fe1118f..a2687bbb1c 100644 --- a/remix-simulator/src/methods/blocks.js +++ b/remix-simulator/src/methods/blocks.js @@ -34,4 +34,8 @@ Blocks.prototype.eth_getBlockByNumber = function (payload, cb) { cb(null, b) } +Blocks.prototype.eth_gasPrice = function (payload, cb) { + cb(null, 1) +} + module.exports = Blocks; diff --git a/remix-simulator/src/methods/misc.js b/remix-simulator/src/methods/misc.js new file mode 100644 index 0000000000..7976253773 --- /dev/null +++ b/remix-simulator/src/methods/misc.js @@ -0,0 +1,16 @@ + +var Misc = function() { +} + +Misc.prototype.methods = function () { + return { + web3_clientVersion: 'web3_clientVersion' + } +} + +Misc.prototype.web3_clientVersion = function (payload, cb) { + cb(null, 'Remix Simulator/0.0.1') +} + + +module.exports = Misc; diff --git a/remix-simulator/src/methods/transactions.js b/remix-simulator/src/methods/transactions.js index c707540482..1095f23a2b 100644 --- a/remix-simulator/src/methods/transactions.js +++ b/remix-simulator/src/methods/transactions.js @@ -13,7 +13,8 @@ Transactions.prototype.methods = function () { eth_sendTransaction: this.eth_sendTransaction.bind(this), eth_getTransactionReceipt: this.eth_getTransactionReceipt.bind(this), eth_getCode: this.eth_getCode.bind(this), - eth_call: this.eth_call.bind(this) + eth_call: this.eth_call.bind(this), + eth_estimateGas: this.eth_estimateGas.bind(this) } } @@ -45,6 +46,10 @@ Transactions.prototype.eth_getTransactionReceipt = function(payload, cb) { }) } +Transactions.prototype.eth_estimateGas = function(payload, cb) { + cb(null, 3000000) +} + Transactions.prototype.eth_getCode = function(payload, cb) { let address = payload.params[0] diff --git a/remix-simulator/src/provider.js b/remix-simulator/src/provider.js index 2713c823a8..47b3ba70b2 100644 --- a/remix-simulator/src/provider.js +++ b/remix-simulator/src/provider.js @@ -1,6 +1,8 @@ -var Web3 = require('web3') var RemixLib = require('remix-lib') const log = require('fancy-log') +const Accounts = require('./methods/accounts.js') +const Blocks = require('./methods/blocks.js') +const Misc = require('./methods/misc.js') const Transactions = require('./methods/transactions.js') const Whisper = require('./methods/whisper.js') const merge = require('merge') @@ -10,17 +12,14 @@ function jsonRPCResponse (id, result) { } var Provider = function () { - this.web3 = new Web3() - // TODO: make it random - this.accounts = [this.web3.eth.accounts.create(['abcd'])] - - this.accounts[this.accounts[0].address.toLowerCase()] = this.accounts[0] - this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex') + this.Accounts = new Accounts(); this.methods = {} - this.methods = merge(this.methods, (new Transactions(this.accounts)).methods()) - this.methods = merge(this.methods, (new Whisper()).methods()) + this.methods = merge(this.methods, this.Accounts.methods()) this.methods = merge(this.methods, (new Blocks()).methods()) + this.methods = merge(this.methods, (new Misc()).methods()) + this.methods = merge(this.methods, (new Transactions(this.Accounts.accounts)).methods()) + this.methods = merge(this.methods, (new Whisper()).methods()) log.dir(this.methods) } @@ -29,19 +28,6 @@ Provider.prototype.sendAsync = function (payload, callback) { log.dir('payload method is ') log.dir(payload.method) - if (payload.method === 'eth_accounts') { - log.dir('eth_accounts') - return callback(null, jsonRPCResponse(payload.id, this.accounts.map((x) => x.address))) - } - if (payload.method === 'eth_estimateGas') { - callback(null, jsonRPCResponse(payload.id, 3000000)) - } - if (payload.method === 'eth_gasPrice') { - callback(null, jsonRPCResponse(payload.id, 1)) - } - if (payload.method === 'web3_clientVersion') { - callback(null, jsonRPCResponse(payload.id, 'Remix Simulator/0.0.1')) - } let method = this.methods[payload.method] if (method) { return method.call(method, payload, (err, result) => {