@gardenfi/react-hooks 0.0.1-beta.103 → 0.0.1-beta.104
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 +40 -38
- 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"),U=a.createContext({isExecuting:!1}),Q=({children:h,config:r})=>{const[c,S]=a.useState(),[l,y]=a.useState(),[s,N]=a.useState(),[O,M]=a.useState(),g=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]),P=a.useMemo(()=>new b.BitcoinProvider(r.network,r.bitcoinRPCUrl),[r.network,r.bitcoinRPCUrl]),{data:i}=x.useWalletClient(),{initializeSecretManager:f}=z.useSecretManager(S),{orderbook:m}=A.useOrderbook(r.orderBookUrl,s,M,B),p=async()=>{if(!i||!s)return n.Err("WalletClient or auth not initialized");const e=await f();if(e.error)return n.Err(e.error);const o=b.BitcoinWallet.fromPrivateKey(e.val.getMasterPrivKey(),P),t=new u.Garden({orderbookURl:r.orderBookUrl,secretManager:e.val,quote:w,auth:s,wallets:{evmWallet:i,btcWallet:o},blockNumberFetcher:B});return y(t),n.Ok(t)},W=async e=>{if(!m||!i||!s)return n.Err("Orderbook or walletClient or auth not initialized");let o=l;if(!c||!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(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 C=d.val.walletClient;console.log("new Wallet client after switch",C);const k=await new u.EvmRelay(t.val,r.orderBookUrl,s).init(C);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)},_=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 p();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)},F=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)return;const e=new q.Siwe(new q.Url(r.orderBookUrl),i,{store:r.store});N(e)},[i]),a.useEffect(()=>{if(!c||!i||!m||!s)return;const e=b.BitcoinWallet.fromPrivateKey(c.getMasterPrivKey(),P),o=new u.Garden({orderbookURl:r.orderBookUrl,secretManager:c,quote:w,auth:s,wallets:{evmWallet:i,btcWallet:e},blockNumberFetcher:B});y(o)},[c,i,m,s]),a.useEffect(()=>{if(!l)return;const e=l.execute(),o=t=>M(t);return l.on("onPendingOrdersChanged",o),()=>{(async()=>(await e)())(),l.off("onPendingOrdersChanged",o)}},[l]),a.createElement(U.Provider,{value:{orderBookUrl:r.orderBookUrl,initializeSecretManager:f,orderBook:m,swapAndInitiate:W,pendingOrders:O,getQuote:F,secretManager:c,garden:l,isExecuting:g,evmInitiate:_,quote:w}},h)},$=()=>{const h=a.useContext(U);if(!h)throw new Error("useGarden must be used within a GardenProvider");return h};exports.GardenContext=U;exports.GardenProvider=Q;exports.useGarden=$;
|
package/dist/index2.js
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import
|
|
1
|
+
import g, { createContext as T, useState as b, useMemo as m, useEffect as B } 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 W } from "@gardenfi/orderbook";
|
|
8
|
+
import { BitcoinNetwork as x, 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 S = T({
|
|
12
12
|
isExecuting: !1
|
|
13
13
|
}), ce = ({
|
|
14
|
-
children:
|
|
14
|
+
children: h,
|
|
15
15
|
config: r
|
|
16
16
|
}) => {
|
|
17
|
-
const [l, z] = b(), [s,
|
|
17
|
+
const [l, z] = b(), [s, P] = b(), [n, A] = b(), [f, y] = 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 === x.Mainnet ? "mainnet" : r.network === x.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]), O = 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: M } = j(z), { orderbook: v } = D(
|
|
30
30
|
r.orderBookUrl,
|
|
31
31
|
n,
|
|
32
|
-
|
|
32
|
+
y,
|
|
33
33
|
U
|
|
34
|
-
),
|
|
34
|
+
), _ = async () => {
|
|
35
35
|
if (!a || !n)
|
|
36
36
|
return i("WalletClient or auth not initialized");
|
|
37
|
-
const e = await
|
|
37
|
+
const e = await M();
|
|
38
38
|
if (e.error) return i(e.error);
|
|
39
39
|
const o = E.fromPrivateKey(
|
|
40
40
|
e.val.getMasterPrivKey(),
|
|
41
|
-
|
|
41
|
+
O
|
|
42
42
|
), t = new F({
|
|
43
43
|
orderbookURl: r.orderBookUrl,
|
|
44
44
|
secretManager: e.val,
|
|
@@ -50,26 +50,28 @@ const g = T({
|
|
|
50
50
|
},
|
|
51
51
|
blockNumberFetcher: U
|
|
52
52
|
});
|
|
53
|
-
return
|
|
53
|
+
return P(t), p(t);
|
|
54
54
|
}, q = async (e) => {
|
|
55
|
-
if (!
|
|
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 R = await _();
|
|
60
|
+
if (R.error) return i(R.error);
|
|
61
|
+
o = R.val;
|
|
62
62
|
}
|
|
63
63
|
const t = await o.swap(e);
|
|
64
64
|
if (t.error) return i(t.error);
|
|
65
|
-
if (
|
|
65
|
+
if (W(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
|
|
72
|
+
const C = c.val.walletClient;
|
|
73
|
+
console.log("new Wallet client after switch", C);
|
|
74
|
+
const d = await new N(t.val, r.orderBookUrl, n).init(C);
|
|
73
75
|
if (d.error) return i(d.error);
|
|
74
76
|
const w = {
|
|
75
77
|
...t.val,
|
|
@@ -82,11 +84,11 @@ const g = T({
|
|
|
82
84
|
}, Q = async (e) => {
|
|
83
85
|
if (!a || !n)
|
|
84
86
|
return i("Orderbook or walletClient or auth not initialized");
|
|
85
|
-
if (
|
|
87
|
+
if (W(e.source_swap.chain))
|
|
86
88
|
return i("Not an EVM order: sourceSwap.chain is Bitcoin");
|
|
87
89
|
let o = s;
|
|
88
90
|
if (!l || !o) {
|
|
89
|
-
const w = await
|
|
91
|
+
const w = await _();
|
|
90
92
|
if (w.error) return i(w.error);
|
|
91
93
|
o = w.val;
|
|
92
94
|
}
|
|
@@ -114,17 +116,17 @@ const g = T({
|
|
|
114
116
|
),
|
|
115
117
|
[u]
|
|
116
118
|
);
|
|
117
|
-
return
|
|
119
|
+
return B(() => {
|
|
118
120
|
if (!a) return;
|
|
119
121
|
const e = new X(new Y(r.orderBookUrl), a, {
|
|
120
122
|
store: r.store
|
|
121
123
|
});
|
|
122
124
|
A(e);
|
|
123
|
-
}, [a]),
|
|
124
|
-
if (!l || !a || !
|
|
125
|
+
}, [a]), B(() => {
|
|
126
|
+
if (!l || !a || !v || !n) return;
|
|
125
127
|
const e = E.fromPrivateKey(
|
|
126
128
|
l.getMasterPrivKey(),
|
|
127
|
-
|
|
129
|
+
O
|
|
128
130
|
), o = new F({
|
|
129
131
|
orderbookURl: r.orderBookUrl,
|
|
130
132
|
secretManager: l,
|
|
@@ -136,20 +138,20 @@ const g = T({
|
|
|
136
138
|
},
|
|
137
139
|
blockNumberFetcher: U
|
|
138
140
|
});
|
|
139
|
-
|
|
140
|
-
}, [l, a,
|
|
141
|
+
P(o);
|
|
142
|
+
}, [l, a, v, n]), B(() => {
|
|
141
143
|
if (!s) return;
|
|
142
|
-
const e = s.execute(), o = (t) =>
|
|
144
|
+
const e = s.execute(), o = (t) => y(t);
|
|
143
145
|
return s.on("onPendingOrdersChanged", o), () => {
|
|
144
146
|
(async () => (await e)())(), s.off("onPendingOrdersChanged", o);
|
|
145
147
|
};
|
|
146
|
-
}, [s]), /* @__PURE__ */
|
|
147
|
-
|
|
148
|
+
}, [s]), /* @__PURE__ */ g.createElement(
|
|
149
|
+
S.Provider,
|
|
148
150
|
{
|
|
149
151
|
value: {
|
|
150
152
|
orderBookUrl: r.orderBookUrl,
|
|
151
|
-
initializeSecretManager:
|
|
152
|
-
orderBook:
|
|
153
|
+
initializeSecretManager: M,
|
|
154
|
+
orderBook: v,
|
|
153
155
|
swapAndInitiate: q,
|
|
154
156
|
pendingOrders: f,
|
|
155
157
|
getQuote: I,
|
|
@@ -160,16 +162,16 @@ const g = T({
|
|
|
160
162
|
quote: u
|
|
161
163
|
}
|
|
162
164
|
},
|
|
163
|
-
|
|
165
|
+
h
|
|
164
166
|
);
|
|
165
167
|
}, ue = () => {
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
168
|
+
const h = g.useContext(S);
|
|
169
|
+
if (!h)
|
|
168
170
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
169
|
-
return
|
|
171
|
+
return h;
|
|
170
172
|
};
|
|
171
173
|
export {
|
|
172
|
-
|
|
174
|
+
S as GardenContext,
|
|
173
175
|
ce as GardenProvider,
|
|
174
176
|
ue as useGarden
|
|
175
177
|
};
|
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.104",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@catalogfi/utils": "^0.1.6",
|
|
30
30
|
"@catalogfi/wallets": "^0.2.51",
|
|
31
31
|
"@gardenfi/core": "^0.2.0-beta.81",
|
|
32
|
-
"@gardenfi/orderbook": "^0.2.0-beta.
|
|
32
|
+
"@gardenfi/orderbook": "^0.2.0-beta.24",
|
|
33
33
|
"@gardenfi/utils": "^0.0.1-beta.18",
|
|
34
34
|
"react": "^18.3.1",
|
|
35
35
|
"viem": "^2.21.15",
|