@gardenfi/react-hooks 3.1.1 → 3.1.3
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/index3.cjs +1 -1
- package/dist/index3.js +60 -73
- package/package.json +3 -3
package/dist/index3.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("@gardenfi/orderbook"),w=require("@gardenfi/utils"),t=require("react"),D=require("./index5.cjs"),F=(e,R,l)=>{const[H,y]=t.useState([]),m=t.useRef(null),g=t.useRef(!1),C=t.useRef(!1),k=5e3,a=t.useMemo(()=>new D.PendingOrdersManager(R,w.StoreKeys.PENDING_ORDERS),[R]),A=t.useCallback(()=>{var s,c,d,n,o,i,f,u,E,I,L,T,v,q;if(!e)return[];const r=new Set;try{[(c=(s=e.htlcs)==null?void 0:s.evm)==null?void 0:c.htlcActorAddress,(n=(d=e.htlcs)==null?void 0:d.sui)==null?void 0:n.htlcActorAddress,(i=(o=e.htlcs)==null?void 0:o.solana)==null?void 0:i.htlcActorAddress,(u=(f=e.htlcs)==null?void 0:f.starknet)==null?void 0:u.htlcActorAddress,(I=(E=e.htlcs)==null?void 0:E.bitcoin)==null?void 0:I.htlcActorAddress,(T=(L=e.htlcs)==null?void 0:L.tron)==null?void 0:T.htlcActorAddress,(q=(v=e.htlcs)==null?void 0:v.xrpl)==null?void 0:q.htlcActorAddress].filter(O=>!!O&&O.length>0).forEach(O=>r.add(O.toLowerCase()))}catch(M){console.error("Error getting HTLC addresses:",M)}return Array.from(r)},[e]),S=t.useCallback(async()=>{if(e)try{const r=A();if(r.length===0){console.log("No HTLC addresses found for initial pending orders fetch");return}const s=r.map(async n=>{try{const o=await e.getOrders({address:n,status:[h.OrderLifecycle.notInitiated,h.OrderLifecycle.pending],per_page:500});return o.ok?o.val.data:[]}catch(o){return console.error(`Error fetching orders for address ${n}:`,o),[]}}),d=(await Promise.all(s)).flat().map(n=>n.order_id);a.set(l,d)}catch(r){console.error("Error fetching initial pending orders:",r)}},[e,a,A,l]),P=t.useCallback(async()=>{if(e)try{const r=a.get(l),s=Array.from(new Set(r));if(s.length===0){y([]);return}const c=await Promise.all(s.map(i=>e.getOrder(i))),d=[],n=[];c.forEach((i,f)=>{if(!i.ok){n.push(s[f]);return}const u=i.val;u.status===h.OrderStatus.Redeemed||u.status===h.OrderStatus.Refunded||u.status===h.OrderStatus.Expired||(d.push(u),n.push(s[f]))});const o=Array.from(new Set(n));a.set(l,o),y(d)}catch(r){console.error("Error fetching pending orders:",r)}},[e,a,l]),p=t.useMemo(()=>{try{return A().join(",")}catch(r){return console.error("Error computing HTLC addresses:",r),""}},[A]),b=t.useRef("");return t.useEffect(()=>{if(!e||!e.redeemServiceEnabled||!p)return;b.current!==p&&(C.current=!1,b.current=p),C.current||(S(),C.current=!0)},[e,p,S,l]),t.useEffect(()=>{if(e)if(e.redeemServiceEnabled){let r=!1;const s=()=>{r||(m.current=setTimeout(c,k))},c=async()=>{if(!r){if(g.current){s();return}g.current=!0;try{await P()}finally{g.current=!1}s()}};return c(),()=>{r=!0,m.current&&(clearTimeout(m.current),m.current=null)}}else{const r=s=>y(s);return e.on("onPendingOrdersChanged",r),()=>{e.off("onPendingOrdersChanged",r)}}},[e,P]),{pendingOrders:H}};exports.useOrderbook=F;
|
package/dist/index3.js
CHANGED
|
@@ -1,123 +1,110 @@
|
|
|
1
|
-
import { OrderLifecycle as
|
|
2
|
-
import { StoreKeys as
|
|
3
|
-
import { useState as
|
|
4
|
-
import { PendingOrdersManager as
|
|
5
|
-
const z = (r,
|
|
6
|
-
const [
|
|
7
|
-
() => new
|
|
8
|
-
[
|
|
9
|
-
),
|
|
10
|
-
var t,
|
|
1
|
+
import { OrderLifecycle as F, OrderStatus as C } from "@gardenfi/orderbook";
|
|
2
|
+
import { StoreKeys as q } from "@gardenfi/utils";
|
|
3
|
+
import { useState as j, useRef as A, useMemo as N, useCallback as P, useEffect as _ } from "react";
|
|
4
|
+
import { PendingOrdersManager as G } from "./index5.js";
|
|
5
|
+
const z = (r, R, l) => {
|
|
6
|
+
const [x, y] = j([]), f = A(null), O = A(!1), g = A(!1), M = 5e3, u = N(
|
|
7
|
+
() => new G(R, q.PENDING_ORDERS),
|
|
8
|
+
[R]
|
|
9
|
+
), h = P(() => {
|
|
10
|
+
var t, o, c, s, n, i, a, d, E, T, v, b, H, w;
|
|
11
11
|
if (!r) return [];
|
|
12
12
|
const e = /* @__PURE__ */ new Set();
|
|
13
13
|
try {
|
|
14
14
|
[
|
|
15
|
-
(
|
|
16
|
-
(
|
|
17
|
-
(
|
|
18
|
-
(
|
|
19
|
-
(
|
|
20
|
-
(b = (
|
|
21
|
-
(w = (
|
|
22
|
-
].filter((
|
|
23
|
-
} catch (
|
|
24
|
-
console.error("Error getting HTLC addresses:",
|
|
15
|
+
(o = (t = r.htlcs) == null ? void 0 : t.evm) == null ? void 0 : o.htlcActorAddress,
|
|
16
|
+
(s = (c = r.htlcs) == null ? void 0 : c.sui) == null ? void 0 : s.htlcActorAddress,
|
|
17
|
+
(i = (n = r.htlcs) == null ? void 0 : n.solana) == null ? void 0 : i.htlcActorAddress,
|
|
18
|
+
(d = (a = r.htlcs) == null ? void 0 : a.starknet) == null ? void 0 : d.htlcActorAddress,
|
|
19
|
+
(T = (E = r.htlcs) == null ? void 0 : E.bitcoin) == null ? void 0 : T.htlcActorAddress,
|
|
20
|
+
(b = (v = r.htlcs) == null ? void 0 : v.tron) == null ? void 0 : b.htlcActorAddress,
|
|
21
|
+
(w = (H = r.htlcs) == null ? void 0 : H.xrpl) == null ? void 0 : w.htlcActorAddress
|
|
22
|
+
].filter((p) => !!p && p.length > 0).forEach((p) => e.add(p.toLowerCase()));
|
|
23
|
+
} catch (D) {
|
|
24
|
+
console.error("Error getting HTLC addresses:", D);
|
|
25
25
|
}
|
|
26
26
|
return Array.from(e);
|
|
27
|
-
}, [r]),
|
|
27
|
+
}, [r]), S = P(async () => {
|
|
28
28
|
if (r)
|
|
29
29
|
try {
|
|
30
|
-
const e =
|
|
30
|
+
const e = h();
|
|
31
31
|
if (e.length === 0) {
|
|
32
32
|
console.log("No HTLC addresses found for initial pending orders fetch");
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
35
|
const t = e.map(async (s) => {
|
|
36
36
|
try {
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
status: _.notInitiated,
|
|
46
|
-
per_page: 500
|
|
47
|
-
})
|
|
48
|
-
]), l = [];
|
|
49
|
-
return o.ok ? l.push(...o.val.data) : console.error(
|
|
50
|
-
`Failed to fetch pending orders for address ${s}:`,
|
|
51
|
-
o.error
|
|
52
|
-
), n.ok ? l.push(...n.val.data) : console.error(
|
|
53
|
-
`Failed to fetch notInitiated orders for address ${s}:`,
|
|
54
|
-
n.error
|
|
55
|
-
), l;
|
|
56
|
-
} catch (o) {
|
|
57
|
-
return console.error(`Error fetching orders for address ${s}:`, o), [];
|
|
37
|
+
const n = await r.getOrders({
|
|
38
|
+
address: s,
|
|
39
|
+
status: [F.notInitiated, F.pending],
|
|
40
|
+
per_page: 500
|
|
41
|
+
});
|
|
42
|
+
return n.ok ? n.val.data : [];
|
|
43
|
+
} catch (n) {
|
|
44
|
+
return console.error(`Error fetching orders for address ${s}:`, n), [];
|
|
58
45
|
}
|
|
59
|
-
}),
|
|
60
|
-
|
|
46
|
+
}), c = (await Promise.all(t)).flat().map((s) => s.order_id);
|
|
47
|
+
u.set(l, c);
|
|
61
48
|
} catch (e) {
|
|
62
49
|
console.error("Error fetching initial pending orders:", e);
|
|
63
50
|
}
|
|
64
|
-
}, [r,
|
|
51
|
+
}, [r, u, h, l]), I = P(async () => {
|
|
65
52
|
if (r)
|
|
66
53
|
try {
|
|
67
|
-
const e =
|
|
54
|
+
const e = u.get(l), t = Array.from(new Set(e));
|
|
68
55
|
if (t.length === 0) {
|
|
69
56
|
y([]);
|
|
70
57
|
return;
|
|
71
58
|
}
|
|
72
|
-
const
|
|
73
|
-
t.map((
|
|
74
|
-
),
|
|
75
|
-
|
|
76
|
-
if (!
|
|
77
|
-
|
|
59
|
+
const o = await Promise.all(
|
|
60
|
+
t.map((i) => r.getOrder(i))
|
|
61
|
+
), c = [], s = [];
|
|
62
|
+
o.forEach((i, a) => {
|
|
63
|
+
if (!i.ok) {
|
|
64
|
+
s.push(t[a]);
|
|
78
65
|
return;
|
|
79
66
|
}
|
|
80
|
-
const
|
|
81
|
-
|
|
67
|
+
const d = i.val;
|
|
68
|
+
d.status === C.Redeemed || d.status === C.Refunded || d.status === C.Expired || (c.push(d), s.push(t[a]));
|
|
82
69
|
});
|
|
83
|
-
const
|
|
84
|
-
|
|
70
|
+
const n = Array.from(new Set(s));
|
|
71
|
+
u.set(l, n), y(c);
|
|
85
72
|
} catch (e) {
|
|
86
73
|
console.error("Error fetching pending orders:", e);
|
|
87
74
|
}
|
|
88
|
-
}, [r,
|
|
75
|
+
}, [r, u, l]), m = N(() => {
|
|
89
76
|
try {
|
|
90
|
-
return
|
|
77
|
+
return h().join(",");
|
|
91
78
|
} catch (e) {
|
|
92
79
|
return console.error("Error computing HTLC addresses:", e), "";
|
|
93
80
|
}
|
|
94
|
-
}, [
|
|
95
|
-
return
|
|
96
|
-
if (!r || !r.redeemServiceEnabled || !
|
|
97
|
-
|
|
98
|
-
}, [r,
|
|
81
|
+
}, [h]), L = A("");
|
|
82
|
+
return _(() => {
|
|
83
|
+
if (!r || !r.redeemServiceEnabled || !m) return;
|
|
84
|
+
L.current !== m && (g.current = !1, L.current = m), g.current || (S(), g.current = !0);
|
|
85
|
+
}, [r, m, S, l]), _(() => {
|
|
99
86
|
if (r)
|
|
100
87
|
if (r.redeemServiceEnabled) {
|
|
101
88
|
let e = !1;
|
|
102
89
|
const t = () => {
|
|
103
|
-
e || (
|
|
104
|
-
},
|
|
90
|
+
e || (f.current = setTimeout(o, M));
|
|
91
|
+
}, o = async () => {
|
|
105
92
|
if (!e) {
|
|
106
|
-
if (
|
|
93
|
+
if (O.current) {
|
|
107
94
|
t();
|
|
108
95
|
return;
|
|
109
96
|
}
|
|
110
|
-
|
|
97
|
+
O.current = !0;
|
|
111
98
|
try {
|
|
112
|
-
await
|
|
99
|
+
await I();
|
|
113
100
|
} finally {
|
|
114
|
-
|
|
101
|
+
O.current = !1;
|
|
115
102
|
}
|
|
116
103
|
t();
|
|
117
104
|
}
|
|
118
105
|
};
|
|
119
|
-
return
|
|
120
|
-
e = !0,
|
|
106
|
+
return o(), () => {
|
|
107
|
+
e = !0, f.current && (clearTimeout(f.current), f.current = null);
|
|
121
108
|
};
|
|
122
109
|
} else {
|
|
123
110
|
const e = (t) => y(t);
|
|
@@ -125,7 +112,7 @@ const z = (r, S, a) => {
|
|
|
125
112
|
r.off("onPendingOrdersChanged", e);
|
|
126
113
|
};
|
|
127
114
|
}
|
|
128
|
-
}, [r,
|
|
115
|
+
}, [r, I]), { pendingOrders: x };
|
|
129
116
|
};
|
|
130
117
|
export {
|
|
131
118
|
z as useOrderbook
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/react-hooks",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"registry": "https://registry.npmjs.org/"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@gardenfi/core": "3.1.
|
|
31
|
-
"@gardenfi/orderbook": "3.1.
|
|
30
|
+
"@gardenfi/core": "3.1.3",
|
|
31
|
+
"@gardenfi/orderbook": "3.1.3",
|
|
32
32
|
"@gardenfi/utils": "3.1.1",
|
|
33
33
|
"react": "^18.3.1",
|
|
34
34
|
"starknet": "8.0.0"
|