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

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"),q=require("wagmi"),C=require("./index4.cjs"),M=require("./index3.cjs"),G=require("@gardenfi/core"),a=require("@catalogfi/utils"),p=require("@gardenfi/orderbook"),P=require("@catalogfi/wallets"),O=t.createContext({}),y=({children:w,config:r})=>{const[o,S]=t.useState(),[c,E]=t.useState(),[f,g]=t.useState(),{data:i}=q.useWalletClient(),{initializeSecretManager:h}=C.useSecretManager(S),{orderbook:s}=M.useOrderbook(r.orderBookUrl,r.store),B=t.useMemo(()=>new P.BitcoinProvider(r.bitcoinNetwork,r.bitcoinRPCUrl),[r.bitcoinNetwork,r.bitcoinRPCUrl]),m=async n=>{if(!c||!s||!o||!i)return a.Err("Garden not initialized");const b=await c.swap(n);if(b.error)return a.Err(b.error);const u=b.val,v=20,l=1e3;let e=await s.getOrder(u,!0);for(let x=0;x<v;x++){if(e=await s.getOrder(u,!0),e.error){if(!e.error.includes("result is undefined"))return a.Err(e.error)}else if(e.val&&e.val.create_order.create_id===u)break;await a.sleep(l)}if(p.isBitcoin(e.val.source_swap.chain))return a.Ok(e.val);const d=await new G.OrderExecutor(e.val,r.orderBookUrl,o,{store:r.store}).init(i,0);return d.error?a.Err(d.error):a.Ok(e.val)};return t.useEffect(()=>{if(!o)return;const n=P.BitcoinWallet.fromPrivateKey(o.getMasterPrivKey(),B);g(n)},[o,B]),t.useEffect(()=>{if(!o||!i||!s)return;const n=new G.Garden(s,r.orderBookUrl,o,{store:r.store});E(n)},[o,i,s]),t.useEffect(()=>{if(!c)return;let n;return(async()=>{const u=async v=>{const l=v.getOrder(),e=p.isBitcoin(l.source_swap.chain)?f:i,k=p.isBitcoin(l.destination_swap.chain)?f:i;if(!e||!k)return;console.log("executing order:",l.create_order.create_id);const d=await v.execute({wallets:{source:e,destination:k}});console.log("execute result :",d.val),console.log("execute error: ",d.error)};n=await c.subscribeOrders(u)})(),()=>{n()}},[c]),t.createElement(O.Provider,{value:{orderBookUrl:r.orderBookUrl,initializeSecretManager:h,orderBook:s,swap:m}},w)},U=()=>{const w=t.useContext(O);if(!w)throw new Error("useGarden must be used within a GardenProvider");return w};exports.GardenContext=O;exports.GardenProvider=y;exports.useGarden=U;
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;
package/dist/index2.js CHANGED
@@ -1,95 +1,90 @@
1
- import h, { createContext as S, useState as b, useMemo as W, useEffect as v } from "react";
2
- import { useWalletClient as g } from "wagmi";
3
- import { useSecretManager as y } from "./index4.js";
4
- import { useOrderbook as E } from "./index3.js";
5
- import { Garden as R, OrderExecutor as z } from "@gardenfi/core";
6
- import { Err as m, sleep as K, Ok as O } from "@catalogfi/utils";
7
- import { isBitcoin as f } from "@gardenfi/orderbook";
8
- import { BitcoinProvider as N, BitcoinWallet as D } from "@catalogfi/wallets";
9
- const P = S({}), L = ({
10
- children: u,
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,
11
12
  config: r
12
13
  }) => {
13
- const [t, G] = b(), [a, C] = b(), [k, U] = b(), { data: s } = g(), { initializeSecretManager: _ } = y(G), { orderbook: n } = E(r.orderBookUrl, r.store), x = W(
14
- () => new N(r.bitcoinNetwork, r.bitcoinRPCUrl),
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),
15
16
  [r.bitcoinNetwork, r.bitcoinRPCUrl]
16
- ), M = async (o) => {
17
- if (!a || !n || !t || !s)
18
- return m("Garden not initialized");
19
- const d = await a.swap(o);
20
- if (d.error) return m(d.error);
21
- const i = d.val, w = 20, c = 1e3;
22
- let e = await n.getOrder(i, !0);
23
- for (let B = 0; B < w; B++) {
24
- if (e = await n.getOrder(i, !0), e.error) {
25
- if (!e.error.includes("result is undefined"))
26
- return m(e.error);
27
- } else if (e.val && e.val.create_order.create_id === i)
28
- break;
29
- await K(c);
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);
30
23
  }
31
- if (f(e.val.source_swap.chain)) return O(e.val);
32
- const l = await new z(
33
- e.val,
34
- r.orderBookUrl,
35
- t,
36
- { store: r.store }
37
- ).init(s, 0);
38
- return l.error ? m(l.error) : O(e.val);
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
30
+ );
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);
39
35
  };
