@secondlayer/stacks 6.0.0 → 6.1.0

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.
Files changed (153) hide show
  1. package/dist/shared/{stacks-qt8ehebn.js → stacks-esm-0rdxcta7.js} +2 -2
  2. package/dist/shared/{stacks-qt8ehebn.js.map → stacks-esm-0rdxcta7.js.map} +1 -1
  3. package/dist/shared/{stacks-4hs8r4q2.js → stacks-esm-1jae0a3r.js} +2 -2
  4. package/dist/shared/{stacks-xjkn3en1.cjs.map → stacks-esm-1jae0a3r.js.map} +1 -1
  5. package/dist/shared/{stacks-4d7c3x27.js → stacks-esm-2mc7ebrm.js} +3 -3
  6. package/dist/shared/{stacks-4d7c3x27.js.map → stacks-esm-2mc7ebrm.js.map} +1 -1
  7. package/dist/shared/{stacks-8brgfvqx.js → stacks-esm-3g33bqsm.js} +4 -4
  8. package/dist/shared/{stacks-8brgfvqx.js.map → stacks-esm-3g33bqsm.js.map} +1 -1
  9. package/dist/shared/{stacks-gjz26k6b.js → stacks-esm-3pv5avgr.js} +59 -15
  10. package/dist/shared/stacks-esm-3pv5avgr.js.map +12 -0
  11. package/dist/shared/{stacks-qex691bh.js → stacks-esm-743jfh10.js} +6 -6
  12. package/dist/shared/{stacks-qex691bh.js.map → stacks-esm-743jfh10.js.map} +1 -1
  13. package/dist/shared/{stacks-wxfd6r4b.js → stacks-esm-7fpqgtyx.js} +3 -3
  14. package/dist/shared/{stacks-wxfd6r4b.js.map → stacks-esm-7fpqgtyx.js.map} +1 -1
  15. package/dist/shared/{stacks-71k0vmct.js → stacks-esm-9rnx0zxr.js} +5 -5
  16. package/dist/shared/{stacks-71k0vmct.js.map → stacks-esm-9rnx0zxr.js.map} +1 -1
  17. package/dist/shared/{stacks-fd4rx56e.js → stacks-esm-aw2mj3sj.js} +7 -7
  18. package/dist/shared/{stacks-fd4rx56e.js.map → stacks-esm-aw2mj3sj.js.map} +1 -1
  19. package/dist/shared/{stacks-qs5fzts5.js → stacks-esm-cvhazvhd.js} +2 -2
  20. package/dist/shared/{stacks-qs5fzts5.js.map → stacks-esm-cvhazvhd.js.map} +1 -1
  21. package/dist/shared/{stacks-69nq48jp.js → stacks-esm-fcfdn2zx.js} +5 -5
  22. package/dist/shared/{stacks-69nq48jp.js.map → stacks-esm-fcfdn2zx.js.map} +1 -1
  23. package/dist/shared/{stacks-x0qqwrev.js → stacks-esm-hr7fpxtj.js} +4 -4
  24. package/dist/shared/{stacks-x0qqwrev.js.map → stacks-esm-hr7fpxtj.js.map} +1 -1
  25. package/dist/shared/{stacks-mbt36bzt.js → stacks-esm-kds4n82p.js} +11 -11
  26. package/dist/shared/{stacks-mbt36bzt.js.map → stacks-esm-kds4n82p.js.map} +1 -1
  27. package/dist/shared/{stacks-kg1npstf.js → stacks-esm-n53kvwmt.js} +2 -2
  28. package/dist/shared/{stacks-kg1npstf.js.map → stacks-esm-n53kvwmt.js.map} +1 -1
  29. package/dist/shared/{stacks-v2rfr46a.js → stacks-esm-pxhfqfff.js} +4 -4
  30. package/dist/shared/{stacks-v2rfr46a.js.map → stacks-esm-pxhfqfff.js.map} +1 -1
  31. package/dist/shared/{stacks-yajyej2y.js → stacks-esm-q19a17fd.js} +3 -3
  32. package/dist/shared/{stacks-yajyej2y.js.map → stacks-esm-q19a17fd.js.map} +1 -1
  33. package/dist/shared/{stacks-v03zef3g.js → stacks-esm-qk0p9fr6.js} +9 -9
  34. package/dist/shared/{stacks-v03zef3g.js.map → stacks-esm-qk0p9fr6.js.map} +1 -1
  35. package/dist/shared/{stacks-qcae5852.js → stacks-esm-qkj67g80.js} +2 -2
  36. package/dist/shared/{stacks-qcae5852.js.map → stacks-esm-qkj67g80.js.map} +1 -1
  37. package/dist/shared/{stacks-mftyrc60.js → stacks-esm-r6dejrgr.js} +4 -4
  38. package/dist/shared/{stacks-mftyrc60.js.map → stacks-esm-r6dejrgr.js.map} +1 -1
  39. package/dist/shared/{stacks-9c1yam8b.js → stacks-esm-r9q4wnyw.js} +5 -5
  40. package/dist/shared/{stacks-9c1yam8b.js.map → stacks-esm-r9q4wnyw.js.map} +1 -1
  41. package/dist/shared/{stacks-51ybf135.js → stacks-esm-skmcf5n2.js} +5 -5
  42. package/dist/shared/{stacks-51ybf135.js.map → stacks-esm-skmcf5n2.js.map} +1 -1
  43. package/dist/shared/{stacks-yvb7rvwk.js → stacks-esm-v45990hx.js} +2 -2
  44. package/dist/shared/{stacks-8vzxax5e.cjs.map → stacks-esm-v45990hx.js.map} +1 -1
  45. package/dist/shared/{stacks-z90xdr1x.js → stacks-esm-wv67m4g6.js} +3 -3
  46. package/dist/shared/{stacks-z90xdr1x.js.map → stacks-esm-wv67m4g6.js.map} +1 -1
  47. package/dist/shared/{stacks-1m3m1agc.js → stacks-esm-x1r01353.js} +5 -5
  48. package/dist/shared/{stacks-1m3m1agc.js.map → stacks-esm-x1r01353.js.map} +1 -1
  49. package/dist/src/accounts/index.cjs +558 -4
  50. package/dist/src/accounts/index.cjs.map +10 -4
  51. package/dist/src/accounts/index.js +3 -3
  52. package/dist/src/actions/index.cjs +3815 -1
  53. package/dist/src/actions/index.cjs.map +65 -3
  54. package/dist/src/actions/index.js +14 -14
  55. package/dist/src/bitcoin/index.cjs +2477 -1
  56. package/dist/src/bitcoin/index.cjs.map +31 -3
  57. package/dist/src/bitcoin/index.js +11 -11
  58. package/dist/src/bns/index.cjs +4439 -17
  59. package/dist/src/bns/index.cjs.map +50 -3
  60. package/dist/src/bns/index.js +14 -14
  61. package/dist/src/chains/index.cjs +135 -1
  62. package/dist/src/chains/index.cjs.map +5 -3
  63. package/dist/src/chains/index.js +1 -1
  64. package/dist/src/clarity/index.cjs +2019 -1
  65. package/dist/src/clarity/index.cjs.map +24 -3
  66. package/dist/src/clarity/index.js +8 -8
  67. package/dist/src/filters/index.cjs +725 -1
  68. package/dist/src/filters/index.cjs.map +11 -3
  69. package/dist/src/filters/index.d.cts +76 -13
  70. package/dist/src/filters/index.d.ts +76 -13
  71. package/dist/src/filters/index.js +8 -4
  72. package/dist/src/filters/index.js.map +1 -1
  73. package/dist/src/index.cjs +3935 -62
  74. package/dist/src/index.cjs.map +69 -4
  75. package/dist/src/index.d.cts +62 -11
  76. package/dist/src/index.d.ts +62 -11
  77. package/dist/src/index.js +16 -16
  78. package/dist/src/postconditions/index.cjs +1726 -1
  79. package/dist/src/postconditions/index.cjs.map +20 -3
  80. package/dist/src/postconditions/index.js +6 -6
  81. package/dist/src/pox/index.cjs +3855 -1
  82. package/dist/src/pox/index.cjs.map +48 -3
  83. package/dist/src/pox/index.js +13 -13
  84. package/dist/src/pox5/index.cjs +4054 -29
  85. package/dist/src/pox5/index.cjs.map +54 -3
  86. package/dist/src/pox5/index.js +15 -15
  87. package/dist/src/sbtc/index.cjs +3705 -4
  88. package/dist/src/sbtc/index.cjs.map +47 -3
  89. package/dist/src/sbtc/index.js +12 -12
  90. package/dist/src/stackingdao/index.cjs +3751 -8
  91. package/dist/src/stackingdao/index.cjs.map +45 -3
  92. package/dist/src/stackingdao/index.js +12 -12
  93. package/dist/src/subscriptions/index.cjs +153 -1
  94. package/dist/src/subscriptions/index.cjs.map +6 -3
  95. package/dist/src/subscriptions/index.js +2 -2
  96. package/dist/src/transactions/index.cjs +2139 -1
  97. package/dist/src/transactions/index.cjs.map +23 -3
  98. package/dist/src/transactions/index.js +6 -6
  99. package/dist/src/utils/index.cjs +435 -3
  100. package/dist/src/utils/index.cjs.map +9 -3
  101. package/dist/src/utils/index.js +3 -3
  102. package/package.json +1 -1
  103. package/dist/shared/stacks-02actsrx.cjs +0 -17
  104. package/dist/shared/stacks-02actsrx.cjs.map +0 -10
  105. package/dist/shared/stacks-03vfm21t.cjs +0 -222
  106. package/dist/shared/stacks-03vfm21t.cjs.map +0 -12
  107. package/dist/shared/stacks-24h5k94q.cjs +0 -74
  108. package/dist/shared/stacks-24h5k94q.cjs.map +0 -11
  109. package/dist/shared/stacks-37kz5t9f.cjs +0 -764
  110. package/dist/shared/stacks-37kz5t9f.cjs.map +0 -23
  111. package/dist/shared/stacks-3mt2q2na.cjs +0 -65
  112. package/dist/shared/stacks-3mt2q2na.cjs.map +0 -9
  113. package/dist/shared/stacks-4hs8r4q2.js.map +0 -10
  114. package/dist/shared/stacks-8vzxax5e.cjs +0 -45
  115. package/dist/shared/stacks-agk106zs.cjs +0 -267
  116. package/dist/shared/stacks-agk106zs.cjs.map +0 -12
  117. package/dist/shared/stacks-bdgh9n83.cjs +0 -212
  118. package/dist/shared/stacks-bdgh9n83.cjs.map +0 -11
  119. package/dist/shared/stacks-dpxnxhqb.cjs +0 -51
  120. package/dist/shared/stacks-dpxnxhqb.cjs.map +0 -11
  121. package/dist/shared/stacks-ee53k9j2.cjs +0 -651
  122. package/dist/shared/stacks-ee53k9j2.cjs.map +0 -13
  123. package/dist/shared/stacks-enpj9krt.cjs +0 -38
  124. package/dist/shared/stacks-enpj9krt.cjs.map +0 -10
  125. package/dist/shared/stacks-f35mnrmg.cjs +0 -164
  126. package/dist/shared/stacks-f35mnrmg.cjs.map +0 -10
  127. package/dist/shared/stacks-fhzwsn5d.cjs +0 -261
  128. package/dist/shared/stacks-fhzwsn5d.cjs.map +0 -12
  129. package/dist/shared/stacks-gjz26k6b.js.map +0 -12
  130. package/dist/shared/stacks-gza2n1zs.cjs +0 -73
  131. package/dist/shared/stacks-gza2n1zs.cjs.map +0 -11
  132. package/dist/shared/stacks-kem68d80.cjs +0 -156
  133. package/dist/shared/stacks-kem68d80.cjs.map +0 -10
  134. package/dist/shared/stacks-n98w7g57.cjs +0 -234
  135. package/dist/shared/stacks-n98w7g57.cjs.map +0 -10
  136. package/dist/shared/stacks-p337hp6m.cjs +0 -336
  137. package/dist/shared/stacks-p337hp6m.cjs.map +0 -25
  138. package/dist/shared/stacks-q34k4vnc.cjs +0 -286
  139. package/dist/shared/stacks-q34k4vnc.cjs.map +0 -12
  140. package/dist/shared/stacks-rryme9bv.cjs +0 -569
  141. package/dist/shared/stacks-rryme9bv.cjs.map +0 -13
  142. package/dist/shared/stacks-s5vej5z6.cjs +0 -410
  143. package/dist/shared/stacks-s5vej5z6.cjs.map +0 -15
  144. package/dist/shared/stacks-sdqkapct.cjs +0 -67
  145. package/dist/shared/stacks-sdqkapct.cjs.map +0 -12
  146. package/dist/shared/stacks-vea7nde7.cjs +0 -731
  147. package/dist/shared/stacks-vea7nde7.cjs.map +0 -13
  148. package/dist/shared/stacks-wk8yp13j.cjs +0 -33
  149. package/dist/shared/stacks-wk8yp13j.cjs.map +0 -11
  150. package/dist/shared/stacks-xjkn3en1.cjs +0 -42
  151. package/dist/shared/stacks-xnrywjf1.cjs +0 -131
  152. package/dist/shared/stacks-xnrywjf1.cjs.map +0 -11
  153. package/dist/shared/stacks-yvb7rvwk.js.map +0 -10
