You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.5 KiB
84 lines
2.5 KiB
5 years ago
|
const RemixLib = require('@remix-project/remix-lib')
|
||
5 years ago
|
const executionContext = RemixLib.execution.executionContext
|
||
6 years ago
|
|
||
7 years ago
|
const log = require('./utils/logs.js')
|
||
7 years ago
|
const merge = require('merge')
|
||
|
|
||
7 years ago
|
const Accounts = require('./methods/accounts.js')
|
||
4 years ago
|
import { Blocks } from './methods/blocks'
|
||
6 years ago
|
const Filters = require('./methods/filters.js')
|
||
7 years ago
|
const Misc = require('./methods/misc.js')
|
||
7 years ago
|
const Net = require('./methods/net.js')
|
||
7 years ago
|
const Transactions = require('./methods/transactions.js')
|
||
4 years ago
|
const Debug = require('./methods/debug.js')
|
||
7 years ago
|
|
||
6 years ago
|
const generateBlock = require('./genesis.js')
|
||
|
|
||
4 years ago
|
export class Provider {
|
||
|
|
||
|
options
|
||
|
executionContext
|
||
|
Accounts
|
||
|
Transactions
|
||
|
methods
|
||
|
|
||
5 years ago
|
constructor(options = {}) {
|
||
|
this.options = options
|
||
5 years ago
|
// TODO: init executionContext here
|
||
|
this.executionContext = executionContext
|
||
|
this.Accounts = new Accounts(this.executionContext)
|
||
|
this.Transactions = new Transactions(this.executionContext)
|
||
7 years ago
|
|
||
5 years ago
|
this.methods = {}
|
||
|
this.methods = merge(this.methods, this.Accounts.methods())
|
||
|
this.methods = merge(this.methods, (new Blocks(this.executionContext, options)).methods())
|
||
|
this.methods = merge(this.methods, (new Misc()).methods())
|
||
|
this.methods = merge(this.methods, (new Filters(this.executionContext)).methods())
|
||
|
this.methods = merge(this.methods, (new Net()).methods())
|
||
|
this.methods = merge(this.methods, this.Transactions.methods())
|
||
4 years ago
|
this.methods = merge(this.methods, (new Debug(this.executionContext)).methods())
|
||
6 years ago
|
|
||
5 years ago
|
generateBlock(this.executionContext)
|
||
|
this.init()
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
async init () {
|
||
5 years ago
|
await this.Accounts.resetAccounts()
|
||
5 years ago
|
this.Transactions.init(this.Accounts.accounts)
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
sendAsync (payload, callback) {
|
||
4 years ago
|
// log.info('payload method is ', payload.method) // commented because, this floods the IDE console
|
||
7 years ago
|
|
||
5 years ago
|
const method = this.methods[payload.method]
|
||
|
if (this.options.logDetails) {
|
||
|
log.info(payload)
|
||
|
}
|
||
|
if (method) {
|
||
|
return method.call(method, payload, (err, result) => {
|
||
|
if (this.options.logDetails) {
|
||
|
log.info(err)
|
||
|
log.info(result)
|
||
|
}
|
||
|
if (err) {
|
||
|
return callback(err)
|
||
|
}
|
||
|
const response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result}
|
||
|
callback(null, response)
|
||
|
})
|
||
|
}
|
||
|
callback(new Error('unknown method ' + payload.method))
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
5 years ago
|
send (payload, callback) {
|
||
|
this.sendAsync(payload, callback || function () {})
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
isConnected () {
|
||
|
return true
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
on (type, cb) {
|
||
|
this.executionContext.logsManager.addListener(type, cb)
|
||
|
}
|
||
4 years ago
|
}
|