@gardenfi/react-hooks 3.0.7-beta.2 → 3.0.7-beta.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.
package/dist/index2.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react"),k=require("./index3.cjs"),d=require("@gardenfi/core"),v=require("./index4.cjs"),E=require("./index5.cjs"),p=require("@gardenfi/utils"),G=require("./index6.cjs"),w=a.createContext({pendingOrders:[]}),K=({children:i,config:e,store:l,setRedeemServiceEnabled:n=!0})=>{const[s,O]=a.useState(),{digestKey:c}=E.useDigestKey(n),{pendingOrders:_}=k.useOrderbook(s,l),h=a.useMemo(()=>{const{api:t}=d.resolveApiConfig(e.environment);return e.quote??new d.Quote(t.baseurl,v.resolveAuth(e.apiKey))},[e.environment,e.quote,e.apiKey]),m=a.useMemo(()=>async({fromAsset:t,toAsset:y,amount:r,isExactOut:u=!1,options:o})=>await(s?s.quote:h).getQuoteFromAssets({fromAsset:t,toAsset:y,amount:r,isExactOut:u,options:o}),[s,h]),g=async(t,y)=>{if(!s)return p.Err("Garden not initialized");const r=await s.createSwap(t,y);if(!r.val)return p.Err(r.error||"Unknown error occurred");if(n)try{const u=l.getItem(G.PENDING_ORDERS_STORE),o=u?JSON.parse(u):[];typeof r.val=="string"?o.includes(r.val)||o.push(r.val):r.val&&typeof r.val.order_id=="string"&&(o.includes(r.val.order_id)||o.push(r.val.order_id));const q=Array.from(new Set(o));l.setItem(G.PENDING_ORDERS_STORE,JSON.stringify(q))}catch(u){console.error("Failed to persist pending order id",u)}return p.Ok(r.val)};return a.useEffect(()=>{if(!("wallets"in e)&&!("htlc"in e))return;let t;if("wallets"in e&&Object.keys(e.wallets??{}).length>0&&v.hasAnyValidValue(e.wallets??{}))t=d.Garden.fromWallets({...e,digestKey:n?void 0:c}).setRedeemServiceEnabled(n);else if("htlc"in e&&Object.keys(e.htlc??{}).length>0&&v.hasAnyValidValue(e.htlc??{}))t=new d.Garden({...e,digestKey:n?void 0:c}).setRedeemServiceEnabled(n);else return;O(t)},[e,c,n]),a.createElement(w.Provider,{value:{swap:g,pendingOrders:_,getQuote:m,garden:s,orderBook:s}},i)},P=()=>{const i=a.useContext(w);if(!i)throw new Error("useGarden must be used within a GardenProvider");return i};exports.GardenContext=w;exports.GardenProvider=K;exports.useGarden=P;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),g=require("./index3.cjs"),a=require("@gardenfi/core"),c=require("./index4.cjs"),E=require("./index5.cjs"),u=require("@gardenfi/utils"),K=require("./index6.cjs"),v=n.createContext({pendingOrders:[]}),M=({children:d,config:e,store:i})=>{const[s,p]=n.useState(),m=!0,{digestKey:q}=E.useDigestKey(m),y=n.useMemo(()=>{const{network:r}=a.resolveApiConfig(e.environment);return r},[e.environment]),G=n.useMemo(()=>new K.PendingOrdersManager(i,u.StoreKeys.PENDING_ORDERS),[i]),{pendingOrders:h}=g.useOrderbook(s,i,y),w=n.useMemo(()=>{const{api:r}=a.resolveApiConfig(e.environment);return e.quote??new a.Quote(r.baseurl,c.resolveAuth(e.apiKey))},[e.environment,e.quote,e.apiKey]),O=n.useMemo(()=>async({fromAsset:r,toAsset:l,amount:t,isExactOut:o=!1,options:b})=>await(s?s.quote:w).getQuoteFromAssets({fromAsset:r,toAsset:l,amount:t,isExactOut:o,options:b}),[s,w]),k=async(r,l)=>{if(!s)return u.Err("Garden not initialized");const t=await s.createSwap(r,l);if(!t.val)return u.Err(t.error||"Unknown error occurred");try{let o;typeof t.val=="string"?o=t.val:t.val&&typeof t.val.order_id=="string"&&(o=t.val.order_id),o&&G.add(y,o)}catch(o){console.error("Failed to persist pending order id",o)}return u.Ok(t.val)};return n.useEffect(()=>{if(!("wallets"in e)&&!("htlc"in e))return;let r;if("wallets"in e&&Object.keys(e.wallets??{}).length>0&&c.hasAnyValidValue(e.wallets??{}))r=a.Garden.fromWallets({...e,digestKey:void 0});else if("htlc"in e&&Object.keys(e.htlc??{}).length>0&&c.hasAnyValidValue(e.htlc??{}))r=new a.Garden({...e,digestKey:void 0});else return;p(r)},[e,q]),n.createElement(v.Provider,{value:{swap:k,pendingOrders:h,getQuote:O,garden:s,orderBook:s}},d)},P=()=>{const d=n.useContext(v);if(!d)throw new Error("useGarden must be used within a GardenProvider");return d};exports.GardenContext=v;exports.GardenProvider=M;exports.useGarden=P;
package/dist/index2.js CHANGED
@@ -1,88 +1,90 @@
1
- import f, { useState as K, useMemo as c, useEffect as x, createContext as A } from "react";
2
- import { useOrderbook as g } from "./index3.js";
3
- import { resolveApiConfig as C, Quote as I, Garden as y } from "@gardenfi/core";
4
- import { resolveAuth as N, hasAnyValidValue as v } from "./index4.js";
5
- import { useDigestKey as P } from "./index5.js";
6
- import { Err as w, Ok as D } from "@gardenfi/utils";
7
- import { PENDING_ORDERS_STORE as h } from "./index6.js";
8
- const O = A({
1
+ import w, { useState as f, useMemo as a, useEffect as P, createContext as S } from "react";
2
+ import { useOrderbook as _ } from "./index3.js";
3
+ import { resolveApiConfig as m, Quote as x, Garden as v } from "@gardenfi/core";
4
+ import { resolveAuth as A, hasAnyValidValue as p } from "./index4.js";
5
+ import { useDigestKey as C } from "./index5.js";
6
+ import { StoreKeys as R, Err as c, Ok as D } from "@gardenfi/utils";
7
+ import { PendingOrdersManager as M } from "./index6.js";
8
+ const y = S({
9
9
  pendingOrders: []
10
- }), b = ({
11
- children: i,
10
+ }), B = ({
11
+ children: s,
12
12
  config: e,
13
- store: u,
14
- setRedeemServiceEnabled: o = !0
13
+ store: d
15
14
  }) => {
16
- const [s, G] = K(), { digestKey: l } = P(o), { pendingOrders: q } = g(s, u), p = c(() => {
17
- const { api: t } = C(e.environment);
18
- return e.quote ?? new I(t.baseurl, N(e.apiKey));
19
- }, [e.environment, e.quote, e.apiKey]), _ = c(
15
+ const [n, h] = f(), k = !0, { digestKey: G } = C(k), l = a(() => {
16
+ const { network: r } = m(e.environment);
17
+ return r;
18
+ }, [e.environment]), O = a(
19
+ () => new M(d, R.PENDING_ORDERS),
20
+ [d]
21
+ ), { pendingOrders: E } = _(n, d, l), u = a(() => {
22
+ const { api: r } = m(e.environment);
23
+ return e.quote ?? new x(r.baseurl, A(e.apiKey));
24
+ }, [e.environment, e.quote, e.apiKey]), K = a(
20
25
  () => async ({
21
- fromAsset: t,
22
- toAsset: d,
23
- amount: r,
24
- isExactOut: a = !1,
25
- options: n
26
- }) => await (s ? s.quote : p).getQuoteFromAssets({
27
- fromAsset: t,
28
- toAsset: d,
29
- amount: r,
30
- isExactOut: a,
31
- options: n
26
+ fromAsset: r,
27
+ toAsset: i,
28
+ amount: t,
29
+ isExactOut: o = !1,
30
+ options: q
31
+ }) => await (n ? n.quote : u).getQuoteFromAssets({
32
+ fromAsset: r,
33
+ toAsset: i,
34
+ amount: t,
35
+ isExactOut: o,
36
+ options: q
32
37
  }),
33
- [s, p]
34
- ), k = async (t, d) => {
35
- if (!s) return w("Garden not initialized");
36
- const r = await s.createSwap(t, d);
37
- if (!r.val) return w(r.error || "Unknown error occurred");
38
- if (o)
39
- try {
40
- const a = u.getItem(h), n = a ? JSON.parse(a) : [];
41
- typeof r.val == "string" ? n.includes(r.val) || n.push(r.val) : r.val && typeof r.val.order_id == "string" && (n.includes(r.val.order_id) || n.push(r.val.order_id));
42
- const m = Array.from(new Set(n));
43
- u.setItem(h, JSON.stringify(m));
44
- } catch (a) {
45
- console.error("Failed to persist pending order id", a);
46
- }
47
- return D(r.val);
38
+ [n, u]
39
+ ), b = async (r, i) => {
40
+ if (!n) return c("Garden not initialized");
41
+ const t = await n.createSwap(r, i);
42
+ if (!t.val) return c(t.error || "Unknown error occurred");
43
+ try {
44
+ let o;
45
+ typeof t.val == "string" ? o = t.val : t.val && typeof t.val.order_id == "string" && (o = t.val.order_id), o && O.add(l, o);
46
+ } catch (o) {
47
+ console.error("Failed to persist pending order id", o);
48
+ }
49
+ return D(t.val);
48
50
  };
49
- return x(() => {
51
+ return P(() => {
50
52
  if (!("wallets" in e) && !("htlc" in e)) return;
51
- let t;
52
- if ("wallets" in e && Object.keys(e.wallets ?? {}).length > 0 && v(e.wallets ?? {}))
53
- t = y.fromWallets({
53
+ let r;
54
+ if ("wallets" in e && Object.keys(e.wallets ?? {}).length > 0 && p(e.wallets ?? {}))
55
+ r = v.fromWallets({
54
56
  ...e,
55
- digestKey: o ? void 0 : l
56
- }).setRedeemServiceEnabled(o);
57
- else if ("htlc" in e && Object.keys(e.htlc ?? {}).length > 0 && v(e.htlc ?? {}))
58
- t = new y({
57
+ digestKey: void 0
58
+ });
59
+ else if ("htlc" in e && Object.keys(e.htlc ?? {}).length > 0 && p(e.htlc ?? {}))
60
+ r = new v({
59
61
  ...e,
60
- digestKey: o ? void 0 : l
61
- }).setRedeemServiceEnabled(o);
62
+ digestKey: void 0
63
+ });
62
64
  else
63
65
  return;
64
- G(t);
65
- }, [e, l, o]), /* @__PURE__ */ f.createElement(
66
- O.Provider,
66
+ h(r);
67
+ }, [e, G]), /* @__PURE__ */ w.createElement(
68
+ y.Provider,
67
69
  {
68
70
  value: {
69
- swap: k,
70
- pendingOrders: q,
71
- getQuote: _,
72
- garden: s,
73
- orderBook: s
71
+ swap: b,
72
+ pendingOrders: E,
73
+ getQuote: K,
74
+ garden: n,
75
+ orderBook: n
74
76
  }
75
77
  },
76
- i
78
+ s
77
79
  );
78
- }, z = () => {
79
- const i = f.useContext(O);
80
- if (!i)
80
+ }, U = () => {
81
+ const s = w.useContext(y);
82
+ if (!s)
81
83
  throw new Error("useGarden must be used within a GardenProvider");
82
- return i;
84
+ return s;
83
85
  };
84
86
  export {
85
- O as GardenContext,
86
- b as GardenProvider,
87
- z as useGarden
87
+ y as GardenContext,
88
+ B as GardenProvider,
89
+ U as useGarden
88
90
  };
package/dist/index3.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("@gardenfi/orderbook"),i=require("react"),R=require("./index6.cjs"),D=(e,a)=>{const[v,y]=i.useState([]),O=i.useRef(null),A=i.useRef(!1),E=i.useRef(!1),w=5e3,p=i.useCallback(()=>{var n,o,f,u,d,s,t,l,c,S,b,N,T,k;if(!e)return[];const r=new Set;try{[(o=(n=e.htlcs)==null?void 0:n.evm)==null?void 0:o.htlcActorAddress,(u=(f=e.htlcs)==null?void 0:f.sui)==null?void 0:u.htlcActorAddress,(s=(d=e.htlcs)==null?void 0:d.solana)==null?void 0:s.htlcActorAddress,(l=(t=e.htlcs)==null?void 0:t.starknet)==null?void 0:l.htlcActorAddress,(S=(c=e.htlcs)==null?void 0:c.bitcoin)==null?void 0:S.htlcActorAddress,(N=(b=e.htlcs)==null?void 0:b.tron)==null?void 0:N.htlcActorAddress,(k=(T=e.htlcs)==null?void 0:T.xrpl)==null?void 0:k.htlcActorAddress].filter(g=>!!g&&g.length>0).forEach(g=>r.add(g.toLowerCase()))}catch(_){console.error("Error getting HTLC addresses:",_)}return Array.from(r)},[e]),C=i.useCallback(async()=>{if(e)try{const r=p();if(r.length===0){console.log("No HTLC addresses found for initial pending orders fetch");return}const n=r.map(async s=>{try{const[t,l]=await Promise.all([e.getOrders({address:s,status:h.OrderLifecycle.pending,per_page:500}),e.getOrders({address:s,status:h.OrderLifecycle.notInitiated,per_page:500})]),c=[];return t.ok?c.push(...t.val.data):console.error(`Failed to fetch pending orders for address ${s}:`,t.error),l.ok?c.push(...l.val.data):console.error(`Failed to fetch notInitiated orders for address ${s}:`,l.error),c}catch(t){return console.error(`Error fetching orders for address ${s}:`,t),[]}}),u=(await Promise.all(n)).flat().map(s=>s.order_id),d=Array.from(new Set(u));if(d.length>0)try{a.setItem(R.PENDING_ORDERS_STORE,JSON.stringify(d))}catch(s){console.error("Error saving initial pending order IDs to localStorage",s)}}catch(r){console.error("Error fetching initial pending orders:",r)}},[e,a,p]),I=i.useCallback(async()=>{if(e)try{const r=a.getItem(R.PENDING_ORDERS_STORE),n=r?JSON.parse(r):[],o=Array.from(new Set(n));if(o.length===0){y([]);return}const f=await Promise.all(o.map(t=>e.getOrder(t))),u=[],d=[];f.forEach((t,l)=>{if(!t.ok){d.push(o[l]);return}const c=t.val;c.status===h.OrderStatus.Redeemed||c.status===h.OrderStatus.Refunded||c.status===h.OrderStatus.Expired||(u.push(c),d.push(o[l]))});const s=Array.from(new Set(d));try{a.setItem(R.PENDING_ORDERS_STORE,JSON.stringify(s))}catch(t){console.error("Error persisting remaining pending order ids",t)}y(u)}catch(r){console.error("Error fetching pending orders:",r)}},[e,a]),m=i.useMemo(()=>{try{return p().join(",")}catch(r){return console.error("Error computing HTLC addresses:",r),""}},[p]),P=i.useRef("");return i.useEffect(()=>{if(!e||!e.redeemServiceEnabled||!m)return;P.current!==m&&(E.current=!1,P.current=m),E.current||(C(),E.current=!0)},[e,m,C]),i.useEffect(()=>{if(e)if(e.redeemServiceEnabled){let r=!1;const n=()=>{r||(O.current=setTimeout(o,w))},o=async()=>{if(!r){if(A.current){n();return}A.current=!0;try{await I()}finally{A.current=!1}n()}};return o(),()=>{r=!0,O.current&&(clearTimeout(O.current),O.current=null)}}else{const r=n=>y(n);return e.on("onPendingOrdersChanged",r),()=>{e.off("onPendingOrdersChanged",r)}}},[e,I]),{pendingOrders:v}};exports.useOrderbook=D;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@gardenfi/orderbook"),w=require("@gardenfi/utils"),n=require("react"),H=require("./index6.cjs"),_=(e,S,a)=>{const[M,g]=n.useState([]),O=n.useRef(null),C=n.useRef(!1),E=n.useRef(!1),k=5e3,h=n.useMemo(()=>new H.PendingOrdersManager(S,w.StoreKeys.PENDING_ORDERS),[S]),m=n.useCallback(()=>{var t,d,f,i,l,s,o,c,u,I,v,L,T,q;if(!e)return[];const r=new Set;try{[(d=(t=e.htlcs)==null?void 0:t.evm)==null?void 0:d.htlcActorAddress,(i=(f=e.htlcs)==null?void 0:f.sui)==null?void 0:i.htlcActorAddress,(s=(l=e.htlcs)==null?void 0:l.solana)==null?void 0:s.htlcActorAddress,(c=(o=e.htlcs)==null?void 0:o.starknet)==null?void 0:c.htlcActorAddress,(I=(u=e.htlcs)==null?void 0:u.bitcoin)==null?void 0:I.htlcActorAddress,(L=(v=e.htlcs)==null?void 0:v.tron)==null?void 0:L.htlcActorAddress,(q=(T=e.htlcs)==null?void 0:T.xrpl)==null?void 0:q.htlcActorAddress].filter(y=>!!y&&y.length>0).forEach(y=>r.add(y.toLowerCase()))}catch(F){console.error("Error getting HTLC addresses:",F)}return Array.from(r)},[e]),R=n.useCallback(async()=>{if(e)try{const r=m();if(r.length===0){console.log("No HTLC addresses found for initial pending orders fetch");return}const t=r.map(async s=>{try{const[o,c]=await Promise.all([e.getOrders({address:s,status:p.OrderLifecycle.pending,per_page:500}),e.getOrders({address:s,status:p.OrderLifecycle.notInitiated,per_page:500})]),u=[];return o.ok?u.push(...o.val.data):console.error(`Failed to fetch pending orders for address ${s}:`,o.error),c.ok?u.push(...c.val.data):console.error(`Failed to fetch notInitiated orders for address ${s}:`,c.error),u}catch(o){return console.error(`Error fetching orders for address ${s}:`,o),[]}}),i=(await Promise.all(t)).flat().map(s=>s.order_id),l=Array.from(new Set(i));l.length>0&&h.set(a,l)}catch(r){console.error("Error fetching initial pending orders:",r)}},[e,h,m,a]),P=n.useCallback(async()=>{if(e)try{const r=h.get(a),t=Array.from(new Set(r));if(t.length===0){g([]);return}const d=await Promise.all(t.map(s=>e.getOrder(s))),f=[],i=[];d.forEach((s,o)=>{if(!s.ok){i.push(t[o]);return}const c=s.val;c.status===p.OrderStatus.Redeemed||c.status===p.OrderStatus.Refunded||c.status===p.OrderStatus.Expired||(f.push(c),i.push(t[o]))});const l=Array.from(new Set(i));h.set(a,l),g(f)}catch(r){console.error("Error fetching pending orders:",r)}},[e,h,a]),A=n.useMemo(()=>{try{return m().join(",")}catch(r){return console.error("Error computing HTLC addresses:",r),""}},[m]),b=n.useRef("");return n.useEffect(()=>{if(!e||!e.redeemServiceEnabled||!A)return;b.current!==A&&(E.current=!1,b.current=A),E.current||(R(),E.current=!0)},[e,A,R,a]),n.useEffect(()=>{if(e)if(e.redeemServiceEnabled){let r=!1;const t=()=>{r||(O.current=setTimeout(d,k))},d=async()=>{if(!r){if(C.current){t();return}C.current=!0;try{await P()}finally{C.current=!1}t()}};return d(),()=>{r=!0,O.current&&(clearTimeout(O.current),O.current=null)}}else{const r=t=>g(t);return e.on("onPendingOrdersChanged",r),()=>{e.off("onPendingOrdersChanged",r)}}},[e,P]),{pendingOrders:M}};exports.useOrderbook=_;
package/dist/index3.js CHANGED
@@ -1,119 +1,110 @@
1
- import { OrderLifecycle as k, OrderStatus as S } from "@gardenfi/orderbook";
2
- import { useState as H, useRef as A, useCallback as C, useMemo as x, useEffect as F } from "react";
3
- import { PENDING_ORDERS_STORE as I } from "./index6.js";
4
- const j = (r, a) => {
5
- const [_, O] = H([]), f = A(null), g = A(!1), y = A(!1), D = 5e3, h = C(() => {
6
- var n, o, u, l, i, s, t, d, c, E, v, T, L, N;
1
+ import { OrderLifecycle as _, OrderStatus as C } from "@gardenfi/orderbook";
2
+ import { StoreKeys as M } from "@gardenfi/utils";
3
+ import { useState as $, useRef as O, useMemo as D, useCallback as P, useEffect as N } from "react";
4
+ import { PendingOrdersManager as j } from "./index6.js";
5
+ const z = (r, S, a) => {
6
+ const [q, y] = $([]), h = O(null), g = O(!1), E = O(!1), x = 5e3, f = D(
7
+ () => new j(S, M.PENDING_ORDERS),
8
+ [S]
9
+ ), m = P(() => {
10
+ var t, c, u, i, d, s, o, n, l, L, T, b, F, w;
7
11
  if (!r) return [];
8
12
  const e = /* @__PURE__ */ new Set();
9
13
  try {
10
14
  [
11
- (o = (n = r.htlcs) == null ? void 0 : n.evm) == null ? void 0 : o.htlcActorAddress,
12
- (l = (u = r.htlcs) == null ? void 0 : u.sui) == null ? void 0 : l.htlcActorAddress,
13
- (s = (i = r.htlcs) == null ? void 0 : i.solana) == null ? void 0 : s.htlcActorAddress,
14
- (d = (t = r.htlcs) == null ? void 0 : t.starknet) == null ? void 0 : d.htlcActorAddress,
15
- (E = (c = r.htlcs) == null ? void 0 : c.bitcoin) == null ? void 0 : E.htlcActorAddress,
16
- (T = (v = r.htlcs) == null ? void 0 : v.tron) == null ? void 0 : T.htlcActorAddress,
17
- (N = (L = r.htlcs) == null ? void 0 : L.xrpl) == null ? void 0 : N.htlcActorAddress
18
- ].filter((p) => !!p && p.length > 0).forEach((p) => e.add(p.toLowerCase()));
19
- } catch (b) {
20
- console.error("Error getting HTLC addresses:", b);
15
+ (c = (t = r.htlcs) == null ? void 0 : t.evm) == null ? void 0 : c.htlcActorAddress,
16
+ (i = (u = r.htlcs) == null ? void 0 : u.sui) == null ? void 0 : i.htlcActorAddress,
17
+ (s = (d = r.htlcs) == null ? void 0 : d.solana) == null ? void 0 : s.htlcActorAddress,
18
+ (n = (o = r.htlcs) == null ? void 0 : o.starknet) == null ? void 0 : n.htlcActorAddress,
19
+ (L = (l = r.htlcs) == null ? void 0 : l.bitcoin) == null ? void 0 : L.htlcActorAddress,
20
+ (b = (T = r.htlcs) == null ? void 0 : T.tron) == null ? void 0 : b.htlcActorAddress,
21
+ (w = (F = r.htlcs) == null ? void 0 : F.xrpl) == null ? void 0 : w.htlcActorAddress
22
+ ].filter((A) => !!A && A.length > 0).forEach((A) => e.add(A.toLowerCase()));
23
+ } catch (H) {
24
+ console.error("Error getting HTLC addresses:", H);
21
25
  }
22
26
  return Array.from(e);
23
- }, [r]), P = C(async () => {
27
+ }, [r]), I = P(async () => {
24
28
  if (r)
25
29
  try {
26
- const e = h();
30
+ const e = m();
27
31
  if (e.length === 0) {
28
32
  console.log("No HTLC addresses found for initial pending orders fetch");
29
33
  return;
30
34
  }
31
- const n = e.map(async (s) => {
35
+ const t = e.map(async (s) => {
32
36
  try {
33
- const [t, d] = await Promise.all([
37
+ const [o, n] = await Promise.all([
34
38
  r.getOrders({
35
39
  address: s,
36
- status: k.pending,
40
+ status: _.pending,
37
41
  per_page: 500
38
42
  }),
39
43
  r.getOrders({
40
44
  address: s,
41
- status: k.notInitiated,
45
+ status: _.notInitiated,
42
46
  per_page: 500
43
47
  })
44
- ]), c = [];
45
- return t.ok ? c.push(...t.val.data) : console.error(
48
+ ]), l = [];
49
+ return o.ok ? l.push(...o.val.data) : console.error(
46
50
  `Failed to fetch pending orders for address ${s}:`,
47
- t.error
48
- ), d.ok ? c.push(...d.val.data) : console.error(
51
+ o.error
52
+ ), n.ok ? l.push(...n.val.data) : console.error(
49
53
  `Failed to fetch notInitiated orders for address ${s}:`,
50
- d.error
51
- ), c;
52
- } catch (t) {
53
- return console.error(`Error fetching orders for address ${s}:`, t), [];
54
- }
55
- }), l = (await Promise.all(n)).flat().map((s) => s.order_id), i = Array.from(new Set(l));
56
- if (i.length > 0)
57
- try {
58
- a.setItem(I, JSON.stringify(i));
59
- } catch (s) {
60
- console.error(
61
- "Error saving initial pending order IDs to localStorage",
62
- s
63
- );
54
+ n.error
55
+ ), l;
56
+ } catch (o) {
57
+ return console.error(`Error fetching orders for address ${s}:`, o), [];
64
58
  }
59
+ }), i = (await Promise.all(t)).flat().map((s) => s.order_id), d = Array.from(new Set(i));
60
+ d.length > 0 && f.set(a, d);
65
61
  } catch (e) {
66
62
  console.error("Error fetching initial pending orders:", e);
67
63
  }
68
- }, [r, a, h]), R = C(async () => {
64
+ }, [r, f, m, a]), R = P(async () => {
69
65
  if (r)
70
66
  try {
71
- const e = a.getItem(I), n = e ? JSON.parse(e) : [], o = Array.from(new Set(n));
72
- if (o.length === 0) {
73
- O([]);
67
+ const e = f.get(a), t = Array.from(new Set(e));
68
+ if (t.length === 0) {
69
+ y([]);
74
70
  return;
75
71
  }
76
- const u = await Promise.all(
77
- o.map((t) => r.getOrder(t))
78
- ), l = [], i = [];
79
- u.forEach((t, d) => {
80
- if (!t.ok) {
81
- i.push(o[d]);
72
+ const c = await Promise.all(
73
+ t.map((s) => r.getOrder(s))
74
+ ), u = [], i = [];
75
+ c.forEach((s, o) => {
76
+ if (!s.ok) {
77
+ i.push(t[o]);
82
78
  return;
83
79
  }
84
- const c = t.val;
85
- c.status === S.Redeemed || c.status === S.Refunded || c.status === S.Expired || (l.push(c), i.push(o[d]));
80
+ const n = s.val;
81
+ n.status === C.Redeemed || n.status === C.Refunded || n.status === C.Expired || (u.push(n), i.push(t[o]));
86
82
  });
87
- const s = Array.from(new Set(i));
88
- try {
89
- a.setItem(I, JSON.stringify(s));
90
- } catch (t) {
91
- console.error("Error persisting remaining pending order ids", t);
92
- }
93
- O(l);
83
+ const d = Array.from(new Set(i));
84
+ f.set(a, d), y(u);
94
85
  } catch (e) {
95
86
  console.error("Error fetching pending orders:", e);
96
87
  }
97
- }, [r, a]), m = x(() => {
88
+ }, [r, f, a]), p = D(() => {
98
89
  try {
99
- return h().join(",");
90
+ return m().join(",");
100
91
  } catch (e) {
101
92
  return console.error("Error computing HTLC addresses:", e), "";
102
93
  }
103
- }, [h]), w = A("");
104
- return F(() => {
105
- if (!r || !r.redeemServiceEnabled || !m) return;
106
- w.current !== m && (y.current = !1, w.current = m), y.current || (P(), y.current = !0);
107
- }, [r, m, P]), F(() => {
94
+ }, [m]), v = O("");
95
+ return N(() => {
96
+ if (!r || !r.redeemServiceEnabled || !p) return;
97
+ v.current !== p && (E.current = !1, v.current = p), E.current || (I(), E.current = !0);
98
+ }, [r, p, I, a]), N(() => {
108
99
  if (r)
109
100
  if (r.redeemServiceEnabled) {
110
101
  let e = !1;
111
- const n = () => {
112
- e || (f.current = setTimeout(o, D));
113
- }, o = async () => {
102
+ const t = () => {
103
+ e || (h.current = setTimeout(c, x));
104
+ }, c = async () => {
114
105
  if (!e) {
115
106
  if (g.current) {
116
- n();
107
+ t();
117
108
  return;
118
109
  }
119
110
  g.current = !0;
@@ -122,20 +113,20 @@ const j = (r, a) => {
122
113
  } finally {
123
114
  g.current = !1;
124
115
  }
125
- n();
116
+ t();
126
117
  }
127
118
  };
128
- return o(), () => {
129
- e = !0, f.current && (clearTimeout(f.current), f.current = null);
119
+ return c(), () => {
120
+ e = !0, h.current && (clearTimeout(h.current), h.current = null);
130
121
  };
131
122
  } else {
132
- const e = (n) => O(n);
123
+ const e = (t) => y(t);
133
124
  return r.on("onPendingOrdersChanged", e), () => {
134
125
  r.off("onPendingOrdersChanged", e);
135
126
  };
136
127
  }
137
- }, [r, R]), { pendingOrders: _ };
128
+ }, [r, R]), { pendingOrders: q };
138
129
  };
139
130
  export {
140
- j as useOrderbook
131
+ z as useOrderbook
141
132
  };
package/dist/index5.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@gardenfi/utils"),e=require("./index6.cjs"),y=require("react"),f=(a=!0)=>{const[E,g]=y.useState();return y.useEffect(()=>{if(a)return;if(!indexedDB){console.error("IndexedDB is not supported in this browser");return}let o;const d=indexedDB.open(e.DB_NAME,e.VERSION);d.onerror=s=>{console.error("IndexedDB error:",s.target.error)},d.onupgradeneeded=s=>{const n=s.target.result;n.objectStoreNames.contains(e.STORE_NAME)||n.createObjectStore(e.STORE_NAME,{keyPath:"id"})},d.onsuccess=s=>{if(o=s.target.result,!o.objectStoreNames.contains(e.STORE_NAME)){o.close();const r=indexedDB.open(e.DB_NAME,o.version+1);r.onupgradeneeded=n=>{n.target.result.createObjectStore(e.STORE_NAME,{keyPath:"id"})},r.onsuccess=()=>{l(r.result)};return}l(o)};function l(s){try{const r=s.transaction(e.STORE_NAME,"readwrite"),n=r.objectStore(e.STORE_NAME),c=n.get(e.DIGEST_KEY);c.onsuccess=()=>{if(c.result){const i=u.DigestKey.from(c.result.value);if(!i.error){g(i.val);return}}const t=u.DigestKey.generateRandom();if(!t.ok){console.error("Error generating new digest key:",t.error);return}if(a)g(u.DigestKey.from(t.val.digestKey).val);else{const i=n.put({id:e.DIGEST_KEY,value:t.val.digestKey});i.onsuccess=()=>{g(u.DigestKey.from(t.val.digestKey).val)},i.onerror=D=>{console.error("Error storing new digest key:",D)}}},c.onerror=t=>{console.error("Error retrieving digest key:",t)},r.oncomplete=()=>{},r.onerror=t=>{console.error("Error in digestKey transaction:",t)}}catch(r){console.error("Transaction error:",r)}}return()=>{o&&o.close()}},[a]),{digestKey:E}};exports.useDigestKey=f;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@gardenfi/utils"),e=require("./index7.cjs"),y=require("react"),f=(a=!0)=>{const[E,g]=y.useState();return y.useEffect(()=>{if(a)return;if(!indexedDB){console.error("IndexedDB is not supported in this browser");return}let o;const d=indexedDB.open(e.DB_NAME,e.VERSION);d.onerror=s=>{console.error("IndexedDB error:",s.target.error)},d.onupgradeneeded=s=>{const n=s.target.result;n.objectStoreNames.contains(e.STORE_NAME)||n.createObjectStore(e.STORE_NAME,{keyPath:"id"})},d.onsuccess=s=>{if(o=s.target.result,!o.objectStoreNames.contains(e.STORE_NAME)){o.close();const r=indexedDB.open(e.DB_NAME,o.version+1);r.onupgradeneeded=n=>{n.target.result.createObjectStore(e.STORE_NAME,{keyPath:"id"})},r.onsuccess=()=>{l(r.result)};return}l(o)};function l(s){try{const r=s.transaction(e.STORE_NAME,"readwrite"),n=r.objectStore(e.STORE_NAME),c=n.get(e.DIGEST_KEY);c.onsuccess=()=>{if(c.result){const i=u.DigestKey.from(c.result.value);if(!i.error){g(i.val);return}}const t=u.DigestKey.generateRandom();if(!t.ok){console.error("Error generating new digest key:",t.error);return}if(a)g(u.DigestKey.from(t.val.digestKey).val);else{const i=n.put({id:e.DIGEST_KEY,value:t.val.digestKey});i.onsuccess=()=>{g(u.DigestKey.from(t.val.digestKey).val)},i.onerror=D=>{console.error("Error storing new digest key:",D)}}},c.onerror=t=>{console.error("Error retrieving digest key:",t)},r.oncomplete=()=>{},r.onerror=t=>{console.error("Error in digestKey transaction:",t)}}catch(r){console.error("Transaction error:",r)}}return()=>{o&&o.close()}},[a]),{digestKey:E}};exports.useDigestKey=f;
package/dist/index5.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DigestKey as a } from "@gardenfi/utils";
2
- import { DB_NAME as f, VERSION as m, STORE_NAME as n, DIGEST_KEY as y } from "./index6.js";
2
+ import { DB_NAME as f, VERSION as m, STORE_NAME as n, DIGEST_KEY as y } from "./index7.js";
3
3
  import { useState as K, useEffect as E } from "react";
4
4
  const S = (u = !0) => {
5
5
  const [p, d] = K();
package/dist/index6.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E="garden-finance-db",e="garden-finance-store",n="digest-key",t=1,o="orders-in-progress";exports.DB_NAME=E;exports.DIGEST_KEY=n;exports.PENDING_ORDERS_STORE=o;exports.STORE_NAME=e;exports.VERSION=t;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@gardenfi/utils");class i{constructor(t,e){this.store=t,this.storeKey=e,this.defaultStorage={mainnet:[],testnet:[],staging:[]}}getNetworkKey(t){if(!t)return"mainnet";const e=String(t).toLowerCase();return e.includes("staging")?"staging":e===a.Network.TESTNET?"testnet":(e===a.Network.MAINNET||e===a.Network.LOCALNET,"mainnet")}getStorageData(){try{const t=this.store.getItem(this.storeKey);return t?JSON.parse(t):{...this.defaultStorage}}catch{return{...this.defaultStorage}}}saveStorageData(t){try{this.store.setItem(this.storeKey,JSON.stringify(t))}catch(e){console.error("Error saving pending orders to storage:",e)}}get(t){const e=this.getStorageData(),r=this.getNetworkKey(t);return e[r]||[]}set(t,e){const r=this.getStorageData(),s=this.getNetworkKey(t);r[s]=Array.from(new Set(e)),this.saveStorageData(r)}add(t,e){const r=this.get(t);r.includes(e)||this.set(t,[...r,e])}remove(t,e){const r=this.get(t);this.set(t,r.filter(s=>s!==e))}clear(t){t?this.set(t,[]):this.saveStorageData({...this.defaultStorage})}}exports.PendingOrdersManager=i;
package/dist/index6.js CHANGED
@@ -1,8 +1,55 @@
1
- const n = "garden-finance-db", e = "garden-finance-store", s = "digest-key", o = 1, E = "orders-in-progress";
1
+ import { Network as a } from "@gardenfi/utils";
2
+ class o {
3
+ constructor(t, e) {
4
+ this.store = t, this.storeKey = e, this.defaultStorage = {
5
+ mainnet: [],
6
+ testnet: [],
7
+ staging: []
8
+ };
9
+ }
10
+ getNetworkKey(t) {
11
+ if (!t) return "mainnet";
12
+ const e = String(t).toLowerCase();
13
+ return e.includes("staging") ? "staging" : e === a.TESTNET ? "testnet" : (e === a.MAINNET || e === a.LOCALNET, "mainnet");
14
+ }
15
+ getStorageData() {
16
+ try {
17
+ const t = this.store.getItem(this.storeKey);
18
+ return t ? JSON.parse(t) : { ...this.defaultStorage };
19
+ } catch {
20
+ return { ...this.defaultStorage };
21
+ }
22
+ }
23
+ saveStorageData(t) {
24
+ try {
25
+ this.store.setItem(this.storeKey, JSON.stringify(t));
26
+ } catch (e) {
27
+ console.error("Error saving pending orders to storage:", e);
28
+ }
29
+ }
30
+ get(t) {
31
+ const e = this.getStorageData(), r = this.getNetworkKey(t);
32
+ return e[r] || [];
33
+ }
34
+ set(t, e) {
35
+ const r = this.getStorageData(), s = this.getNetworkKey(t);
36
+ r[s] = Array.from(new Set(e)), this.saveStorageData(r);
37
+ }
38
+ add(t, e) {
39
+ const r = this.get(t);
40
+ r.includes(e) || this.set(t, [...r, e]);
41
+ }
42
+ remove(t, e) {
43
+ const r = this.get(t);
44
+ this.set(
45
+ t,
46
+ r.filter((s) => s !== e)
47
+ );
48
+ }
49
+ clear(t) {
50
+ t ? this.set(t, []) : this.saveStorageData({ ...this.defaultStorage });
51
+ }
52
+ }
2
53
  export {
3
- n as DB_NAME,
4
- s as DIGEST_KEY,
5
- E as PENDING_ORDERS_STORE,
6
- e as STORE_NAME,
7
- o as VERSION
54
+ o as PendingOrdersManager
8
55
  };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e="garden-finance-db",n="garden-finance-store",t="digest-key",E=1;exports.DB_NAME=e;exports.DIGEST_KEY=t;exports.STORE_NAME=n;exports.VERSION=E;
package/dist/index7.js ADDED
@@ -0,0 +1,7 @@
1
+ const n = "garden-finance-db", e = "garden-finance-store", t = "digest-key", c = 1;
2
+ export {
3
+ n as DB_NAME,
4
+ t as DIGEST_KEY,
5
+ e as STORE_NAME,
6
+ c as VERSION
7
+ };
@@ -2,4 +2,3 @@ export declare const DB_NAME = "garden-finance-db";
2
2
  export declare const STORE_NAME = "garden-finance-store";
3
3
  export declare const DIGEST_KEY = "digest-key";
4
4
  export declare const VERSION = 1;
5
- export declare const PENDING_ORDERS_STORE = "orders-in-progress";
@@ -37,11 +37,4 @@ export type GardenProviderProps = {
37
37
  children: React.ReactNode;
38
38
  config: Omit<GardenConfigWithHTLCs, 'digestKey'> | Omit<GardenConfigWithWallets, 'digestKey'>;
39
39
  store: IStore;
40
- /**
41
- * Controls whether the redeem service is enabled.
42
- * - When `true` (default): Manual order fetching with digestKey required
43
- * - When `false`: Event-based order updates, no digestKey required
44
- * @default true
45
- */
46
- setRedeemServiceEnabled?: boolean;
47
40
  };
@@ -1,6 +1,6 @@
1
1
  import { IGardenJS, OrderWithStatus } from '@gardenfi/core';
2
- import { IStore } from '@gardenfi/utils';
2
+ import { IStore, Network } from '@gardenfi/utils';
3
3
 
4
- export declare const useOrderbook: (garden: IGardenJS | undefined, store: IStore) => {
4
+ export declare const useOrderbook: (garden: IGardenJS | undefined, store: IStore, network?: Network | string) => {
5
5
  pendingOrders: OrderWithStatus[];
6
6
  };
@@ -0,0 +1,16 @@
1
+ import { Network, IStore, StoreKeys } from '@gardenfi/utils';
2
+
3
+ export declare class PendingOrdersManager {
4
+ private readonly store;
5
+ private readonly storeKey;
6
+ private readonly defaultStorage;
7
+ constructor(store: IStore, storeKey: StoreKeys);
8
+ private getNetworkKey;
9
+ private getStorageData;
10
+ private saveStorageData;
11
+ get(network?: Network | string): string[];
12
+ set(network: Network | string | undefined, orderIds: string[]): void;
13
+ add(network: Network | string | undefined, orderId: string): void;
14
+ remove(network: Network | string | undefined, orderId: string): void;
15
+ clear(network?: Network | string): void;
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/react-hooks",
3
- "version": "3.0.7-beta.2",
3
+ "version": "3.0.7-beta.4",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -27,9 +27,9 @@
27
27
  "registry": "https://registry.npmjs.org/"
28
28
  },
29
29
  "dependencies": {
30
- "@gardenfi/core": "3.0.8-beta.3",
31
- "@gardenfi/orderbook": "3.0.6-beta.2",
32
- "@gardenfi/utils": "3.0.0-beta.4",
30
+ "@gardenfi/core": "3.0.8-beta.6",
31
+ "@gardenfi/orderbook": "3.0.6-beta.6",
32
+ "@gardenfi/utils": "3.0.0-beta.7",
33
33
  "react": "^18.3.1",
34
34
  "starknet": "8.0.0"
35
35
  },