@gardenfi/react-hooks 0.0.1-beta.37 → 0.0.1-beta.39

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