@gardenfi/react-hooks 0.0.1-beta.4 → 0.0.1-beta.40

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