@gardenfi/react-hooks 3.0.6 → 3.0.7-beta.1
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 +1 -1
- package/dist/index2.js +41 -41
- package/dist/index3.cjs +1 -1
- package/dist/index3.js +23 -21
- package/dist/src/lib/context/gardenProvider.types.d.ts +3 -2
- package/package.json +7 -7
package/dist/index2.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react"),
|
|
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;
|
package/dist/index2.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useOrderbook as
|
|
3
|
-
import { resolveApiConfig as
|
|
4
|
-
import { resolveAuth as
|
|
5
|
-
import { useDigestKey as
|
|
6
|
-
import { Err as
|
|
7
|
-
import { PENDING_ORDERS_STORE as
|
|
8
|
-
const
|
|
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({
|
|
9
9
|
pendingOrders: []
|
|
10
10
|
}), b = ({
|
|
11
11
|
children: i,
|
|
@@ -13,62 +13,62 @@ const f = x({
|
|
|
13
13
|
store: u,
|
|
14
14
|
setRedeemServiceEnabled: o = !0
|
|
15
15
|
}) => {
|
|
16
|
-
const [s,
|
|
17
|
-
const { api: t } =
|
|
18
|
-
return e.quote ?? new
|
|
19
|
-
}, [e.environment, e.quote, e.apiKey]),
|
|
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(
|
|
20
20
|
() => async ({
|
|
21
21
|
fromAsset: t,
|
|
22
|
-
toAsset:
|
|
23
|
-
amount:
|
|
24
|
-
isExactOut:
|
|
25
|
-
options:
|
|
22
|
+
toAsset: d,
|
|
23
|
+
amount: r,
|
|
24
|
+
isExactOut: a = !1,
|
|
25
|
+
options: n
|
|
26
26
|
}) => await (s ? s.quote : p).getQuoteFromAssets({
|
|
27
27
|
fromAsset: t,
|
|
28
|
-
toAsset:
|
|
29
|
-
amount:
|
|
30
|
-
isExactOut:
|
|
31
|
-
options:
|
|
28
|
+
toAsset: d,
|
|
29
|
+
amount: r,
|
|
30
|
+
isExactOut: a,
|
|
31
|
+
options: n
|
|
32
32
|
}),
|
|
33
33
|
[s, p]
|
|
34
|
-
),
|
|
35
|
-
if (!s) return
|
|
36
|
-
const r = await s.createSwap(t);
|
|
37
|
-
if (!r.val) return
|
|
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
38
|
if (o)
|
|
39
39
|
try {
|
|
40
|
-
const a = u.getItem(
|
|
40
|
+
const a = u.getItem(h), n = a ? JSON.parse(a) : [];
|
|
41
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
|
|
43
|
-
u.setItem(
|
|
42
|
+
const m = Array.from(new Set(n));
|
|
43
|
+
u.setItem(h, JSON.stringify(m));
|
|
44
44
|
} catch (a) {
|
|
45
45
|
console.error("Failed to persist pending order id", a);
|
|
46
46
|
}
|
|
47
|
-
return
|
|
47
|
+
return D(r.val);
|
|
48
48
|
};
|
|
49
|
-
return
|
|
49
|
+
return x(() => {
|
|
50
50
|
if (!("wallets" in e) && !("htlc" in e)) return;
|
|
51
51
|
let t;
|
|
52
|
-
if ("wallets" in e && Object.keys(e.wallets ?? {}).length > 0 &&
|
|
53
|
-
t =
|
|
52
|
+
if ("wallets" in e && Object.keys(e.wallets ?? {}).length > 0 && v(e.wallets ?? {}))
|
|
53
|
+
t = y.fromWallets({
|
|
54
54
|
...e,
|
|
55
55
|
digestKey: o ? void 0 : l
|
|
56
56
|
}).setRedeemServiceEnabled(o);
|
|
57
|
-
else if ("htlc" in e && Object.keys(e.htlc ?? {}).length > 0 &&
|
|
58
|
-
t = new
|
|
57
|
+
else if ("htlc" in e && Object.keys(e.htlc ?? {}).length > 0 && v(e.htlc ?? {}))
|
|
58
|
+
t = new y({
|
|
59
59
|
...e,
|
|
60
60
|
digestKey: o ? void 0 : l
|
|
61
61
|
}).setRedeemServiceEnabled(o);
|
|
62
62
|
else
|
|
63
63
|
return;
|
|
64
|
-
|
|
65
|
-
}, [e, l, o]), /* @__PURE__ */
|
|
66
|
-
|
|
64
|
+
G(t);
|
|
65
|
+
}, [e, l, o]), /* @__PURE__ */ f.createElement(
|
|
66
|
+
O.Provider,
|
|
67
67
|
{
|
|
68
68
|
value: {
|
|
69
|
-
swap:
|
|
70
|
-
pendingOrders:
|
|
71
|
-
getQuote:
|
|
69
|
+
swap: k,
|
|
70
|
+
pendingOrders: q,
|
|
71
|
+
getQuote: _,
|
|
72
72
|
garden: s,
|
|
73
73
|
orderBook: s
|
|
74
74
|
}
|
|
@@ -76,13 +76,13 @@ const f = x({
|
|
|
76
76
|
i
|
|
77
77
|
);
|
|
78
78
|
}, z = () => {
|
|
79
|
-
const i =
|
|
79
|
+
const i = f.useContext(O);
|
|
80
80
|
if (!i)
|
|
81
81
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
82
82
|
return i;
|
|
83
83
|
};
|
|
84
84
|
export {
|
|
85
|
-
|
|
85
|
+
O as GardenContext,
|
|
86
86
|
b as GardenProvider,
|
|
87
87
|
z as useGarden
|
|
88
88
|
};
|
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"),
|
|
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;
|
package/dist/index3.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { OrderLifecycle as
|
|
2
|
-
import { useState as
|
|
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
3
|
import { PENDING_ORDERS_STORE as I } from "./index6.js";
|
|
4
|
-
const
|
|
5
|
-
const [
|
|
6
|
-
var n, o, u, l, i, s, t, d, c, E;
|
|
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;
|
|
7
7
|
if (!r) return [];
|
|
8
8
|
const e = /* @__PURE__ */ new Set();
|
|
9
9
|
try {
|
|
@@ -12,10 +12,12 @@ const q = (r, a) => {
|
|
|
12
12
|
(l = (u = r.htlcs) == null ? void 0 : u.sui) == null ? void 0 : l.htlcActorAddress,
|
|
13
13
|
(s = (i = r.htlcs) == null ? void 0 : i.solana) == null ? void 0 : s.htlcActorAddress,
|
|
14
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
|
|
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
|
|
16
18
|
].filter((p) => !!p && p.length > 0).forEach((p) => e.add(p.toLowerCase()));
|
|
17
|
-
} catch (
|
|
18
|
-
console.error("Error getting HTLC addresses:",
|
|
19
|
+
} catch (b) {
|
|
20
|
+
console.error("Error getting HTLC addresses:", b);
|
|
19
21
|
}
|
|
20
22
|
return Array.from(e);
|
|
21
23
|
}, [r]), P = C(async () => {
|
|
@@ -31,12 +33,12 @@ const q = (r, a) => {
|
|
|
31
33
|
const [t, d] = await Promise.all([
|
|
32
34
|
r.getOrders({
|
|
33
35
|
address: s,
|
|
34
|
-
status:
|
|
36
|
+
status: k.pending,
|
|
35
37
|
per_page: 500
|
|
36
38
|
}),
|
|
37
39
|
r.getOrders({
|
|
38
40
|
address: s,
|
|
39
|
-
status:
|
|
41
|
+
status: k.notInitiated,
|
|
40
42
|
per_page: 500
|
|
41
43
|
})
|
|
42
44
|
]), c = [];
|
|
@@ -92,33 +94,33 @@ const q = (r, a) => {
|
|
|
92
94
|
} catch (e) {
|
|
93
95
|
console.error("Error fetching pending orders:", e);
|
|
94
96
|
}
|
|
95
|
-
}, [r, a]), m =
|
|
97
|
+
}, [r, a]), m = x(() => {
|
|
96
98
|
try {
|
|
97
99
|
return h().join(",");
|
|
98
100
|
} catch (e) {
|
|
99
101
|
return console.error("Error computing HTLC addresses:", e), "";
|
|
100
102
|
}
|
|
101
|
-
}, [h]), w =
|
|
102
|
-
return
|
|
103
|
+
}, [h]), w = A("");
|
|
104
|
+
return F(() => {
|
|
103
105
|
if (!r || !r.redeemServiceEnabled || !m) return;
|
|
104
|
-
w.current !== m && (
|
|
105
|
-
}, [r, m, P]),
|
|
106
|
+
w.current !== m && (y.current = !1, w.current = m), y.current || (P(), y.current = !0);
|
|
107
|
+
}, [r, m, P]), F(() => {
|
|
106
108
|
if (r)
|
|
107
109
|
if (r.redeemServiceEnabled) {
|
|
108
110
|
let e = !1;
|
|
109
111
|
const n = () => {
|
|
110
|
-
e || (f.current = setTimeout(o,
|
|
112
|
+
e || (f.current = setTimeout(o, D));
|
|
111
113
|
}, o = async () => {
|
|
112
114
|
if (!e) {
|
|
113
|
-
if (
|
|
115
|
+
if (g.current) {
|
|
114
116
|
n();
|
|
115
117
|
return;
|
|
116
118
|
}
|
|
117
|
-
|
|
119
|
+
g.current = !0;
|
|
118
120
|
try {
|
|
119
121
|
await R();
|
|
120
122
|
} finally {
|
|
121
|
-
|
|
123
|
+
g.current = !1;
|
|
122
124
|
}
|
|
123
125
|
n();
|
|
124
126
|
}
|
|
@@ -132,8 +134,8 @@ const q = (r, a) => {
|
|
|
132
134
|
r.off("onPendingOrdersChanged", e);
|
|
133
135
|
};
|
|
134
136
|
}
|
|
135
|
-
}, [r, R]), { pendingOrders:
|
|
137
|
+
}, [r, R]), { pendingOrders: _ };
|
|
136
138
|
};
|
|
137
139
|
export {
|
|
138
|
-
|
|
140
|
+
j as useOrderbook
|
|
139
141
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OrderWithStatus, QuoteParamsForAssets, IGardenJS, QuoteResponse, SwapParams, GardenConfigWithHTLCs, GardenConfigWithWallets } from '@gardenfi/core';
|
|
1
|
+
import { OrderWithStatus, QuoteParamsForAssets, OrderCreationStatus, IGardenJS, QuoteResponse, SwapParams, GardenConfigWithHTLCs, GardenConfigWithWallets } from '@gardenfi/core';
|
|
2
2
|
import { CreateOrderResponse, IOrderbook } from '@gardenfi/orderbook';
|
|
3
3
|
import { AsyncResult, IStore } from '@gardenfi/utils';
|
|
4
4
|
|
|
@@ -11,9 +11,10 @@ export type GardenContextType = {
|
|
|
11
11
|
/**
|
|
12
12
|
* Create an order and wait until its matched and then initiates if source chain is EVM.
|
|
13
13
|
* @params {SwapParams} - The parameters for creating the order.
|
|
14
|
+
* @params onProgress - Optional callback for progress messages: 'order created', 'order initiating', 'order initiated'.
|
|
14
15
|
* @returns {AsyncResult<string, string>} - create order ID.
|
|
15
16
|
*/
|
|
16
|
-
swap?: (params: SwapParams) => AsyncResult<CreateOrderResponse | string, string>;
|
|
17
|
+
swap?: (params: SwapParams, onProgress?: (status: OrderCreationStatus) => void) => AsyncResult<CreateOrderResponse | string, string>;
|
|
17
18
|
/**
|
|
18
19
|
* Get all the pending orders of the user. This will return all the orders that are yet to be initiated, redeemed, or refunded.
|
|
19
20
|
* It will not return orders that have expired (deadline expiry).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/react-hooks",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.7-beta.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -20,18 +20,18 @@
|
|
|
20
20
|
"build": "vite build",
|
|
21
21
|
"test": "vitest run",
|
|
22
22
|
"dev": "vite build --watch",
|
|
23
|
-
"link": "
|
|
23
|
+
"link": "bun link"
|
|
24
24
|
},
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public",
|
|
27
27
|
"registry": "https://registry.npmjs.org/"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@gardenfi/core": "3.0.
|
|
31
|
-
"@gardenfi/orderbook": "3.0.
|
|
32
|
-
"@gardenfi/utils": "3.0.0",
|
|
30
|
+
"@gardenfi/core": "3.0.8-beta.1",
|
|
31
|
+
"@gardenfi/orderbook": "3.0.6-beta.1",
|
|
32
|
+
"@gardenfi/utils": "3.0.0-beta.3",
|
|
33
33
|
"react": "^18.3.1",
|
|
34
|
-
"starknet": "
|
|
34
|
+
"starknet": "8.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/react": "^18",
|
|
@@ -44,4 +44,4 @@
|
|
|
44
44
|
"vitest": "^1.6.0"
|
|
45
45
|
},
|
|
46
46
|
"sideEffects": false
|
|
47
|
-
}
|
|
47
|
+
}
|