@gardenfi/react-hooks 0.0.1-beta.8 → 0.0.1-beta.81

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