@gardenfi/utils 2.0.3 → 2.0.4

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 (113) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +22 -17
  3. package/dist/index10.cjs +1 -1
  4. package/dist/index10.js +32 -17
  5. package/dist/index11.cjs +1 -1
  6. package/dist/index11.js +14 -183
  7. package/dist/index12.cjs +1 -1
  8. package/dist/index12.js +186 -92
  9. package/dist/index13.cjs +1 -16
  10. package/dist/index13.js +93 -113
  11. package/dist/index14.cjs +16 -1
  12. package/dist/index14.js +112 -30
  13. package/dist/index15.cjs +1 -1
  14. package/dist/index15.js +18 -17
  15. package/dist/index16.cjs +1 -1
  16. package/dist/index16.js +31 -18
  17. package/dist/index17.cjs +1 -1
  18. package/dist/index17.js +17 -7
  19. package/dist/index18.cjs +1 -1
  20. package/dist/index18.js +6 -19
  21. package/dist/index19.cjs +1 -1
  22. package/dist/index19.js +20 -21
  23. package/dist/index20.cjs +1 -1
  24. package/dist/index20.js +21 -8
  25. package/dist/index21.cjs +1 -1
  26. package/dist/index21.js +8 -31
  27. package/dist/index22.cjs +1 -1
  28. package/dist/index22.js +31 -26
  29. package/dist/index23.cjs +1 -1
  30. package/dist/index23.js +25 -22
  31. package/dist/index24.cjs +1 -1
  32. package/dist/index24.js +22 -50
  33. package/dist/index25.cjs +1 -1
  34. package/dist/index25.js +51 -12
  35. package/dist/index26.cjs +1 -1
  36. package/dist/index26.js +12 -24
  37. package/dist/index27.cjs +1 -1
  38. package/dist/index27.js +24 -75
  39. package/dist/index28.cjs +1 -1
  40. package/dist/index28.js +74 -22
  41. package/dist/index29.cjs +1 -1
  42. package/dist/index29.js +23 -7
  43. package/dist/index30.cjs +1 -1
  44. package/dist/index30.js +7 -32
  45. package/dist/index31.cjs +1 -1
  46. package/dist/index31.js +31 -14
  47. package/dist/index32.cjs +1 -1
  48. package/dist/index32.js +16 -2
  49. package/dist/index33.cjs +1 -1
  50. package/dist/index33.js +2 -9
  51. package/dist/index34.cjs +1 -1
  52. package/dist/index34.js +8 -20
  53. package/dist/index35.cjs +1 -1
  54. package/dist/index35.js +20 -12
  55. package/dist/index36.cjs +1 -1
  56. package/dist/index36.js +10 -32
  57. package/dist/index37.cjs +1 -2
  58. package/dist/index37.js +33 -58
  59. package/dist/index38.cjs +2 -1
  60. package/dist/index38.js +60 -2
  61. package/dist/index39.cjs +1 -1
  62. package/dist/index39.js +2 -5
  63. package/dist/index4.cjs +1 -1
  64. package/dist/index4.js +2 -2
  65. package/dist/index40.cjs +1 -1
  66. package/dist/index40.js +4 -11
  67. package/dist/index41.cjs +1 -1
  68. package/dist/index41.js +11 -3
  69. package/dist/index42.cjs +1 -1
  70. package/dist/index42.js +3 -21
  71. package/dist/index43.cjs +1 -1
  72. package/dist/index43.js +21 -12
  73. package/dist/index44.cjs +1 -1
  74. package/dist/index44.js +11 -54
  75. package/dist/index45.cjs +1 -1
  76. package/dist/index45.js +55 -6
  77. package/dist/index46.cjs +1 -1
  78. package/dist/index46.js +6 -21
  79. package/dist/index47.cjs +1 -1
  80. package/dist/index47.js +21 -11
  81. package/dist/index48.cjs +1 -1
  82. package/dist/index48.js +11 -3
  83. package/dist/index49.cjs +1 -1
  84. package/dist/index49.js +3 -11
  85. package/dist/index5.cjs +1 -1
  86. package/dist/index5.js +43 -19
  87. package/dist/index50.cjs +1 -1
  88. package/dist/index50.js +11 -19
  89. package/dist/index51.cjs +1 -1
  90. package/dist/index51.js +19 -6
  91. package/dist/index52.cjs +1 -1
  92. package/dist/index52.js +6 -8
  93. package/dist/index53.cjs +1 -1
  94. package/dist/index53.js +8 -104
  95. package/dist/index54.cjs +1 -1
  96. package/dist/index54.js +102 -26
  97. package/dist/index55.cjs +1 -1
  98. package/dist/index55.js +27 -17
  99. package/dist/index56.cjs +1 -1
  100. package/dist/index56.js +16 -36
  101. package/dist/index57.cjs +1 -0
  102. package/dist/index57.js +41 -0
  103. package/dist/index6.cjs +1 -1
  104. package/dist/index6.js +19 -104
  105. package/dist/index7.cjs +1 -1
  106. package/dist/index7.js +104 -10
  107. package/dist/index8.cjs +1 -1
  108. package/dist/index8.js +15 -2
  109. package/dist/index9.cjs +1 -1
  110. package/dist/index9.js +2 -34
  111. package/dist/src/index.d.ts +1 -0
  112. package/dist/src/lib/result.d.ts +16 -0
  113. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./index2.cjs"),e=require("./index3.cjs"),i=require("./index4.cjs"),n=require("./index5.cjs"),r=require("./index6.cjs"),c=require("./index7.cjs"),s=require("./index8.cjs"),t=require("./index9.cjs"),u=require("./index10.cjs");exports.ApiStatus=o.ApiStatus;exports.Authorization=e.Authorization;exports.Environment=e.Environment;exports.Network=e.Network;exports.sleep=e.sleep;exports.with0x=e.with0x;exports.checkAllowanceAndApprove=i.checkAllowanceAndApprove;exports.Url=n.Url;exports.Auth=r.Auth;exports.Siwe=r.Siwe;exports.parseJwt=r.parseJwt;exports.MemoryStorage=c.MemoryStorage;exports.StoreKeys=s.StoreKeys;exports.fetchBitcoinBlockNumber=t.fetchBitcoinBlockNumber;exports.fetchEVMBlockNumber=t.fetchEVMBlockNumber;exports.EventBroker=u.EventBroker;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./index2.cjs"),e=require("./index3.cjs"),n=require("./index4.cjs"),r=require("./index5.cjs"),c=require("./index6.cjs"),t=require("./index7.cjs"),s=require("./index8.cjs"),u=require("./index9.cjs"),o=require("./index10.cjs"),l=require("./index11.cjs");exports.ApiStatus=i.ApiStatus;exports.Authorization=e.Authorization;exports.Environment=e.Environment;exports.Network=e.Network;exports.sleep=e.sleep;exports.with0x=e.with0x;exports.checkAllowanceAndApprove=n.checkAllowanceAndApprove;exports.Err=r.Err;exports.Ok=r.Ok;exports.Result=r.Result;exports.Void=r.Void;exports.Url=c.Url;exports.Auth=t.Auth;exports.Siwe=t.Siwe;exports.parseJwt=t.parseJwt;exports.MemoryStorage=s.MemoryStorage;exports.StoreKeys=u.StoreKeys;exports.fetchBitcoinBlockNumber=o.fetchBitcoinBlockNumber;exports.fetchEVMBlockNumber=o.fetchEVMBlockNumber;exports.EventBroker=l.EventBroker;