@@ -1,764 +0,0 @@
1
- // src/actions/public/estimateFee.ts
2
- function isValidEstimation(e) {
3
- return typeof e === "object" && e !== null && "fee_rate" in e && "fee" in e && (typeof e.fee_rate === "number" || typeof e.fee_rate === "string") && (typeof e.fee === "number" || typeof e.fee === "string");
4
- }
5
- async function estimateFee(client, params) {
6
- const payloadHex = bytesToHex(serializePayload(params.transaction.payload));
7
- const txHex = bytesToHex(serializeTransaction(params.transaction));
8
- const data = await client.request("/v2/fees/transaction", {
9
- method: "POST",
10
- body: {
11
- estimated_len: Math.ceil(txHex.length / 2),
12
- transaction_payload: `0x${payloadHex}`
13
- }
14
- });
15
- const raw = data?.estimations ?? [];
16
- return raw.map((e) => {
17
- if (!isValidEstimation(e)) {
18
- throw new MalformedResponseError("estimateFee: node response contains invalid fee estimation entry");
19
- }
20
- return {
21
- feeRate: Number(e.fee_rate),
22
- fee: Number(e.fee)
23
- };
24
- });
25
- }
26
-
27
- // src/actions/public/getBlockHeight.ts
28
- async function getBlockHeight(client) {
29
- const data = await client.request("/v2/info", { method: "GET" });
30
- if (typeof data?.stacks_tip_height !== "number") {
31
- throw new MalformedResponseError('getBlockHeight: /v2/info response is missing "stacks_tip_height"');
32
- }
33
- return data.stacks_tip_height;
34
- }
35
-
36
- // src/errors/http.ts
37
- class HttpRequestError extends BaseError {
38
- name = "HttpRequestError";
39
- status;
40
- url;
41
- method;
42
- constructor(status, options) {
43
- const where = options?.url !== undefined ? ` (${options.method ?? "GET"} ${options.url})` : "";
44
- super(`HTTP request failed with status ${status}${where}`, options);
45
- this.status = status;
46
- this.url = options?.url;
47
- this.method = options?.method;
48
- }
49
- }
50
-
51
- // src/actions/public/getMapEntry.ts
52
- async function getMapEntry(client, params) {
53
- const [address, name] = parseContractId(params.contract);
54
- const serializedKey = with0x(bytesToHex(serializeCVBytes(params.key)));
55
- const data = await client.request(`/v2/map_entry/${address}/${name}/${params.mapName}`, {
56
- method: "POST",
57
- body: serializedKey
58
- });
59
- if (typeof data?.data !== "string") {
60
- throw new MalformedResponseError(`getMapEntry: /v2/map_entry/${address}/${name}/${params.mapName} response is missing "data"`);
61
- }
62
- return deserializeCVBytes(data.data);
63
- }
64
-
65
- // src/actions/public/getNonce.ts
66
- async function getNonce(client, params) {
67
- const data = await client.request(`/v2/accounts/${encodeURIComponent(params.address)}`, { method: "GET" });
68
- const nonce = data?.nonce;
69
- if (typeof nonce !== "number" && typeof nonce !== "string") {
70
- throw new MalformedResponseError(`getNonce: /v2/accounts/${params.address} response is missing "nonce"`);
71
- }
72
- return BigInt(nonce);
73
- }
74
-
75
- // src/errors/transport.ts
76
- class TimeoutError extends BaseError {
77
- name = "TimeoutError";
78
- method;
79
- url;
80
- timeout;
81
- attempt;
82
- constructor(params) {
83
- super(`${params.method} ${params.url} did not complete within ${params.timeout}ms (attempt ${params.attempt + 1})`);
84
- this.method = params.method;
85
- this.url = params.url;
86
- this.timeout = params.timeout;
87
- this.attempt = params.attempt;
88
- }
89
- }
90
-
91
- // src/transports/createTransport.ts
92
- function createTransport(type, config) {
93
- const { request, apiKey: _apiKey, ...exposed } = config;
94
- return {
95
- type,
96
- request,
97
- config: exposed
98
- };
99
- }
100
- var MAX_ERROR_DETAILS_BYTES = 4096;
101
- function capDetails(body) {
102
- if (body === undefined)
103
- return;
104
- const bytes = new TextEncoder().encode(body);
105
- if (bytes.length <= MAX_ERROR_DETAILS_BYTES)
106
- return body;
107
- const head = new TextDecoder().decode(bytes.subarray(0, MAX_ERROR_DETAILS_BYTES));
108
- return `${head}... [truncated ${bytes.length - MAX_ERROR_DETAILS_BYTES} bytes]`;
109
- }
110
- function isRetryableStatus(status) {
111
- return status === 429 || status >= 500;
112
- }
113
- var MAX_RETRY_AFTER_MS = 60000;
114
- function retryAfterMs(response) {
115
- const value = response.headers.get("Retry-After");
116
- if (!value)
117
- return;
118
- const seconds = Number(value);
119
- const ms = Number.isFinite(seconds) ? seconds * 1000 : Date.parse(value) - Date.now();
120
- if (!Number.isFinite(ms) || ms < 0 || ms > MAX_RETRY_AFTER_MS)
121
- return;
122
- return ms;
123
- }
124
- function abortReason(signal) {
125
- const reason = signal.reason;
126
- if (reason instanceof Error)
127
- return reason;
128
- return new DOMException(typeof reason === "string" ? reason : "The operation was aborted", "AbortError");
129
- }
130
- function sleep(ms, signal) {
131
- return new Promise((resolve, reject) => {
132
- if (signal?.aborted)
133
- return reject(abortReason(signal));
134
- const onAbort = () => {
135
- clearTimeout(id);
136
- reject(abortReason(signal));
137
- };
138
- const id = setTimeout(() => {
139
- signal?.removeEventListener("abort", onAbort);
140
- resolve();
141
- }, ms);
142
- signal?.addEventListener("abort", onAbort, { once: true });
143
- });
144
- }
145
- async function fetchWithRetry(url, options, retryCount, retryDelay, timeout, extra = {}) {
146
- const { signal } = extra;
147
- const read = extra.read ?? (async (response) => response);
148
- const method = options.method ?? "GET";
149
- let lastError;
150
- for (let attempt = 0;attempt <= retryCount; attempt++) {
151
- if (signal?.aborted)
152
- throw abortReason(signal);
153
- const controller = new AbortController;
154
- let timedOut = false;
155
- const timeoutId = setTimeout(() => {
156
- timedOut = true;
157
- controller.abort(new TimeoutError({ method, url, timeout, attempt }));
158
- }, timeout);
159
- const onCallerAbort = () => controller.abort(abortReason(signal));
160
- signal?.addEventListener("abort", onCallerAbort, { once: true });
161
- const aborted = new Promise((_, reject) => {
162
- if (controller.signal.aborted)
163
- return reject(controller.signal.reason);
164
- controller.signal.addEventListener("abort", () => reject(controller.signal.reason), { once: true });
165
- });
166
- let response;
167
- try {
168
- try {
169
- response = await Promise.race([
170
- fetch(url, { ...options, signal: controller.signal }),
171
- aborted
172
- ]);
173
- } catch (error) {
174
- if (signal?.aborted)
175
- throw abortReason(signal);
176
- lastError = timedOut ? new TimeoutError({ method, url, timeout, attempt }) : error instanceof Error ? error : new Error(String(error));
177
- if (attempt < retryCount) {
178
- clearTimeout(timeoutId);
179
- await sleep(retryDelay * (attempt + 1), signal);
180
- continue;
181
- }
182
- throw lastError;
183
- }
184
- if (response.ok) {
185
- try {
186
- return await Promise.race([read(response), aborted]);
187
- } catch (error) {
188
- response.body?.cancel().catch(() => {});
189
- if (signal?.aborted)
190
- throw abortReason(signal);
191
- if (!timedOut)
192
- throw error;
193
- lastError = new TimeoutError({ method, url, timeout, attempt });
194
- if (attempt < retryCount) {
195
- clearTimeout(timeoutId);
196
- await sleep(retryDelay * (attempt + 1), signal);
197
- continue;
198
- }
199
- throw lastError;
200
- }
201
- }
202
- const retryable = isRetryableStatus(response.status);
203
- if (!retryable || attempt === retryCount) {
204
- const body = await Promise.race([
205
- response.text().catch(() => {
206
- return;
207
- }),
208
- aborted.catch(() => {
209
- return;
210
- })
211
- ]);
212
- throw new HttpRequestError(response.status, {
213
- details: capDetails(body),
214
- url,
215
- method
216
- });
217
- }
218
- lastError = new HttpRequestError(response.status, { url, method });
219
- const delay = retryAfterMs(response) ?? retryDelay * (attempt + 1);
220
- response.body?.cancel().catch(() => {});
221
- clearTimeout(timeoutId);
222
- await sleep(delay, signal);
223
- } finally {
224
- clearTimeout(timeoutId);
225
- signal?.removeEventListener("abort", onCallerAbort);
226
- }
227
- }
228
- throw lastError ?? new Error("Request failed");
229
- }
230
- async function readBody(response) {
231
- const contentType = response.headers.get("content-type") ?? "";
232
- if (contentType.includes("application/json")) {
233
- return response.json();
234
- }
235
- return response.text();
236
- }
237
- function buildRequestFn(baseUrl, config) {
238
- const {
239
- timeout = 30000,
240
- retryCount = 3,
241
- retryDelay = 150,
242
- fetchOptions = {},
243
- apiKey
244
- } = config;
245
- return async (path, options) => {
246
- const url = `${baseUrl.replace(/\/$/, "")}${path}`;
247
- const headers = {
248
- "Content-Type": "application/json",
249
- ...fetchOptions.headers,
250
- ...options?.headers
251
- };
252
- if (apiKey) {
253
- headers["x-api-key"] = apiKey;
254
- }
255
- const init = {
256
- ...fetchOptions,
257
- method: options?.method ?? "GET",
258
- headers
259
- };
260
- if (options?.body !== undefined) {
261
- init.body = JSON.stringify(options.body);
262
- }
263
- return fetchWithRetry(url, init, options?.retryCount ?? retryCount, retryDelay, timeout, { signal: options?.signal, read: readBody });
264
- };
265
- }
266
-
267
- // src/actions/public/txSources.ts
268
- class IndexSourceConfigError extends Error {
269
- name = "IndexSourceConfigError";
270
- }
271
- function isHiroHost(url, client) {
272
- let hostname;
273
- try {
274
- hostname = new URL(url).hostname;
275
- } catch {
276
- return false;
277
- }
278
- if (hostname === "hiro.so" || hostname.endsWith(".hiro.so"))
279
- return true;
280
- const defaults = client.chain?.rpcUrls?.default?.http ?? [];
281
- return defaults.some((d) => {
282
- try {
283
- return new URL(d).hostname === hostname && hostname !== "localhost";
284
- } catch {
285
- return false;
286
- }
287
- });
288
- }
289
- function indexRequestFn(client, baseUrl, sourceName) {
290
- const transportUrl = client.transport.config.url?.replace(/\/$/, "");
291
- if (baseUrl === undefined) {
292
- if (!transportUrl) {
293
- throw new IndexSourceConfigError(`${sourceName} needs the URL of your Secondlayer instance: pass baseUrl, or use an http() transport pointed at the instance`);
294
- }
295
- if (isHiroHost(transportUrl, client)) {
296
- throw new IndexSourceConfigError(`${sourceName} reads /v1/index on a Secondlayer instance, and ${transportUrl} is a Hiro host: pass baseUrl with the URL of your instance`);
297
- }
298
- return client.request;
299
- }
300
- const target = baseUrl.replace(/\/$/, "");
301
- if (target === transportUrl)
302
- return client.request;
303
- const { timeout, retryCount, retryDelay } = client.transport.config;
304
- return buildRequestFn(target, { timeout, retryCount, retryDelay });
305
- }
306
- function indexNotFound(error, sourceName) {
307
- if (!(error instanceof HttpRequestError) || error.status !== 404) {
308
- return false;
309
- }
310
- const body = error.details;
311
- if (body !== undefined) {
312
- try {
313
- JSON.parse(body);
314
- } catch {
315
- throw new IndexSourceConfigError(`${sourceName} got a non-JSON 404 from ${error.url ?? "the transport host"}: it does not serve /v1/index, pass baseUrl with the URL of your Secondlayer instance`, { cause: error });
316
- }
317
- }
318
- return true;
319
- }
320
- function normalizeTxid(txid) {
321
- return txid.startsWith("0x") ? txid : `0x${txid}`;
322
- }
323
- function decodeResultHex(hex) {
324
- if (!hex)
325
- return {};
326
- try {
327
- return { result: deserializeCVBytes(hex), resultHex: hex };
328
- } catch {
329
- return { resultHex: hex };
330
- }
331
- }
332
- function normalizeStatus(txStatus) {
333
- switch (txStatus) {
334
- case "pending":
335
- case "success":
336
- case "abort_by_response":
337
- case "abort_by_post_condition":
338
- return txStatus;
339
- default:
340
- return txStatus.startsWith("dropped") ? "dropped" : undefined;
341
- }
342
- }
343
- function extendedApiSource() {
344
- return {
345
- async get({ client, txid }) {
346
- let data;
347
- try {
348
- data = await client.request(`/extended/v1/tx/${encodeURIComponent(normalizeTxid(txid))}`, { method: "GET" });
349
- } catch (error) {
350
- if (error instanceof HttpRequestError && error.status === 404) {
351
- return { receipt: null };
352
- }
353
- throw error;
354
- }
355
- const txStatus = typeof data?.tx_status === "string" ? data.tx_status : undefined;
356
- if (!txStatus)
357
- return { receipt: null };
358
- const status = normalizeStatus(txStatus);
359
- if (!status)
360
- return { receipt: null };
361
- return {
362
- receipt: {
363
- txid: normalizeTxid(txid),
364
- status,
365
- blockHeight: typeof data.block_height === "number" && data.block_height > 0 ? data.block_height : undefined,
366
- blockHash: data.block_hash ?? undefined,
367
- ...decodeResultHex(data.tx_result?.hex),
368
- events: Array.isArray(data.events) ? data.events : [],
369
- raw: data
370
- }
371
- };
372
- }
373
- };
374
- }
375
- function indexTxSource(params = {}) {
376
- const headers = params.apiKey ? { authorization: `Bearer ${params.apiKey}` } : undefined;
377
- return {
378
- canonicalOnly: true,
379
- async get({ client, txid }) {
380
- const request = indexRequestFn(client, params.baseUrl, "indexTxSource");
381
- let data;
382
- try {
383
- data = await request(`/v1/index/transactions/${normalizeTxid(txid)}`, {
384
- method: "GET",
385
- headers
386
- });
387
- } catch (error) {
388
- if (indexNotFound(error, "indexTxSource"))
389
- return { receipt: null };
390
- throw error;
391
- }
392
- const tx = data.transaction;
393
- const tip = data.tip?.block_height;
394
- if (!tx)
395
- return { receipt: null, tip };
396
- const status = normalizeStatus(tx.status) ?? "success";
397
- return {
398
- receipt: {
399
- txid: normalizeTxid(tx.tx_id),
400
- status,
401
- blockHeight: tx.block_height,
402
- ...decodeResultHex(tx.contract_call?.result_hex),
403
- events: [],
404
- raw: data
405
- },
406
- tip
407
- };
408
- }
409
- };
410
- }
411
-
412
- // src/actions/public/getTransaction.ts
413
- async function getTransaction(client, params) {
414
- const source = params.source ?? extendedApiSource();
415
- const { receipt } = await source.get({ client, txid: params.txid });
416
- return receipt;
417
- }
418
-
419
- // src/actions/public/waitForTransactionReceipt.ts
420
- var sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
421
- async function waitForTransactionReceipt(client, params) {
422
- const {
423
- txid,
424
- confirmations = 1,
425
- timeout = 180000,
426
- pollingInterval = 3000
427
- } = params;
428
- const source = params.source ?? extendedApiSource();
429
- const droppedGracePeriod = params.droppedGracePeriod ?? (source.canonicalOnly ? timeout : 30000);
430
- const startedAt = Date.now();
431
- let unknownSince = null;
432
- let everPending = false;
433
- while (true) {
434
- const snapshot = await source.get({ client, txid });
435
- const receipt = snapshot.receipt;
436
- if (receipt === null) {
437
- unknownSince ??= Date.now();
438
- if (Date.now() - unknownSince >= droppedGracePeriod) {
439
- throw new TransactionDroppedError(everPending ? `Transaction ${txid} left the mempool without being mined` : `Transaction ${txid} was never observed by the status source`, { txid });
440
- }
441
- } else {
442
- unknownSince = null;
443
- switch (receipt.status) {
444
- case "dropped":
445
- throw new TransactionDroppedError(`Transaction ${txid} was dropped from the mempool`, { txid });
446
- case "abort_by_response":
447
- case "abort_by_post_condition":
448
- throw new TransactionAbortedError(`Transaction ${txid} aborted (${receipt.status})`, { receipt });
449
- case "pending":
450
- everPending = true;
451
- break;
452
- case "success": {
453
- if (confirmations <= 1)
454
- return receipt;
455
- if (receipt.blockHeight !== undefined) {
456
- const tip = snapshot.tip ?? await getBlockHeight(client);
457
- if (tip - receipt.blockHeight + 1 >= confirmations)
458
- return receipt;
459
- }
460
- break;
461
- }
462
- }
463
- }
464
- if (Date.now() - startedAt >= timeout) {
465
- throw new WaitForTransactionTimeoutError(`Timed out after ${timeout}ms waiting for transaction ${txid}`, { txid });
466
- }
467
- await sleep2(pollingInterval);
468
- }
469
- }
470
-
471
- // src/actions/wallet/sendTransaction.ts
472
- async function sendTransaction(client, params) {
473
- const hex = bytesToHex(serializeTransaction(params.transaction));
474
- const body = { tx: hex };
475
- if (params.attachment) {
476
- body.attachment = typeof params.attachment === "string" ? params.attachment : bytesToHex(params.attachment);
477
- }
478
- let data;
479
- try {
480
- data = await client.request("/v2/transactions", {
481
- method: "POST",
482
- body,
483
- retryCount: 0
484
- });
485
- } catch (error) {
486
- if (error instanceof HttpRequestError && error.details) {
487
- try {
488
- data = JSON.parse(error.details);
489
- } catch {
490
- throw error;
491
- }
492
- } else if (error instanceof TimeoutError) {
493
- const txid2 = getTransactionId(params.transaction);
494
- const receipt = await getTransaction(client, { txid: txid2 }).catch(() => null);
495
- if (receipt === null)
496
- throw error;
497
- data = { txid: txid2 };
498
- } else {
499
- throw error;
500
- }
501
- }
502
- if (data.error) {
503
- throw new BroadcastError(data.reason ?? data.error, {
504
- reason: data.reason,
505
- reasonData: data.reason_data,
506
- txid: data.txid ?? getTransactionId(params.transaction)
507
- });
508
- }
509
- const txid = typeof data === "string" ? data.replace(/"/g, "") : data.txid ?? getTransactionId(params.transaction);
510
- if (params.wait) {
511
- const confirmations = params.wait === true ? 1 : params.wait;
512
- const receipt = await waitForTransactionReceipt(client, {
513
- txid,
514
- confirmations
515
- });
516
- return { txid, receipt };
517
- }
518
- return { txid };
519
- }
520
-
521
- // src/actions/wallet/nonceManager.ts
522
- function jsonRpcSource() {
523
- return {
524
- get: ({ client, address }) => getNonce(client, { address })
525
- };
526
- }
527
- function memoryStore() {
528
- const next = new Map;
529
- const locks = new Map;
530
- function withLock(key, fn) {
531
- const prev = locks.get(key) ?? Promise.resolve();
532
- const run = prev.then(fn, fn);
533
- locks.set(key, run.catch(() => {}));
534
- return run;
535
- }
536
- return {
537
- reserve(key, getFloor) {
538
- return withLock(key, async () => {
539
- let current = next.get(key);
540
- if (current === undefined)
541
- current = await getFloor();
542
- next.set(key, current + 1n);
543
- return current;
544
- });
545
- },
546
- reset(key) {
547
- next.delete(key);
548
- },
549
- release(key, nonce) {
550
- return withLock(key, async () => {
551
- if (next.get(key) === nonce + 1n)
552
- next.set(key, nonce);
553
- });
554
- },
555
- peek(key) {
556
- return next.get(key);
557
- }
558
- };
559
- }
560
- function nonceKey(client, address) {
561
- return `${client.chain?.id ?? "stacks"}:${address}`;
562
- }
563
- function createNonceManager(params = {}) {
564
- const source = params.source ?? jsonRpcSource();
565
- const store = params.store ?? memoryStore();
566
- return {
567
- consume({ client, address }) {
568
- return store.reserve(nonceKey(client, address), () => source.get({ client, address }));
569
- },
570
- reset({ client, address }) {
571
- return store.reset(nonceKey(client, address));
572
- },
573
- release({ client, address, nonce }) {
574
- return store.release?.(nonceKey(client, address), nonce);
575
- },
576
- async peek({ client, address }) {
577
- return store.peek?.(nonceKey(client, address));
578
- }
579
- };
580
- }
581
- async function resolveNonce(client, address) {
582
- if (client.nonceManager)
583
- return client.nonceManager.consume({ client, address });
584
- return getNonce(client, { address });
585
- }
586
- async function releaseNonce(client, address, nonce) {
587
- if (!client.nonceManager)
588
- return;
589
- try {
590
- await client.nonceManager.release({ client, address, nonce });
591
- } catch {}
592
- }
593
- async function reconcileNonce(manager, params) {
594
- const { client, address, source } = params;
595
- const downward = params.downward ?? true;
596
- const authoritative = await source.get({ client, address });
597
- const tracked = await manager.peek({ client, address });
598
- if (tracked === undefined || authoritative === tracked) {
599
- return { reset: false, authoritative, tracked };
600
- }
601
- if (authoritative < tracked && !downward) {
602
- return { reset: false, authoritative, tracked };
603
- }
604
- await manager.reset({ client, address });
605
- return { reset: true, authoritative, tracked };
606
- }
607
- function startNonceReconciler(manager, params) {
608
- const { client, addresses, source, clock } = params;
609
- const intervalMs = params.intervalMs ?? 60000;
610
- let running = true;
611
- const tick = async () => {
612
- if (!running)
613
- return;
614
- for (const address of addresses) {
615
- try {
616
- const result = await reconcileNonce(manager, {
617
- client,
618
- address,
619
- source,
620
- downward: params.downward
621
- });
622
- params.onReconcile?.(address, result);
623
- } catch (error) {
624
- try {
625
- params.onError?.(address, error);
626
- } catch {}
627
- }
628
- }
629
- if (running && clock) {
630
- await clock.advance(intervalMs);
631
- tick();
632
- }
633
- };
634
- if (clock) {
635
- tick();
636
- return {
637
- stop: () => {
638
- running = false;
639
- }
640
- };
641
- }
642
- const timer = setInterval(tick, intervalMs);
643
- timer.unref?.();
644
- return {
645
- stop: () => clearInterval(timer)
646
- };
647
- }
648
- function isNonceConflictError(error) {
649
- if (!(error instanceof BroadcastError))
650
- return false;
651
- if (error.reason === "ConflictingNonceInMempool" || error.reason === "BadNonce") {
652
- return true;
653
- }
654
- if (error.reason !== undefined)
655
- return false;
656
- return error.message.toLowerCase().includes("nonce");
657
- }
658
- async function broadcastWithNonceReset(client, params) {
659
- try {
660
- const result = await sendTransaction(client, {
661
- transaction: params.transaction
662
- });
663
- return result.txid;
664
- } catch (error) {
665
- if (client.nonceManager && isNonceConflictError(error)) {
666
- await client.nonceManager.reset({ client, address: params.address });
667
- }
668
- throw error;
669
- }
670
- }
671
-
672
- // src/actions/wallet/utils.ts
673
- function isProviderAccount(account) {
674
- return account.type === "provider";
675
- }
676
- var FEE_TIERS = ["min", "low", "mid", "high"];
677
- var TIER_INDEX = { low: 0, mid: 1, high: 2 };
678
- function isFeeTier(fee) {
679
- return typeof fee === "string" && FEE_TIERS.includes(fee);
680
- }
681
- function minimumFee(transaction) {
682
- return BigInt(serializeTransaction(transaction).length);
683
- }
684
- function isNoEstimateAvailable(error) {
685
- return error instanceof HttpRequestError && error.status === 400 && (error.details?.includes("NoEstimateAvailable") ?? false);
686
- }
687
- async function resolveFee(client, transaction, fee) {
688
- if (fee !== undefined && !isFeeTier(fee))
689
- return { fee: intToBigInt(fee) };
690
- const tier = fee ?? "mid";
691
- if (tier === "min")
692
- return { fee: minimumFee(transaction), tier };
693
- let estimates;
694
- try {
695
- estimates = await estimateFee(client, { transaction });
696
- } catch (error) {
697
- if (!isNoEstimateAvailable(error))
698
- throw error;
699
- return { fee: minimumFee(transaction), tier: "min" };
700
- }
701
- const pick = estimates[TIER_INDEX[tier]] ?? estimates[estimates.length - 1];
702
- if (pick)
703
- return { fee: BigInt(pick.fee), tier };
704
- return { fee: minimumFee(transaction), tier: "min" };
705
- }
706
- function setUnsignedFee(transaction, fee) {
707
- transaction.auth.spendingCondition.fee = fee;
708
- clearTxCache(transaction);
709
- }
710
- function assertNoFeeTierForProvider(fee) {
711
- if (isFeeTier(fee)) {
712
- throw new Error(`Fee tier '${fee}' requires local signing; provider (wallet) accounts set their own fee`);
713
- }
714
- }
715
-
716
- // src/actions/wallet/callContract.ts
717
- async function callContract(client, params) {
718
- const account = client.account;
719
- if (!account)
720
- throw new Error("Account required");
721
- const [contractAddress, contractName] = parseContractId(params.contract);
722
- if (isProviderAccount(account)) {
723
- assertNoFeeTierForProvider(params.fee);
724
- const result = await account.provider.request("stx_callContract", {
725
- contract: params.contract,
726
- functionName: params.functionName,
727
- functionArgs: params.functionArgs ?? []
728
- });
729
- return result.txid;
730
- }
731
- const managed = params.nonce === undefined && client.nonceManager !== undefined;
732
- const nonce = params.nonce ?? await resolveNonce(client, account.address);
733
- try {
734
- const needsFeeResolution = params.fee === undefined || isFeeTier(params.fee);
735
- const unsigned = buildContractCall({
736
- contractAddress,
737
- contractName,
738
- functionName: params.functionName,
739
- functionArgs: params.functionArgs ?? [],
740
- fee: needsFeeResolution ? 0n : params.fee,
741
- nonce,
742
- publicKey: account.publicKey,
743
- chain: client.chain,
744
- postConditionMode: params.postConditionMode,
745
- postConditions: params.postConditions
746
- });
747
- if (needsFeeResolution) {
748
- const { fee } = await resolveFee(client, unsigned, params.fee);
749
- setUnsignedFee(unsigned, fee);
750
- }
751
- const signed = await signTransactionWithAccount(unsigned, account);
752
- return await broadcastWithNonceReset(client, {
753
- transaction: signed,
754
- address: account.address
755
- });
756
- } catch (error) {
757
- if (managed)
758
- await releaseNonce(client, account.address, nonce);
759
- throw error;
760
- }
761
- }
762
-
763
- //# debugId=C746B6AF7E1C8C7C64756E2164756E21
764
- //# sourceMappingURL=stacks-37kz5t9f.cjs.map