40
- return v(() => {
36
+ return u(() => {
41
37
  if (!t) return;
42
- const o = D.fromPrivateKey(
43
- t.getMasterPrivKey(),
44
- x
45
- );
46
- U(o);
47
- }, [t, x]), v(() => {
48
- if (!t || !s || !n) return;
49
- const o = new R(n, r.orderBookUrl, t, {
38
+ const e = new _(new j(r.orderBookUrl), t, {
50
39
  store: r.store
51
40
  });
52
- C(o);
53
- }, [t, s, n]), v(() => {
54
- if (!a) return;
55
- let o;
56
- return (async () => {
57
- const i = async (w) => {
58
- const c = w.getOrder(), e = f(c.source_swap.chain) ? k : s, p = f(c.destination_swap.chain) ? k : s;
59
- if (!e || !p) return;
60
- console.log("executing order:", c.create_order.create_id);
61
- const l = await w.execute({
62
- wallets: {
63
- source: e,
64
- destination: p
65
- }
66
- });
67
- console.log("execute result :", l.val), console.log("execute error: ", l.error);
68
- };
69
- o = await a.subscribeOrders(i);
70
- })(), () => {
71
- o();
72
- };
73
- }, [a]), /* @__PURE__ */ h.createElement(
74
- P.Provider,
41
+ C(e);
42
+ }, [t]), u(() => {
43
+ 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,
55
+ auth: a,
56
+ wallets: {
57
+ evmWallet: t,
58
+ btcWallet: G
59
+ }
60
+ });
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,
75
68
  {
76
69
  value: {
77
70
  orderBookUrl: r.orderBookUrl,
78
- initializeSecretManager: _,
79
- orderBook: n,
80
- swap: M
71
+ initializeSecretManager: w,
72
+ orderBook: d,
73
+ swap: O,
74
+ pendingOrdersCount: y,
75
+ quote: m
81
76
  }
82
77
  },
83
- u
78
+ c
84
79
  );
85
- }, Q = () => {
86
- const u = h.useContext(P);
87
- if (!u)
80
+ }, rr = () => {
81
+ const c = b.useContext(B);
82
+ if (!c)
88
83
  throw new Error("useGarden must be used within a GardenProvider");
89
- return u;
84
+ return c;
90
85
  };
91
86
  export {
92
- P as GardenContext,
93
- L as GardenProvider,
94
- Q as useGarden
87
+ B as GardenContext,
88
+ $ as GardenProvider,
89
+ rr as useGarden
95
90
  };
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)return;const u=new c.Orderbook({url:e,walletClient:o,opts:{store:r}});n(u)},[o,e]),{orderbook:s}};exports.useOrderbook=i;
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;
package/dist/index3.js CHANGED
@@ -1,20 +1,18 @@
1
- import { Orderbook as u } from "@gardenfi/orderbook";
2
- import { useState as f, useEffect as i } from "react";
3
- import { useWalletClient as m } from "wagmi";
4
- const l = (t, o) => {
5
- const { data: e } = m(), [r, s] = f();
6
- return i(() => {
7
- if (!e || !t) return;
8
- const n = new u({
9
- url: t,
10
- walletClient: e,
11
- opts: {
12
- store: o
13
- }
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({
9
+ url: e,
10
+ walletClient: t,
11
+ auth: o
14
12
  });
15
- s(n);
16
- }, [e, t]), { orderbook: r };
13
+ n(s);
14
+ }, [t, e]), { orderbook: r };
17
15
  };
18
16
  export {
19
- l as useOrderbook
17
+ a as useOrderbook
20
18
  };
@@ -1,6 +1,6 @@
1
1
  import { AsyncResult } from '@catalogfi/utils';
2
2
  import { BitcoinNetwork } from '@catalogfi/wallets';
3
- import { SecretManager, SwapParams } from '@gardenfi/core';
3
+ import { IQuote, SecretManager, SwapParams } from '@gardenfi/core';
4
4
  import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
5
5
  import { IStore } from '@gardenfi/utils';
6
6
 
@@ -14,11 +14,15 @@ 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
20
  };
18
21
  export type GardenProviderProps = {
19
22
  children: React.ReactNode;
20
23
  config: {
21
24
  orderBookUrl: string;
25
+ quoteUrl: string;
22
26
  store: IStore;
23
27
  bitcoinNetwork: BitcoinNetwork;
24
28
  bitcoinRPCUrl?: string;
@@ -1,6 +1,6 @@
1
1
  import { IOrderbook } from '@gardenfi/orderbook';
2
- import { IStore } from '@gardenfi/utils';
2
+ import { IAuth } from '@gardenfi/utils';
3
3
 
4
- export declare const useOrderbook: (orderBookUrl: string, store: IStore) => {
4
+ export declare const useOrderbook: (orderBookUrl: string, auth: IAuth | undefined) => {
5
5
  orderbook: IOrderbook | undefined;
6
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/react-hooks",
3
- "version": "0.0.1-beta.3",
3
+ "version": "0.0.1-beta.6",
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.49",
31
- "@gardenfi/core": "^0.2.0-beta.3",
32
- "@gardenfi/orderbook": "^0.2.0-beta.3",
33
- "@gardenfi/utils": "^0.0.1-beta.3",
31
+ "@gardenfi/core": "^0.2.0-beta.5",
32
+ "@gardenfi/orderbook": "^0.2.0-beta.6",
33
+ "@gardenfi/utils": "^0.0.1-beta.13",
34
34
  "react": "^18.3.1",
35
35
  "viem": "^2.21.15",
36
36
  "wagmi": "^2.12.16"