@gardenfi/react-hooks 0.0.2-beta.8 → 2.0.0

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 n=require("react"),q=require("./index3.cjs"),s=require("@gardenfi/core"),a=require("@catalogfi/utils"),m=require("@gardenfi/orderbook"),x=require("./index5.cjs"),w=n.createContext({isExecuting:!1,isExecutorRequired:!1}),S=({children:u,config:o})=>{const[e,v]=n.useState();console.log("garden :",e);const[C,O]=n.useState(),{pendingOrders:d}=q.useOrderbook(e),E=n.useMemo(()=>!!(e!=null&&e.secretManager.isInitialized),[e]),f=n.useMemo(()=>!!d.find(r=>{const t=r.status;return t===s.OrderStatus.InitiateDetected||t===s.OrderStatus.Initiated||t===s.OrderStatus.CounterPartyInitiateDetected||t===s.OrderStatus.CounterPartyInitiated||t===s.OrderStatus.RedeemDetected||t===s.OrderStatus.Expired}),[d]),G=async r=>{if(!e||!o.walletClient)return a.Err("Garden not initialized");const t=await e.swap(r);if(t.error)return a.Err(t.error);if(m.isBitcoin(t.val.source_swap.chain))return a.Ok(t.val);const i=await s.switchOrAddNetwork(r.fromAsset.chain,o.walletClient);if(i.error)return a.Err("Failed to switch network: "+i.error);const l=i.val.walletClient,c=await e.evmRelay.init(l,t.val);if(c.error)return a.Err(c.error);const h={...t.val,source_swap:{...t.val.source_swap,initiate_tx_hash:c.val}};return a.Ok(h)};return n.useEffect(()=>{var r;if(o.walletClient){if(!((r=o.walletClient.account)!=null&&r.address))throw new Error("WalletClient doesn't have an account");v(new s.Garden({environment:o.environment,evmWallet:o.walletClient}))}},[o.walletClient]),n.useEffect(()=>{e&&O(()=>async({fromAsset:r,toAsset:t,amount:i,isExactOut:l=!1})=>await e.quote.getQuote(x.constructOrderpair(r,t),i,l))},[e]),n.createElement(w.Provider,{value:{orderBook:e==null?void 0:e.orderbook,quote:e==null?void 0:e.quote,swapAndInitiate:G,pendingOrders:d,getQuote:C,garden:e,isExecuting:E,isExecutorRequired:f}},u)},k=()=>{const u=n.useContext(w);if(!u)throw new Error("useGarden must be used within a GardenProvider");return u};exports.GardenContext=w;exports.GardenProvider=S;exports.useGarden=k;
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,92 +1,115 @@
1
- import v, { createContext as k, useState as d, useMemo as w, useEffect as m } from "react";
2
- import { useOrderbook as I } from "./index3.js";
3
- import { OrderStatus as o, Garden as q, switchOrAddNetwork as y } from "@gardenfi/core";
4
- import { Err as s, Ok as f } from "@catalogfi/utils";
5
- import { isBitcoin as P } from "@gardenfi/orderbook";
6
- import { constructOrderpair as _ } from "./index5.js";
7
- const p = k({
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
- }), B = ({
11
- children: a,
10
+ }), z = ({
11
+ children: u,
12
12
  config: n
13
13
  }) => {
14
- const [e, C] = d();
15
- console.log("garden :", e);
16
- const [h, x] = d(), { pendingOrders: u } = I(e), E = w(() => !!(e != null && e.secretManager.isInitialized), [e]), G = w(() => !!u.find((r) => {
17
- const t = r.status;
18
- return t === o.InitiateDetected || t === o.Initiated || t === o.CounterPartyInitiateDetected || t === o.CounterPartyInitiated || t === o.RedeemDetected || t === o.Expired;
19
- }), [u]), O = async (r) => {
20
- if (!e || !n.walletClient) return s("Garden not initialized");
21
- const t = await e.swap(r);
22
- if (t.error) return s(t.error);
23
- if (P(t.val.source_swap.chain)) return f(t.val);
24
- const i = await y(
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(
25
23
  r.fromAsset.chain,
26
24
  n.walletClient
27
25
  );
28
- if (i.error)
29
- return s("Failed to switch network: " + i.error);
30
- const l = i.val.walletClient, c = await e.evmRelay.init(l, t.val);
31
- if (c.error) return s(c.error);
32
- const R = {
33
- ...t.val,
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,
34
32
  source_swap: {
35
- ...t.val.source_swap,
36
- initiate_tx_hash: c.val
33
+ ...e.val.source_swap,
34
+ initiate_tx_hash: l.val
37
35
  }
38
36
  };
39
- return f(R);
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);
40
58
  };
41
59
  return m(() => {
42
60
  var r;
43
61
  if (n.walletClient) {
44
62
  if (!((r = n.walletClient.account) != null && r.address))
45
63
  throw new Error("WalletClient doesn't have an account");
46
- C(
47
- new q({
64
+ f(
65
+ new P({
48
66
  environment: n.environment,
49
- evmWallet: n.walletClient
67
+ evmWallet: n.walletClient,
68
+ siweOpts: {
69
+ domain: window.location.hostname,
70
+ store: n.store
71
+ }
50
72
  })
51
73
  );
52
74
  }
53
75
  }, [n.walletClient]), m(() => {
54
- e && x(
76
+ t && E(
55
77
  () => async ({
56
78
  fromAsset: r,
57
- toAsset: t,
58
- amount: i,
59
- isExactOut: l = !1
60
- }) => await e.quote.getQuote(
61
- _(r, t),
62
- i,
63
- l
79
+ toAsset: e,
80
+ amount: a,
81
+ isExactOut: o = !1
82
+ }) => await t.quote.getQuote(
83
+ W(r, e),
84
+ a,
85
+ o
64
86
  )
65
87
  );
66
- }, [e]), /* @__PURE__ */ v.createElement(
67
- p.Provider,
88
+ }, [t]), /* @__PURE__ */ h.createElement(
89
+ C.Provider,
68
90
  {
69
91
  value: {
70
- orderBook: e == null ? void 0 : e.orderbook,
71
- quote: e == null ? void 0 : e.quote,
72
- swapAndInitiate: O,
73
- pendingOrders: u,
74
- getQuote: h,
75
- garden: e,
76
- isExecuting: E,
77
- isExecutorRequired: G
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
78
101
  }
79
102
  },
80
- a
103
+ u
81
104
  );
82
- }, M = () => {
83
- const a = v.useContext(p);
84
- if (!a)
105
+ }, F = () => {
106
+ const u = h.useContext(C);
107
+ if (!u)
85
108
  throw new Error("useGarden must be used within a GardenProvider");
86
- return a;
109
+ return u;
87
110
  };
88
111
  export {
89
- p as GardenContext,
90
- B as GardenProvider,
91
- M as useGarden
112
+ C as GardenContext,
113
+ z as GardenProvider,
114
+ F as useGarden
92
115
  };
package/dist/index3.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@gardenfi/core"),i=require("react"),O=e=>{const[c,u]=i.useState([]);console.log("pendingOrders inside orderbook :",c),console.log("garden inside orderbook",e);const[o,f]=i.useState(!1);return i.useEffect(()=>{if(!e)return;const t=()=>{e.secretManager.isInitialized&&f(!0)};return t(),e.secretManager.on("initialized",t),()=>{e.secretManager.off("initialized",t)}},[e]),i.useEffect(()=>{if(!e||!o)return;console.log("started executor",o);const t=e.execute(),s=n=>u(n);return e.on("onPendingOrdersChanged",s),()=>{(async()=>(await t)())(),e.off("onPendingOrdersChanged",s)}},[e,o]),i.useEffect(()=>{e&&e.blockNumberFetcher.fetchBlockNumbers().then(t=>{if(t.error)return;const{val:s}=t;e.orderbook.fetchOrders(!0,!0,{per_page:500}).then(n=>{if(n.error)return;const b=d.filterDeadlineExpiredOrders(n.val.data).map(r=>{const a=s[r.source_swap.chain],l=s[r.destination_swap.chain];if(!a||!l)return;const h=d.ParseOrderStatus(r,a,l);return{...r,status:h}}).filter(r=>r!==void 0);u(b)})})},[e]),{pendingOrders:c}};exports.useOrderbook=O;
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,49 +1,46 @@
1
- import { filterDeadlineExpiredOrders as p, ParseOrderStatus as k } from "@gardenfi/core";
2
- import { useState as d, useEffect as o } from "react";
1
+ import { filterDeadlineExpiredOrders as m, ParseOrderStatus as p } from "@gardenfi/core";
2
+ import { useState as f, useEffect as o } from "react";
3
3
  const z = (e) => {
4
- const [c, u] = d([]);
5
- console.log("pendingOrders inside orderbook :", c), console.log("garden inside orderbook", e);
6
- const [s, f] = d(!1);
4
+ const [l, c] = f([]), [s, b] = f(!1);
7
5
  return o(() => {
8
6
  if (!e) return;
9
7
  const t = () => {
10
- e.secretManager.isInitialized && f(!0);
8
+ e.secretManager.isInitialized && b(!0);
11
9
  };
12
10
  return t(), e.secretManager.on("initialized", t), () => {
13
11
  e.secretManager.off("initialized", t);
14
12
  };
15
13
  }, [e]), o(() => {
16
14
  if (!e || !s) return;
17
- console.log("started executor", s);
18
- const t = e.execute(), n = (i) => u(i);
19
- return e.on("onPendingOrdersChanged", n), () => {
20
- (async () => (await t)())(), e.off("onPendingOrdersChanged", n);
15
+ const t = e.execute(), i = (n) => c(n);
16
+ return e.on("onPendingOrdersChanged", i), () => {
17
+ (async () => (await t)())(), e.off("onPendingOrdersChanged", i);
21
18
  };
22
19
  }, [e, s]), o(() => {
23
20
  e && e.blockNumberFetcher.fetchBlockNumbers().then((t) => {
24
21
  if (t.error) return;
25
- const { val: n } = t;
22
+ const { val: i } = t;
26
23
  e.orderbook.fetchOrders(!0, !0, {
27
24
  per_page: 500
28
- }).then((i) => {
29
- if (i.error) return;
30
- const b = p(i.val.data).map((r) => {
31
- const a = n[r.source_swap.chain], l = n[r.destination_swap.chain];
32
- if (!a || !l) return;
33
- const h = k(
25
+ }).then((n) => {
26
+ if (n.error) return;
27
+ const d = m(n.val.data).map((r) => {
28
+ const u = i[r.source_swap.chain], a = i[r.destination_swap.chain];
29
+ if (!u || !a) return;
30
+ const h = p(
34
31
  r,
35
- a,
36
- l
32
+ u,
33
+ a
37
34
  );
38
35
  return {
39
36
  ...r,
40
37
  status: h
41
38
  };
42
39
  }).filter((r) => r !== void 0);
43
- u(b);
40
+ c(d);
44
41
  });
45
42
  });
46
- }, [e]), { pendingOrders: c };
43
+ }, [e]), { pendingOrders: l, isExecuting: s };
47
44
  };
48
45
  export {
49
46
  z as useOrderbook
@@ -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;
@@ -2,4 +2,5 @@ import { IGardenJS, OrderWithStatus } from '@gardenfi/core';
2
2
 
3
3
  export declare const useOrderbook: (garden: IGardenJS | undefined) => {
4
4
  pendingOrders: OrderWithStatus[];
5
+ isExecuting: boolean;
5
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/react-hooks",
3
- "version": "0.0.2-beta.8",
3
+ "version": "2.0.0",
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": "^0.3.0-beta.17",
32
- "@gardenfi/orderbook": "^0.2.0-beta.42",
33
- "@gardenfi/utils": "^0.0.1-beta.21",
31
+ "@gardenfi/core": "^2.0.0",
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"