@gardenfi/react-hooks 0.0.1-beta.63 → 0.0.1-beta.65
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
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),N=require("wagmi"),F=require("./index4.cjs"),_=require("./index3.cjs"),d=require("@gardenfi/core"),c=require("@catalogfi/utils"),K=require("@gardenfi/orderbook"),w=require("@catalogfi/wallets"),O=require("@gardenfi/utils"),A=require("./index5.cjs"),P=t.createContext({isExecuting:!1}),Q=({children:k,config:e})=>{const[i,M]=t.useState(),[l,U]=t.useState(),[a,R]=t.useState(),[B,C]=t.useState(),S=!!(i&&l&&a&&B),h=new d.Quote(e.quoteUrl),{data:n}=N.useWalletClient(),{initializeSecretManager:E}=F.useSecretManager(M),{orderbook:b}=_.useOrderbook(e.orderBookUrl,a,C),q=e.network===w.BitcoinNetwork.Mainnet?"mainnet":e.network===w.BitcoinNetwork.Testnet?"testnet":void 0,W=e.blockNumberFetcherUrl&&q?{url:e.blockNumberFetcherUrl,network:q}:void 0,y=t.useMemo(()=>new w.BitcoinProvider(e.network,e.bitcoinRPCUrl),[e.network,e.bitcoinRPCUrl]),f=async o=>{if(!b||!n||!a)return c.Err("Orderbook or walletClient or auth not initialized");let r=l;if(!i||!r){const v=await E();if(v.error)return c.Err(v.error);const x=w.BitcoinWallet.fromPrivateKey(v.val.getMasterPrivKey(),y);r=new d.Garden({orderbookURl:e.orderBookUrl,secretManager:v.val,quote:h,auth:a,wallets:{evmWallet:n,btcWallet:x},blockNumberFetcher:W}),U(r)}const s=await r.swap(o);if(s.error)return c.Err(s.error);if(K.isBitcoin(s.val.source_swap.chain))return c.Ok(s.val);const u=await d.switchOrAddNetwork(o.fromAsset.chain,n);if(u.error)return c.Err("Failed to switch network: "+u.error);const G=u.val.walletClient;console.log("newWalletClient :",G);const m=await new d.EvmRelay(s.val,e.orderBookUrl,a).init(G);if(m.error)return c.Err(m.error);const p={...s.val,source_swap:{...s.val.source_swap,initiate_tx_hash:m.val}};return c.Ok(p)},g=async({fromAsset:o,toAsset:r,amount:s,isExactOut:u=!1})=>await h.getQuote(A.constructOrderpair(o,r),s,u);return t.useEffect(()=>{if(!n)return;const o=new O.Siwe(new O.Url(e.orderBookUrl),n,{store:e.store});R(o)},[n]),t.useEffect(()=>{if(!i||!n||!b||!a)return;const o=w.BitcoinWallet.fromPrivateKey(i.getMasterPrivKey(),y),r=new d.Garden({orderbookURl:e.orderBookUrl,secretManager:i,quote:h,auth:a,wallets:{evmWallet:n,btcWallet:o}});U(r)},[i,n,b,a]),t.useEffect(()=>{if(!l)return;const o=l.execute();return l.on("onPendingOrdersChanged",r=>{C(r)}),()=>{(async()=>(await o)())()}},[l]),t.createElement(P.Provider,{value:{orderBookUrl:e.orderBookUrl,initializeSecretManager:E,orderBook:b,swap:f,pendingOrders:B,getQuote:g,secretManager:i,garden:l,isExecuting:S}},k)},$=()=>{const k=t.useContext(P);if(!k)throw new Error("useGarden must be used within a GardenProvider");return k};exports.GardenContext=P;exports.GardenProvider=Q;exports.useGarden=$;
|
package/dist/index2.js
CHANGED
|
@@ -1,131 +1,138 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useWalletClient as
|
|
3
|
-
import { useSecretManager as
|
|
4
|
-
import { useOrderbook as
|
|
5
|
-
import { Quote as
|
|
6
|
-
import { Err as c, Ok as
|
|
7
|
-
import { isBitcoin as
|
|
8
|
-
import { BitcoinProvider as
|
|
9
|
-
import { Siwe as
|
|
10
|
-
import { constructOrderpair as
|
|
11
|
-
const
|
|
12
|
-
|
|
1
|
+
import x, { createContext as A, useState as m, useMemo as Q, useEffect as b } from "react";
|
|
2
|
+
import { useWalletClient as q } from "wagmi";
|
|
3
|
+
import { useSecretManager as z } from "./index4.js";
|
|
4
|
+
import { useOrderbook as T } from "./index3.js";
|
|
5
|
+
import { Quote as j, Garden as R, switchOrAddNetwork as D, EvmRelay as H } from "@gardenfi/core";
|
|
6
|
+
import { Err as c, Ok as y } from "@catalogfi/utils";
|
|
7
|
+
import { isBitcoin as I } from "@gardenfi/orderbook";
|
|
8
|
+
import { BitcoinNetwork as M, BitcoinProvider as J, BitcoinWallet as O } from "@catalogfi/wallets";
|
|
9
|
+
import { Siwe as L, Url as V } from "@gardenfi/utils";
|
|
10
|
+
import { constructOrderpair as X } from "./index5.js";
|
|
11
|
+
const G = A({
|
|
12
|
+
isExecuting: !1
|
|
13
|
+
}), le = ({
|
|
14
|
+
children: u,
|
|
13
15
|
config: e
|
|
14
16
|
}) => {
|
|
15
|
-
const [s,
|
|
17
|
+
const [s, W] = m(), [i, h] = m(), [a, E] = m(), [p, U] = m(), F = !!(s && i && a && p), k = new j(e.quoteUrl), { data: o } = q(), { initializeSecretManager: f } = z(W), { orderbook: d } = T(
|
|
16
18
|
e.orderBookUrl,
|
|
17
19
|
a,
|
|
18
|
-
|
|
19
|
-
), P =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
U
|
|
21
|
+
), P = e.network === M.Mainnet ? "mainnet" : e.network === M.Testnet ? "testnet" : void 0, N = e.blockNumberFetcherUrl && P ? {
|
|
22
|
+
url: e.blockNumberFetcherUrl,
|
|
23
|
+
network: P
|
|
24
|
+
} : void 0, B = Q(
|
|
25
|
+
() => new J(e.network, e.bitcoinRPCUrl),
|
|
26
|
+
[e.network, e.bitcoinRPCUrl]
|
|
27
|
+
), S = async (t) => {
|
|
28
|
+
if (!d || !o || !a)
|
|
24
29
|
return c("Orderbook or walletClient or auth not initialized");
|
|
25
30
|
let r = i;
|
|
26
31
|
if (!s || !r) {
|
|
27
|
-
const w = await
|
|
32
|
+
const w = await f();
|
|
28
33
|
if (w.error) return c(w.error);
|
|
29
|
-
const
|
|
34
|
+
const K = O.fromPrivateKey(
|
|
30
35
|
w.val.getMasterPrivKey(),
|
|
31
|
-
|
|
36
|
+
B
|
|
32
37
|
);
|
|
33
|
-
r = new
|
|
38
|
+
r = new R({
|
|
34
39
|
orderbookURl: e.orderBookUrl,
|
|
35
40
|
secretManager: w.val,
|
|
36
|
-
quote:
|
|
41
|
+
quote: k,
|
|
37
42
|
auth: a,
|
|
38
43
|
wallets: {
|
|
39
44
|
evmWallet: o,
|
|
40
|
-
btcWallet:
|
|
41
|
-
}
|
|
42
|
-
|
|
45
|
+
btcWallet: K
|
|
46
|
+
},
|
|
47
|
+
blockNumberFetcher: N
|
|
48
|
+
}), h(r);
|
|
43
49
|
}
|
|
44
50
|
const n = await r.swap(t);
|
|
45
51
|
if (n.error) return c(n.error);
|
|
46
|
-
if (
|
|
47
|
-
const l = await
|
|
52
|
+
if (I(n.val.source_swap.chain)) return y(n.val);
|
|
53
|
+
const l = await D(
|
|
48
54
|
t.fromAsset.chain,
|
|
49
55
|
o
|
|
50
56
|
);
|
|
51
57
|
if (l.error)
|
|
52
58
|
return c("Failed to switch network: " + l.error);
|
|
53
|
-
const
|
|
54
|
-
console.log("newWalletClient :",
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
const
|
|
59
|
+
const C = l.val.walletClient;
|
|
60
|
+
console.log("newWalletClient :", C);
|
|
61
|
+
const v = await new H(n.val, e.orderBookUrl, a).init(C);
|
|
62
|
+
if (v.error) return c(v.error);
|
|
63
|
+
const g = {
|
|
58
64
|
...n.val,
|
|
59
65
|
source_swap: {
|
|
60
66
|
...n.val.source_swap,
|
|
61
|
-
initiate_tx_hash:
|
|
67
|
+
initiate_tx_hash: v.val
|
|
62
68
|
}
|
|
63
69
|
};
|
|
64
|
-
return
|
|
65
|
-
},
|
|
70
|
+
return y(g);
|
|
71
|
+
}, _ = async ({
|
|
66
72
|
fromAsset: t,
|
|
67
73
|
toAsset: r,
|
|
68
74
|
amount: n,
|
|
69
75
|
isExactOut: l = !1
|
|
70
|
-
}) => await
|
|
71
|
-
|
|
76
|
+
}) => await k.getQuote(
|
|
77
|
+
X(t, r),
|
|
72
78
|
n,
|
|
73
79
|
l
|
|
74
80
|
);
|
|
75
|
-
return
|
|
81
|
+
return b(() => {
|
|
76
82
|
if (!o) return;
|
|
77
|
-
const t = new
|
|
83
|
+
const t = new L(new V(e.orderBookUrl), o, {
|
|
78
84
|
store: e.store
|
|
79
85
|
});
|
|
80
|
-
|
|
81
|
-
}, [o]),
|
|
82
|
-
if (!s || !o || !
|
|
83
|
-
const t =
|
|
86
|
+
E(t);
|
|
87
|
+
}, [o]), b(() => {
|
|
88
|
+
if (!s || !o || !d || !a) return;
|
|
89
|
+
const t = O.fromPrivateKey(
|
|
84
90
|
s.getMasterPrivKey(),
|
|
85
|
-
|
|
86
|
-
), r = new
|
|
91
|
+
B
|
|
92
|
+
), r = new R({
|
|
87
93
|
orderbookURl: e.orderBookUrl,
|
|
88
94
|
secretManager: s,
|
|
89
|
-
quote:
|
|
95
|
+
quote: k,
|
|
90
96
|
auth: a,
|
|
91
97
|
wallets: {
|
|
92
98
|
evmWallet: o,
|
|
93
99
|
btcWallet: t
|
|
94
100
|
}
|
|
95
101
|
});
|
|
96
|
-
|
|
97
|
-
}, [s, o,
|
|
102
|
+
h(r);
|
|
103
|
+
}, [s, o, d, a]), b(() => {
|
|
98
104
|
if (!i) return;
|
|
99
105
|
const t = i.execute();
|
|
100
106
|
return i.on("onPendingOrdersChanged", (r) => {
|
|
101
|
-
|
|
107
|
+
U(r);
|
|
102
108
|
}), () => {
|
|
103
109
|
(async () => (await t)())();
|
|
104
110
|
};
|
|
105
|
-
}, [i]), /* @__PURE__ */
|
|
106
|
-
|
|
111
|
+
}, [i]), /* @__PURE__ */ x.createElement(
|
|
112
|
+
G.Provider,
|
|
107
113
|
{
|
|
108
114
|
value: {
|
|
109
115
|
orderBookUrl: e.orderBookUrl,
|
|
110
|
-
initializeSecretManager:
|
|
111
|
-
orderBook:
|
|
112
|
-
swap:
|
|
113
|
-
pendingOrders:
|
|
114
|
-
getQuote:
|
|
116
|
+
initializeSecretManager: f,
|
|
117
|
+
orderBook: d,
|
|
118
|
+
swap: S,
|
|
119
|
+
pendingOrders: p,
|
|
120
|
+
getQuote: _,
|
|
115
121
|
secretManager: s,
|
|
116
|
-
garden: i
|
|
122
|
+
garden: i,
|
|
123
|
+
isExecuting: F
|
|
117
124
|
}
|
|
118
125
|
},
|
|
119
|
-
|
|
126
|
+
u
|
|
120
127
|
);
|
|
121
|
-
},
|
|
122
|
-
const
|
|
123
|
-
if (!
|
|
128
|
+
}, ce = () => {
|
|
129
|
+
const u = x.useContext(G);
|
|
130
|
+
if (!u)
|
|
124
131
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
125
|
-
return
|
|
132
|
+
return u;
|
|
126
133
|
};
|
|
127
134
|
export {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
135
|
+
G as GardenContext,
|
|
136
|
+
le as GardenProvider,
|
|
137
|
+
ce as useGarden
|
|
131
138
|
};
|
|
@@ -18,6 +18,7 @@ export type GardenContextType = {
|
|
|
18
18
|
getQuote?: (params: QuoteParams) => AsyncResult<QuoteResponse, string>;
|
|
19
19
|
secretManager?: ISecretManager;
|
|
20
20
|
garden?: IGardenJS;
|
|
21
|
+
isExecuting: boolean;
|
|
21
22
|
};
|
|
22
23
|
export type GardenProviderProps = {
|
|
23
24
|
children: React.ReactNode;
|
|
@@ -25,8 +26,9 @@ export type GardenProviderProps = {
|
|
|
25
26
|
orderBookUrl: string;
|
|
26
27
|
quoteUrl: string;
|
|
27
28
|
store: IStore;
|
|
28
|
-
|
|
29
|
+
network: BitcoinNetwork;
|
|
29
30
|
bitcoinRPCUrl?: string;
|
|
31
|
+
blockNumberFetcherUrl?: string;
|
|
30
32
|
};
|
|
31
33
|
};
|
|
32
34
|
export type QuoteParams = {
|
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.65",
|
|
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.50",
|
|
31
|
-
"@gardenfi/core": "^0.2.0-beta.
|
|
31
|
+
"@gardenfi/core": "^0.2.0-beta.46",
|
|
32
32
|
"@gardenfi/orderbook": "^0.2.0-beta.14",
|
|
33
33
|
"@gardenfi/utils": "^0.0.1-beta.18",
|
|
34
34
|
"react": "^18.3.1",
|