@gardenfi/react-hooks 0.0.1-beta.63 → 0.0.1-beta.65

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