UNPKG

2.33 kBJavaScriptView Raw
1const RemixLib = require('remix-lib')
2const executionContext = RemixLib.execution.executionContext
3
4const log = require('./utils/logs.js')
5const merge = require('merge')
6
7const Accounts = require('./methods/accounts.js')
8const Blocks = require('./methods/blocks.js')
9const Filters = require('./methods/filters.js')
10const Misc = require('./methods/misc.js')
11const Net = require('./methods/net.js')
12const Transactions = require('./methods/transactions.js')
13
14const generateBlock = require('./genesis.js')
15
16var Provider = function (options) {
17 this.options = options || {}
18 // TODO: init executionContext here
19 this.executionContext = executionContext
20 this.Accounts = new Accounts(this.executionContext)
21 this.Transactions = new Transactions(this.executionContext)
22
23 this.methods = {}
24 this.methods = merge(this.methods, this.Accounts.methods())
25 this.methods = merge(this.methods, (new Blocks(this.executionContext, options)).methods())
26 this.methods = merge(this.methods, (new Misc()).methods())
27 this.methods = merge(this.methods, (new Filters(this.executionContext)).methods())
28 this.methods = merge(this.methods, (new Net()).methods())
29 this.methods = merge(this.methods, this.Transactions.methods())
30
31 generateBlock(this.executionContext)
32 this.init()
33}
34
35Provider.prototype.init = async function () {
36 await this.Accounts.init()
37 this.Transactions.init(this.Accounts.accounts)
38}
39
40Provider.prototype.sendAsync = function (payload, callback) {
41 log.info('payload method is ', payload.method)
42
43 const method = this.methods[payload.method]
44 if (this.options.logDetails) {
45 log.info(payload)
46 }
47 if (method) {
48 return method.call(method, payload, (err, result) => {
49 if (this.options.logDetails) {
50 log.info(err)
51 log.info(result)
52 }
53 if (err) {
54 return callback(err)
55 }
56 const response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result}
57 callback(null, response)
58 })
59 }
60 callback(new Error('unknown method ' + payload.method))
61}
62
63Provider.prototype.send = function (payload, callback) {
64 this.sendAsync(payload, callback || function () {})
65}
66
67Provider.prototype.isConnected = function () {
68 return true
69}
70
71Provider.prototype.on = function (type, cb) {
72 this.executionContext.logsManager.addListener(type, cb)
73}
74
75module.exports = Provider