@inversealtruism/csd-client 0.1.2 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +73 -17
- package/dist/index.d.cts +66 -16
- package/dist/index.d.ts +66 -16
- package/dist/index.js +73 -17
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -29,26 +29,46 @@ var CsdClient = class {
|
|
|
29
29
|
base;
|
|
30
30
|
f;
|
|
31
31
|
timeoutMs;
|
|
32
|
+
retries;
|
|
32
33
|
constructor(opts) {
|
|
33
34
|
this.base = opts.baseUrl.replace(/\/+$/, "");
|
|
34
35
|
this.f = opts.fetch ?? globalThis.fetch;
|
|
35
36
|
this.timeoutMs = opts.timeoutMs ?? 1e4;
|
|
37
|
+
this.retries = Math.max(0, opts.retries ?? 0);
|
|
36
38
|
if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
|
|
37
39
|
}
|
|
38
|
-
async
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
40
|
+
async req(path, init) {
|
|
41
|
+
let lastErr;
|
|
42
|
+
for (let attempt = 0; ; attempt++) {
|
|
43
|
+
try {
|
|
44
|
+
const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
|
|
45
|
+
if (r.status >= 500 && attempt < this.retries) {
|
|
46
|
+
lastErr = new Error(`HTTP ${r.status}`);
|
|
47
|
+
} else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
|
|
48
|
+
else return await r.json();
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (attempt >= this.retries) throw e;
|
|
51
|
+
lastErr = e;
|
|
52
|
+
}
|
|
53
|
+
const cap = Math.min(5e3, 250 * 2 ** attempt);
|
|
54
|
+
await new Promise((res) => setTimeout(res, Math.floor(Math.random() * cap)));
|
|
55
|
+
void lastErr;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
get(path) {
|
|
59
|
+
return this.req(path);
|
|
60
|
+
}
|
|
61
|
+
post(path, body) {
|
|
62
|
+
return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
63
|
+
}
|
|
64
|
+
// The node returns application errors as `{ok:false, err}` with HTTP **200**, so a bare `get()`
|
|
65
|
+
// can't see them. For endpoints whose `{ok:false}` result is useless to the caller (a missing
|
|
66
|
+
// block), surface it as a thrown error — otherwise a beyond-tip/not-found block flows downstream
|
|
67
|
+
// as a malformed object and crashes opaquely (e.g. the light client reading `header.prev`).
|
|
68
|
+
async getOk(path) {
|
|
69
|
+
const j = await this.get(path);
|
|
70
|
+
if (j && j.ok === false) throw new Error(`GET ${path} \u2192 node error: ${j.err ?? "ok:false"}`);
|
|
71
|
+
return j;
|
|
52
72
|
}
|
|
53
73
|
tip() {
|
|
54
74
|
return this.get("/tip");
|
|
@@ -57,10 +77,10 @@ var CsdClient = class {
|
|
|
57
77
|
return this.get("/health");
|
|
58
78
|
}
|
|
59
79
|
blockByHeight(h) {
|
|
60
|
-
return this.
|
|
80
|
+
return this.getOk(`/block/height/${h}`);
|
|
61
81
|
}
|
|
62
82
|
blockByHash(hash) {
|
|
63
|
-
return this.
|
|
83
|
+
return this.getOk(`/block/${hash}`);
|
|
64
84
|
}
|
|
65
85
|
tx(id) {
|
|
66
86
|
return this.get(`/tx/${id}`);
|
|
@@ -83,10 +103,46 @@ var CsdClient = class {
|
|
|
83
103
|
mempool() {
|
|
84
104
|
return this.get("/mempool");
|
|
85
105
|
}
|
|
86
|
-
/**
|
|
106
|
+
/**
|
|
107
|
+
* Await a txid reaching `confirmations` (default 1) — the submit-then-confirm flow every
|
|
108
|
+
* consumer was hand-rolling. Polls /tx + /tip; resolves {txid, height, confirmations};
|
|
109
|
+
* rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
|
|
110
|
+
* A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
|
|
111
|
+
* times out — never resolves on a stale sighting.
|
|
112
|
+
*/
|
|
113
|
+
async waitForTx(txid, opts = {}) {
|
|
114
|
+
const want = Math.max(1, opts.confirmations ?? 1);
|
|
115
|
+
const deadline = Date.now() + (opts.timeoutMs ?? 6e5);
|
|
116
|
+
const poll = Math.max(500, opts.pollMs ?? 5e3);
|
|
117
|
+
for (; ; ) {
|
|
118
|
+
try {
|
|
119
|
+
const t = await this.tx(txid);
|
|
120
|
+
if (t.ok && t.height != null) {
|
|
121
|
+
const tip = await this.tip();
|
|
122
|
+
const conf = tip.height - t.height + 1;
|
|
123
|
+
if (conf >= want) return { txid, height: t.height, confirmations: conf };
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
}
|
|
127
|
+
if (Date.now() > deadline) throw new Error(`waitForTx ${txid}: not at ${want} confirmation(s) within ${opts.timeoutMs ?? 6e5}ms`);
|
|
128
|
+
await new Promise((res) => setTimeout(res, poll));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Broadcast a node-JSON tx (from @inversealtruism/csd-tx `txToNodeJson`).
|
|
133
|
+
* ⚠ The node returns `{ok:false, err}` with HTTP 200 on REJECTION — and **`txid` is populated even
|
|
134
|
+
* then** (it's the computed id of the rejected tx). Callers MUST check `.ok`; reading `.txid`
|
|
135
|
+
* alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
|
|
136
|
+
*/
|
|
87
137
|
submit(nodeJsonTx) {
|
|
88
138
|
return this.post("/tx/submit", { tx: nodeJsonTx });
|
|
89
139
|
}
|
|
140
|
+
/** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
|
|
141
|
+
async submitOrThrow(nodeJsonTx) {
|
|
142
|
+
const r = await this.submit(nodeJsonTx);
|
|
143
|
+
if (!r.ok) throw new Error(`tx rejected by node: ${r.err ?? "unknown error"}`);
|
|
144
|
+
return r;
|
|
145
|
+
}
|
|
90
146
|
templatePropose(body) {
|
|
91
147
|
return this.post("/tx/template/propose", body);
|
|
92
148
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -71,39 +71,89 @@ interface RpcSubmit {
|
|
|
71
71
|
mempool_len?: number;
|
|
72
72
|
err?: string | null;
|
|
73
73
|
}
|
|
74
|
+
interface RpcProposal {
|
|
75
|
+
ok?: boolean;
|
|
76
|
+
txid: string;
|
|
77
|
+
domain: string;
|
|
78
|
+
payload_hash: string;
|
|
79
|
+
uri: string;
|
|
80
|
+
expires_epoch: number;
|
|
81
|
+
proposer: string;
|
|
82
|
+
height: number;
|
|
83
|
+
attestations?: unknown[];
|
|
84
|
+
[k: string]: unknown;
|
|
85
|
+
}
|
|
86
|
+
interface RpcTxInfo {
|
|
87
|
+
ok: boolean;
|
|
88
|
+
txid: string;
|
|
89
|
+
block_hash?: string;
|
|
90
|
+
height?: number;
|
|
91
|
+
time?: number;
|
|
92
|
+
tx?: RpcTxJson;
|
|
93
|
+
err?: string;
|
|
94
|
+
}
|
|
95
|
+
interface RpcHealth {
|
|
96
|
+
ok: boolean;
|
|
97
|
+
height?: number;
|
|
98
|
+
peers?: number;
|
|
99
|
+
mempool_len?: number;
|
|
100
|
+
[k: string]: unknown;
|
|
101
|
+
}
|
|
102
|
+
interface WaitForTxResult {
|
|
103
|
+
txid: string;
|
|
104
|
+
height: number;
|
|
105
|
+
confirmations: number;
|
|
106
|
+
}
|
|
74
107
|
interface ClientOptions {
|
|
75
108
|
baseUrl: string;
|
|
76
109
|
fetch?: typeof fetch;
|
|
77
110
|
timeoutMs?: number;
|
|
111
|
+
/** retry NETWORK failures (timeouts, refused, 5xx) this many times with jittered backoff.
|
|
112
|
+
* NEVER retries an application result (`ok:false`) or 4xx — those are answers, not outages. */
|
|
113
|
+
retries?: number;
|
|
78
114
|
}
|
|
79
115
|
declare class CsdClient {
|
|
80
116
|
private readonly base;
|
|
81
117
|
private readonly f;
|
|
82
118
|
private readonly timeoutMs;
|
|
119
|
+
private readonly retries;
|
|
83
120
|
constructor(opts: ClientOptions);
|
|
121
|
+
private req;
|
|
84
122
|
private get;
|
|
85
123
|
private post;
|
|
124
|
+
private getOk;
|
|
86
125
|
tip(): Promise<RpcTip>;
|
|
87
126
|
health(): Promise<any>;
|
|
88
127
|
blockByHeight(h: number): Promise<RpcBlock>;
|
|
89
128
|
blockByHash(hash: string): Promise<RpcBlock>;
|
|
90
|
-
tx(id: string): Promise<
|
|
91
|
-
ok: boolean;
|
|
92
|
-
txid: string;
|
|
93
|
-
block_hash?: string;
|
|
94
|
-
height?: number;
|
|
95
|
-
time?: number;
|
|
96
|
-
tx?: RpcTxJson;
|
|
97
|
-
err?: string;
|
|
98
|
-
}>;
|
|
129
|
+
tx(id: string): Promise<RpcTxInfo>;
|
|
99
130
|
utxos(addr: string): Promise<RpcUtxos>;
|
|
100
|
-
proposal(id: string): Promise<
|
|
101
|
-
proposals(domain: string, limit?: number): Promise<
|
|
102
|
-
topDomain(domain: string, epoch?: number): Promise<
|
|
103
|
-
domains(): Promise<
|
|
104
|
-
mempool(): Promise<
|
|
105
|
-
/**
|
|
131
|
+
proposal(id: string): Promise<RpcProposal>;
|
|
132
|
+
proposals(domain: string, limit?: number): Promise<RpcProposal[]>;
|
|
133
|
+
topDomain(domain: string, epoch?: number): Promise<unknown>;
|
|
134
|
+
domains(): Promise<string[]>;
|
|
135
|
+
mempool(): Promise<unknown>;
|
|
136
|
+
/**
|
|
137
|
+
* Await a txid reaching `confirmations` (default 1) — the submit-then-confirm flow every
|
|
138
|
+
* consumer was hand-rolling. Polls /tx + /tip; resolves {txid, height, confirmations};
|
|
139
|
+
* rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
|
|
140
|
+
* A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
|
|
141
|
+
* times out — never resolves on a stale sighting.
|
|
142
|
+
*/
|
|
143
|
+
waitForTx(txid: string, opts?: {
|
|
144
|
+
confirmations?: number;
|
|
145
|
+
timeoutMs?: number;
|
|
146
|
+
pollMs?: number;
|
|
147
|
+
}): Promise<WaitForTxResult>;
|
|
148
|
+
/**
|
|
149
|
+
* Broadcast a node-JSON tx (from @inversealtruism/csd-tx `txToNodeJson`).
|
|
150
|
+
* ⚠ The node returns `{ok:false, err}` with HTTP 200 on REJECTION — and **`txid` is populated even
|
|
151
|
+
* then** (it's the computed id of the rejected tx). Callers MUST check `.ok`; reading `.txid`
|
|
152
|
+
* alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
|
|
153
|
+
*/
|
|
106
154
|
submit(nodeJsonTx: unknown): Promise<RpcSubmit>;
|
|
155
|
+
/** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
|
|
156
|
+
submitOrThrow(nodeJsonTx: unknown): Promise<RpcSubmit>;
|
|
107
157
|
templatePropose(body: unknown): Promise<any>;
|
|
108
158
|
templateAttest(body: unknown): Promise<any>;
|
|
109
159
|
}
|
|
@@ -112,4 +162,4 @@ declare function rpcTxToTx(j: RpcTxJson): Tx;
|
|
|
112
162
|
/** Convert a node /block header JSON into the codec's BlockHeader. */
|
|
113
163
|
declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
|
|
114
164
|
|
|
115
|
-
export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcSubmit, type RpcTip, type RpcTxJson, type RpcUtxo, type RpcUtxos, rpcHeaderToHeader, rpcTxToTx };
|
|
165
|
+
export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcHealth, type RpcProposal, type RpcSubmit, type RpcTip, type RpcTxInfo, type RpcTxJson, type RpcUtxo, type RpcUtxos, type WaitForTxResult, rpcHeaderToHeader, rpcTxToTx };
|
package/dist/index.d.ts
CHANGED
|
@@ -71,39 +71,89 @@ interface RpcSubmit {
|
|
|
71
71
|
mempool_len?: number;
|
|
72
72
|
err?: string | null;
|
|
73
73
|
}
|
|
74
|
+
interface RpcProposal {
|
|
75
|
+
ok?: boolean;
|
|
76
|
+
txid: string;
|
|
77
|
+
domain: string;
|
|
78
|
+
payload_hash: string;
|
|
79
|
+
uri: string;
|
|
80
|
+
expires_epoch: number;
|
|
81
|
+
proposer: string;
|
|
82
|
+
height: number;
|
|
83
|
+
attestations?: unknown[];
|
|
84
|
+
[k: string]: unknown;
|
|
85
|
+
}
|
|
86
|
+
interface RpcTxInfo {
|
|
87
|
+
ok: boolean;
|
|
88
|
+
txid: string;
|
|
89
|
+
block_hash?: string;
|
|
90
|
+
height?: number;
|
|
91
|
+
time?: number;
|
|
92
|
+
tx?: RpcTxJson;
|
|
93
|
+
err?: string;
|
|
94
|
+
}
|
|
95
|
+
interface RpcHealth {
|
|
96
|
+
ok: boolean;
|
|
97
|
+
height?: number;
|
|
98
|
+
peers?: number;
|
|
99
|
+
mempool_len?: number;
|
|
100
|
+
[k: string]: unknown;
|
|
101
|
+
}
|
|
102
|
+
interface WaitForTxResult {
|
|
103
|
+
txid: string;
|
|
104
|
+
height: number;
|
|
105
|
+
confirmations: number;
|
|
106
|
+
}
|
|
74
107
|
interface ClientOptions {
|
|
75
108
|
baseUrl: string;
|
|
76
109
|
fetch?: typeof fetch;
|
|
77
110
|
timeoutMs?: number;
|
|
111
|
+
/** retry NETWORK failures (timeouts, refused, 5xx) this many times with jittered backoff.
|
|
112
|
+
* NEVER retries an application result (`ok:false`) or 4xx — those are answers, not outages. */
|
|
113
|
+
retries?: number;
|
|
78
114
|
}
|
|
79
115
|
declare class CsdClient {
|
|
80
116
|
private readonly base;
|
|
81
117
|
private readonly f;
|
|
82
118
|
private readonly timeoutMs;
|
|
119
|
+
private readonly retries;
|
|
83
120
|
constructor(opts: ClientOptions);
|
|
121
|
+
private req;
|
|
84
122
|
private get;
|
|
85
123
|
private post;
|
|
124
|
+
private getOk;
|
|
86
125
|
tip(): Promise<RpcTip>;
|
|
87
126
|
health(): Promise<any>;
|
|
88
127
|
blockByHeight(h: number): Promise<RpcBlock>;
|
|
89
128
|
blockByHash(hash: string): Promise<RpcBlock>;
|
|
90
|
-
tx(id: string): Promise<
|
|
91
|
-
ok: boolean;
|
|
92
|
-
txid: string;
|
|
93
|
-
block_hash?: string;
|
|
94
|
-
height?: number;
|
|
95
|
-
time?: number;
|
|
96
|
-
tx?: RpcTxJson;
|
|
97
|
-
err?: string;
|
|
98
|
-
}>;
|
|
129
|
+
tx(id: string): Promise<RpcTxInfo>;
|
|
99
130
|
utxos(addr: string): Promise<RpcUtxos>;
|
|
100
|
-
proposal(id: string): Promise<
|
|
101
|
-
proposals(domain: string, limit?: number): Promise<
|
|
102
|
-
topDomain(domain: string, epoch?: number): Promise<
|
|
103
|
-
domains(): Promise<
|
|
104
|
-
mempool(): Promise<
|
|
105
|
-
/**
|
|
131
|
+
proposal(id: string): Promise<RpcProposal>;
|
|
132
|
+
proposals(domain: string, limit?: number): Promise<RpcProposal[]>;
|
|
133
|
+
topDomain(domain: string, epoch?: number): Promise<unknown>;
|
|
134
|
+
domains(): Promise<string[]>;
|
|
135
|
+
mempool(): Promise<unknown>;
|
|
136
|
+
/**
|
|
137
|
+
* Await a txid reaching `confirmations` (default 1) — the submit-then-confirm flow every
|
|
138
|
+
* consumer was hand-rolling. Polls /tx + /tip; resolves {txid, height, confirmations};
|
|
139
|
+
* rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
|
|
140
|
+
* A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
|
|
141
|
+
* times out — never resolves on a stale sighting.
|
|
142
|
+
*/
|
|
143
|
+
waitForTx(txid: string, opts?: {
|
|
144
|
+
confirmations?: number;
|
|
145
|
+
timeoutMs?: number;
|
|
146
|
+
pollMs?: number;
|
|
147
|
+
}): Promise<WaitForTxResult>;
|
|
148
|
+
/**
|
|
149
|
+
* Broadcast a node-JSON tx (from @inversealtruism/csd-tx `txToNodeJson`).
|
|
150
|
+
* ⚠ The node returns `{ok:false, err}` with HTTP 200 on REJECTION — and **`txid` is populated even
|
|
151
|
+
* then** (it's the computed id of the rejected tx). Callers MUST check `.ok`; reading `.txid`
|
|
152
|
+
* alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
|
|
153
|
+
*/
|
|
106
154
|
submit(nodeJsonTx: unknown): Promise<RpcSubmit>;
|
|
155
|
+
/** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
|
|
156
|
+
submitOrThrow(nodeJsonTx: unknown): Promise<RpcSubmit>;
|
|
107
157
|
templatePropose(body: unknown): Promise<any>;
|
|
108
158
|
templateAttest(body: unknown): Promise<any>;
|
|
109
159
|
}
|
|
@@ -112,4 +162,4 @@ declare function rpcTxToTx(j: RpcTxJson): Tx;
|
|
|
112
162
|
/** Convert a node /block header JSON into the codec's BlockHeader. */
|
|
113
163
|
declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
|
|
114
164
|
|
|
115
|
-
export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcSubmit, type RpcTip, type RpcTxJson, type RpcUtxo, type RpcUtxos, rpcHeaderToHeader, rpcTxToTx };
|
|
165
|
+
export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcHealth, type RpcProposal, type RpcSubmit, type RpcTip, type RpcTxInfo, type RpcTxJson, type RpcUtxo, type RpcUtxos, type WaitForTxResult, rpcHeaderToHeader, rpcTxToTx };
|
package/dist/index.js
CHANGED
|
@@ -3,26 +3,46 @@ var CsdClient = class {
|
|
|
3
3
|
base;
|
|
4
4
|
f;
|
|
5
5
|
timeoutMs;
|
|
6
|
+
retries;
|
|
6
7
|
constructor(opts) {
|
|
7
8
|
this.base = opts.baseUrl.replace(/\/+$/, "");
|
|
8
9
|
this.f = opts.fetch ?? globalThis.fetch;
|
|
9
10
|
this.timeoutMs = opts.timeoutMs ?? 1e4;
|
|
11
|
+
this.retries = Math.max(0, opts.retries ?? 0);
|
|
10
12
|
if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
|
|
11
13
|
}
|
|
12
|
-
async
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
14
|
+
async req(path, init) {
|
|
15
|
+
let lastErr;
|
|
16
|
+
for (let attempt = 0; ; attempt++) {
|
|
17
|
+
try {
|
|
18
|
+
const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
|
|
19
|
+
if (r.status >= 500 && attempt < this.retries) {
|
|
20
|
+
lastErr = new Error(`HTTP ${r.status}`);
|
|
21
|
+
} else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
|
|
22
|
+
else return await r.json();
|
|
23
|
+
} catch (e) {
|
|
24
|
+
if (attempt >= this.retries) throw e;
|
|
25
|
+
lastErr = e;
|
|
26
|
+
}
|
|
27
|
+
const cap = Math.min(5e3, 250 * 2 ** attempt);
|
|
28
|
+
await new Promise((res) => setTimeout(res, Math.floor(Math.random() * cap)));
|
|
29
|
+
void lastErr;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
get(path) {
|
|
33
|
+
return this.req(path);
|
|
34
|
+
}
|
|
35
|
+
post(path, body) {
|
|
36
|
+
return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
37
|
+
}
|
|
38
|
+
// The node returns application errors as `{ok:false, err}` with HTTP **200**, so a bare `get()`
|
|
39
|
+
// can't see them. For endpoints whose `{ok:false}` result is useless to the caller (a missing
|
|
40
|
+
// block), surface it as a thrown error — otherwise a beyond-tip/not-found block flows downstream
|
|
41
|
+
// as a malformed object and crashes opaquely (e.g. the light client reading `header.prev`).
|
|
42
|
+
async getOk(path) {
|
|
43
|
+
const j = await this.get(path);
|
|
44
|
+
if (j && j.ok === false) throw new Error(`GET ${path} \u2192 node error: ${j.err ?? "ok:false"}`);
|
|
45
|
+
return j;
|
|
26
46
|
}
|
|
27
47
|
tip() {
|
|
28
48
|
return this.get("/tip");
|
|
@@ -31,10 +51,10 @@ var CsdClient = class {
|
|
|
31
51
|
return this.get("/health");
|
|
32
52
|
}
|
|
33
53
|
blockByHeight(h) {
|
|
34
|
-
return this.
|
|
54
|
+
return this.getOk(`/block/height/${h}`);
|
|
35
55
|
}
|
|
36
56
|
blockByHash(hash) {
|
|
37
|
-
return this.
|
|
57
|
+
return this.getOk(`/block/${hash}`);
|
|
38
58
|
}
|
|
39
59
|
tx(id) {
|
|
40
60
|
return this.get(`/tx/${id}`);
|
|
@@ -57,10 +77,46 @@ var CsdClient = class {
|
|
|
57
77
|
mempool() {
|
|
58
78
|
return this.get("/mempool");
|
|
59
79
|
}
|
|
60
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Await a txid reaching `confirmations` (default 1) — the submit-then-confirm flow every
|
|
82
|
+
* consumer was hand-rolling. Polls /tx + /tip; resolves {txid, height, confirmations};
|
|
83
|
+
* rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
|
|
84
|
+
* A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
|
|
85
|
+
* times out — never resolves on a stale sighting.
|
|
86
|
+
*/
|
|
87
|
+
async waitForTx(txid, opts = {}) {
|
|
88
|
+
const want = Math.max(1, opts.confirmations ?? 1);
|
|
89
|
+
const deadline = Date.now() + (opts.timeoutMs ?? 6e5);
|
|
90
|
+
const poll = Math.max(500, opts.pollMs ?? 5e3);
|
|
91
|
+
for (; ; ) {
|
|
92
|
+
try {
|
|
93
|
+
const t = await this.tx(txid);
|
|
94
|
+
if (t.ok && t.height != null) {
|
|
95
|
+
const tip = await this.tip();
|
|
96
|
+
const conf = tip.height - t.height + 1;
|
|
97
|
+
if (conf >= want) return { txid, height: t.height, confirmations: conf };
|
|
98
|
+
}
|
|
99
|
+
} catch {
|
|
100
|
+
}
|
|
101
|
+
if (Date.now() > deadline) throw new Error(`waitForTx ${txid}: not at ${want} confirmation(s) within ${opts.timeoutMs ?? 6e5}ms`);
|
|
102
|
+
await new Promise((res) => setTimeout(res, poll));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Broadcast a node-JSON tx (from @inversealtruism/csd-tx `txToNodeJson`).
|
|
107
|
+
* ⚠ The node returns `{ok:false, err}` with HTTP 200 on REJECTION — and **`txid` is populated even
|
|
108
|
+
* then** (it's the computed id of the rejected tx). Callers MUST check `.ok`; reading `.txid`
|
|
109
|
+
* alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
|
|
110
|
+
*/
|
|
61
111
|
submit(nodeJsonTx) {
|
|
62
112
|
return this.post("/tx/submit", { tx: nodeJsonTx });
|
|
63
113
|
}
|
|
114
|
+
/** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
|
|
115
|
+
async submitOrThrow(nodeJsonTx) {
|
|
116
|
+
const r = await this.submit(nodeJsonTx);
|
|
117
|
+
if (!r.ok) throw new Error(`tx rejected by node: ${r.err ?? "unknown error"}`);
|
|
118
|
+
return r;
|
|
119
|
+
}
|
|
64
120
|
templatePropose(body) {
|
|
65
121
|
return this.post("/tx/template/propose", body);
|
|
66
122
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inversealtruism/csd-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Typed HTTP RPC client for a Compute Substrate node (or the Cairn proxy / a discovered gateway) — tip/block/tx/utxos/proposals/top/domains/mempool/submit/templates.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
"README.md"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@inversealtruism/csd-codec": "0.1.
|
|
22
|
+
"@inversealtruism/csd-codec": "0.1.5"
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@inversealtruism/csd-vectors": "0.1.
|
|
27
|
+
"@inversealtruism/csd-vectors": "0.1.5"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|