@gardenfi/react-hooks 0.0.1-beta.3 → 0.0.1-beta.4

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"),q=require("wagmi"),C=require("./index4.cjs"),M=require("./index3.cjs"),G=require("@gardenfi/core"),a=require("@catalogfi/utils"),p=require("@gardenfi/orderbook"),P=require("@catalogfi/wallets"),O=t.createContext({}),y=({children:w,config:r})=>{const[o,S]=t.useState(),[c,E]=t.useState(),[f,g]=t.useState(),{data:i}=q.useWalletClient(),{initializeSecretManager:h}=C.useSecretManager(S),{orderbook:s}=M.useOrderbook(r.orderBookUrl,r.store),B=t.useMemo(()=>new P.BitcoinProvider(r.bitcoinNetwork,r.bitcoinRPCUrl),[r.bitcoinNetwork,r.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 x=0;x<v;x++){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(p.isBitcoin(e.val.source_swap.chain))return a.Ok(e.val);const d=await new G.OrderExecutor(e.val,r.orderBookUrl,o,{store:r.store}).init(i,0);return d.error?a.Err(d.error):a.Ok(e.val)};return t.useEffect(()=>{if(!o)return;const n=P.BitcoinWallet.fromPrivateKey(o.getMasterPrivKey(),B);g(n)},[o,B]),t.useEffect(()=>{if(!o||!i||!s)return;const n=new G.Garden(s,r.orderBookUrl,o,{store:r.store});E(n)},[o,i,s]),t.useEffect(()=>{if(!c)return;let n;return(async()=>{const u=async v=>{const l=v.getOrder(),e=p.isBitcoin(l.source_swap.chain)?f:i,k=p.isBitcoin(l.destination_swap.chain)?f: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]),t.createElement(O.Provider,{value:{orderBookUrl:r.orderBookUrl,initializeSecretManager:h,orderBook:s,swap:m}},w)},U=()=>{const w=t.useContext(O);if(!w)throw new Error("useGarden must be used within a GardenProvider");return w};exports.GardenContext=O;exports.GardenProvider=y;exports.useGarden=U;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react"),U=require("wagmi"),y=require("./index4.cjs"),f=require("./index3.cjs"),S=require("@gardenfi/core"),u=require("@catalogfi/utils"),w=require("@gardenfi/orderbook"),p=require("@catalogfi/wallets"),b=r.createContext({}),g=({children:l,config:e})=>{const[t,q]=r.useState(),[i,E]=r.useState(),[k,O]=r.useState(),{data:s}=U.useWalletClient(),{initializeSecretManager:C}=y.useSecretManager(q),{orderbook:a}=f.useOrderbook(e.orderBookUrl,e.store),v=r.useMemo(()=>new p.BitcoinProvider(e.bitcoinNetwork,e.bitcoinRPCUrl),[e.bitcoinNetwork,e.bitcoinRPCUrl]),M=async o=>{if(!i||!a||!t||!s)return u.Err("Garden not initialized");const n=await i.swap(o);if(n.error)return u.Err(n.error);if(w.isBitcoin(n.val.source_swap.chain))return u.Ok(n.val);const c=await new S.OrderExecutor(n.val,e.orderBookUrl,t,{store:e.store}).init(s,0);return c.error?u.Err(c.error):u.Ok(n.val)};return r.useEffect(()=>{if(!t)return;const o=p.BitcoinWallet.fromPrivateKey(t.getMasterPrivKey(),v);O(o)},[t,v]),r.useEffect(()=>{if(!t||!s||!a)return;const o=new S.Garden(a,t,e.orderBookUrl,e.quoteUrl,{store:e.store});E(o)},[t,s,a]),r.useEffect(()=>{if(!i)return;let o;return(async()=>{const B=async c=>{const d=c.getOrder(),x=w.isBitcoin(d.source_swap.chain)?k:s,G=w.isBitcoin(d.destination_swap.chain)?k:s;if(!x||!G)return;console.log("executing order:",d.create_order.create_id);const P=await c.execute({wallets:{source:x,destination:G}});console.log("execute result :",P.val),console.log("execute error: ",P.error)};o=await i.subscribeOrders(B)})(),()=>{o()}},[i]),r.createElement(b.Provider,{value:{orderBookUrl:e.orderBookUrl,initializeSecretManager:C,orderBook:a,swap:M}},l)},h=()=>{const l=r.useContext(b);if(!l)throw new Error("useGarden must be used within a GardenProvider");return l};exports.GardenContext=b;exports.GardenProvider=g;exports.useGarden=h;
package/dist/index2.js CHANGED
@@ -1,95 +1,91 @@
1
- import h, { createContext as S, useState as b, useMemo as W, useEffect as v } from "react";
2
- import { useWalletClient as g } 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 f } from "@gardenfi/orderbook";
8
- import { BitcoinProvider as N, BitcoinWallet as D } from "@catalogfi/wallets";
9
- const P = S({}), L = ({
10
- children: u,
11
- config: r
1
+ import P, { createContext as S, useState as u, useMemo as W, useEffect as d } from "react";
2
+ import { useWalletClient as y } from "wagmi";
3
+ import { useSecretManager as E } from "./index4.js";
4
+ import { useOrderbook as _ } from "./index3.js";
5
+ import { Garden as R, OrderExecutor as g } from "@gardenfi/core";
6
+ import { Err as m, Ok as f } from "@catalogfi/utils";
7
+ import { isBitcoin as w } from "@gardenfi/orderbook";
8
+ import { BitcoinProvider as z, BitcoinWallet as K } from "@catalogfi/wallets";
9
+ const G = S({}), J = ({
10
+ children: c,
11
+ config: e
12
12
  }) => {
13
- const [t, G] = b(), [a, C] = b(), [k, U] = b(), { data: s } = g(), { initializeSecretManager: _ } = y(G), { orderbook: n } = E(r.orderBookUrl, r.store), x = W(
14
- () => new N(r.bitcoinNetwork, r.bitcoinRPCUrl),
15
- [r.bitcoinNetwork, r.bitcoinRPCUrl]
16
- ), M = async (o) => {
17
- if (!a || !n || !t || !s)
13
+ const [r, U] = u(), [s, C] = u(), [p, h] = u(), { data: n } = y(), { initializeSecretManager: M } = E(U), { orderbook: i } = _(e.orderBookUrl, e.store), b = W(
14
+ () => new z(e.bitcoinNetwork, e.bitcoinRPCUrl),
15
+ [e.bitcoinNetwork, e.bitcoinRPCUrl]
16
+ ), O = async (t) => {
17
+ if (!s || !i || !r || !n)
18
18
  return m("Garden not initialized");
19
- const d = await a.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 (f(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(s, 0);
38
- return l.error ? m(l.error) : O(e.val);
19
+ const o = await s.swap(t);
20
+ if (o.error) return m(o.error);
21
+ if (w(o.val.source_swap.chain)) return f(o.val);
22
+ const a = await new g(
23
+ o.val,
24
+ e.orderBookUrl,
25
+ r,
26
+ { store: e.store }
27
+ ).init(n, 0);
28
+ return a.error ? m(a.error) : f(o.val);
39
29
  };
40
- return v(() => {
41
- if (!t) return;
42
- const o = D.fromPrivateKey(
43
- t.getMasterPrivKey(),
44
- x
30
+ return d(() => {
31
+ if (!r) return;
32
+ const t = K.fromPrivateKey(
33
+ r.getMasterPrivKey(),
34
+ b
45
35
  );
46
- U(o);
47
- }, [t, x]), v(() => {
48
- if (!t || !s || !n) return;
49
- const o = new R(n, r.orderBookUrl, t, {
50
- store: r.store
51
- });
52
- C(o);
53
- }, [t, s, n]), v(() => {
54
- if (!a) return;
55
- let o;
36
+ h(t);
37
+ }, [r, b]), d(() => {
38
+ if (!r || !n || !i) return;
39
+ const t = new R(
40
+ i,
41
+ r,
42
+ e.orderBookUrl,
43
+ e.quoteUrl,
44
+ {
45
+ store: e.store
46
+ }
47
+ );
48
+ C(t);
49
+ }, [r, n, i]), d(() => {
50
+ if (!s) return;
51
+ let t;
56
52
  return (async () => {
57
- const i = async (w) => {
58
- const c = w.getOrder(), e = f(c.source_swap.chain) ? k : s, p = f(c.destination_swap.chain) ? k : s;
59
- if (!e || !p) return;
60
- console.log("executing order:", c.create_order.create_id);
61
- const l = await w.execute({
53
+ const v = async (a) => {
54
+ const l = a.getOrder(), k = w(l.source_swap.chain) ? p : n, x = w(l.destination_swap.chain) ? p : n;
55
+ if (!k || !x) return;
56
+ console.log("executing order:", l.create_order.create_id);
57
+ const B = await a.execute({
62
58
  wallets: {
63
- source: e,
64
- destination: p
59
+ source: k,
60
+ destination: x
65
61
  }
66
62
  });
67
- console.log("execute result :", l.val), console.log("execute error: ", l.error);
63
+ console.log("execute result :", B.val), console.log("execute error: ", B.error);
68
64
  };
69
- o = await a.subscribeOrders(i);
65
+ t = await s.subscribeOrders(v);
70
66
  })(), () => {
71
- o();
67
+ t();
72
68
  };
73
- }, [a]), /* @__PURE__ */ h.createElement(
74
- P.Provider,
69
+ }, [s]), /* @__PURE__ */ P.createElement(
70
+ G.Provider,
75
71
  {
76
72
  value: {
77
- orderBookUrl: r.orderBookUrl,
78
- initializeSecretManager: _,
79
- orderBook: n,
80
- swap: M
73
+ orderBookUrl: e.orderBookUrl,
74
+ initializeSecretManager: M,
75
+ orderBook: i,
76
+ swap: O
81
77
  }
82
78
  },
83
- u
79
+ c
84
80
  );
85
- }, Q = () => {
86
- const u = h.useContext(P);
87
- if (!u)
81
+ }, L = () => {
82
+ const c = P.useContext(G);
83
+ if (!c)
88
84
  throw new Error("useGarden must be used within a GardenProvider");
89
- return u;
85
+ return c;
90
86
  };
91
87
  export {
92
- P as GardenContext,
93
- L as GardenProvider,
94
- Q as useGarden
88
+ G as GardenContext,
89
+ J as GardenProvider,
90
+ L as useGarden
95
91
  };
@@ -19,6 +19,7 @@ export type GardenProviderProps = {
19
19
  children: React.ReactNode;
20
20
  config: {
21
21
  orderBookUrl: string;
22
+ quoteUrl: string;
22
23
  store: IStore;
23
24
  bitcoinNetwork: BitcoinNetwork;
24
25
  bitcoinRPCUrl?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/react-hooks",
3
- "version": "0.0.1-beta.3",
3
+ "version": "0.0.1-beta.4",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -28,9 +28,9 @@
28
28
  "dependencies": {
29
29
  "@catalogfi/utils": "^0.1.6",
30
30
  "@catalogfi/wallets": "^0.2.49",
31
- "@gardenfi/core": "^0.2.0-beta.3",
32
- "@gardenfi/orderbook": "^0.2.0-beta.3",
33
- "@gardenfi/utils": "^0.0.1-beta.3",
31
+ "@gardenfi/core": "^0.2.0-beta.4",
32
+ "@gardenfi/orderbook": "^0.2.0-beta.4",
33
+ "@gardenfi/utils": "^0.0.1-beta.4",
34
34
  "react": "^18.3.1",
35
35
  "viem": "^2.21.15",
36
36
  "wagmi": "^2.12.16"