@gardenfi/react-hooks 0.0.1-beta.0

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 ADDED
@@ -0,0 +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;
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import { GardenProvider as o, useGarden as a } from "./index2.js";
2
+ import { useOrderbook as t } from "./index3.js";
3
+ import { useSecretManager as m } from "./index4.js";
4
+ export {
5
+ o as GardenProvider,
6
+ a as useGarden,
7
+ t as useOrderbook,
8
+ m as useSecretManager
9
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react"),q=require("wagmi"),C=require("./index4.cjs"),M=require("./index3.cjs"),x=require("@gardenfi/core"),a=require("@catalogfi/utils"),f=require("@gardenfi/orderbook"),G=require("@catalogfi/wallets"),p=r.createContext({}),y=({children:w,config:t})=>{const[o,P]=r.useState(),[c,S]=r.useState(),[O,E]=r.useState(),{data:i}=q.useWalletClient(),{initializeSecretManager:h}=C.useSecretManager(P),{orderbook:s}=M.useOrderbook(t.orderBookUrl),B=r.useMemo(()=>new G.BitcoinProvider(t.bitcoinNetwork,t.bitcoinRPCUrl),[t.bitcoinNetwork,t.bitcoinRPCUrl]),m=async n=>{if(!c||!s||!o||!i)return a.Err("Garden not initialized");const b=await c.swap(n);if(b.error)return a.Err(b.error);const u=b.val,v=20,l=1e3;let e=await s.getOrder(u,!0);for(let g=0;g<v;g++){if(e=await s.getOrder(u,!0),e.error){if(!e.error.includes("result is undefined"))return a.Err(e.error)}else if(e.val&&e.val.create_order.create_id===u)break;await a.sleep(l)}if(f.isBitcoin(e.val.source_swap.chain))return a.Ok(e.val);const d=await new x.OrderExecutor(e.val,t.orderBookUrl,o,{store:t.store}).init(i,0);return d.error?a.Err(d.error):a.Ok(e.val)};return r.useEffect(()=>{if(!o)return;const n=G.BitcoinWallet.fromPrivateKey(o.getMasterPrivKey(),B);E(n)},[o,B]),r.useEffect(()=>{if(!o||!i||!s)return;const n=new x.Garden(s,t.orderBookUrl,o);S(n)},[o,i,s]),r.useEffect(()=>{if(!c)return;let n;return(async()=>{const u=async v=>{const l=v.getOrder(),e=f.isBitcoin(l.source_swap.chain)?O:i,k=f.isBitcoin(l.destination_swap.chain)?O:i;if(!e||!k)return;console.log("executing order:",l.create_order.create_id);const d=await v.execute({wallets:{source:e,destination:k}});console.log("execute result :",d.val),console.log("execute error: ",d.error)};n=await c.subscribeOrders(u)})(),()=>{n()}},[c]),r.createElement(p.Provider,{value:{orderBookUrl:t.orderBookUrl,initializeSecretManager:h,orderBook:s,swap:m}},w)},U=()=>{const w=r.useContext(p);if(!w)throw new Error("useGarden must be used within a GardenProvider");return w};exports.GardenContext=p;exports.GardenProvider=y;exports.useGarden=U;
package/dist/index2.js ADDED
@@ -0,0 +1,93 @@
1
+ import h, { createContext as M, useState as f, useMemo as S, useEffect as b } from "react";
2
+ import { useWalletClient as W } from "wagmi";
3
+ import { useSecretManager as y } from "./index4.js";
4
+ import { useOrderbook as E } from "./index3.js";
5
+ import { Garden as R, OrderExecutor as z } from "@gardenfi/core";
6
+ import { Err as m, sleep as K, Ok as O } from "@catalogfi/utils";
7
+ import { isBitcoin as v } from "@gardenfi/orderbook";
8
+ import { BitcoinProvider as N, BitcoinWallet as D } from "@catalogfi/wallets";
9
+ const P = M({}), L = ({
10
+ children: u,
11
+ config: r
12
+ }) => {
13
+ const [t, G] = f(), [s, g] = f(), [k, C] = f(), { data: a } = W(), { initializeSecretManager: U } = y(G), { orderbook: n } = E(r.orderBookUrl), x = S(
14
+ () => new N(r.bitcoinNetwork, r.bitcoinRPCUrl),
15
+ [r.bitcoinNetwork, r.bitcoinRPCUrl]
16
+ ), _ = async (o) => {
17
+ if (!s || !n || !t || !a)
18
+ return m("Garden not initialized");
19
+ const d = await s.swap(o);
20
+ if (d.error) return m(d.error);
21
+ const i = d.val, w = 20, c = 1e3;
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);
30
+ }
31
+ if (v(e.val.source_swap.chain)) return O(e.val);
32
+ const l = await new z(
33
+ e.val,
34
+ r.orderBookUrl,
35
+ t,
36
+ { store: r.store }
37
+ ).init(a, 0);
38
+ return l.error ? m(l.error) : O(e.val);
39
+ };
40
+ return b(() => {
41
+ if (!t) return;
42
+ const o = D.fromPrivateKey(
43
+ t.getMasterPrivKey(),
44
+ x
45
+ );
46
+ C(o);
47
+ }, [t, x]), b(() => {
48
+ if (!t || !a || !n) return;
49
+ const o = new R(n, r.orderBookUrl, t);
50
+ g(o);
51
+ }, [t, a, n]), b(() => {
52
+ if (!s) return;
53
+ let o;
54
+ return (async () => {
55
+ const i = async (w) => {
56
+ const c = w.getOrder(), e = v(c.source_swap.chain) ? k : a, p = v(c.destination_swap.chain) ? k : a;
57
+ if (!e || !p) return;
58
+ console.log("executing order:", c.create_order.create_id);
59
+ const l = await w.execute({
60
+ wallets: {
61
+ source: e,
62
+ destination: p
63
+ }
64
+ });
65
+ console.log("execute result :", l.val), console.log("execute error: ", l.error);
66
+ };
67
+ o = await s.subscribeOrders(i);
68
+ })(), () => {
69
+ o();
70
+ };
71
+ }, [s]), /* @__PURE__ */ h.createElement(
72
+ P.Provider,
73
+ {
74
+ value: {
75
+ orderBookUrl: r.orderBookUrl,
76
+ initializeSecretManager: U,
77
+ orderBook: n,
78
+ swap: _
79
+ }
80
+ },
81
+ u
82
+ );
83
+ }, Q = () => {
84
+ const u = h.useContext(P);
85
+ if (!u)
86
+ throw new Error("useGarden must be used within a GardenProvider");
87
+ return u;
88
+ };
89
+ export {
90
+ P as GardenContext,
91
+ L as GardenProvider,
92
+ Q as useGarden
93
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@gardenfi/orderbook"),r=require("react"),c=require("wagmi"),b=e=>{const{data:o}=c.useWalletClient(),[t,s]=r.useState();return r.useEffect(()=>{if(!o||!e)return;const n=new u.Orderbook({url:e,walletClient:o});s(n)},[o,e]),{orderbook:t}};exports.useOrderbook=b;
package/dist/index3.js ADDED
@@ -0,0 +1,17 @@
1
+ import { Orderbook as s } from "@gardenfi/orderbook";
2
+ import { useState as u, useEffect as f } from "react";
3
+ import { useWalletClient as i } from "wagmi";
4
+ const c = (t) => {
5
+ const { data: e } = i(), [o, r] = u();
6
+ return f(() => {
7
+ if (!e || !t) return;
8
+ const n = new s({
9
+ url: t,
10
+ walletClient: e
11
+ });
12
+ r(n);
13
+ }, [e, t]), { orderbook: o };
14
+ };
15
+ export {
16
+ c as useOrderbook
17
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@catalogfi/utils"),n=require("@gardenfi/core"),a=require("wagmi"),l=i=>{const{data:r}=a.useWalletClient();return{initializeSecretManager:async()=>{if(!r)return t.Err("WalletClient not initialized");const e=await n.SecretManager.fromWalletClient(r);return e.error?t.Err(e.error):(i(e.val),e)}}};exports.useSecretManager=l;
package/dist/index4.js ADDED
@@ -0,0 +1,14 @@
1
+ import { Err as t } from "@catalogfi/utils";
2
+ import { SecretManager as a } from "@gardenfi/core";
3
+ import { useWalletClient as n } from "wagmi";
4
+ const s = (i) => {
5
+ const { data: r } = n();
6
+ return { initializeSecretManager: async () => {
7
+ if (!r) return t("WalletClient not initialized");
8
+ const e = await a.fromWalletClient(r);
9
+ return e.error ? t(e.error) : (i(e.val), e);
10
+ } };
11
+ };
12
+ export {
13
+ s as useSecretManager
14
+ };
@@ -0,0 +1,3 @@
1
+ export { GardenProvider, useGarden } from './lib/context/GardenProvider';
2
+ export { useOrderbook } from './lib/hooks/useOrderbook';
3
+ export { useSecretManager } from './lib/hooks/useSecretManager';
@@ -0,0 +1,6 @@
1
+ import { default as React, FC } from 'react';
2
+ import { GardenContextType, GardenProviderProps } from './gardenProvider.types';
3
+
4
+ export declare const GardenContext: React.Context<GardenContextType>;
5
+ export declare const GardenProvider: FC<GardenProviderProps>;
6
+ export declare const useGarden: () => GardenContextType;
@@ -0,0 +1,26 @@
1
+ import { AsyncResult } from '@catalogfi/utils';
2
+ import { BitcoinNetwork } from '@catalogfi/wallets';
3
+ import { SecretManager, SwapParams } from '@gardenfi/core';
4
+ import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
5
+ import { IStore } from '@gardenfi/utils';
6
+
7
+ export type GardenContextType = {
8
+ orderBookUrl?: string;
9
+ initializeSecretManager?: () => AsyncResult<SecretManager, string>;
10
+ orderBook?: IOrderbook | undefined;
11
+ /**
12
+ * Create an order and wait until its matched and then initiates if source chain is EVM.
13
+ * @params {SwapParams} - The parameters for creating the order.
14
+ * @returns {AsyncResult<string, string>} - create order ID.
15
+ */
16
+ swap?: (params: SwapParams) => AsyncResult<MatchedOrder, string>;
17
+ };
18
+ export type GardenProviderProps = {
19
+ children: React.ReactNode;
20
+ config: {
21
+ orderBookUrl: string;
22
+ store: IStore;
23
+ bitcoinNetwork: BitcoinNetwork;
24
+ bitcoinRPCUrl?: string;
25
+ };
26
+ };
@@ -0,0 +1,5 @@
1
+ import { IOrderbook } from '@gardenfi/orderbook';
2
+
3
+ export declare const useOrderbook: (orderBookUrl: string) => {
4
+ orderbook: IOrderbook | undefined;
5
+ };
@@ -0,0 +1,6 @@
1
+ import { ISecretManager, SecretManager } from '@gardenfi/core';
2
+ import { SetStateAction } from 'react';
3
+
4
+ export declare const useSecretManager: (setSecretManager: React.Dispatch<SetStateAction<ISecretManager | undefined>>) => {
5
+ initializeSecretManager: () => Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<SecretManager, never>>;
6
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@gardenfi/react-hooks",
3
+ "version": "0.0.1-beta.0",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "typings": "./dist/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "require": "./dist/index.cjs",
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/src/index.d.ts"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "vite build",
21
+ "test": "vitest run",
22
+ "dev": "vite build --watch"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public",
26
+ "registry": "https://registry.npmjs.org/"
27
+ },
28
+ "dependencies": {
29
+ "@catalogfi/utils": "^0.1.6",
30
+ "@catalogfi/wallets": "^0.2.49",
31
+ "@gardenfi/core": "^0.2.0-beta.0",
32
+ "@gardenfi/orderbook": "^0.2.0-beta.0",
33
+ "@gardenfi/utils": "^0.0.1-beta.0",
34
+ "react": "^18.3.1",
35
+ "viem": "^2.21.15",
36
+ "wagmi": "^2.12.16"
37
+ },
38
+ "devDependencies": {
39
+ "@types/react": "^18",
40
+ "dotenv": "^16.3.1",
41
+ "typescript": "^5.2.2",
42
+ "vite": "^5.1.6",
43
+ "vite-plugin-dts": "^3.7.3",
44
+ "vitest": "^1.6.0"
45
+ },
46
+ "sideEffects": false
47
+ }