@gardenfi/react-hooks 0.0.1-beta.2 → 0.0.1-beta.20

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