@gardenfi/react-hooks 0.0.2-beta.9 → 2.0.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 +81 -56
- package/dist/index3.cjs +1 -1
- package/dist/index3.js +13 -14
- package/dist/src/index.d.ts +1 -0
- package/dist/src/lib/context/gardenProvider.types.d.ts +7 -0
- package/dist/src/lib/hooks/useOrderbook.d.ts +1 -0
- package/package.json +4 -4
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 s=require("react"),G=require("./index3.cjs"),a=require("@gardenfi/core"),n=require("@catalogfi/utils"),v=require("@gardenfi/orderbook"),q=require("./index5.cjs"),w=s.createContext({isExecuting:!1,isExecutorRequired:!1}),x=({children:c,config:i})=>{const[t,h]=s.useState(),[C,E]=s.useState(),{pendingOrders:d,isExecuting:O}=G.useOrderbook(t),p=s.useMemo(()=>!!d.find(r=>{const e=r.status;return e===a.OrderStatus.InitiateDetected||e===a.OrderStatus.Initiated||e===a.OrderStatus.CounterPartyInitiateDetected||e===a.OrderStatus.CounterPartyInitiated||e===a.OrderStatus.RedeemDetected||e===a.OrderStatus.Expired}),[d]),f=async r=>{if(!t||!i.walletClient)return n.Err("Garden not initialized");const e=await t.swap(r);if(e.error)return n.Err(e.error);if(v.isBitcoin(e.val.source_swap.chain))return n.Ok(e.val);const o=await a.switchOrAddNetwork(r.fromAsset.chain,i.walletClient);if(o.error)return n.Err("Failed to switch network: "+o.error);const u=o.val.walletClient,l=await t.evmRelay.init(u,e.val);if(l.error)return n.Err(l.error);const k={...e.val,source_swap:{...e.val.source_swap,initiate_tx_hash:l.val}};return n.Ok(k)},m=async r=>{if(!t||!i.walletClient)return n.Err("garden not initialized");if(v.isBitcoin(r.source_swap.chain))return n.Err("Not an EVM order: sourceSwap.chain is Bitcoin");const e=await a.switchOrAddNetwork(r.source_swap.chain,i.walletClient);if(e.error)return n.Err("Failed to switch network: "+e.error);const o=e.val.walletClient,u=await t.evmRelay.init(o,r);if(u.error)return n.Err(u.error);const l={...r,source_swap:{...r.source_swap,initiate_tx_hash:u.val}};return n.Ok(l)};return s.useEffect(()=>{var r;if(i.walletClient){if(!((r=i.walletClient.account)!=null&&r.address))throw new Error("WalletClient doesn't have an account");h(new a.Garden({environment:i.environment,evmWallet:i.walletClient,siweOpts:{domain:window.location.hostname,store:i.store}}))}},[i.walletClient]),s.useEffect(()=>{t&&E(()=>async({fromAsset:r,toAsset:e,amount:o,isExactOut:u=!1})=>await t.quote.getQuote(q.constructOrderpair(r,e),o,u))},[t]),s.createElement(w.Provider,{value:{orderBook:t==null?void 0:t.orderbook,quote:t==null?void 0:t.quote,swapAndInitiate:f,pendingOrders:d,getQuote:C,garden:t,isExecuting:O,isExecutorRequired:p,evmInitiate:m}},c)},S=()=>{const c=s.useContext(w);if(!c)throw new Error("useGarden must be used within a GardenProvider");return c};exports.GardenContext=w;exports.GardenProvider=x;exports.useGarden=S;
|
package/dist/index2.js
CHANGED
|
@@ -1,90 +1,115 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useOrderbook as
|
|
3
|
-
import { OrderStatus as
|
|
4
|
-
import { Err as
|
|
5
|
-
import { isBitcoin as
|
|
6
|
-
import { constructOrderpair as
|
|
7
|
-
const
|
|
1
|
+
import h, { createContext as y, useState as d, useMemo as I, useEffect as m } from "react";
|
|
2
|
+
import { useOrderbook as q } from "./index3.js";
|
|
3
|
+
import { OrderStatus as s, Garden as P, switchOrAddNetwork as p } from "@gardenfi/core";
|
|
4
|
+
import { Err as i, Ok as w } from "@catalogfi/utils";
|
|
5
|
+
import { isBitcoin as v } from "@gardenfi/orderbook";
|
|
6
|
+
import { constructOrderpair as W } from "./index5.js";
|
|
7
|
+
const C = y({
|
|
8
8
|
isExecuting: !1,
|
|
9
9
|
isExecutorRequired: !1
|
|
10
|
-
}),
|
|
11
|
-
children:
|
|
10
|
+
}), z = ({
|
|
11
|
+
children: u,
|
|
12
12
|
config: n
|
|
13
13
|
}) => {
|
|
14
|
-
const [
|
|
15
|
-
const
|
|
16
|
-
return
|
|
17
|
-
}), [
|
|
18
|
-
if (!
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
21
|
-
if (
|
|
22
|
-
const
|
|
14
|
+
const [t, f] = d(), [x, E] = d(), { pendingOrders: c, isExecuting: _ } = q(t), R = I(() => !!c.find((r) => {
|
|
15
|
+
const e = r.status;
|
|
16
|
+
return e === s.InitiateDetected || e === s.Initiated || e === s.CounterPartyInitiateDetected || e === s.CounterPartyInitiated || e === s.RedeemDetected || e === s.Expired;
|
|
17
|
+
}), [c]), G = async (r) => {
|
|
18
|
+
if (!t || !n.walletClient) return i("Garden not initialized");
|
|
19
|
+
const e = await t.swap(r);
|
|
20
|
+
if (e.error) return i(e.error);
|
|
21
|
+
if (v(e.val.source_swap.chain)) return w(e.val);
|
|
22
|
+
const a = await p(
|
|
23
23
|
r.fromAsset.chain,
|
|
24
24
|
n.walletClient
|
|
25
25
|
);
|
|
26
|
-
if (
|
|
27
|
-
return
|
|
28
|
-
const
|
|
29
|
-
if (
|
|
30
|
-
const
|
|
31
|
-
...
|
|
26
|
+
if (a.error)
|
|
27
|
+
return i("Failed to switch network: " + a.error);
|
|
28
|
+
const o = a.val.walletClient, l = await t.evmRelay.init(o, e.val);
|
|
29
|
+
if (l.error) return i(l.error);
|
|
30
|
+
const k = {
|
|
31
|
+
...e.val,
|
|
32
32
|
source_swap: {
|
|
33
|
-
...
|
|
34
|
-
initiate_tx_hash:
|
|
33
|
+
...e.val.source_swap,
|
|
34
|
+
initiate_tx_hash: l.val
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
return
|
|
37
|
+
return w(k);
|
|
38
|
+
}, O = async (r) => {
|
|
39
|
+
if (!t || !n.walletClient) return i("garden not initialized");
|
|
40
|
+
if (v(r.source_swap.chain))
|
|
41
|
+
return i("Not an EVM order: sourceSwap.chain is Bitcoin");
|
|
42
|
+
const e = await p(
|
|
43
|
+
r.source_swap.chain,
|
|
44
|
+
n.walletClient
|
|
45
|
+
);
|
|
46
|
+
if (e.error)
|
|
47
|
+
return i("Failed to switch network: " + e.error);
|
|
48
|
+
const a = e.val.walletClient, o = await t.evmRelay.init(a, r);
|
|
49
|
+
if (o.error) return i(o.error);
|
|
50
|
+
const l = {
|
|
51
|
+
...r,
|
|
52
|
+
source_swap: {
|
|
53
|
+
...r.source_swap,
|
|
54
|
+
initiate_tx_hash: o.val
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
return w(l);
|
|
38
58
|
};
|
|
39
59
|
return m(() => {
|
|
40
60
|
var r;
|
|
41
61
|
if (n.walletClient) {
|
|
42
62
|
if (!((r = n.walletClient.account) != null && r.address))
|
|
43
63
|
throw new Error("WalletClient doesn't have an account");
|
|
44
|
-
|
|
45
|
-
new
|
|
64
|
+
f(
|
|
65
|
+
new P({
|
|
46
66
|
environment: n.environment,
|
|
47
|
-
evmWallet: n.walletClient
|
|
67
|
+
evmWallet: n.walletClient,
|
|
68
|
+
siweOpts: {
|
|
69
|
+
domain: window.location.hostname,
|
|
70
|
+
store: n.store
|
|
71
|
+
}
|
|
48
72
|
})
|
|
49
73
|
);
|
|
50
74
|
}
|
|
51
75
|
}, [n.walletClient]), m(() => {
|
|
52
|
-
|
|
76
|
+
t && E(
|
|
53
77
|
() => async ({
|
|
54
78
|
fromAsset: r,
|
|
55
|
-
toAsset:
|
|
56
|
-
amount:
|
|
57
|
-
isExactOut:
|
|
58
|
-
}) => await
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
79
|
+
toAsset: e,
|
|
80
|
+
amount: a,
|
|
81
|
+
isExactOut: o = !1
|
|
82
|
+
}) => await t.quote.getQuote(
|
|
83
|
+
W(r, e),
|
|
84
|
+
a,
|
|
85
|
+
o
|
|
62
86
|
)
|
|
63
87
|
);
|
|
64
|
-
}, [
|
|
65
|
-
|
|
88
|
+
}, [t]), /* @__PURE__ */ h.createElement(
|
|
89
|
+
C.Provider,
|
|
66
90
|
{
|
|
67
91
|
value: {
|
|
68
|
-
orderBook:
|
|
69
|
-
quote:
|
|
70
|
-
swapAndInitiate:
|
|
71
|
-
pendingOrders:
|
|
72
|
-
getQuote:
|
|
73
|
-
garden:
|
|
74
|
-
isExecuting:
|
|
75
|
-
isExecutorRequired:
|
|
92
|
+
orderBook: t == null ? void 0 : t.orderbook,
|
|
93
|
+
quote: t == null ? void 0 : t.quote,
|
|
94
|
+
swapAndInitiate: G,
|
|
95
|
+
pendingOrders: c,
|
|
96
|
+
getQuote: x,
|
|
97
|
+
garden: t,
|
|
98
|
+
isExecuting: _,
|
|
99
|
+
isExecutorRequired: R,
|
|
100
|
+
evmInitiate: O
|
|
76
101
|
}
|
|
77
102
|
},
|
|
78
|
-
|
|
103
|
+
u
|
|
79
104
|
);
|
|
80
|
-
},
|
|
81
|
-
const
|
|
82
|
-
if (!
|
|
105
|
+
}, F = () => {
|
|
106
|
+
const u = h.useContext(C);
|
|
107
|
+
if (!u)
|
|
83
108
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
84
|
-
return
|
|
109
|
+
return u;
|
|
85
110
|
};
|
|
86
111
|
export {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
112
|
+
C as GardenContext,
|
|
113
|
+
z as GardenProvider,
|
|
114
|
+
F as useGarden
|
|
90
115
|
};
|
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 f=require("@gardenfi/core"),n=require("react"),O=e=>{const[l,u]=n.useState([]),[c,b]=n.useState(!1);return n.useEffect(()=>{if(!e)return;const t=()=>{e.secretManager.isInitialized&&b(!0)};return t(),e.secretManager.on("initialized",t),()=>{e.secretManager.off("initialized",t)}},[e]),n.useEffect(()=>{if(!e||!c)return;const t=e.execute(),i=s=>u(s);return e.on("onPendingOrdersChanged",i),()=>{(async()=>(await t)())(),e.off("onPendingOrdersChanged",i)}},[e,c]),n.useEffect(()=>{e&&e.blockNumberFetcher.fetchBlockNumbers().then(t=>{if(t.error)return;const{val:i}=t;e.orderbook.fetchOrders(!0,!0,{per_page:500}).then(s=>{if(s.error)return;const d=f.filterDeadlineExpiredOrders(s.val.data).map(r=>{const o=i[r.source_swap.chain],a=i[r.destination_swap.chain];if(!o||!a)return;const h=f.ParseOrderStatus(r,o,a);return{...r,status:h}}).filter(r=>r!==void 0);u(d)})})},[e]),{pendingOrders:l,isExecuting:c}};exports.useOrderbook=O;
|
package/dist/index3.js
CHANGED
|
@@ -1,47 +1,46 @@
|
|
|
1
1
|
import { filterDeadlineExpiredOrders as m, ParseOrderStatus as p } from "@gardenfi/core";
|
|
2
|
-
import { useState as
|
|
2
|
+
import { useState as f, useEffect as o } from "react";
|
|
3
3
|
const z = (e) => {
|
|
4
|
-
const [
|
|
4
|
+
const [l, c] = f([]), [s, b] = f(!1);
|
|
5
5
|
return o(() => {
|
|
6
6
|
if (!e) return;
|
|
7
7
|
const t = () => {
|
|
8
|
-
|
|
8
|
+
e.secretManager.isInitialized && b(!0);
|
|
9
9
|
};
|
|
10
10
|
return t(), e.secretManager.on("initialized", t), () => {
|
|
11
11
|
e.secretManager.off("initialized", t);
|
|
12
12
|
};
|
|
13
13
|
}, [e]), o(() => {
|
|
14
14
|
if (!e || !s) return;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
(async () => (await t)())(), e.off("onPendingOrdersChanged", r);
|
|
15
|
+
const t = e.execute(), i = (n) => c(n);
|
|
16
|
+
return e.on("onPendingOrdersChanged", i), () => {
|
|
17
|
+
(async () => (await t)())(), e.off("onPendingOrdersChanged", i);
|
|
19
18
|
};
|
|
20
19
|
}, [e, s]), o(() => {
|
|
21
20
|
e && e.blockNumberFetcher.fetchBlockNumbers().then((t) => {
|
|
22
21
|
if (t.error) return;
|
|
23
|
-
const { val:
|
|
22
|
+
const { val: i } = t;
|
|
24
23
|
e.orderbook.fetchOrders(!0, !0, {
|
|
25
24
|
per_page: 500
|
|
26
25
|
}).then((n) => {
|
|
27
26
|
if (n.error) return;
|
|
28
|
-
const d = m(n.val.data).map((
|
|
29
|
-
const u = r
|
|
27
|
+
const d = m(n.val.data).map((r) => {
|
|
28
|
+
const u = i[r.source_swap.chain], a = i[r.destination_swap.chain];
|
|
30
29
|
if (!u || !a) return;
|
|
31
30
|
const h = p(
|
|
32
|
-
|
|
31
|
+
r,
|
|
33
32
|
u,
|
|
34
33
|
a
|
|
35
34
|
);
|
|
36
35
|
return {
|
|
37
|
-
...
|
|
36
|
+
...r,
|
|
38
37
|
status: h
|
|
39
38
|
};
|
|
40
|
-
}).filter((
|
|
39
|
+
}).filter((r) => r !== void 0);
|
|
41
40
|
c(d);
|
|
42
41
|
});
|
|
43
42
|
});
|
|
44
|
-
}, [e]), { pendingOrders:
|
|
43
|
+
}, [e]), { pendingOrders: l, isExecuting: s };
|
|
45
44
|
};
|
|
46
45
|
export {
|
|
47
46
|
z as useOrderbook
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { GardenProvider, useGarden } from './lib/context/GardenProvider';
|
|
2
|
+
export type { QuoteParams, GardenContextType, GardenProviderProps, } from './lib/context/gardenProvider.types';
|
|
2
3
|
export { useOrderbook } from './lib/hooks/useOrderbook';
|
|
3
4
|
export { BitcoinNetwork } from '@catalogfi/wallets';
|
|
4
5
|
export { environment, GARDEN_CONFIG } from './lib/gardenConfig';
|
|
@@ -46,6 +46,13 @@ export type GardenContextType = {
|
|
|
46
46
|
* @returns {IQuote}
|
|
47
47
|
*/
|
|
48
48
|
quote?: IQuote;
|
|
49
|
+
/**
|
|
50
|
+
* Initiates the order in the EVM chain. This can be useful if the initiation is failed when `swapAndInitiate` function is called.
|
|
51
|
+
* @param order - The order to initiate.
|
|
52
|
+
* @returns {AsyncResult<MatchedOrder, string>} - The initiated order.
|
|
53
|
+
* @NOTE This is only required if the source chain is EVM.
|
|
54
|
+
*/
|
|
55
|
+
evmInitiate?: (order: MatchedOrder) => AsyncResult<MatchedOrder, string>;
|
|
49
56
|
};
|
|
50
57
|
export type GardenProviderProps = {
|
|
51
58
|
children: React.ReactNode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/react-hooks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@catalogfi/utils": "^0.1.6",
|
|
30
30
|
"@catalogfi/wallets": "^0.2.51",
|
|
31
|
-
"@gardenfi/core": "^
|
|
32
|
-
"@gardenfi/orderbook": "^
|
|
33
|
-
"@gardenfi/utils": "^0.0
|
|
31
|
+
"@gardenfi/core": "^2.0.2",
|
|
32
|
+
"@gardenfi/orderbook": "^2.0.0",
|
|
33
|
+
"@gardenfi/utils": "^2.0.0",
|
|
34
34
|
"react": "^18.3.1",
|
|
35
35
|
"viem": "^2.21.15",
|
|
36
36
|
"wagmi": "^2.12.16"
|