package/dist/index.js CHANGED
@@ -1,27 +1,32 @@
1
1
  import { ApiStatus as e } from "./index2.js";
2
- import { Authorization as p, Environment as m, Network as f, sleep as x, with0x as c } from "./index3.js";
2
+ import { Authorization as p, Environment as m, Network as f, sleep as x, with0x as i } from "./index3.js";
3
3
  import { checkAllowanceAndApprove as n } from "./index4.js";
4
- import { Url as l } from "./index5.js";
5
- import { Auth as a, Siwe as k, parseJwt as u } from "./index6.js";
6
- import { MemoryStorage as s } from "./index7.js";
7
- import { StoreKeys as S } from "./index8.js";
8
- import { fetchBitcoinBlockNumber as E, fetchEVMBlockNumber as N } from "./index9.js";
9
- import { EventBroker as y } from "./index10.js";
4
+ import { Err as h, Ok as k, Result as u, Void as A } from "./index5.js";
5
+ import { Url as s } from "./index6.js";
6
+ import { Auth as B, Siwe as E, parseJwt as S } from "./index7.js";
7
+ import { MemoryStorage as N } from "./index8.js";
8
+ import { StoreKeys as d } from "./index9.js";
9
+ import { fetchBitcoinBlockNumber as M, fetchEVMBlockNumber as V } from "./index10.js";
10
+ import { EventBroker as z } from "./index11.js";
10
11
  export {
11
12
  e as ApiStatus,
12
- a as Auth,
13
+ B as Auth,
13
14
  p as Authorization,
14
15
  m as Environment,
15
- y as EventBroker,
16
- s as MemoryStorage,
16
+ h as Err,
17
+ z as EventBroker,
18
+ N as MemoryStorage,
17
19
  f as Network,
18
- k as Siwe,
19
- S as StoreKeys,
20
- l as Url,
20
+ k as Ok,
21
+ u as Result,
22
+ E as Siwe,
23
+ d as StoreKeys,
24
+ s as Url,
25
+ A as Void,
21
26
  n as checkAllowanceAndApprove,
22
- E as fetchBitcoinBlockNumber,
23
- N as fetchEVMBlockNumber,
24
- u as parseJwt,
27
+ M as fetchBitcoinBlockNumber,
28
+ V as fetchEVMBlockNumber,
29
+ S as parseJwt,
25
30
  x as sleep,
26
- c as with0x
31
+ i as with0x
27
32
  };
