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

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