-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.js
53 lines (45 loc) · 1.39 KB
/
rpc.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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
"use strict";
class RPCServer {
constructor(rpcUrl) {
this.rpcUrl = rpcUrl;
}
getNonce(address) {
return this.call({jsonrpc: "2.0", method: "eth_getTransactionCount", params: [address, "pending"]})
.then(json => json.result);
}
getBlockNumber() {
return this.call({jsonrpc: "2.0", method: "eth_blockNumber", params: []})
.then(json => json.result);
}
getBlockByNumber(number) {
return this.call({jsonrpc: "2.0", method: "eth_getBlockByNumber", params: [number, false]})
.then(json => json.result);
}
getFilterLogs(filter) {
return this.call({jsonrpc: "2.0", method: "eth_getLogs", params: [filter]});
}
call(payload) {
return fetch(this.rpcUrl, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
})
.then(response => response.json())
.then(json => {
if (!json.result && json.error) {
console.log("<== rpc error", json.error);
throw new Error(json.error.message || "rpc error");
}
return json;
});
}
}
module.exports = RPCServer;