package/dist/index10.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class n{constructor(){this.listeners=new Map}emit(e,...t){(this.listeners.get(e)??[]).forEach(i=>{i(...t)})}on(e,t){const s=this.listeners.get(e)??[];s.push(t),this.listeners.set(e,s)}off(e,t){const s=this.listeners.get(e)??[],i=s.indexOf(t);i!==-1&&s.splice(i,1)}}exports.EventBroker=n;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./index12.cjs"),n=require("viem"),s=require("./index15.cjs"),l=require("./index16.cjs"),o={42161:s.mainnet,421614:l.sepolia},b=async e=>{const{chain:t}=e;if(!t)return r.Err("No chain found");const a=(u=>u in o)(t.id)?o[t.id]:t;return await i(a)},i=async e=>{const t=n.createPublicClient({chain:e,transport:n.http()});try{const c=await t.getBlockNumber();return r.Ok(Number(c))}catch(c){return r.Err("Failed to fetch evm block number",c)}},m=async e=>{try{const t=await e.getLatestTip();return r.Ok(t)}catch(t){return r.Err("Failed to fetch bitcoin block number",t)}};exports._fetchEVMBlockNumber=i;exports.fetchBitcoinBlockNumber=m;exports.fetchEVMBlockNumber=b;
package/dist/index10.js CHANGED
@@ -1,21 +1,36 @@
1
- class r {
2
- constructor() {
3
- this.listeners = /* @__PURE__ */ new Map();
1
+ import { Err as o, Ok as n } from "./index12.js";
2
+ import { createPublicClient as s, http as m } from "viem";
3
+ import { mainnet as u } from "./index15.js";
4
+ import { sepolia as b } from "./index16.js";
5
+ const e = {
6
+ 42161: u,
7
+ 421614: b
8
+ }, N = async (r) => {
9
+ const { chain: t } = r;
10
+ if (!t) return o("No chain found");
11
+ const i = ((a) => a in e)(t.id) ? e[t.id] : t;
12
+ return await l(i);
13
+ }, l = async (r) => {
14
+ const t = s({
15
+ chain: r,
16
+ transport: m()
17
+ });
18
+ try {
19
+ const c = await t.getBlockNumber();
20
+ return n(Number(c));
21
+ } catch (c) {
22
+ return o("Failed to fetch evm block number", c);
4
23
  }
5
- emit(s, ...t) {
6
- (this.listeners.get(s) ?? []).forEach((i) => {
7
- i(...t);
8
- });
24
+ }, C = async (r) => {
25
+ try {
26
+ const t = await r.getLatestTip();
27
+ return n(t);
28
+ } catch (t) {
29
+ return o("Failed to fetch bitcoin block number", t);
9
30
  }
10
- on(s, t) {
11
- const e = this.listeners.get(s) ?? [];
12
- e.push(t), this.listeners.set(s, e);
13
- }
14
- off(s, t) {
15
- const e = this.listeners.get(s) ?? [], i = e.indexOf(t);
16
- i !== -1 && e.splice(i, 1);
17
- }
18
- }
31
+ };
19
32
  export {
20
- r as EventBroker
33
+ l as _fetchEVMBlockNumber,
34
+ C as fetchBitcoinBlockNumber,
35
+ N as fetchEVMBlockNumber
21
36
  };
package/dist/index11.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var p=(a,t,e)=>{if(!t.has(a))throw TypeError("Cannot "+e)},u=(a,t,e)=>(p(a,t,"read from private field"),e?e.call(a):t.get(a)),w=(a,t,e)=>{if(t.has(a))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(a):t.set(a,e)},f=(a,t,e,r)=>(p(a,t,"write to private field"),t.set(a,e),e);const d=a=>new Promise(t=>setTimeout(t,a));class g{constructor(t,e){this.maxRetries=Math.max(t,0),this.delay=e}async retry(t){let e=0,r;for(;e<this.maxRetries+1;)try{return await t()}catch(s){e++,r=s,await d(this.delay*e)}throw r}}const v=a=>{try{return JSON.parse(a)}catch{return a}};class m{static async _postWithFallback(t,e){let r="";for(const s of t)try{const i=await fetch(s,{method:"POST",...e});if(i.status>=500){r=await(i.text()||i.json());continue}return await this.parse(i)}catch(i){r=y(i);continue}throw new Error(r||"All APIs failed")}static async postWithFallback(t,e){return await n(e).retry(()=>this._postWithFallback(t,e))}static async _getWithFallback(t,e){let r="";for(const s of t)try{const i=await fetch(s,e);if(i.status>=500){r=await(i.text()||i.json());continue}return await this.parse(i)}catch(i){r=y(i);continue}throw new Error(r||"All APIs failed")}static async getWithFallback(t,e){return await n(e).retry(()=>this._getWithFallback(t,e))}static async _get(t,e){return await this.parse(await fetch(t,e))}static async get(t,e){return await n(e).retry(()=>this._get(t,e))}static async _post(t,e){return await this.parse(await fetch(t,{method:"POST",...e}))}static async post(t,e){return await n(e).retry(()=>this._post(t,e))}static async parse(t){const e=await t.text();if(t.status>=200&&t.status<300)return v(e);throw new Error(e)}}function y(a){return(a==null?void 0:a.message)||(a==null?void 0:a.toString())||"unknown error"}const n=a=>new g((a==null?void 0:a.retryCount)??2,(a==null?void 0:a.retryDelay)??1e3);var o,c,l;class h{constructor(t,e,r=void 0){w(this,o,void 0),w(this,c,void 0),w(this,l,void 0),f(this,o,t),f(this,l,r),f(this,c,e)}get ok(){return u(this,o)}get error(){return u(this,l)}get val(){return u(this,c)}}o=new WeakMap,c=new WeakMap,l=new WeakMap;const k=a=>new h(!0,a),W=(a,...t)=>{if(typeof a=="string"&&t&&t.length>0){let e=[a,...t].map(r=>{if(r){if(r instanceof Error)return r.message;if(typeof r=="string")return r;if(r!=null&&r.toString)return r.toString()}});return new h(!1,null,e.filter(r=>r!==void 0).join(" "))}return new h(!1,null,a)};exports.Err=W;exports.Fetcher=m;exports.Ok=k;exports.Result=h;exports.Retry=g;exports.safeParseJson=v;exports.sleep=d;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class n{constructor(){this.listeners=new Map}emit(e,...t){(this.listeners.get(e)??[]).forEach(i=>{i(...t)})}on(e,t){const s=this.listeners.get(e)??[];s.push(t),this.listeners.set(e,s)}off(e,t){const s=this.listeners.get(e)??[],i=s.indexOf(t);i!==-1&&s.splice(i,1)}}exports.EventBroker=n;
package/dist/index11.js CHANGED
@@ -1,190 +1,21 @@
1
- var p = (e, t, a) => {
2
- if (!t.has(e))
3
- throw TypeError("Cannot " + a);
4
- }, h = (e, t, a) => (p(e, t, "read from private field"), a ? a.call(e) : t.get(e)), u = (e, t, a) => {
5
- if (t.has(e))
6
- throw TypeError("Cannot add the same private member more than once");
7
- t instanceof WeakSet ? t.add(e) : t.set(e, a);
8
- }, w = (e, t, a, r) => (p(e, t, "write to private field"), t.set(e, a), a);
9
- const d = (e) => new Promise((t) => setTimeout(t, e));
10
- class g {
11
- /**
12
- * @param {number} maxRetries - The maximum number of retries, if less < 0 then it is set to 0
13
- * @param {number} delay - The delay between retries
14
- */
15
- constructor(t, a) {
16
- this.maxRetries = Math.max(t, 0), this.delay = a;
1
+ class r {
2
+ constructor() {
3
+ this.listeners = /* @__PURE__ */ new Map();
17
4
  }
18
- /**
19
- * Retries a function until it succeeds or the max number of retries is reached
20
- *
21
- * @param {() => Promise<T>} fn - The function to retry
22
- * @return {Promise<T>} a Promise that resolves to the result of the function
23
- */
24
- async retry(t) {
25
- let a = 0, r;
26
- for (; a < this.maxRetries + 1; )
27
- try {
28
- return await t();
29
- } catch (i) {
30
- a++, r = i, await d(this.delay * a);
31
- }
32
- throw r;
33
- }
34
- }
35
- const v = (e) => {
36
- try {
37
- return JSON.parse(e);
38
- } catch {
39
- return e;
40
- }
41
- };
42
- class m {
43
- static async _postWithFallback(t, a) {
44
- let r = "";
45
- for (const i of t)
46
- try {
47
- const s = await fetch(i, {
48
- method: "POST",
49
- ...a
50
- });
51
- if (s.status >= 500) {
52
- r = await (s.text() || s.json());
53
- continue;
54
- }
55
- return await this.parse(s);
56
- } catch (s) {
57
- r = y(s);
58
- continue;
59
- }
60
- throw new Error(r || "All APIs failed");
61
- }
62
- /**
63
- * Asynchronously sends a POST request to multiple URLs with fallback logic.
64
- *
65
- * @param {string[]} input - array of URLs to send the POST request to
66
- * @param {RequestInit} [init] - optional request initialization options
67
- * @return {Promise<T>} a Promise that resolves to the parsed response data
68
- */
69
- static async postWithFallback(t, a) {
70
- return await n(a).retry(
71
- () => this._postWithFallback(t, a)
72
- );
73
- }
74
- static async _getWithFallback(t, a) {
75
- let r = "";
76
- for (const i of t)
77
- try {
78
- const s = await fetch(i, a);
79
- if (s.status >= 500) {
80
- r = await (s.text() || s.json());
81
- continue;
82
- }
83
- return await this.parse(s);
84
- } catch (s) {
85
- r = y(s);
86
- continue;
87
- }
88
- throw new Error(r || "All APIs failed");
89
- }
90
- /**
91
- * Asynchronously sends a POST request to multiple URLs with fallback mechanism.
92
- *
93
- * @param {string[]} input - An array of URLs to retrieve data from.
94
- * @param {RequestInit} [init] - Optional request options.
95
- * @return {Promise<T>} A promise that resolves to the retrieved data.
96
- */
97
- static async getWithFallback(t, a) {
98
- return await n(a).retry(
99
- () => this._getWithFallback(t, a)
100
- );
101
- }
102
- static async _get(t, a) {
103
- return await this.parse(await fetch(t, a));
104
- }
105
- /**
106
- * Asynchronously retrieves data of type T from the specified URL or RequestInfo, with optional initialization options.
107
- *
108
- * @param {RequestInfo | URL} input - The URL or RequestInfo to fetch data from
109
- * @param {RequestInit} init - Optional initialization options for the fetch request
110
- * @return {Promise<T>} The retrieved data of type T
111
- */
112
- static async get(t, a) {
113
- return await n(a).retry(() => this._get(t, a));
114
- }
115
- static async _post(t, a) {
116
- return await this.parse(
117
- await fetch(t, {
118
- method: "POST",
119
- ...a
120
- })
121
- );
122
- }
123
- /**
124
- * Asynchronously sends a POST request to the specified URL or RequestInfo, with optional initialization options.
125
- */
126
- static async post(t, a) {
127
- return await n(a).retry(() => this._post(t, a));
128
- }
129
- /**
130
- * Parses the response and returns the result as the specified type.
131
- *
132
- * @param {Response} res - the response object to be parsed
133
- * @return {Promise<T>} the parsed result of type T
134
- */
135
- static async parse(t) {
136
- const a = await t.text();
137
- if (t.status >= 200 && t.status < 300)
138
- return v(a);
139
- throw new Error(a);
140
- }
141
- }
142
- function y(e) {
143
- return (e == null ? void 0 : e.message) || (e == null ? void 0 : e.toString()) || "unknown error";
144
- }
145
- const n = (e) => new g((e == null ? void 0 : e.retryCount) ?? 2, (e == null ? void 0 : e.retryDelay) ?? 1e3);
146
- var o, c, l;
147
- class f {
148
- constructor(t, a, r = void 0) {
149
- u(this, o, void 0), u(this, c, void 0), u(this, l, void 0), w(this, o, t), w(this, l, r), w(this, c, a);
150
- }
151
- get ok() {
152
- return h(this, o);
5
+ emit(s, ...t) {
6
+ (this.listeners.get(s) ?? []).forEach((i) => {
7
+ i(...t);
8
+ });
153
9
  }
154
- get error() {
155
- return h(this, l);
10
+ on(s, t) {
11
+ const e = this.listeners.get(s) ?? [];
12
+ e.push(t), this.listeners.set(s, e);
156
13
  }
157
- get val() {
158
- return h(this, c);
14
+ off(s, t) {
15
+ const e = this.listeners.get(s) ?? [], i = e.indexOf(t);
16
+ i !== -1 && e.splice(i, 1);
159
17
  }
160
18
  }
161
- o = /* @__PURE__ */ new WeakMap(), c = /* @__PURE__ */ new WeakMap(), l = /* @__PURE__ */ new WeakMap();
162
- const k = (e) => new f(!0, e), W = (e, ...t) => {
163
- if (typeof e == "string" && t && t.length > 0) {
164
- let a = [e, ...t].map((r) => {
165
- if (r) {
166
- if (r instanceof Error)
167
- return r.message;
168
- if (typeof r == "string")
169
- return r;
170
- if (r != null && r.toString)
171
- return r.toString();
172
- }
173
- });
174
- return new f(
175
- !1,
176
- null,
177
- a.filter((r) => r !== void 0).join(" ")
178
- );
179
- }
180
- return new f(!1, null, e);
181
- };
182
19
  export {
183
- W as Err,
184
- m as Fetcher,
185
- k as Ok,
186
- f as Result,
187
- g as Retry,
188
- v as safeParseJson,
189
- d as sleep
20
+ r as EventBroker
190
21
  };
package/dist/index12.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const M=require("./index17.cjs"),k=require("./index23.cjs"),g=require("./index29.cjs"),O=require("./index30.cjs"),S=require("./index33.cjs"),y=require("./index34.cjs"),$=require("./index32.cjs"),P=require("./index16.cjs"),W=require("./index22.cjs"),q=require("./index25.cjs"),x=require("./index27.cjs");async function z(n,{confirmations:c=1,hash:p,onReplaced:h,pollingInterval:m=n.pollingInterval,retryCount:w=6,retryDelay:R=({count:d})=>~~(1<<d)*200,timeout:N=18e4}){const d=$.stringify(["waitForTransactionReceipt",n.uid,p]);let a,o,e,i=!1;const{promise:B,resolve:F,reject:v}=S.withResolvers(),A=N?setTimeout(()=>v(new k.WaitForTransactionReceiptTimeoutError({hash:p})),N):void 0,j=O.observe(d,{onReplaced:h,resolve:F,reject:v},r=>{const E=g.getAction(n,x.watchBlockNumber,"watchBlockNumber")({emitMissed:!0,emitOnBegin:!0,poll:!0,pollingInterval:m,async onBlockNumber(_){const u=b=>{clearTimeout(A),E(),b(),j()};let l=_;if(!i)try{if(e){if(c>1&&(!e.blockNumber||l-e.blockNumber+1n<c))return;u(()=>r.resolve(e));return}if(a||(i=!0,await y.withRetry(async()=>{a=await g.getAction(n,W.getTransaction,"getTransaction")({hash:p}),a.blockNumber&&(l=a.blockNumber)},{delay:R,retryCount:w}),i=!1),e=await g.getAction(n,q.getTransactionReceipt,"getTransactionReceipt")({hash:p}),c>1&&(!e.blockNumber||l-e.blockNumber+1n<c))return;u(()=>r.resolve(e))}catch(b){if(b instanceof k.TransactionNotFoundError||b instanceof k.TransactionReceiptNotFoundError){if(!a){i=!1;return}try{o=a,i=!0;const f=await y.withRetry(()=>g.getAction(n,P.getBlock,"getBlock")({blockNumber:l,includeTransactions:!0}),{delay:R,retryCount:w,shouldRetry:({error:s})=>s instanceof M.BlockNotFoundError});i=!1;const t=f.transactions.find(({from:s,nonce:I})=>s===o.from&&I===o.nonce);if(!t||(e=await g.getAction(n,q.getTransactionReceipt,"getTransactionReceipt")({hash:t.hash}),c>1&&(!e.blockNumber||l-e.blockNumber+1n<c)))return;let T="replaced";t.to===o.to&&t.value===o.value&&t.input===o.input?T="repriced":t.from===t.to&&t.value===0n&&(T="cancelled"),u(()=>{var s;(s=r.onReplaced)==null||s.call(r,{reason:T,replacedTransaction:o,transaction:t,transactionReceipt:e}),r.resolve(e)})}catch(f){u(()=>r.reject(f))}}else u(()=>r.reject(b))}}})});return B}exports.waitForTransactionReceipt=z;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var p=(a,t,e)=>{if(!t.has(a))throw TypeError("Cannot "+e)},u=(a,t,e)=>(p(a,t,"read from private field"),e?e.call(a):t.get(a)),w=(a,t,e)=>{if(t.has(a))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(a):t.set(a,e)},f=(a,t,e,r)=>(p(a,t,"write to private field"),t.set(a,e),e);const d=a=>new Promise(t=>setTimeout(t,a));class g{constructor(t,e){this.maxRetries=Math.max(t,0),this.delay=e}async retry(t){let e=0,r;for(;e<this.maxRetries+1;)try{return await t()}catch(s){e++,r=s,await d(this.delay*e)}throw r}}const v=a=>{try{return JSON.parse(a)}catch{return a}};class m{static async _postWithFallback(t,e){let r="";for(const s of t)try{const i=await fetch(s,{method:"POST",...e});if(i.status>=500){r=await(i.text()||i.json());continue}return await this.parse(i)}catch(i){r=y(i);continue}throw new Error(r||"All APIs failed")}static async postWithFallback(t,e){return await n(e).retry(()=>this._postWithFallback(t,e))}static async _getWithFallback(t,e){let r="";for(const s of t)try{const i=await fetch(s,e);if(i.status>=500){r=await(i.text()||i.json());continue}return await this.parse(i)}catch(i){r=y(i);continue}throw new Error(r||"All APIs failed")}static async getWithFallback(t,e){return await n(e).retry(()=>this._getWithFallback(t,e))}static async _get(t,e){return await this.parse(await fetch(t,e))}static async get(t,e){return await n(e).retry(()=>this._get(t,e))}static async _post(t,e){return await this.parse(await fetch(t,{method:"POST",...e}))}static async post(t,e){return await n(e).retry(()=>this._post(t,e))}static async parse(t){const e=await t.text();if(t.status>=200&&t.status<300)return v(e);throw new Error(e)}}function y(a){return(a==null?void 0:a.message)||(a==null?void 0:a.toString())||"unknown error"}const n=a=>new g((a==null?void 0:a.retryCount)??2,(a==null?void 0:a.retryDelay)??1e3);var o,c,l;class h{constructor(t,e,r=void 0){w(this,o,void 0),w(this,c,void 0),w(this,l,void 0),f(this,o,t),f(this,l,r),f(this,c,e)}get ok(){return u(this,o)}get error(){return u(this,l)}get val(){return u(this,c)}}o=new WeakMap,c=new WeakMap,l=new WeakMap;const k=a=>new h(!0,a),W=(a,...t)=>{if(typeof a=="string"&&t&&t.length>0){let e=[a,...t].map(r=>{if(r){if(r instanceof Error)return r.message;if(typeof r=="string")return r;if(r!=null&&r.toString)return r.toString()}});return new h(!1,null,e.filter(r=>r!==void 0).join(" "))}return new h(!1,null,a)};exports.Err=W;exports.Fetcher=m;exports.Ok=k;exports.Result=h;exports.Retry=g;exports.safeParseJson=v;exports.sleep=d;
package/dist/index12.js CHANGED
@@ -1,96 +1,190 @@
1
- import { BlockNotFoundError as A } from "./index17.js";
2
- import { WaitForTransactionReceiptTimeoutError as M, TransactionNotFoundError as O, TransactionReceiptNotFoundError as W } from "./index23.js";
3
- import { getAction as f } from "./index29.js";
4
- import { observe as q } from "./index30.js";
5
- import { withResolvers as z } from "./index33.js";
6
- import { withRetry as v } from "./index34.js";
7
- import { stringify as G } from "./index32.js";
8
- import { getBlock as H } from "./index16.js";
9
- import { getTransaction as J } from "./index22.js";
10
- import { getTransactionReceipt as R } from "./index25.js";
11
- import { watchBlockNumber as K } from "./index27.js";
12
- async function D(o, {
13
- confirmations: a = 1,
14
- hash: m,
15
- onReplaced: y,
16
- pollingInterval: B = o.pollingInterval,
17
- retryCount: k = 6,
18
- retryDelay: N = ({ count: b }) => ~~(1 << b) * 200,
19
- // exponential backoff
20
- timeout: w = 18e4
21
- }) {
22
- const b = G(["waitForTransactionReceipt", o.uid, m]);
23
- let c, n, r, i = !1;
24
- const { promise: h, resolve: F, reject: g } = z(), E = w ? setTimeout(() => g(new M({ hash: m })), w) : void 0, _ = q(b, { onReplaced: y, resolve: F, reject: g }, (t) => {
25
- const j = f(o, K, "watchBlockNumber")({
26
- emitMissed: !0,
27
- emitOnBegin: !0,
28
- poll: !0,
29
- pollingInterval: B,
30
- async onBlockNumber(I) {
31
- const l = (p) => {
32
- clearTimeout(E), j(), p(), _();
33
- };
34
- let u = I;
35
- if (!i)
36
- try {
37
- if (r) {
38
- if (a > 1 && (!r.blockNumber || u - r.blockNumber + 1n < a))
39
- return;
40
- l(() => t.resolve(r));
41
- return;
42
- }
43
- if (c || (i = !0, await v(async () => {
44
- c = await f(o, J, "getTransaction")({ hash: m }), c.blockNumber && (u = c.blockNumber);
45
- }, {
46
- delay: N,
47
- retryCount: k
48
- }), i = !1), r = await f(o, R, "getTransactionReceipt")({ hash: m }), a > 1 && (!r.blockNumber || u - r.blockNumber + 1n < a))
49
- return;
50
- l(() => t.resolve(r));
51
- } catch (p) {
52
- if (p instanceof O || p instanceof W) {
53
- if (!c) {
54
- i = !1;
55
- return;
56
- }
57
- try {
58
- n = c, i = !0;
59
- const d = await v(() => f(o, H, "getBlock")({
60
- blockNumber: u,
61
- includeTransactions: !0
62
- }), {
63
- delay: N,
64
- retryCount: k,
65
- shouldRetry: ({ error: s }) => s instanceof A
66
- });
67
- i = !1;
68
- const e = d.transactions.find(({ from: s, nonce: x }) => s === n.from && x === n.nonce);
69
- if (!e || (r = await f(o, R, "getTransactionReceipt")({
70
- hash: e.hash
71
- }), a > 1 && (!r.blockNumber || u - r.blockNumber + 1n < a)))
72
- return;
73
- let T = "replaced";
74
- e.to === n.to && e.value === n.value && e.input === n.input ? T = "repriced" : e.from === e.to && e.value === 0n && (T = "cancelled"), l(() => {
75
- var s;
76
- (s = t.onReplaced) == null || s.call(t, {
77
- reason: T,
78
- replacedTransaction: n,
79
- transaction: e,
80
- transactionReceipt: r
81
- }), t.resolve(r);
82
- });
83
- } catch (d) {
84
- l(() => t.reject(d));
85
- }
86
- } else
87
- l(() => t.reject(p));
88
- }
1
+ var p = (e, t, a) => {
2
+ if (!t.has(e))
3
+ throw TypeError("Cannot " + a);
4
+ }, h = (e, t, a) => (p(e, t, "read from private field"), a ? a.call(e) : t.get(e)), u = (e, t, a) => {
5
+ if (t.has(e))
6
+ throw TypeError("Cannot add the same private member more than once");
7
+ t instanceof WeakSet ? t.add(e) : t.set(e, a);
8
+ }, w = (e, t, a, r) => (p(e, t, "write to private field"), t.set(e, a), a);
9
+ const d = (e) => new Promise((t) => setTimeout(t, e));
10
+ class g {
11
+ /**
12
+ * @param {number} maxRetries - The maximum number of retries, if less < 0 then it is set to 0
13
+ * @param {number} delay - The delay between retries
14
+ */
15
+ constructor(t, a) {
16
+ this.maxRetries = Math.max(t, 0), this.delay = a;
17
+ }
18
+ /**
19
+ * Retries a function until it succeeds or the max number of retries is reached
20
+ *
21
+ * @param {() => Promise<T>} fn - The function to retry
22
+ * @return {Promise<T>} a Promise that resolves to the result of the function
23
+ */
24
+ async retry(t) {
25
+ let a = 0, r;
26
+ for (; a < this.maxRetries + 1; )
27
+ try {
28
+ return await t();
29
+ } catch (i) {
30
+ a++, r = i, await d(this.delay * a);
89
31
  }
90
- });
91
- });
92
- return h;
32
+ throw r;
33
+ }
34
+ }
35
+ const v = (e) => {
36
+ try {
37
+ return JSON.parse(e);
38
+ } catch {
39
+ return e;
40
+ }
41
+ };
42
+ class m {
43
+ static async _postWithFallback(t, a) {
44
+ let r = "";
45
+ for (const i of t)
46
+ try {
47
+ const s = await fetch(i, {
48
+ method: "POST",
49
+ ...a
50
+ });
51
+ if (s.status >= 500) {
52
+ r = await (s.text() || s.json());
53
+ continue;
54
+ }
55
+ return await this.parse(s);
56
+ } catch (s) {
57
+ r = y(s);
58
+ continue;
59
+ }
60
+ throw new Error(r || "All APIs failed");
61
+ }
62
+ /**
63
+ * Asynchronously sends a POST request to multiple URLs with fallback logic.
64
+ *
65
+ * @param {string[]} input - array of URLs to send the POST request to
66
+ * @param {RequestInit} [init] - optional request initialization options
67
+ * @return {Promise<T>} a Promise that resolves to the parsed response data
68
+ */
69
+ static async postWithFallback(t, a) {
70
+ return await n(a).retry(
71
+ () => this._postWithFallback(t, a)
72
+ );
73
+ }
74
+ static async _getWithFallback(t, a) {
75
+ let r = "";
76
+ for (const i of t)
77
+ try {
78
+ const s = await fetch(i, a);
79
+ if (s.status >= 500) {
80
+ r = await (s.text() || s.json());
81
+ continue;
82
+ }
83
+ return await this.parse(s);
84
+ } catch (s) {
85
+ r = y(s);
86
+ continue;
87
+ }
88
+ throw new Error(r || "All APIs failed");
89
+ }
90
+ /**
91
+ * Asynchronously sends a POST request to multiple URLs with fallback mechanism.
92
+ *
93
+ * @param {string[]} input - An array of URLs to retrieve data from.
94
+ * @param {RequestInit} [init] - Optional request options.
95
+ * @return {Promise<T>} A promise that resolves to the retrieved data.
96
+ */
97
+ static async getWithFallback(t, a) {
98
+ return await n(a).retry(
99
+ () => this._getWithFallback(t, a)
100
+ );
101
+ }
102
+ static async _get(t, a) {
103
+ return await this.parse(await fetch(t, a));
104
+ }
105
+ /**
106
+ * Asynchronously retrieves data of type T from the specified URL or RequestInfo, with optional initialization options.
107
+ *
108
+ * @param {RequestInfo | URL} input - The URL or RequestInfo to fetch data from
109
+ * @param {RequestInit} init - Optional initialization options for the fetch request
110
+ * @return {Promise<T>} The retrieved data of type T
111
+ */
112
+ static async get(t, a) {
113
+ return await n(a).retry(() => this._get(t, a));
114
+ }
115
+ static async _post(t, a) {
116
+ return await this.parse(
117
+ await fetch(t, {
118
+ method: "POST",
119
+ ...a
120
+ })
121
+ );
122
+ }
123
+ /**
124
+ * Asynchronously sends a POST request to the specified URL or RequestInfo, with optional initialization options.
125
+ */
126
+ static async post(t, a) {
127
+ return await n(a).retry(() => this._post(t, a));
128
+ }
129
+ /**
130
+ * Parses the response and returns the result as the specified type.
131
+ *
132
+ * @param {Response} res - the response object to be parsed
133
+ * @return {Promise<T>} the parsed result of type T
134
+ */
135
+ static async parse(t) {
136
+ const a = await t.text();
137
+ if (t.status >= 200 && t.status < 300)
138
+ return v(a);
139
+ throw new Error(a);
140
+ }
93
141
  }
142
+ function y(e) {
143
+ return (e == null ? void 0 : e.message) || (e == null ? void 0 : e.toString()) || "unknown error";
144
+ }
145
+ const n = (e) => new g((e == null ? void 0 : e.retryCount) ?? 2, (e == null ? void 0 : e.retryDelay) ?? 1e3);
146
+ var o, c, l;
147
+ class f {
148
+ constructor(t, a, r = void 0) {
149
+ u(this, o, void 0), u(this, c, void 0), u(this, l, void 0), w(this, o, t), w(this, l, r), w(this, c, a);
150
+ }
151
+ get ok() {
152
+ return h(this, o);
153
+ }
154
+ get error() {
155
+ return h(this, l);
156
+ }
157
+ get val() {
158
+ return h(this, c);
159
+ }
160
+ }
161
+ o = /* @__PURE__ */ new WeakMap(), c = /* @__PURE__ */ new WeakMap(), l = /* @__PURE__ */ new WeakMap();
162
+ const k = (e) => new f(!0, e), W = (e, ...t) => {
163
+ if (typeof e == "string" && t && t.length > 0) {
164
+ let a = [e, ...t].map((r) => {
165
+ if (r) {
166
+ if (r instanceof Error)
167
+ return r.message;
168
+ if (typeof r == "string")
169
+ return r;
170
+ if (r != null && r.toString)
171
+ return r.toString();
172
+ }
173
+ });
174
+ return new f(
175
+ !1,
176
+ null,
177
+ a.filter((r) => r !== void 0).join(" ")
178
+ );
179
+ }
180
+ return new f(!1, null, e);
181
+ };
94
182
  export {
95
- D as waitForTransactionReceipt
183
+ W as Err,
184
+ m as Fetcher,
185
+ k as Ok,
186
+ f as Result,
187
+ g as Retry,
188
+ v as safeParseJson,
189
+ d as sleep
96
190
  };