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