@gardenfi/react-hooks 2.0.22 → 2.0.24
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 +69 -62
- package/dist/index3.cjs +1 -1
- package/dist/index3.js +45 -33
- package/dist/src/lib/hooks/useOrderbook.d.ts +3 -1
- package/package.json +2 -2
package/dist/index2.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 i=require("react"),R=require("./index3.cjs"),n=require("@gardenfi/core"),a=require("./index5.cjs"),O=require("@gardenfi/orderbook"),S=require("./index6.cjs"),h=i.createContext({isExecuting:!1,isExecutorRequired:!1}),_=({children:d,config:t})=>{var m,C;const[s,p]=i.useState(),{pendingOrders:c,isExecuting:E,digestKey:w}=R.useOrderbook(s,(C=(m=t.walletClient)==null?void 0:m.account)==null?void 0:C.address),k=i.useMemo(()=>!!c.find(r=>{const e=r.status;return e===n.OrderStatus.InitiateDetected||e===n.OrderStatus.Initiated||e===n.OrderStatus.CounterPartyInitiateDetected||e===n.OrderStatus.CounterPartyInitiated||e===n.OrderStatus.RedeemDetected||e===n.OrderStatus.Expired}),[c]),v=i.useMemo(()=>new n.Quote(t.quoteUrl||n.API[t.environment].quote),[t.quoteUrl,t.environment]),q=i.useMemo(()=>async({fromAsset:r,toAsset:e,amount:o,isExactOut:u=!1,request:l})=>await v.getQuote(S.constructOrderpair(r,e),o,u,l),[v]),y=async r=>{if(!s||!t.walletClient)return a.Err("Garden not initialized");const e=await s.swap(r);if(e.error)return a.Err(e.error);if(O.isBitcoin(e.val.source_swap.chain))return a.Ok(e.val);const o=await n.switchOrAddNetwork(r.fromAsset.chain,t.walletClient);if(o.error)return a.Err("Failed to switch network: "+o.error);const u=o.val.walletClient,l=await s.evmRelay.init(u,e.val);if(l.error)return a.Err(l.error);const G={...e.val,source_swap:{...e.val.source_swap,initiate_tx_hash:l.val}};return a.Ok(G)},x=async r=>{if(!s||!t.walletClient)return a.Err("garden not initialized");if(O.isBitcoin(r.source_swap.chain))return a.Err("Not an EVM order: sourceSwap.chain is Bitcoin");const e=await n.switchOrAddNetwork(r.source_swap.chain,t.walletClient);if(e.error)return a.Err("Failed to switch network: "+e.error);const o=e.val.walletClient,u=await s.evmRelay.init(o,r);if(u.error)return a.Err(u.error);const l={...r,source_swap:{...r.source_swap,initiate_tx_hash:u.val}};return a.Ok(l)};return i.useEffect(()=>{var e;if(!t.walletClient)return;if(!((e=t.walletClient.account)!=null&&e.address))throw new Error("WalletClient doesn't have an account");const r=w?n.SecretManager.fromDigestKey(w):void 0;p(new n.Garden({environment:t.environment,evmWallet:t.walletClient,siweOpts:t.siweOpts??{domain:window.location.hostname,store:t.store},apiKey:t.apiKey,secretManager:r,quote:t.quoteUrl??n.API[t.environment].quote,orderbookURl:t.orderBookUrl??n.API[t.environment].orderbook}))},[t.walletClient,w]),i.createElement(h.Provider,{value:{orderBook:s==null?void 0:s.orderbook,quote:v,swapAndInitiate:y,pendingOrders:c,getQuote:q,garden:s,isExecuting:E,isExecutorRequired:k,evmInitiate:x}},d)},P=()=>{const d=i.useContext(h);if(!d)throw new Error("useGarden must be used within a GardenProvider");return d};exports.GardenContext=h;exports.GardenProvider=_;exports.useGarden=P;
|
package/dist/index2.js
CHANGED
|
@@ -1,117 +1,124 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useOrderbook as
|
|
3
|
-
import { OrderStatus as l, Quote as
|
|
4
|
-
import { Err as a, Ok as
|
|
5
|
-
import { isBitcoin as
|
|
6
|
-
import { constructOrderpair as
|
|
7
|
-
const
|
|
1
|
+
import E, { createContext as P, useState as b, useMemo as m, useEffect as U } from "react";
|
|
2
|
+
import { useOrderbook as W } from "./index3.js";
|
|
3
|
+
import { OrderStatus as l, Quote as A, API as p, SecretManager as B, Garden as D, switchOrAddNetwork as x } from "@gardenfi/core";
|
|
4
|
+
import { Err as a, Ok as v } from "./index5.js";
|
|
5
|
+
import { isBitcoin as y } from "@gardenfi/orderbook";
|
|
6
|
+
import { constructOrderpair as K } from "./index6.js";
|
|
7
|
+
const R = P({
|
|
8
8
|
isExecuting: !1,
|
|
9
9
|
isExecutorRequired: !1
|
|
10
|
-
}),
|
|
10
|
+
}), V = ({
|
|
11
11
|
children: u,
|
|
12
12
|
config: t
|
|
13
13
|
}) => {
|
|
14
|
-
|
|
14
|
+
var h, C;
|
|
15
|
+
const [n, _] = b(), { pendingOrders: d, isExecuting: k, digestKey: w } = W(
|
|
16
|
+
n,
|
|
17
|
+
(C = (h = t.walletClient) == null ? void 0 : h.account) == null ? void 0 : C.address
|
|
18
|
+
), O = m(() => !!d.find((r) => {
|
|
15
19
|
const e = r.status;
|
|
16
20
|
return e === l.InitiateDetected || e === l.Initiated || e === l.CounterPartyInitiateDetected || e === l.CounterPartyInitiated || e === l.RedeemDetected || e === l.Expired;
|
|
17
|
-
}), [
|
|
21
|
+
}), [d]), c = m(() => new A(t.quoteUrl || p[t.environment].quote), [t.quoteUrl, t.environment]), q = m(
|
|
18
22
|
() => async ({
|
|
19
23
|
fromAsset: r,
|
|
20
24
|
toAsset: e,
|
|
21
|
-
amount:
|
|
22
|
-
isExactOut:
|
|
25
|
+
amount: o,
|
|
26
|
+
isExactOut: i = !1,
|
|
23
27
|
request: s
|
|
24
|
-
}) => await
|
|
25
|
-
|
|
26
|
-
i,
|
|
28
|
+
}) => await c.getQuote(
|
|
29
|
+
K(r, e),
|
|
27
30
|
o,
|
|
31
|
+
i,
|
|
28
32
|
s
|
|
29
33
|
),
|
|
30
|
-
[
|
|
31
|
-
),
|
|
34
|
+
[c]
|
|
35
|
+
), G = async (r) => {
|
|
32
36
|
if (!n || !t.walletClient) return a("Garden not initialized");
|
|
33
37
|
const e = await n.swap(r);
|
|
34
38
|
if (e.error) return a(e.error);
|
|
35
|
-
if (
|
|
36
|
-
const
|
|
39
|
+
if (y(e.val.source_swap.chain)) return v(e.val);
|
|
40
|
+
const o = await x(
|
|
37
41
|
r.fromAsset.chain,
|
|
38
42
|
t.walletClient
|
|
39
43
|
);
|
|
40
|
-
if (
|
|
41
|
-
return a("Failed to switch network: " +
|
|
42
|
-
const
|
|
44
|
+
if (o.error)
|
|
45
|
+
return a("Failed to switch network: " + o.error);
|
|
46
|
+
const i = o.val.walletClient, s = await n.evmRelay.init(i, e.val);
|
|
43
47
|
if (s.error) return a(s.error);
|
|
44
|
-
const
|
|
48
|
+
const f = {
|
|
45
49
|
...e.val,
|
|
46
50
|
source_swap: {
|
|
47
51
|
...e.val.source_swap,
|
|
48
52
|
initiate_tx_hash: s.val
|
|
49
53
|
}
|
|
50
54
|
};
|
|
51
|
-
return
|
|
52
|
-
},
|
|
55
|
+
return v(f);
|
|
56
|
+
}, I = async (r) => {
|
|
53
57
|
if (!n || !t.walletClient) return a("garden not initialized");
|
|
54
|
-
if (
|
|
58
|
+
if (y(r.source_swap.chain))
|
|
55
59
|
return a("Not an EVM order: sourceSwap.chain is Bitcoin");
|
|
56
|
-
const e = await
|
|
60
|
+
const e = await x(
|
|
57
61
|
r.source_swap.chain,
|
|
58
62
|
t.walletClient
|
|
59
63
|
);
|
|
60
64
|
if (e.error)
|
|
61
65
|
return a("Failed to switch network: " + e.error);
|
|
62
|
-
const
|
|
63
|
-
if (
|
|
66
|
+
const o = e.val.walletClient, i = await n.evmRelay.init(o, r);
|
|
67
|
+
if (i.error) return a(i.error);
|
|
64
68
|
const s = {
|
|
65
69
|
...r,
|
|
66
70
|
source_swap: {
|
|
67
71
|
...r.source_swap,
|
|
68
|
-
initiate_tx_hash:
|
|
72
|
+
initiate_tx_hash: i.val
|
|
69
73
|
}
|
|
70
74
|
};
|
|
71
|
-
return
|
|
75
|
+
return v(s);
|
|
72
76
|
};
|
|
73
|
-
return
|
|
74
|
-
var
|
|
75
|
-
if (t.walletClient)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
77
|
+
return U(() => {
|
|
78
|
+
var e;
|
|
79
|
+
if (!t.walletClient) return;
|
|
80
|
+
if (!((e = t.walletClient.account) != null && e.address))
|
|
81
|
+
throw new Error("WalletClient doesn't have an account");
|
|
82
|
+
const r = w ? B.fromDigestKey(w) : void 0;
|
|
83
|
+
_(
|
|
84
|
+
new D({
|
|
85
|
+
environment: t.environment,
|
|
86
|
+
evmWallet: t.walletClient,
|
|
87
|
+
siweOpts: t.siweOpts ?? {
|
|
88
|
+
domain: window.location.hostname,
|
|
89
|
+
store: t.store
|
|
90
|
+
},
|
|
91
|
+
apiKey: t.apiKey,
|
|
92
|
+
secretManager: r,
|
|
93
|
+
quote: t.quoteUrl ?? p[t.environment].quote,
|
|
94
|
+
orderbookURl: t.orderBookUrl ?? p[t.environment].orderbook
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
}, [t.walletClient, w]), /* @__PURE__ */ E.createElement(
|
|
98
|
+
R.Provider,
|
|
92
99
|
{
|
|
93
100
|
value: {
|
|
94
101
|
orderBook: n == null ? void 0 : n.orderbook,
|
|
95
|
-
quote:
|
|
96
|
-
swapAndInitiate:
|
|
97
|
-
pendingOrders:
|
|
98
|
-
getQuote:
|
|
102
|
+
quote: c,
|
|
103
|
+
swapAndInitiate: G,
|
|
104
|
+
pendingOrders: d,
|
|
105
|
+
getQuote: q,
|
|
99
106
|
garden: n,
|
|
100
|
-
isExecuting:
|
|
101
|
-
isExecutorRequired:
|
|
102
|
-
evmInitiate:
|
|
107
|
+
isExecuting: k,
|
|
108
|
+
isExecutorRequired: O,
|
|
109
|
+
evmInitiate: I
|
|
103
110
|
}
|
|
104
111
|
},
|
|
105
112
|
u
|
|
106
113
|
);
|
|
107
|
-
},
|
|
108
|
-
const u =
|
|
114
|
+
}, j = () => {
|
|
115
|
+
const u = E.useContext(R);
|
|
109
116
|
if (!u)
|
|
110
117
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
111
118
|
return u;
|
|
112
119
|
};
|
|
113
120
|
export {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
121
|
+
R as GardenContext,
|
|
122
|
+
V as GardenProvider,
|
|
123
|
+
j as useGarden
|
|
117
124
|
};
|
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 l=require("@gardenfi/core"),s=require("react"),M=(e,o)=>{const[b,u]=s.useState([]),[c,h]=s.useState(!1),[d,O]=s.useState({}),p=o?d[o]:void 0;return s.useEffect(()=>{if(!e)return;const r=()=>{if(e.secretManager.isInitialized){if(h(!0),!o)return;e.secretManager.getDigestKey().then(t=>{if(t.error){console.error("Failed to get Master DigestKey:",t.error);return}O(i=>({...i,[o]:t.val}))})}};return r(),e.secretManager.on("initialized",r),()=>{e.secretManager.off("initialized",r)}},[e]),s.useEffect(()=>{if(!e||!c)return;const r=e.execute(),t=i=>u(i);return e.on("onPendingOrdersChanged",t),()=>{(async()=>(await r)())(),e.off("onPendingOrdersChanged",t)}},[e,c]),s.useEffect(()=>{e&&e.blockNumberFetcher.fetchBlockNumbers().then(r=>{if(r.error)return;const{val:t}=r;e.orderbook.fetchOrders(!0,!0,{per_page:500}).then(i=>{if(i.error)return;const g=l.filterDeadlineExpiredOrders(i.val.data).map(n=>{const a=t[n.source_swap.chain],f=t[n.destination_swap.chain];if(!a||!f)return;const y=l.ParseOrderStatus(n,a,f);return{...n,status:y}}).filter(n=>n!==void 0);u(g)})})},[e]),{pendingOrders:b,isExecuting:c,digestKey:p}};exports.useOrderbook=M;
|
package/dist/index3.js
CHANGED
|
@@ -1,47 +1,59 @@
|
|
|
1
|
-
import { filterDeadlineExpiredOrders as
|
|
2
|
-
import { useState as
|
|
3
|
-
const
|
|
4
|
-
const [
|
|
5
|
-
return
|
|
1
|
+
import { filterDeadlineExpiredOrders as M, ParseOrderStatus as k } from "@gardenfi/core";
|
|
2
|
+
import { useState as c, useEffect as u } from "react";
|
|
3
|
+
const v = (e, s) => {
|
|
4
|
+
const [b, a] = c([]), [o, h] = c(!1), [p, m] = c({}), O = s ? p[s] : void 0;
|
|
5
|
+
return u(() => {
|
|
6
6
|
if (!e) return;
|
|
7
|
-
const
|
|
8
|
-
e.secretManager.isInitialized
|
|
7
|
+
const r = () => {
|
|
8
|
+
if (e.secretManager.isInitialized) {
|
|
9
|
+
if (h(!0), !s) return;
|
|
10
|
+
e.secretManager.getDigestKey().then((t) => {
|
|
11
|
+
if (t.error) {
|
|
12
|
+
console.error("Failed to get Master DigestKey:", t.error);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
m((i) => ({
|
|
16
|
+
...i,
|
|
17
|
+
[s]: t.val
|
|
18
|
+
}));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
9
21
|
};
|
|
10
|
-
return
|
|
11
|
-
e.secretManager.off("initialized",
|
|
22
|
+
return r(), e.secretManager.on("initialized", r), () => {
|
|
23
|
+
e.secretManager.off("initialized", r);
|
|
12
24
|
};
|
|
13
|
-
}, [e]),
|
|
14
|
-
if (!e || !
|
|
15
|
-
const
|
|
16
|
-
return e.on("onPendingOrdersChanged",
|
|
17
|
-
(async () => (await
|
|
25
|
+
}, [e]), u(() => {
|
|
26
|
+
if (!e || !o) return;
|
|
27
|
+
const r = e.execute(), t = (i) => a(i);
|
|
28
|
+
return e.on("onPendingOrdersChanged", t), () => {
|
|
29
|
+
(async () => (await r)())(), e.off("onPendingOrdersChanged", t);
|
|
18
30
|
};
|
|
19
|
-
}, [e,
|
|
20
|
-
e && e.blockNumberFetcher.fetchBlockNumbers().then((
|
|
21
|
-
if (
|
|
22
|
-
const { val:
|
|
31
|
+
}, [e, o]), u(() => {
|
|
32
|
+
e && e.blockNumberFetcher.fetchBlockNumbers().then((r) => {
|
|
33
|
+
if (r.error) return;
|
|
34
|
+
const { val: t } = r;
|
|
23
35
|
e.orderbook.fetchOrders(!0, !0, {
|
|
24
36
|
per_page: 500
|
|
25
|
-
}).then((
|
|
26
|
-
if (
|
|
27
|
-
const d =
|
|
28
|
-
const
|
|
29
|
-
if (!
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
}).then((i) => {
|
|
38
|
+
if (i.error) return;
|
|
39
|
+
const d = M(i.val.data).map((n) => {
|
|
40
|
+
const f = t[n.source_swap.chain], l = t[n.destination_swap.chain];
|
|
41
|
+
if (!f || !l) return;
|
|
42
|
+
const g = k(
|
|
43
|
+
n,
|
|
44
|
+
f,
|
|
45
|
+
l
|
|
34
46
|
);
|
|
35
47
|
return {
|
|
36
|
-
...
|
|
37
|
-
status:
|
|
48
|
+
...n,
|
|
49
|
+
status: g
|
|
38
50
|
};
|
|
39
|
-
}).filter((
|
|
40
|
-
|
|
51
|
+
}).filter((n) => n !== void 0);
|
|
52
|
+
a(d);
|
|
41
53
|
});
|
|
42
54
|
});
|
|
43
|
-
}, [e]), { pendingOrders:
|
|
55
|
+
}, [e]), { pendingOrders: b, isExecuting: o, digestKey: O };
|
|
44
56
|
};
|
|
45
57
|
export {
|
|
46
|
-
|
|
58
|
+
v as useOrderbook
|
|
47
59
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { IGardenJS, OrderWithStatus } from '@gardenfi/core';
|
|
2
|
+
import { Address } from 'viem';
|
|
2
3
|
|
|
3
|
-
export declare const useOrderbook: (garden: IGardenJS | undefined) => {
|
|
4
|
+
export declare const useOrderbook: (garden: IGardenJS | undefined, address: Address | undefined) => {
|
|
4
5
|
pendingOrders: OrderWithStatus[];
|
|
5
6
|
isExecuting: boolean;
|
|
7
|
+
digestKey: string | undefined;
|
|
6
8
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/react-hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.24",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@catalogfi/wallets": "^0.2.59",
|
|
30
|
-
"@gardenfi/core": "^2.0.
|
|
30
|
+
"@gardenfi/core": "^2.0.20",
|
|
31
31
|
"@gardenfi/orderbook": "^2.0.4",
|
|
32
32
|
"@gardenfi/utils": "^2.0.1",
|
|
33
33
|
"react": "^18.3.1",
|