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