@gardenfi/react-hooks 0.0.1-beta.6 → 0.0.1-beta.61

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/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index2.cjs"),r=require("./index3.cjs"),o=require("./index4.cjs");exports.GardenProvider=e.GardenProvider;exports.useGarden=e.useGarden;exports.useOrderbook=r.useOrderbook;exports.useSecretManager=o.useSecretManager;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index2.cjs"),r=require("./index3.cjs"),o=require("./index4.cjs"),t=require("@catalogfi/wallets");exports.GardenProvider=e.GardenProvider;exports.useGarden=e.useGarden;exports.useOrderbook=r.useOrderbook;exports.useSecretManager=o.useSecretManager;Object.defineProperty(exports,"BitcoinNetwork",{enumerable:!0,get:()=>t.BitcoinNetwork});
package/dist/index.js CHANGED
@@ -1,9 +1,11 @@
1
- import { GardenProvider as o, useGarden as a } from "./index2.js";
2
- import { useOrderbook as t } from "./index3.js";
1
+ import { GardenProvider as o, useGarden as t } from "./index2.js";
2
+ import { useOrderbook as d } from "./index3.js";
3
3
  import { useSecretManager as m } from "./index4.js";
4
+ import { BitcoinNetwork as p } from "@catalogfi/wallets";
4
5
  export {
6
+ p as BitcoinNetwork,
5
7
  o as GardenProvider,
6
- a as useGarden,
7
- t as useOrderbook,
8
+ t as useGarden,
9
+ d as useOrderbook,
8
10
  m as useSecretManager
9
11
  };
