-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc-client.js
More file actions
55 lines (46 loc) · 1.76 KB
/
Copy pathrpc-client.js
File metadata and controls
55 lines (46 loc) · 1.76 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
/**
* Minimal Quantova q_ JSON-RPC client for the consensus test labs.
*
* Every call is a POST to the HTTP endpoint with body
* { jsonrpc, id, method, params }. Addresses are Q-format strings; numeric
* results are QUANTITY hex. Block references take a block-number QUANTITY —
* "latest" is not implemented.
*/
export class QuantovaClient {
constructor(rpcUrl = process.env.QUANTOVA_RPC_URL) {
if (!rpcUrl) throw new Error("No RPC URL. Set QUANTOVA_RPC_URL (e.g. https://testnet.quantova.io).");
this.rpcUrl = rpcUrl;
this._id = 0;
}
async call(method, params = []) {
const res = await fetch(this.rpcUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ jsonrpc: "2.0", id: ++this._id, method, params }),
});
if (!res.ok) throw new Error(`RPC HTTP ${res.status}`);
const { result, error } = await res.json();
if (error) throw new Error(`RPC error ${error.code}: ${error.message}`);
return result;
}
async blockNumber() {
return parseInt(await this.call("q_blockNumber"), 16);
}
async latestHex() {
return "0x" + (await this.blockNumber()).toString(16);
}
async isSynced() {
return (await this.call("q_syncing")) === false;
}
async chainId() {
return await this.call("q_chainId");
}
/** A block by height, with full transactions when `fullTx` is true. */
async blockByNumber(height, fullTx = true) {
const hex = typeof height === "number" ? "0x" + height.toString(16) : height;
return await this.call("q_getBlockByNumber", [hex, fullTx]);
}
}
/** True if QUANTOVA_RPC_URL is set, so labs can skip live checks cleanly when it isn't. */
export const HAS_ENDPOINT = Boolean(process.env.QUANTOVA_RPC_URL);
export default QuantovaClient;