@gardenfi/react-hooks 0.0.1-beta.66 → 0.0.1-beta.67

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