diff --git a/src/provider.js b/src/provider.js index bd163fa2b9..ff2b85cf45 100644 --- a/src/provider.js +++ b/src/provider.js @@ -1,171 +1,38 @@ var Web3 = require('web3') var utils = require('ethereumjs-util') var RemixLib = require('remix-lib') -var TxExecution = RemixLib.execution.txExecution -var TxRunner = RemixLib.execution.txRunner var executionContext = RemixLib.execution.executionContext -function jsonRPCResponse(id, result) { - let json = {"id":id,"jsonrpc":"2.0","result":result}; - console.dir("== response"); - console.dir(json); - return json; -} - -// TODO: fix me; this is a temporary and very hackish thing just to get the getCode working for now -var deployedContracts = { -} - -function processTx(accounts, payload, isCall, callback) { - let api = { - logMessage: (msg) => { - //self._components.editorpanel.log({ type: 'log', value: msg }) - }, - logHtmlMessage: (msg) => { - //self._components.editorpanel.log({ type: 'html', value: msg }) - }, - //config: self._api.config, - config: { - getUnpersistedProperty: (key) => { - console.dir("== getUnpersistedProperty ==") - console.dir(key) - if (key === 'settings/always-use-vm') { - return true - } - return true - }, - get: () => { - return true - } - }, - detectNetwork: (cb) => { - //executionContext.detectNetwork(cb) - cb() - }, - personalMode: () => { - //return self._api.config.get('settings/personal-mode') - return false - } - } - - executionContext.init(api.config); - - //console.dir(accounts); - let txRunner = new TxRunner(accounts, api); - - if (payload.params[0].to) { - console.log("== processing transaction"); - // tx - - let from = payload.params[0].from; - let to = payload.params[0].to; - let data = payload.params[0].data; - let value = payload.params[0].value; - let gasLimit = payload.params[0].gasLimit || 800000; - - let callbacks = { - confirmationCb: (network, tx, gasEstimation, continueTxExecution, cancelCb) => { - console.dir("confirmationCb"); - continueTxExecution(null); - }, - gasEstimationForceSend: (error, continueTxExecution, cancelCb) => { - console.dir("gasEstimationForceSend"); - continueTxExecution(); - }, - promptCb: (okCb, cancelCb) => { - console.dir("promptCb"); - okCb(); - } - } +var processTx = require('./txProcess.js') - let finalCallback = function(err, result) { - console.dir(arguments) - console.log("called final callback") - - let toReturn; - if (isCall) { - console.dir(result.result.vm.return); - toReturn = "0x" + result.result.vm.return.toString('hex') - if (toReturn === '0x') { - toReturn = '0x0' - } - } else { - toReturn = result.transactionHash - } - console.dir("isCall is " + isCall); - - callback(null, jsonRPCResponse(payload.id, toReturn)) - } - - TxExecution.callFunction(from, to, data, value, gasLimit, null, txRunner, callbacks, finalCallback) - } else { - console.dir("== contract creation"); - // contract creation - let from = payload.params[0].from; - let data = payload.params[0].data; - let value = payload.params[0].value; - let gasLimit = payload.params[0].gasLimit || 800000; - - let callbacks = { - confirmationCb: (network, tx, gasEstimation, continueTxExecution, cancelCb) => { - console.dir("confirmationCb"); - continueTxExecution(null); - }, - gasEstimationForceSend: (error, continueTxExecution, cancelCb) => { - console.dir("gasEstimationForceSend"); - continueTxExecution(); - }, - promptCb: (okCb, cancelCb) => { - console.dir("promptCb"); - okCb(); - } - } - - let finalCallback = function(err, result) { - console.dir(arguments) - console.log("called final callback") - console.dir(result) - let contractAddress = ('0x' + result.result.createdAddress.toString('hex')) - //console.dir(contractAddress) - console.dir(result.transactionHash) - - // TODO: fix me; this is a temporary and very hackish thing just to get the receipts working for now - // deployedContracts[contractAddress] = contractAddress; - callback(null, jsonRPCResponse(payload.id, result.transactionHash)) - } - - TxExecution.createContract(from, data, value, gasLimit, txRunner, callbacks, finalCallback); - } +function jsonRPCResponse(id, result) { + return {"id":id,"jsonrpc":"2.0","result":result}; } 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]; - //_accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer(_accounts[this.accounts[0].address.toLowerCase()].privateKey); this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex'); -} -//Provider.prototype.send = function(payload) { -// console.log("=========== send"); -// console.dir(payload); -// //return this.manager.request(payload); -//} + // TODO: fix me; this is a temporary and very hackish thing just to get the getCode working for now + this.deployedContracts = {}; +} Provider.prototype.sendAsync = function(payload, callback) { - console.log("=========== sendAsync"); - console.dir(payload); + const self = this; + //console.log("=========== sendAsync"); + //console.dir(payload); if (payload.method === 'eth_accounts') { return callback(null, jsonRPCResponse(payload.id, this.accounts.map((x) => x.address))) } if (payload.method === 'eth_estimateGas') { - //return callback(null, jsonRPCResponseutils.bufferToInt(this.web3.utils.toHex(800000))) callback(null, jsonRPCResponse(payload.id, 800000)) } if (payload.method === 'eth_gasPrice') { - //return callback(null, jsonRPCResponseutils.bufferToInt(this.web3.utils.toHex(800000))) callback(null, jsonRPCResponse(payload.id, 1)) } if (payload.method === 'eth_sendTransaction') { @@ -173,8 +40,7 @@ Provider.prototype.sendAsync = function(payload, callback) { } if (payload.method === 'eth_getTransactionReceipt') { executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => { - //console.dir(receipt); - deployedContracts[receipt.contractAddress] = receipt.data + self.deployedContracts[receipt.contractAddress] = receipt.data var r = { "transactionHash": receipt.hash, @@ -195,12 +61,11 @@ Provider.prototype.sendAsync = function(payload, callback) { let address = payload.params[0]; let block = payload.params[1]; - callback(null, jsonRPCResponse(payload.id, deployedContracts[address])); + callback(null, jsonRPCResponse(payload.id, self.deployedContracts[address])); } if (payload.method === 'eth_call') { processTx(this.accounts, payload, true, callback) } - //return this.manager.request(payload, callback); } Provider.prototype.isConnected = function() { diff --git a/src/txProcess.js b/src/txProcess.js new file mode 100644 index 0000000000..89a421f277 --- /dev/null +++ b/src/txProcess.js @@ -0,0 +1,95 @@ +var RemixLib = require('remix-lib') +var TxExecution = RemixLib.execution.txExecution +var TxRunner = RemixLib.execution.txRunner +var executionContext = RemixLib.execution.executionContext + +function jsonRPCResponse(id, result) { + return {"id":id,"jsonrpc":"2.0","result":result}; +} + +function runTx(payload, from, to, data, value, gasLimit, txRunner, callbacks, isCall, callback) { + console.log('-- runTx'); + let finalCallback = function(err, result) { + let toReturn; + if (isCall) { + console.dir(result.result.vm.return); + console.dir(result.result.vm); + toReturn = "0x" + result.result.vm.return.toString('hex') + if (toReturn === '0x') { + toReturn = '0x0' + } + } else { + toReturn = result.transactionHash + } + + callback(null, jsonRPCResponse(payload.id, toReturn)) + } + + TxExecution.callFunction(from, to, data, value, gasLimit, null, txRunner, callbacks, finalCallback) +} + +function createContract(payload, from, data, value, gasLimit, txRunner, callbacks, callback) { + console.log('-- createContract'); + console.dir(arguments); + let finalCallback = function(err, result) { + let contractAddress = ('0x' + result.result.createdAddress.toString('hex')) + callback(null, jsonRPCResponse(payload.id, result.transactionHash)) + } + + TxExecution.createContract(from, data, value, gasLimit, txRunner, callbacks, finalCallback); +} + +function processTx(accounts, payload, isCall, callback) { + console.log('-- processTx'); + let api = { + logMessage: (msg) => { + }, + logHtmlMessage: (msg) => { + }, + //config: self._api.config, + config: { + getUnpersistedProperty: (key) => { + //if (key === 'settings/always-use-vm') { + // return true + //} + return true + }, + get: () => { + return true + } + }, + detectNetwork: (cb) => { + cb() + }, + personalMode: () => { + //return self._api.config.get('settings/personal-mode') + return false + } + } + + executionContext.init(api.config); + + let txRunner = new TxRunner(accounts, api); + let { from: from, to: to, data: data, value: value, gasLimit: gasLimit } = payload.params[0]; + gasLimit = gasLimit || 800000; + + let callbacks = { + confirmationCb: (network, tx, gasEstimation, continueTxExecution, cancelCb) => { + continueTxExecution(null); + }, + gasEstimationForceSend: (error, continueTxExecution, cancelCb) => { + continueTxExecution(); + }, + promptCb: (okCb, cancelCb) => { + okCb(); + } + } + + if (to) { + runTx(payload, from, to, data, value, gasLimit, txRunner, callbacks, isCall, callback); + } else { + createContract(payload, from, data, value, gasLimit, txRunner, callbacks, callback); + } +} + +module.exports = processTx;