Commit 807ca5aa authored by Iuri Matias's avatar Iuri Matias

move transactions methods

parent 13b41702
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
"body-parser": "^1.18.2", "body-parser": "^1.18.2",
"express": "^4.16.3", "express": "^4.16.3",
"fancy-log": "^1.3.2", "fancy-log": "^1.3.2",
"merge": "^1.2.0",
"remix-lib": "latest", "remix-lib": "latest",
"standard": "^10.0.3", "standard": "^10.0.3",
"web3": "1.0.0-beta.27" "web3": "1.0.0-beta.27"
......
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;
var Web3 = require('web3') var Web3 = require('web3')
var RemixLib = require('remix-lib') var RemixLib = require('remix-lib')
var executionContext = RemixLib.execution.executionContext
const log = require('fancy-log') const log = require('fancy-log')
const Transactions = require('./methods/transactions.js')
var processTx = require('./txProcess.js') const merge = require('merge')
function jsonRPCResponse (id, result) { function jsonRPCResponse (id, result) {
return {'id': id, 'jsonrpc': '2.0', 'result': result} return {'id': id, 'jsonrpc': '2.0', 'result': result}
...@@ -17,8 +16,11 @@ var Provider = function () { ...@@ -17,8 +16,11 @@ var Provider = function () {
this.accounts[this.accounts[0].address.toLowerCase()] = this.accounts[0] 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[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.Transactions = new Transactions(this.accounts);
this.deployedContracts = {}
this.methods = {}
this.methods = merge(this.methods, this.Transactions.methods())
log.dir(this.methods)
} }
Provider.prototype.sendAsync = function (payload, callback) { Provider.prototype.sendAsync = function (payload, callback) {
...@@ -36,42 +38,6 @@ Provider.prototype.sendAsync = function (payload, callback) { ...@@ -36,42 +38,6 @@ Provider.prototype.sendAsync = function (payload, callback) {
if (payload.method === 'eth_gasPrice') { if (payload.method === 'eth_gasPrice') {
callback(null, jsonRPCResponse(payload.id, 1)) 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') { if (payload.method === 'web3_clientVersion') {
callback(null, jsonRPCResponse(payload.id, 'Remix Simulator/0.0.1')) callback(null, jsonRPCResponse(payload.id, 'Remix Simulator/0.0.1'))
} }
...@@ -103,6 +69,16 @@ Provider.prototype.sendAsync = function (payload, callback) { ...@@ -103,6 +69,16 @@ Provider.prototype.sendAsync = function (payload, callback) {
} }
callback(null, jsonRPCResponse(payload.id, b)) 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 () { Provider.prototype.isConnected = function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment