-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
99 lines (85 loc) · 3.55 KB
/
client.js
File metadata and controls
99 lines (85 loc) · 3.55 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
/**
* Quantova q_ JSON-RPC client.
*
* A small, dependency-light wrapper over the Quantova `q_` JSON-RPC namespace.
* Every method is a POST to the HTTP endpoint with a JSON body
* { jsonrpc, id, method, params }. Account addresses are Q-format strings;
* numeric results come back as QUANTITY hex you convert to a number.
*
* See docs/interacting.md for usage and the full method list.
*/
const DEFAULT_RPC = process.env.QUANTOVA_RPC_URL || "http://127.0.0.1:9933";
export class QuantovaClient {
constructor(rpcUrl = DEFAULT_RPC) {
this.rpcUrl = rpcUrl;
this._id = 0;
}
/** Low-level JSON-RPC call. `params` is a JSON array in the documented order. */
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;
}
// ---- convenience helpers -------------------------------------------------
/** Height of the most recent block, as a number. */
async blockNumber() {
return parseInt(await this.call("q_blockNumber"), 16);
}
/** Chain id (used in signing to prevent cross-chain replay). */
async chainId() {
return await this.call("q_chainId");
}
/**
* Balance of a Q-format address, in QTOV (base units / 10^18).
* `block` is a block-number QUANTITY hex; defaults to the latest height.
* Named tags like "latest" are NOT implemented — we resolve a height first.
*/
async getBalanceQTOV(address, block) {
const ref = block ?? "0x" + (await this.blockNumber()).toString(16);
const raw = await this.call("q_getBalance", [address, ref]);
return Number(BigInt(raw)) / 1e18;
}
/** Account nonce — set this on the next transaction you build. */
async getNonce(address, block) {
const ref = block ?? "0x" + (await this.blockNumber()).toString(16);
return parseInt(await this.call("q_getTransactionCount", [address, ref]), 16);
}
/** Read-only contract call (no QGAS, no transaction). */
async callContract(callObject, block) {
const ref = block ?? "0x" + (await this.blockNumber()).toString(16);
return await this.call("q_call", [callObject, ref]);
}
/** Estimate QGAS for a call or deployment. */
async estimateGas(callObject) {
return parseInt(await this.call("q_estimateGas", [callObject]), 16);
}
/** Broadcast an already-signed, serialized transaction. Returns the tx hash. */
async sendRawTransaction(signedTxHex) {
return await this.call("q_sendRawTransaction", [signedTxHex]);
}
/** Fetch a transaction receipt (status, QGAS used, logs, contract address). */
async getTransactionReceipt(txHash) {
return await this.call("q_getTransactionReceipt", [txHash]);
}
/**
* Poll until a transaction has a receipt, then return it.
* Note: a receipt means included — confirm finality (~3s) before treating
* value as settled. See docs/interacting.md.
*/
async waitForReceipt(txHash, { intervalMs = 1000, timeoutMs = 60000 } = {}) {
const start = Date.now();
for (;;) {
const receipt = await this.getTransactionReceipt(txHash).catch(() => null);
if (receipt) return receipt;
if (Date.now() - start > timeoutMs) throw new Error("Timed out waiting for receipt");
await new Promise((r) => setTimeout(r, intervalMs));
}
}
}
export default QuantovaClient;