@gardenfi/react-hooks 0.0.1-beta.1 → 0.0.1-beta.10

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