@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 +1 -1
- package/dist/index2.js +143 -111
- package/dist/src/lib/context/gardenProvider.types.d.ts +1 -0
- 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 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
|
|
2
|
-
import { useWalletClient as
|
|
3
|
-
import { useSecretManager as
|
|
4
|
-
import { useOrderbook as
|
|
5
|
-
import { Quote as
|
|
6
|
-
import { Err as
|
|
7
|
-
import { isBitcoin as
|
|
8
|
-
import { BitcoinNetwork as
|
|
9
|
-
import { Siwe as
|
|
10
|
-
import { constructOrderpair as
|
|
11
|
-
const
|
|
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
|
-
}),
|
|
14
|
-
children:
|
|
15
|
-
config:
|
|
13
|
+
}), ce = ({
|
|
14
|
+
children: w,
|
|
15
|
+
config: r
|
|
16
16
|
}) => {
|
|
17
|
-
const [s,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
),
|
|
22
|
-
url:
|
|
23
|
-
network:
|
|
24
|
-
} : void 0,
|
|
25
|
-
() => new
|
|
26
|
-
[
|
|
27
|
-
),
|
|
28
|
-
if (!
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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 (
|
|
58
|
-
return
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
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
|
-
...
|
|
97
|
+
...e.source_swap,
|
|
67
98
|
initiate_tx_hash: v.val
|
|
68
99
|
}
|
|
69
100
|
};
|
|
70
|
-
return
|
|
71
|
-
},
|
|
72
|
-
fromAsset:
|
|
73
|
-
toAsset:
|
|
74
|
-
amount:
|
|
75
|
-
isExactOut:
|
|
76
|
-
}) => await
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
|
82
|
-
if (!
|
|
83
|
-
const
|
|
84
|
-
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
|
-
|
|
87
|
-
}, [
|
|
88
|
-
if (!s || !
|
|
89
|
-
const
|
|
117
|
+
z(e);
|
|
118
|
+
}, [n]), f(() => {
|
|
119
|
+
if (!s || !n || !m || !i) return;
|
|
120
|
+
const e = F.fromPrivateKey(
|
|
90
121
|
s.getMasterPrivKey(),
|
|
91
|
-
|
|
92
|
-
),
|
|
93
|
-
orderbookURl:
|
|
122
|
+
P
|
|
123
|
+
), o = new _({
|
|
124
|
+
orderbookURl: r.orderBookUrl,
|
|
94
125
|
secretManager: s,
|
|
95
|
-
quote:
|
|
96
|
-
auth:
|
|
126
|
+
quote: b,
|
|
127
|
+
auth: i,
|
|
97
128
|
wallets: {
|
|
98
|
-
evmWallet:
|
|
99
|
-
btcWallet:
|
|
129
|
+
evmWallet: n,
|
|
130
|
+
btcWallet: e
|
|
100
131
|
}
|
|
101
132
|
});
|
|
102
|
-
|
|
103
|
-
}, [s,
|
|
104
|
-
if (!
|
|
105
|
-
const
|
|
106
|
-
return
|
|
107
|
-
|
|
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
|
|
140
|
+
(async () => (await e)())();
|
|
110
141
|
};
|
|
111
|
-
}, [
|
|
112
|
-
|
|
142
|
+
}, [l]), /* @__PURE__ */ N.createElement(
|
|
143
|
+
S.Provider,
|
|
113
144
|
{
|
|
114
145
|
value: {
|
|
115
|
-
orderBookUrl:
|
|
116
|
-
initializeSecretManager:
|
|
117
|
-
orderBook:
|
|
118
|
-
swap:
|
|
119
|
-
pendingOrders:
|
|
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:
|
|
123
|
-
isExecuting:
|
|
153
|
+
garden: l,
|
|
154
|
+
isExecuting: K,
|
|
155
|
+
evmInitiate: q
|
|
124
156
|
}
|
|
125
157
|
},
|
|
126
|
-
|
|
158
|
+
w
|
|
127
159
|
);
|
|
128
|
-
},
|
|
129
|
-
const
|
|
130
|
-
if (!
|
|
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
|
|
164
|
+
return w;
|
|
133
165
|
};
|
|
134
166
|
export {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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 = {
|