refactor & clean up code a bit

pull/7/head
Iuri Matias 7 years ago
parent f5196f9ea7
commit 09f820310e
  1. 159
      src/provider.js
  2. 95
      src/txProcess.js

@ -1,171 +1,38 @@
var Web3 = require('web3') var Web3 = require('web3')
var utils = require('ethereumjs-util') var utils = require('ethereumjs-util')
var RemixLib = require('remix-lib') var RemixLib = require('remix-lib')
var TxExecution = RemixLib.execution.txExecution
var TxRunner = RemixLib.execution.txRunner
var executionContext = RemixLib.execution.executionContext var executionContext = RemixLib.execution.executionContext
function jsonRPCResponse(id, result) { var processTx = require('./txProcess.js')
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();
}
}
let finalCallback = function(err, result) { function jsonRPCResponse(id, result) {
console.dir(arguments) return {"id":id,"jsonrpc":"2.0","result":result};
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);
}
} }
Provider = function() { Provider = function() {
this.web3 = new Web3(); this.web3 = new Web3();
// TODO: make it random
this.accounts = [this.web3.eth.accounts.create(["abcd"])] 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()] = 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'); 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) { // TODO: fix me; this is a temporary and very hackish thing just to get the getCode working for now
// console.log("=========== send"); this.deployedContracts = {};
// console.dir(payload); }
// //return this.manager.request(payload);
//}
Provider.prototype.sendAsync = function(payload, callback) { Provider.prototype.sendAsync = function(payload, callback) {
console.log("=========== sendAsync"); const self = this;
console.dir(payload); //console.log("=========== sendAsync");
//console.dir(payload);
if (payload.method === 'eth_accounts') { if (payload.method === 'eth_accounts') {
return callback(null, jsonRPCResponse(payload.id, this.accounts.map((x) => x.address))) return callback(null, jsonRPCResponse(payload.id, this.accounts.map((x) => x.address)))
} }
if (payload.method === 'eth_estimateGas') { if (payload.method === 'eth_estimateGas') {
//return callback(null, jsonRPCResponseutils.bufferToInt(this.web3.utils.toHex(800000)))
callback(null, jsonRPCResponse(payload.id, 800000)) callback(null, jsonRPCResponse(payload.id, 800000))
} }
if (payload.method === 'eth_gasPrice') { if (payload.method === 'eth_gasPrice') {
//return callback(null, jsonRPCResponseutils.bufferToInt(this.web3.utils.toHex(800000)))
callback(null, jsonRPCResponse(payload.id, 1)) callback(null, jsonRPCResponse(payload.id, 1))
} }
if (payload.method === 'eth_sendTransaction') { if (payload.method === 'eth_sendTransaction') {
@ -173,8 +40,7 @@ Provider.prototype.sendAsync = function(payload, callback) {
} }
if (payload.method === 'eth_getTransactionReceipt') { if (payload.method === 'eth_getTransactionReceipt') {
executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => { executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => {
//console.dir(receipt); self.deployedContracts[receipt.contractAddress] = receipt.data
deployedContracts[receipt.contractAddress] = receipt.data
var r = { var r = {
"transactionHash": receipt.hash, "transactionHash": receipt.hash,
@ -195,12 +61,11 @@ Provider.prototype.sendAsync = function(payload, callback) {
let address = payload.params[0]; let address = payload.params[0];
let block = payload.params[1]; 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') { if (payload.method === 'eth_call') {
processTx(this.accounts, payload, true, callback) processTx(this.accounts, payload, true, callback)
} }
//return this.manager.request(payload, callback);
} }
Provider.prototype.isConnected = function() { Provider.prototype.isConnected = function() {

@ -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;
Loading…
Cancel
Save