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