package/dist/index2.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),M=require("wagmi"),R=require("./index4.cjs"),g=require("./index3.cjs"),d=require("@gardenfi/core"),s=require("@catalogfi/utils"),W=require("@gardenfi/orderbook"),E=require("@catalogfi/wallets"),S=require("@gardenfi/utils"),w=e.createContext({}),x=({children:c,config:r})=>{const[n,q]=e.useState(),[a,B]=e.useState(),[u,C]=e.useState(),[G,U]=e.useState(),[m,O]=e.useState(0),{data:o}=M.useWalletClient(),{initializeSecretManager:k}=R.useSecretManager(q),{orderbook:l}=g.useOrderbook(r.orderBookUrl,u),v=new d.Quote(r.quoteUrl),b=e.useMemo(()=>new E.BitcoinProvider(r.bitcoinNetwork,r.bitcoinRPCUrl),[r.bitcoinNetwork,r.bitcoinRPCUrl]),y=async t=>{if(!a||!l||!o||!u)return s.Err("Garden not initialized");if(!n){const h=await k();if(h.error)return s.Err(h.error)}const i=await a.swap(t);if(i.error)return s.Err(i.error);if(W.isBitcoin(i.val.source_swap.chain))return s.Ok(i.val);const f=await d.switchOrAddNetwork(t.fromAsset.chain,o);if(f.error)return s.Err("Failed to switch network: "+f.error);const P=await new d.EvmRelay(i.val,r.orderBookUrl,u).init(o);return P.error?s.Err(P.error):s.Ok(i.val)};return e.useEffect(()=>{if(!o)return;const t=new S.Siwe(new S.Url(r.orderBookUrl),o,{store:r.store});C(t)},[o]),e.useEffect(()=>{if(!n)return;const t=E.BitcoinWallet.fromPrivateKey(n.getMasterPrivKey(),b);U(t)},[n,b]),e.useEffect(()=>{if(!n||!o||!l||!u)return;const t=new d.Garden({orderbookURl:r.orderBookUrl,secretManager:n,quote:v,auth:u,wallets:{evmWallet:o,btcWallet:G}});B(t)},[n,o,l]),e.useEffect(()=>{a&&(a.execute(),a.on("onPendingOrdersChanged",t=>{O(t.length)}))},[a]),e.createElement(w.Provider,{value:{orderBookUrl:r.orderBookUrl,initializeSecretManager:k,orderBook:l,swap:y,pendingOrdersCount:m,quote:v}},c)},p=()=>{const c=e.useContext(w);if(!c)throw new Error("useGarden must be used within a GardenProvider");return c};exports.GardenContext=w;exports.GardenProvider=x;exports.useGarden=p;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),W=require("wagmi"),p=require("./index4.cjs"),x=require("./index3.cjs"),d=require("@gardenfi/core"),i=require("@catalogfi/utils"),_=require("@gardenfi/orderbook"),h=require("@catalogfi/wallets"),G=require("@gardenfi/utils"),K=require("./index5.cjs"),m=t.createContext({}),A=({children:w,config:e})=>{const[l,O]=t.useState(),[c,C]=t.useState(),[a,f]=t.useState(),[E,U]=t.useState(),k=new d.Quote(e.quoteUrl),{data:n}=W.useWalletClient(),{initializeSecretManager:q}=p.useSecretManager(O),{orderbook:v}=x.useOrderbook(e.orderBookUrl,a,U),y=t.useMemo(()=>new h.BitcoinProvider(e.bitcoinNetwork,e.bitcoinRPCUrl),[e.bitcoinNetwork,e.bitcoinRPCUrl]),R=async o=>{if(!v||!n||!a)return i.Err("Orderbook or walletClient or auth not initialized");let r=c;if(!l||!r){const b=await q();if(b.error)return i.Err(b.error);const M=h.BitcoinWallet.fromPrivateKey(b.val.getMasterPrivKey(),y);r=new d.Garden({orderbookURl:e.orderBookUrl,secretManager:b.val,quote:k,auth:a,wallets:{evmWallet:n,btcWallet:M}}),C(r)}const s=await r.swap(o);if(s.error)return i.Err(s.error);if(_.isBitcoin(s.val.source_swap.chain))return i.Ok(s.val);const u=await d.switchOrAddNetwork(o.fromAsset.chain,n);if(u.error)return i.Err("Failed to switch network: "+u.error);const B=u.val.walletClient;console.log("newWalletClient :",B);const P=await new d.EvmRelay(s.val,e.orderBookUrl,a).init(B);if(P.error)return i.Err(P.error);const g={...s.val,source_swap:{...s.val.source_swap,initiate_tx_hash:P.val}};return i.Ok(g)},S=async({fromAsset:o,toAsset:r,amount:s,isExactOut:u=!1})=>await k.getQuote(K.constructOrderpair(o,r),s,u);return t.useEffect(()=>{if(!n)return;const o=new G.Siwe(new G.Url(e.orderBookUrl),n,{store:e.store});f(o)},[n]),t.useEffect(()=>{if(!l||!n||!v||!a)return;const o=h.BitcoinWallet.fromPrivateKey(l.getMasterPrivKey(),y),r=new d.Garden({orderbookURl:e.orderBookUrl,secretManager:l,quote:k,auth:a,wallets:{evmWallet:n,btcWallet:o}});C(r)},[l,n,v,a]),t.useEffect(()=>{if(!c)return;const o=c.execute();return c.on("onPendingOrdersChanged",r=>{U(r)}),()=>{(async()=>(await o)())()}},[c]),t.createElement(m.Provider,{value:{orderBookUrl:e.orderBookUrl,initializeSecretManager:q,orderBook:v,swap:R,pendingOrders:E,getQuote:S,secretManager:l,garden:c}},w)},F=()=>{const w=t.useContext(m);if(!w)throw new Error("useGarden must be used within a GardenProvider");return w};exports.GardenContext=m;exports.GardenProvider=A;exports.useGarden=F;
package/dist/index2.js CHANGED
@@ -1,90 +1,131 @@
1
- import b, { createContext as W, useState as s, useMemo as x, useEffect as u } from "react";
2
- import { useWalletClient as E } from "wagmi";
3
- import { useSecretManager as S } from "./index4.js";
4
- import { useOrderbook as g } from "./index3.js";
5
- import { Quote as A, Garden as N, switchOrAddNetwork as q, EvmRelay as z } from "@gardenfi/core";
6
- import { Err as l, Ok as h } from "@catalogfi/utils";
7
- import { isBitcoin as K } from "@gardenfi/orderbook";
8
- import { BitcoinProvider as F, BitcoinWallet as Q } from "@catalogfi/wallets";
9
- import { Siwe as _, Url as j } from "@gardenfi/utils";
10
- const B = W({}), $ = ({
11
- children: c,
12
- config: r
1
+ import y, { createContext as _, useState as m, useMemo as K, useEffect as k } from "react";
2
+ import { useWalletClient as A } from "wagmi";
3
+ import { useSecretManager as F } from "./index4.js";
4
+ import { useOrderbook as N } from "./index3.js";
5
+ import { Quote as Q, Garden as C, switchOrAddNetwork as q, EvmRelay as z } from "@gardenfi/core";
6
+ import { Err as c, Ok as B } from "@catalogfi/utils";
7
+ import { isBitcoin as j } from "@gardenfi/orderbook";
8
+ import { BitcoinProvider as D, BitcoinWallet as R } from "@catalogfi/wallets";
9
+ import { Siwe as H, Url as I } from "@gardenfi/utils";
10
+ import { constructOrderpair as J } from "./index5.js";
11
+ const O = _({}), ne = ({
12
+ children: d,
13
+ config: e
13
14
  }) => {
14
- const [o, P] = s(), [n, U] = s(), [a, C] = s(), [G, R] = s(), [y, M] = s(0), { data: t } = E(), { initializeSecretManager: w } = S(P), { orderbook: d } = g(r.orderBookUrl, a), m = new A(r.quoteUrl), v = x(
15
- () => new F(r.bitcoinNetwork, r.bitcoinRPCUrl),
16
- [r.bitcoinNetwork, r.bitcoinRPCUrl]
17
- ), O = async (e) => {
18
- if (!n || !d || !t || !a)
19
- return l("Garden not initialized");
20
- if (!o) {
21
- const p = await w();
22
- if (p.error) return l(p.error);
15
+ const [s, G] = m(), [i, p] = m(), [a, M] = m(), [W, f] = m(), v = new Q(e.quoteUrl), { data: o } = A(), { initializeSecretManager: h } = F(G), { orderbook: u } = N(
16
+ e.orderBookUrl,
17
+ a,
18
+ f
19
+ ), P = K(
20
+ () => new D(e.bitcoinNetwork, e.bitcoinRPCUrl),
21
+ [e.bitcoinNetwork, e.bitcoinRPCUrl]
22
+ ), g = async (t) => {
23
+ if (!u || !o || !a)
24
+ return c("Orderbook or walletClient or auth not initialized");
25
+ let r = i;
26
+ if (!s || !r) {
27
+ const w = await h();
28
+ if (w.error) return c(w.error);
29
+ const S = R.fromPrivateKey(
30
+ w.val.getMasterPrivKey(),
31
+ P
32
+ );
33
+ r = new C({
34
+ orderbookURl: e.orderBookUrl,
35
+ secretManager: w.val,
36
+ quote: v,
37
+ auth: a,
38
+ wallets: {
39
+ evmWallet: o,
40
+ btcWallet: S
41
+ }
42
+ }), p(r);
23
43
  }
24
- const i = await n.swap(e);
25
- if (i.error) return l(i.error);
26
- if (K(i.val.source_swap.chain)) return h(i.val);
27
- const f = await q(
28
- e.fromAsset.chain,
29
- t
44
+ const n = await r.swap(t);
45
+ if (n.error) return c(n.error);
46
+ if (j(n.val.source_swap.chain)) return B(n.val);
47
+ const l = await q(
48
+ t.fromAsset.chain,
49
+ o
30
50
  );
31
- if (f.error)
32
- return l("Failed to switch network: " + f.error);
33
- const k = await new z(i.val, r.orderBookUrl, a).init(t);
34
- return k.error ? l(k.error) : h(i.val);
35
- };
36
- return u(() => {
37
- if (!t) return;
38
- const e = new _(new j(r.orderBookUrl), t, {
39
- store: r.store
40
- });
41
- C(e);
42
- }, [t]), u(() => {
51
+ if (l.error)
52
+ return c("Failed to switch network: " + l.error);
53
+ const U = l.val.walletClient;
54
+ console.log("newWalletClient :", U);
55
+ const b = await new z(n.val, e.orderBookUrl, a).init(U);
56
+ if (b.error) return c(b.error);
57
+ const E = {
58
+ ...n.val,
59
+ source_swap: {
60
+ ...n.val.source_swap,
61
+ initiate_tx_hash: b.val
62
+ }
63
+ };
64
+ return B(E);
65
+ }, x = async ({
66
+ fromAsset: t,
67
+ toAsset: r,
68
+ amount: n,
69
+ isExactOut: l = !1
70
+ }) => await v.getQuote(
71
+ J(t, r),
72
+ n,
73
+ l
74
+ );
75
+ return k(() => {
43
76
  if (!o) return;
44
- const e = Q.fromPrivateKey(
45
- o.getMasterPrivKey(),
46
- v
47
- );
48
- R(e);
49
- }, [o, v]), u(() => {
50
- if (!o || !t || !d || !a) return;
51
- const e = new N({
52
- orderbookURl: r.orderBookUrl,
53
- secretManager: o,
54
- quote: m,
77
+ const t = new H(new I(e.orderBookUrl), o, {
78
+ store: e.store
79
+ });
80
+ M(t);
81
+ }, [o]), k(() => {
82
+ if (!s || !o || !u || !a) return;
83
+ const t = R.fromPrivateKey(
84
+ s.getMasterPrivKey(),
85
+ P
86
+ ), r = new C({
87
+ orderbookURl: e.orderBookUrl,
88
+ secretManager: s,
89
+ quote: v,
55
90
  auth: a,
56
91
  wallets: {
57
- evmWallet: t,
58
- btcWallet: G
92
+ evmWallet: o,
93
+ btcWallet: t
59
94
  }
60
95
  });
61
- U(e);
62
- }, [o, t, d]), u(() => {
63
- n && (n.execute(), n.on("onPendingOrdersChanged", (e) => {
64
- M(e.length);
65
- }));
66
- }, [n]), /* @__PURE__ */ b.createElement(
67
- B.Provider,
96
+ p(r);
97
+ }, [s, o, u, a]), k(() => {
98
+ if (!i) return;
99
+ const t = i.execute();
100
+ return i.on("onPendingOrdersChanged", (r) => {
101
+ f(r);
102
+ }), () => {
103
+ (async () => (await t)())();
104
+ };
105
+ }, [i]), /* @__PURE__ */ y.createElement(
106
+ O.Provider,
68
107
  {
69
108
  value: {
70
- orderBookUrl: r.orderBookUrl,
71
- initializeSecretManager: w,
72
- orderBook: d,
73
- swap: O,
74
- pendingOrdersCount: y,
75
- quote: m
109
+ orderBookUrl: e.orderBookUrl,
110
+ initializeSecretManager: h,
111
+ orderBook: u,
112
+ swap: g,
113
+ pendingOrders: W,
114
+ getQuote: x,
115
+ secretManager: s,
116
+ garden: i
76
117
  }
77
118
  },
78
- c
119
+ d
79
120
  );
80
- }, rr = () => {
81
- const c = b.useContext(B);
82
- if (!c)
121
+ }, ae = () => {
122
+ const d = y.useContext(O);
123
+ if (!d)
83
124
  throw new Error("useGarden must be used within a GardenProvider");
84
- return c;
125
+ return d;
85
126
  };
86
127
  export {
87
- B as GardenContext,
88
- $ as GardenProvider,
89
- rr as useGarden
128
+ O as GardenContext,
129
+ ne as GardenProvider,
130
+ ae as useGarden
90
131
  };
package/dist/index3.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@gardenfi/orderbook"),t=require("react"),b=require("wagmi"),i=(e,r)=>{const{data:o}=b.useWalletClient(),[s,n]=t.useState();return t.useEffect(()=>{if(!o||!e||!r)return;const u=new c.Orderbook({url:e,walletClient:o,auth:r});n(u)},[o,e]),{orderbook:s}};exports.useOrderbook=i;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@gardenfi/orderbook"),s=require("react"),b=require("wagmi"),l=(e,r,u)=>{const{data:t}=b.useWalletClient(),[a,c]=s.useState();return s.useEffect(()=>{if(!t||!e||!r)return;const n=new i.Orderbook({url:e,walletClient:t,auth:r});c(n),n.fetchOrders(!0,!0,{per_page:500}).then(o=>{!o.error&&o.val&&u(o.val.data)})},[t,e,r]),{orderbook:a}};exports.useOrderbook=l;
package/dist/index3.js CHANGED
@@ -1,18 +1,22 @@
1
- import { Orderbook as f } from "@gardenfi/orderbook";
2
- import { useState as i, useEffect as m } from "react";
3
- import { useWalletClient as u } from "wagmi";
4
- const a = (e, o) => {
5
- const { data: t } = u(), [r, n] = i();
6
- return m(() => {
7
- if (!t || !e || !o) return;
8
- const s = new f({
1
+ import { Orderbook as i } from "@gardenfi/orderbook";
2
+ import { useState as l, useEffect as u } from "react";
3
+ import { useWalletClient as c } from "wagmi";
4
+ const d = (e, t, a) => {
5
+ const { data: r } = c(), [f, s] = l();
6
+ return u(() => {
7
+ if (!r || !e || !t) return;
8
+ const n = new i({
9
9
  url: e,
10
- walletClient: t,
11
- auth: o
10
+ walletClient: r,
11
+ auth: t
12
12
  });
13
- n(s);
14
- }, [t, e]), { orderbook: r };
13
+ s(n), n.fetchOrders(!0, !0, {
14
+ per_page: 500
15
+ }).then((o) => {
16
+ !o.error && o.val && a(o.val.data);
17
+ });
18
+ }, [r, e, t]), { orderbook: f };
15
19
  };
16
20
  export {
17
- a as useOrderbook
21
+ d as useOrderbook
18
22
  };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=(r,c)=>`${r.chain}:${r.atomicSwapAddress}::${c.chain}:${c.atomicSwapAddress}`;exports.constructOrderpair=a;
package/dist/index5.js ADDED
@@ -0,0 +1,4 @@
1
+ const r = (a, c) => `${a.chain}:${a.atomicSwapAddress}::${c.chain}:${c.atomicSwapAddress}`;
2
+ export {
3
+ r as constructOrderpair
4
+ };
@@ -1,3 +1,4 @@
1
1
  export { GardenProvider, useGarden } from './lib/context/GardenProvider';
2
2
  export { useOrderbook } from './lib/hooks/useOrderbook';
3
3
  export { useSecretManager } from './lib/hooks/useSecretManager';
4
+ export { BitcoinNetwork } from '@catalogfi/wallets';
@@ -1,7 +1,7 @@
1
1
  import { AsyncResult } from '@catalogfi/utils';
2
2
  import { BitcoinNetwork } from '@catalogfi/wallets';
3
- import { IQuote, SecretManager, SwapParams } from '@gardenfi/core';
4
- import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
3
+ import { IGardenJS, ISecretManager, QuoteResponse, SecretManager, SwapParams } from '@gardenfi/core';
4
+ import { Asset, IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
5
5
  import { IStore } from '@gardenfi/utils';
6
6
 
7
7
  export type GardenContextType = {
@@ -14,9 +14,10 @@ export type GardenContextType = {
14
14
  * @returns {AsyncResult<string, string>} - create order ID.
15
15
  */
16
16
  swap?: (params: SwapParams) => AsyncResult<MatchedOrder, string>;
17
- pendingOrders?: number;
18
- quote?: IQuote;
19
- pendingOrdersCount?: number;
17
+ pendingOrders?: MatchedOrder[];
18
+ getQuote?: (params: QuoteParams) => AsyncResult<QuoteResponse, string>;
19
+ secretManager?: ISecretManager;
20
+ garden?: IGardenJS;
20
21
  };
21
22
  export type GardenProviderProps = {
22
23
  children: React.ReactNode;
@@ -28,3 +29,9 @@ export type GardenProviderProps = {
28
29
  bitcoinRPCUrl?: string;
29
30
  };
30
31
  };
32
+ export type QuoteParams = {
33
+ fromAsset: Asset;
34
+ toAsset: Asset;
35
+ amount: number;
36
+ isExactOut?: boolean;
37
+ };
@@ -1,6 +1,6 @@
1
- import { IOrderbook } from '@gardenfi/orderbook';
1
+ import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
2
2
  import { IAuth } from '@gardenfi/utils';
3
3
 
4
- export declare const useOrderbook: (orderBookUrl: string, auth: IAuth | undefined) => {
4
+ export declare const useOrderbook: (orderBookUrl: string, auth: IAuth | undefined, setPendingOrders: React.Dispatch<React.SetStateAction<MatchedOrder[] | undefined>>) => {
5
5
  orderbook: IOrderbook | undefined;
6
6
  };
@@ -0,0 +1,3 @@
1
+ import { Asset } from '@gardenfi/orderbook';
2
+
3
+ export declare const constructOrderpair: (fromAsset: Asset, toAsset: Asset) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/react-hooks",
3
- "version": "0.0.1-beta.6",
3
+ "version": "0.0.1-beta.61",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -27,10 +27,10 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@catalogfi/utils": "^0.1.6",
30
- "@catalogfi/wallets": "^0.2.49",
31
- "@gardenfi/core": "^0.2.0-beta.5",
32
- "@gardenfi/orderbook": "^0.2.0-beta.6",
33
- "@gardenfi/utils": "^0.0.1-beta.13",
30
+ "@catalogfi/wallets": "^0.2.50",
31
+ "@gardenfi/core": "^0.2.0-beta.42",
32
+ "@gardenfi/orderbook": "^0.2.0-beta.14",
33
+ "@gardenfi/utils": "^0.0.1-beta.18",
34
34
  "react": "^18.3.1",
35
35
  "viem": "^2.21.15",
36
36
  "wagmi": "^2.12.16"