forked from ethereumjs/eth-query
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
199 lines (189 loc) · 6.5 KB
/
Copy pathindex.js
File metadata and controls
199 lines (189 loc) · 6.5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const createRandomId = require('json-rpc-random-id')();
const extend = require('xtend');
module.exports = EthQuery;
/**
* Wrapping an Ethereum provider object, EthQuery provides some conveniences
* around making requests to an RPC endpoint:
*
* - Each of the RPC methods in the Ethereum spec may be requested not only
* via `sendAsync`, but also via its own instance method, whose API is suited
* for the RPC method.
* - When requesting an RPC method, `id` and `jsonrpc` do not need to be
* specified and are filled in with reasonable defaults.
* - The mechanics of `sendAsync` (or any of the RPC-method-specific instance
* methods) are simplified such that its callback will be called with an error
* argument not only if the callback on the provider's `sendAsync` method was
* called with an argument, but also if the `response` object has an `error`
* property.
*
* @param {any} provider - The provider object.
*/
function EthQuery(provider) {
const self = this;
self.currentProvider = provider;
}
//
// base queries
//
// default block
EthQuery.prototype.getBalance = generateFnWithDefaultBlockFor(
2,
'eth_getBalance',
);
EthQuery.prototype.getCode = generateFnWithDefaultBlockFor(2, 'eth_getCode');
EthQuery.prototype.getTransactionCount = generateFnWithDefaultBlockFor(
2,
'eth_getTransactionCount',
);
EthQuery.prototype.getStorageAt = generateFnWithDefaultBlockFor(
3,
'eth_getStorageAt',
);
EthQuery.prototype.call = generateFnWithDefaultBlockFor(2, 'eth_call');
// standard
EthQuery.prototype.protocolVersion = generateFnFor('eth_protocolVersion');
EthQuery.prototype.syncing = generateFnFor('eth_syncing');
EthQuery.prototype.coinbase = generateFnFor('eth_coinbase');
EthQuery.prototype.mining = generateFnFor('eth_mining');
EthQuery.prototype.hashrate = generateFnFor('eth_hashrate');
EthQuery.prototype.gasPrice = generateFnFor('eth_gasPrice');
EthQuery.prototype.accounts = generateFnFor('eth_accounts');
EthQuery.prototype.blockNumber = generateFnFor('eth_blockNumber');
EthQuery.prototype.getBlockTransactionCountByHash = generateFnFor(
'eth_getBlockTransactionCountByHash',
);
EthQuery.prototype.getBlockTransactionCountByNumber = generateFnFor(
'eth_getBlockTransactionCountByNumber',
);
EthQuery.prototype.getUncleCountByBlockHash = generateFnFor(
'eth_getUncleCountByBlockHash',
);
EthQuery.prototype.getUncleCountByBlockNumber = generateFnFor(
'eth_getUncleCountByBlockNumber',
);
EthQuery.prototype.sign = generateFnFor('eth_sign');
EthQuery.prototype.sendTransaction = generateFnFor('eth_sendTransaction');
EthQuery.prototype.sendRawTransaction = generateFnFor('eth_sendRawTransaction');
EthQuery.prototype.estimateGas = generateFnFor('eth_estimateGas');
EthQuery.prototype.getBlockByHash = generateFnFor('eth_getBlockByHash');
EthQuery.prototype.getBlockByNumber = generateFnFor('eth_getBlockByNumber');
EthQuery.prototype.getTransactionByHash = generateFnFor(
'eth_getTransactionByHash',
);
EthQuery.prototype.getTransactionByBlockHashAndIndex = generateFnFor(
'eth_getTransactionByBlockHashAndIndex',
);
EthQuery.prototype.getTransactionByBlockNumberAndIndex = generateFnFor(
'eth_getTransactionByBlockNumberAndIndex',
);
EthQuery.prototype.getTransactionReceipt = generateFnFor(
'eth_getTransactionReceipt',
);
EthQuery.prototype.getUncleByBlockHashAndIndex = generateFnFor(
'eth_getUncleByBlockHashAndIndex',
);
EthQuery.prototype.getUncleByBlockNumberAndIndex = generateFnFor(
'eth_getUncleByBlockNumberAndIndex',
);
EthQuery.prototype.getCompilers = generateFnFor('eth_getCompilers');
EthQuery.prototype.compileLLL = generateFnFor('eth_compileLLL');
EthQuery.prototype.compileSolidity = generateFnFor('eth_compileSolidity');
EthQuery.prototype.compileSerpent = generateFnFor('eth_compileSerpent');
EthQuery.prototype.newFilter = generateFnFor('eth_newFilter');
EthQuery.prototype.newBlockFilter = generateFnFor('eth_newBlockFilter');
EthQuery.prototype.newPendingTransactionFilter = generateFnFor(
'eth_newPendingTransactionFilter',
);
EthQuery.prototype.uninstallFilter = generateFnFor('eth_uninstallFilter');
EthQuery.prototype.getFilterChanges = generateFnFor('eth_getFilterChanges');
EthQuery.prototype.getFilterLogs = generateFnFor('eth_getFilterLogs');
EthQuery.prototype.getLogs = generateFnFor('eth_getLogs');
EthQuery.prototype.getWork = generateFnFor('eth_getWork');
EthQuery.prototype.submitWork = generateFnFor('eth_submitWork');
EthQuery.prototype.submitHashrate = generateFnFor('eth_submitHashrate');
// network level
EthQuery.prototype.sendAsync = function (opts, callback) {
const self = this;
self.currentProvider.sendAsync(
createPayload(opts),
function (error, response) {
let improvedError = error;
if (!error && response.error) {
improvedError = new Error(
`EthQuery - RPC Error - ${response.error.message}`,
);
}
if (improvedError) {
return callback(improvedError);
}
return callback(null, response.result);
},
);
};
// util
/**
* Generates an instance method designed to call an RPC method that takes no
* parameters. This instance method uses `sendAsync` internally to make the
* request to the network.
*
* @param {any} methodName - The RPC method.
* @returns {any} The method.
*/
function generateFnFor(methodName) {
return function (...args) {
const self = this;
const callback = args.pop();
self.sendAsync(
{
method: methodName,
params: args,
},
callback,
);
};
}
/**
* Generates an instance method designed to call an RPC methods that takes one
* or more parameters. This instance method uses `sendAsync` internally to make
* the request to the network.
*
* @param {any} argCount - The number of parameters that the RPC method is
* expected to take.
* @param {any} methodName - The RPC method.
* @returns {any} The method.
*/
function generateFnWithDefaultBlockFor(argCount, methodName) {
return function (...args) {
const self = this;
const callback = args.pop();
// set optional default block param
if (args.length < argCount) {
args.push('latest');
}
self.sendAsync(
{
method: methodName,
params: args,
},
callback,
);
};
}
/**
* Builds a complete request payload object from a partial version.
*
* @param {any} data - The partial request object.
* @returns {any} The complete request object.
*/
function createPayload(data) {
return extend(
{
// defaults
id: createRandomId(),
jsonrpc: '2.0',
params: [],
// user-specified
},
data,
);
}