From 2cb2fc5220513af52a0db5c381174327e6be444b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 20 Apr 2018 07:53:55 -0400 Subject: [PATCH] move transactions methods --- remix-simulator/package.json | 1 + remix-simulator/src/methods/transactions.js | 58 +++++++++++++++++++ .../src/{ => methods}/txProcess.js | 0 remix-simulator/src/provider.js | 58 ++++++------------- 4 files changed, 76 insertions(+), 41 deletions(-) create mode 100644 remix-simulator/src/methods/transactions.js rename remix-simulator/src/{ => methods}/txProcess.js (100%) diff --git a/remix-simulator/package.json b/remix-simulator/package.json index cc878af009..fee69cf275 100644 --- a/remix-simulator/package.json +++ b/remix-simulator/package.json @@ -17,6 +17,7 @@ "body-parser": "^1.18.2", "express": "^4.16.3", "fancy-log": "^1.3.2", + "merge": "^1.2.0", "remix-lib": "latest", "standard": "^10.0.3", "web3": "1.0.0-beta.27" diff --git a/remix-simulator/src/methods/transactions.js b/remix-simulator/src/methods/transactions.js new file mode 100644 index 0000000000..c707540482 --- /dev/null +++ b/remix-simulator/src/methods/transactions.js @@ -0,0 +1,58 @@ +var RemixLib = require('remix-lib') +var executionContext = RemixLib.execution.executionContext +var processTx = require('./txProcess.js') + +var Transactions = function(accounts) { + this.accounts = accounts; + // TODO: fix me; this is a temporary and very hackish thing just to get the getCode working for now + this.deployedContracts = {} +} + +Transactions.prototype.methods = function () { + return { + 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) + } +} + +Transactions.prototype.eth_sendTransaction = function(payload, cb) { + processTx(this.accounts, payload, false, cb); +} + +Transactions.prototype.eth_getTransactionReceipt = function(payload, cb) { + const self = this; + executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => { + if (error) { + return cb(error) + } + self.deployedContracts[receipt.contractAddress] = receipt.data + + var r = { + 'transactionHash': receipt.hash, + 'transactionIndex': '0x00', + 'blockHash': '0x766d18646a06cf74faeabf38597314f84a82c3851859d9da9d94fc8d037269e5', + 'blockNumber': '0x06', + 'gasUsed': '0x06345f', + 'cumulativeGasUsed': '0x06345f', + 'contractAddress': receipt.contractAddress, + 'logs': [], + 'status': 1 + } + + cb(null, r) + }) +} + +Transactions.prototype.eth_getCode = function(payload, cb) { + let address = payload.params[0] + + cb(null, this.deployedContracts[address] || '0x') +} + +Transactions.prototype.eth_call = function(payload, cb) { + processTx(this.accounts, payload, true, cb) +} + + module.exports = Transactions; diff --git a/remix-simulator/src/txProcess.js b/remix-simulator/src/methods/txProcess.js similarity index 100% rename from remix-simulator/src/txProcess.js rename to remix-simulator/src/methods/txProcess.js diff --git a/remix-simulator/src/provider.js b/remix-simulator/src/provider.js index c834278251..040d104462 100644 --- a/remix-simulator/src/provider.js +++ b/remix-simulator/src/provider.js @@ -1,9 +1,8 @@ var Web3 = require('web3') var RemixLib = require('remix-lib') -var executionContext = RemixLib.execution.executionContext const log = require('fancy-log') - -var processTx = require('./txProcess.js') +const Transactions = require('./methods/transactions.js') +const merge = require('merge') function jsonRPCResponse (id, result) { return {'id': id, 'jsonrpc': '2.0', 'result': result} @@ -17,8 +16,11 @@ var Provider = function () { 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') - // TODO: fix me; this is a temporary and very hackish thing just to get the getCode working for now - this.deployedContracts = {} + this.Transactions = new Transactions(this.accounts); + + this.methods = {} + this.methods = merge(this.methods, this.Transactions.methods()) + log.dir(this.methods) } Provider.prototype.sendAsync = function (payload, callback) { @@ -36,42 +38,6 @@ Provider.prototype.sendAsync = function (payload, callback) { if (payload.method === 'eth_gasPrice') { callback(null, jsonRPCResponse(payload.id, 1)) } - if (payload.method === 'eth_sendTransaction') { - processTx(this.accounts, payload, false, (_err, result) => { - callback(null, jsonRPCResponse(payload.id, result)) - }) - } - if (payload.method === 'eth_getTransactionReceipt') { - executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => { - if (error) { - return callback(error) - } - self.deployedContracts[receipt.contractAddress] = receipt.data - - var r = { - 'transactionHash': receipt.hash, - 'transactionIndex': '0x00', - 'blockHash': '0x766d18646a06cf74faeabf38597314f84a82c3851859d9da9d94fc8d037269e5', - 'blockNumber': '0x06', - 'gasUsed': '0x06345f', - 'cumulativeGasUsed': '0x06345f', - 'contractAddress': receipt.contractAddress, - 'logs': [], - 'status': 1 - } - - callback(null, jsonRPCResponse(payload.id, r)) - }) - } - if (payload.method === 'eth_getCode') { - let address = payload.params[0] - // let block = payload.params[1] - - callback(null, jsonRPCResponse(payload.id, self.deployedContracts[address] || '0x')) - } - if (payload.method === 'eth_call') { - processTx(this.accounts, payload, true, callback) - } if (payload.method === 'web3_clientVersion') { callback(null, jsonRPCResponse(payload.id, 'Remix Simulator/0.0.1')) } @@ -103,6 +69,16 @@ Provider.prototype.sendAsync = function (payload, callback) { } callback(null, jsonRPCResponse(payload.id, b)) } + let method = this.methods[payload.method] + if (method) { + return method.call(method, payload, (err, result) => { + if (err) { + return callback({error: err}) + } + callback(null, jsonRPCResponse(payload.id, result)) + }); + } + callback("unknown method " + payload.method); } Provider.prototype.isConnected = function () {