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