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