move transactions methods

pull/3094/head
Iuri Matias 7 years ago
parent 3951acdd99
commit 2cb2fc5220
  1. 1
      remix-simulator/package.json
  2. 58
      remix-simulator/src/methods/transactions.js
  3. 0
      remix-simulator/src/methods/txProcess.js
  4. 58
      remix-simulator/src/provider.js

@ -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"

@ -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;

@ -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 () {

Loading…
Cancel
Save