forked from ethjs/ethjs-rpc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
85 lines (73 loc) · 2.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const promiseToCallback = require('promise-to-callback');
module.exports = EthRPC;
/**
* Constructs the EthRPC instance
*
* @method EthRPC
* @param {Object} cprovider the eth rpc provider web3 standard..
* @param {Object} options the options, if any
* @returns {Object} ethrpc instance
*/
function EthRPC(cprovider, options) {
const self = this;
const optionsObject = options || {};
if (!(this instanceof EthRPC)) { throw new Error('[ethjs-rpc] the EthRPC object requires the "new" flag in order to function normally (i.e. `const eth = new EthRPC(provider);`).'); }
self.options = Object.assign({
jsonSpace: optionsObject.jsonSpace || 0,
max: optionsObject.max || 9999999999999,
});
self.idCounter = Math.floor(Math.random() * self.options.max);
self.setProvider = (provider) => {
if (typeof provider !== 'object') { throw new Error(`[ethjs-rpc] the EthRPC object requires that the first input 'provider' must be an object, got '${typeof provider}' (i.e. 'const eth = new EthRPC(provider);')`); }
self.currentProvider = provider;
};
self.setProvider(cprovider);
}
/**
* The main send async method
*
* @method sendAsync
* @param {Object} payload the rpc payload object
* @param {Function} cb the async standard callback
* @callback {Object|Array|Boolean|String} vary result instance output
*/
EthRPC.prototype.sendAsync = function sendAsync(payload, callback) {
const self = this;
self.idCounter = self.idCounter % self.options.max;
const parsedPayload = createPayload(payload, self.idCounter++);
const promise = new Promise((resolve, reject) => {
self.currentProvider.sendAsync(parsedPayload, (err, response) => {
const responseObject = response || {};
if (err || responseObject.error) {
const payloadErrorMessage = `[ethjs-rpc] ${(responseObject.error && 'rpc' || '')} error with payload ${JSON.stringify(parsedPayload, null, self.options.jsonSpace)} ${err ? String(err) : (JSON.stringify(responseObject.error, null, self.options.jsonSpace))}`;
const payloadError = new Error(payloadErrorMessage);
payloadError.value = (err || responseObject.error);
reject(payloadError);
return;
}
resolve(responseObject.result);
return;
});
});
if (callback) {
// connect promise resolve handlers to callback
return promiseToCallback(promise)(callback);
}
// only return promise if no callback specified
return promise;
};
/**
* A simple create payload method
*
* @method createPayload
* @param {Object} data the rpc payload data
* @param {String} id the rpc data payload ID
* @returns {Object} payload the completed payload object
*/
function createPayload(data, id) {
return Object.assign({}, {
id,
jsonrpc: '2.0',
params: [],
}, data);
}