@gardenfi/react-hooks 2.0.22 → 2.0.23
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 +63 -58
- 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 s=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react"),S=require("./index3.cjs"),n=require("@gardenfi/core"),a=require("./index5.cjs"),p=require("@gardenfi/orderbook"),_=require("./index6.cjs"),h=s.createContext({isExecuting:!1,isExecutorRequired:!1}),R=({children:d,config:t})=>{var C,O;const[i,m]=s.useState(),{pendingOrders:c,isExecuting:E,digestKey:w}=S.useOrderbook(i,(O=(C=t.walletClient)==null?void 0:C.account)==null?void 0:O.address),y=s.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=s.useMemo(()=>new n.Quote(t.quoteUrl||n.API[t.environment].quote),[t.quoteUrl,t.environment]),k=s.useMemo(()=>async({fromAsset:r,toAsset:e,amount:o,isExactOut:u=!1,request:l})=>await v.getQuote(_.constructOrderpair(r,e),o,u,l),[v]),q=async r=>{if(!i||!t.walletClient)return a.Err("Garden not initialized");const e=await i.swap(r);if(e.error)return a.Err(e.error);if(p.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 i.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(!i||!t.walletClient)return a.Err("garden not initialized");if(p.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 i.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 s.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;m(new n.Garden({environment:t.environment,evmWallet:t.walletClient,siweOpts:t.siweOpts??{domain:window.location.hostname,store:t.store},apiKey:t.apiKey,secretManager:r}))},[t.walletClient,w]),s.createElement(h.Provider,{value:{orderBook:i==null?void 0:i.orderbook,quote:v,swapAndInitiate:q,pendingOrders:c,getQuote:k,garden:i,isExecuting:E,isExecutorRequired:y,evmInitiate:x}},d)},P=()=>{const d=s.useContext(h);if(!d)throw new Error("useGarden must be used within a GardenProvider");return d};exports.GardenContext=h;exports.GardenProvider=R;exports.useGarden=P;
|
package/dist/index2.js
CHANGED
|
@@ -1,117 +1,122 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useOrderbook as
|
|
3
|
-
import { OrderStatus as l, Quote as
|
|
4
|
-
import { Err as a, Ok as
|
|
5
|
-
import { isBitcoin as
|
|
1
|
+
import y, { createContext as q, useState as P, useMemo as m, useEffect as W } from "react";
|
|
2
|
+
import { useOrderbook as A } from "./index3.js";
|
|
3
|
+
import { OrderStatus as l, Quote as D, API as K, SecretManager as M, Garden as S, switchOrAddNetwork as C } from "@gardenfi/core";
|
|
4
|
+
import { Err as a, Ok as p } from "./index5.js";
|
|
5
|
+
import { isBitcoin as x } from "@gardenfi/orderbook";
|
|
6
6
|
import { constructOrderpair as b } from "./index6.js";
|
|
7
|
-
const
|
|
7
|
+
const E = q({
|
|
8
8
|
isExecuting: !1,
|
|
9
9
|
isExecutorRequired: !1
|
|
10
|
-
}),
|
|
10
|
+
}), V = ({
|
|
11
11
|
children: u,
|
|
12
12
|
config: t
|
|
13
13
|
}) => {
|
|
14
|
-
|
|
14
|
+
var v, h;
|
|
15
|
+
const [n, _] = P(), { pendingOrders: c, isExecuting: f, digestKey: d } = A(
|
|
16
|
+
n,
|
|
17
|
+
(h = (v = t.walletClient) == null ? void 0 : v.account) == null ? void 0 : h.address
|
|
18
|
+
), O = m(() => !!c.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
|
-
}), [c]), w =
|
|
21
|
+
}), [c]), w = m(() => new D(t.quoteUrl || K[t.environment].quote), [t.quoteUrl, t.environment]), R = m(
|
|
18
22
|
() => async ({
|
|
19
23
|
fromAsset: r,
|
|
20
24
|
toAsset: e,
|
|
21
25
|
amount: i,
|
|
22
|
-
isExactOut:
|
|
23
|
-
request:
|
|
26
|
+
isExactOut: s = !1,
|
|
27
|
+
request: o
|
|
24
28
|
}) => await w.getQuote(
|
|
25
29
|
b(r, e),
|
|
26
30
|
i,
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
s,
|
|
32
|
+
o
|
|
29
33
|
),
|
|
30
34
|
[w]
|
|
31
|
-
),
|
|
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 i = await
|
|
39
|
+
if (x(e.val.source_swap.chain)) return p(e.val);
|
|
40
|
+
const i = await C(
|
|
37
41
|
r.fromAsset.chain,
|
|
38
42
|
t.walletClient
|
|
39
43
|
);
|
|
40
44
|
if (i.error)
|
|
41
45
|
return a("Failed to switch network: " + i.error);
|
|
42
|
-
const
|
|
43
|
-
if (
|
|
44
|
-
const
|
|
46
|
+
const s = i.val.walletClient, o = await n.evmRelay.init(s, e.val);
|
|
47
|
+
if (o.error) return a(o.error);
|
|
48
|
+
const I = {
|
|
45
49
|
...e.val,
|
|
46
50
|
source_swap: {
|
|
47
51
|
...e.val.source_swap,
|
|
48
|
-
initiate_tx_hash:
|
|
52
|
+
initiate_tx_hash: o.val
|
|
49
53
|
}
|
|
50
54
|
};
|
|
51
|
-
return
|
|
52
|
-
},
|
|
55
|
+
return p(I);
|
|
56
|
+
}, k = async (r) => {
|
|
53
57
|
if (!n || !t.walletClient) return a("garden not initialized");
|
|
54
|
-
if (
|
|
58
|
+
if (x(r.source_swap.chain))
|
|
55
59
|
return a("Not an EVM order: sourceSwap.chain is Bitcoin");
|
|
56
|
-
const e = await
|
|
60
|
+
const e = await C(
|
|
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 i = e.val.walletClient,
|
|
63
|
-
if (
|
|
64
|
-
const
|
|
66
|
+
const i = e.val.walletClient, s = await n.evmRelay.init(i, r);
|
|
67
|
+
if (s.error) return a(s.error);
|
|
68
|
+
const o = {
|
|
65
69
|
...r,
|
|
66
70
|
source_swap: {
|
|
67
71
|
...r.source_swap,
|
|
68
|
-
initiate_tx_hash:
|
|
72
|
+
initiate_tx_hash: s.val
|
|
69
73
|
}
|
|
70
74
|
};
|
|
71
|
-
return
|
|
75
|
+
return p(o);
|
|
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 W(() => {
|
|
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 = d ? M.fromDigestKey(d) : void 0;
|
|
83
|
+
_(
|
|
84
|
+
new S({
|
|
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
|
+
})
|
|
94
|
+
);
|
|
95
|
+
}, [t.walletClient, d]), /* @__PURE__ */ y.createElement(
|
|
96
|
+
E.Provider,
|
|
92
97
|
{
|
|
93
98
|
value: {
|
|
94
99
|
orderBook: n == null ? void 0 : n.orderbook,
|
|
95
100
|
quote: w,
|
|
96
|
-
swapAndInitiate:
|
|
101
|
+
swapAndInitiate: G,
|
|
97
102
|
pendingOrders: c,
|
|
98
|
-
getQuote:
|
|
103
|
+
getQuote: R,
|
|
99
104
|
garden: n,
|
|
100
|
-
isExecuting:
|
|
101
|
-
isExecutorRequired:
|
|
102
|
-
evmInitiate:
|
|
105
|
+
isExecuting: f,
|
|
106
|
+
isExecutorRequired: O,
|
|
107
|
+
evmInitiate: k
|
|
103
108
|
}
|
|
104
109
|
},
|
|
105
110
|
u
|
|
106
111
|
);
|
|
107
|
-
},
|
|
108
|
-
const u =
|
|
112
|
+
}, j = () => {
|
|
113
|
+
const u = y.useContext(E);
|
|
109
114
|
if (!u)
|
|
110
115
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
111
116
|
return u;
|
|
112
117
|
};
|
|
113
118
|
export {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
119
|
+
E as GardenContext,
|
|
120
|
+
V as GardenProvider,
|
|
121
|
+
j as useGarden
|
|
117
122
|
};
|
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.23",
|
|
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.19",
|
|
31
31
|
"@gardenfi/orderbook": "^2.0.4",
|
|
32
32
|
"@gardenfi/utils": "^2.0.1",
|
|
33
33
|
"react": "^18.3.1",
|