move other methods to their own modules

pull/7/head
Iuri Matias 7 years ago
parent 1c9a1b7e3d
commit 5dfd594abd
  1. 22
      remix-simulator/src/methods/accounts.js
  2. 4
      remix-simulator/src/methods/blocks.js
  3. 16
      remix-simulator/src/methods/misc.js
  4. 7
      remix-simulator/src/methods/transactions.js
  5. 30
      remix-simulator/src/provider.js

@ -0,0 +1,22 @@
var Web3 = require('web3')
var Accounts = function(web3) {
this.web3 = new Web3()
// TODO: make it random and/or use remix-libs
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()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex')
}
Accounts.prototype.methods = function () {
return {
eth_accounts: this.eth_accounts.bind(this)
}
}
Accounts.prototype.eth_accounts = function(payload, cb) {
return cb(null, this.accounts.map((x) => x.address))
}
module.exports = Accounts;

@ -34,4 +34,8 @@ Blocks.prototype.eth_getBlockByNumber = function (payload, cb) {
cb(null, b)
}
Blocks.prototype.eth_gasPrice = function (payload, cb) {
cb(null, 1)
}
module.exports = Blocks;

@ -0,0 +1,16 @@
var Misc = function() {
}
Misc.prototype.methods = function () {
return {
web3_clientVersion: 'web3_clientVersion'
}
}
Misc.prototype.web3_clientVersion = function (payload, cb) {
cb(null, 'Remix Simulator/0.0.1')
}
module.exports = Misc;

@ -13,7 +13,8 @@ Transactions.prototype.methods = function () {
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)
eth_call: this.eth_call.bind(this),
eth_estimateGas: this.eth_estimateGas.bind(this)
}
}
@ -45,6 +46,10 @@ Transactions.prototype.eth_getTransactionReceipt = function(payload, cb) {
})
}
Transactions.prototype.eth_estimateGas = function(payload, cb) {
cb(null, 3000000)
}
Transactions.prototype.eth_getCode = function(payload, cb) {
let address = payload.params[0]

@ -1,6 +1,8 @@
var Web3 = require('web3')
var RemixLib = require('remix-lib')
const log = require('fancy-log')
const Accounts = require('./methods/accounts.js')
const Blocks = require('./methods/blocks.js')
const Misc = require('./methods/misc.js')
const Transactions = require('./methods/transactions.js')
const Whisper = require('./methods/whisper.js')
const merge = require('merge')
@ -10,17 +12,14 @@ function jsonRPCResponse (id, result) {
}
var 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]
this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex')
this.Accounts = new Accounts();
this.methods = {}
this.methods = merge(this.methods, (new Transactions(this.accounts)).methods())
this.methods = merge(this.methods, (new Whisper()).methods())
this.methods = merge(this.methods, this.Accounts.methods())
this.methods = merge(this.methods, (new Blocks()).methods())
this.methods = merge(this.methods, (new Misc()).methods())
this.methods = merge(this.methods, (new Transactions(this.Accounts.accounts)).methods())
this.methods = merge(this.methods, (new Whisper()).methods())
log.dir(this.methods)
}
@ -29,19 +28,6 @@ Provider.prototype.sendAsync = function (payload, callback) {
log.dir('payload method is ')
log.dir(payload.method)
if (payload.method === 'eth_accounts') {
log.dir('eth_accounts')
return callback(null, jsonRPCResponse(payload.id, this.accounts.map((x) => x.address)))
}
if (payload.method === 'eth_estimateGas') {
callback(null, jsonRPCResponse(payload.id, 3000000))
}
if (payload.method === 'eth_gasPrice') {
callback(null, jsonRPCResponse(payload.id, 1))
}
if (payload.method === 'web3_clientVersion') {
callback(null, jsonRPCResponse(payload.id, 'Remix Simulator/0.0.1'))
}
let method = this.methods[payload.method]
if (method) {
return method.call(method, payload, (err, result) => {

Loading…
Cancel
Save