1 | const RemixLib = require('remix-lib')
|
2 | const executionContext = RemixLib.execution.executionContext
|
3 |
|
4 | const log = require('./utils/logs.js')
|
5 | const merge = require('merge')
|
6 |
|
7 | const Accounts = require('./methods/accounts.js')
|
8 | const Blocks = require('./methods/blocks.js')
|
9 | const Filters = require('./methods/filters.js')
|
10 | const Misc = require('./methods/misc.js')
|
11 | const Net = require('./methods/net.js')
|
12 | const Transactions = require('./methods/transactions.js')
|
13 |
|
14 | const generateBlock = require('./genesis.js')
|
15 |
|
16 | var Provider = function (options) {
|
17 | this.options = options || {}
|
18 |
|
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 |
|
35 | Provider.prototype.init = async function () {
|
36 | await this.Accounts.init()
|
37 | this.Transactions.init(this.Accounts.accounts)
|
38 | }
|
39 |
|
40 | Provider.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 |
|
63 | Provider.prototype.send = function (payload, callback) {
|
64 | this.sendAsync(payload, callback || function () {})
|
65 | }
|
66 |
|
67 | Provider.prototype.isConnected = function () {
|
68 | return true
|
69 | }
|
70 |
|
71 | Provider.prototype.on = function (type, cb) {
|
72 | this.executionContext.logsManager.addListener(type, cb)
|
73 | }
|
74 |
|
75 | module.exports = Provider
|