@gardenfi/react-hooks 0.0.1-beta.8 → 0.0.1-beta.80
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 +150 -68
- package/dist/index3.cjs +1 -1
- package/dist/index3.js +17 -13
- package/dist/index5.cjs +1 -0
- package/dist/index5.js +4 -0
- package/dist/src/lib/context/gardenProvider.types.d.ts +16 -5
- package/dist/src/lib/hooks/useOrderbook.d.ts +2 -2
- package/dist/src/lib/utils.d.ts +3 -0
- package/package.json +5 -5
package/dist/index2.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 s=require("react"),z=require("wagmi"),A=require("./index4.cjs"),K=require("./index3.cjs"),d=require("@gardenfi/core"),n=require("@catalogfi/utils"),q=require("@gardenfi/orderbook"),v=require("@catalogfi/wallets"),S=require("@gardenfi/utils"),Q=require("./index5.cjs"),y=s.createContext({isExecuting:!1}),$=({children:b,config:r})=>{const[l,_]=s.useState(),[c,B]=s.useState(),[i,N]=s.useState(),[O,U]=s.useState(),W=!!(l&&c&&i&&O),h=s.useMemo(()=>new d.Quote(r.quoteUrl),[r.quoteUrl]),{data:a}=z.useWalletClient(),{initializeSecretManager:C}=A.useSecretManager(_),{orderbook:m}=K.useOrderbook(r.orderBookUrl,i,U),P=r.network===v.BitcoinNetwork.Mainnet?"mainnet":r.network===v.BitcoinNetwork.Testnet?"testnet":void 0,f=r.blockNumberFetcherUrl&&P?{url:r.blockNumberFetcherUrl,network:P}:void 0,p=s.useMemo(()=>new v.BitcoinProvider(r.network,r.bitcoinRPCUrl),[r.network,r.bitcoinRPCUrl]),G=async()=>{if(!a||!i)return n.Err("WalletClient or auth not initialized");const e=await C();if(e.error)return n.Err(e.error);const o=v.BitcoinWallet.fromPrivateKey(e.val.getMasterPrivKey(),p),t=new d.Garden({orderbookURl:r.orderBookUrl,secretManager:e.val,quote:h,auth:i,wallets:{evmWallet:a,btcWallet:o},blockNumberFetcher:f});return B(t),n.Ok(t)},x=async e=>{if(!m||!a||!i)return n.Err("Orderbook or walletClient or auth not initialized");let o=c;if(!l||!o){const R=await G();if(R.error)return n.Err(R.error);o=R.val}const t=await o.swap(e);if(t.error)return n.Err(t.error);if(q.isBitcoin(t.val.source_swap.chain))return n.Ok(t.val);const u=await d.switchOrAddNetwork(e.fromAsset.chain,a);if(u.error)return n.Err("Failed to switch network: "+u.error);const M=u.val.walletClient,w=await new d.EvmRelay(t.val,r.orderBookUrl,i).init(M);if(w.error)return n.Err(w.error);const k={...t.val,source_swap:{...t.val.source_swap,initiate_tx_hash:w.val}};return n.Ok(k)},g=async e=>{if(!a||!i)return n.Err("Orderbook or walletClient or auth not initialized");let o=c;if(!l||!o){const k=await G();if(k.error)return n.Err(k.error);o=k.val}if(q.isBitcoin(e.source_swap.chain))return n.Err("Not an EVM order: sourceSwap.chain is Bitcoin");const t=await d.switchOrAddNetwork(e.source_swap.chain,a);if(t.error)return n.Err("Failed to switch network: "+t.error);const u=t.val.walletClient,E=await new d.EvmRelay(e,r.orderBookUrl,i).init(u);if(E.error)return n.Err(E.error);const w={...e,source_swap:{...e.source_swap,initiate_tx_hash:E.val}};return n.Ok(w)},F=async({fromAsset:e,toAsset:o,amount:t,isExactOut:u=!1})=>await h.getQuote(Q.constructOrderpair(e,o),t,u);return s.useEffect(()=>{if(!a)return;const e=new S.Siwe(new S.Url(r.orderBookUrl),a,{store:r.store});N(e)},[a]),s.useEffect(()=>{if(!l||!a||!m||!i)return;const e=v.BitcoinWallet.fromPrivateKey(l.getMasterPrivKey(),p),o=new d.Garden({orderbookURl:r.orderBookUrl,secretManager:l,quote:h,auth:i,wallets:{evmWallet:a,btcWallet:e},blockNumberFetcher:f});B(o)},[l,a,m,i]),s.useEffect(()=>{if(!c)return;const e=c.execute();return c.on("onPendingOrdersChanged",o=>{U(o)}),()=>{(async()=>(await e)())()}},[c]),s.createElement(y.Provider,{value:{orderBookUrl:r.orderBookUrl,initializeSecretManager:C,orderBook:m,swap:x,pendingOrders:O,getQuote:F,secretManager:l,garden:c,isExecuting:W,evmInitiate:g,quote:h}},b)},T=()=>{const b=s.useContext(y);if(!b)throw new Error("useGarden must be used within a GardenProvider");return b};exports.GardenContext=y;exports.GardenProvider=$;exports.useGarden=T;
|
package/dist/index2.js
CHANGED
|
@@ -1,90 +1,172 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useWalletClient as
|
|
3
|
-
import { useSecretManager as
|
|
4
|
-
import { useOrderbook as
|
|
5
|
-
import { Quote as
|
|
6
|
-
import { Err as
|
|
7
|
-
import { isBitcoin as
|
|
8
|
-
import { BitcoinProvider as
|
|
9
|
-
import { Siwe as
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import z, { createContext as V, useState as h, useMemo as G, useEffect as f } from "react";
|
|
2
|
+
import { useWalletClient as j } from "wagmi";
|
|
3
|
+
import { useSecretManager as D } from "./index4.js";
|
|
4
|
+
import { useOrderbook as H } from "./index3.js";
|
|
5
|
+
import { Quote as J, Garden as x, switchOrAddNetwork as W, EvmRelay as E } from "@gardenfi/core";
|
|
6
|
+
import { Err as a, Ok as b } from "@catalogfi/utils";
|
|
7
|
+
import { isBitcoin as F } from "@gardenfi/orderbook";
|
|
8
|
+
import { BitcoinNetwork as N, BitcoinProvider as L, BitcoinWallet as S } from "@catalogfi/wallets";
|
|
9
|
+
import { Siwe as X, Url as Y } from "@gardenfi/utils";
|
|
10
|
+
import { constructOrderpair as Z } from "./index5.js";
|
|
11
|
+
const g = V({
|
|
12
|
+
isExecuting: !1
|
|
13
|
+
}), ce = ({
|
|
14
|
+
children: w,
|
|
12
15
|
config: r
|
|
13
16
|
}) => {
|
|
14
|
-
const [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
const [s, K] = h(), [l, R] = h(), [i, q] = h(), [U, C] = h(), A = !!(s && l && i && U), m = G(() => new J(r.quoteUrl), [r.quoteUrl]), { data: n } = j(), { initializeSecretManager: y } = D(K), { orderbook: v } = H(
|
|
18
|
+
r.orderBookUrl,
|
|
19
|
+
i,
|
|
20
|
+
C
|
|
21
|
+
), B = r.network === N.Mainnet ? "mainnet" : r.network === N.Testnet ? "testnet" : void 0, P = r.blockNumberFetcherUrl && B ? {
|
|
22
|
+
url: r.blockNumberFetcherUrl,
|
|
23
|
+
network: B
|
|
24
|
+
} : void 0, M = G(
|
|
25
|
+
() => new L(r.network, r.bitcoinRPCUrl),
|
|
26
|
+
[r.network, r.bitcoinRPCUrl]
|
|
27
|
+
), O = async () => {
|
|
28
|
+
if (!n || !i)
|
|
29
|
+
return a("WalletClient or auth not initialized");
|
|
30
|
+
const e = await y();
|
|
31
|
+
if (e.error) return a(e.error);
|
|
32
|
+
const o = S.fromPrivateKey(
|
|
33
|
+
e.val.getMasterPrivKey(),
|
|
34
|
+
M
|
|
35
|
+
), t = new x({
|
|
36
|
+
orderbookURl: r.orderBookUrl,
|
|
37
|
+
secretManager: e.val,
|
|
38
|
+
quote: m,
|
|
39
|
+
auth: i,
|
|
40
|
+
wallets: {
|
|
41
|
+
evmWallet: n,
|
|
42
|
+
btcWallet: o
|
|
43
|
+
},
|
|
44
|
+
blockNumberFetcher: P
|
|
45
|
+
});
|
|
46
|
+
return R(t), b(t);
|
|
47
|
+
}, Q = async (e) => {
|
|
48
|
+
if (!v || !n || !i)
|
|
49
|
+
return a("Orderbook or walletClient or auth not initialized");
|
|
50
|
+
let o = l;
|
|
51
|
+
if (!s || !o) {
|
|
52
|
+
const p = await O();
|
|
53
|
+
if (p.error) return a(p.error);
|
|
54
|
+
o = p.val;
|
|
23
55
|
}
|
|
24
|
-
const
|
|
25
|
-
if (
|
|
26
|
-
if (
|
|
27
|
-
const
|
|
56
|
+
const t = await o.swap(e);
|
|
57
|
+
if (t.error) return a(t.error);
|
|
58
|
+
if (F(t.val.source_swap.chain)) return b(t.val);
|
|
59
|
+
const c = await W(
|
|
28
60
|
e.fromAsset.chain,
|
|
29
|
-
|
|
61
|
+
n
|
|
62
|
+
);
|
|
63
|
+
if (c.error)
|
|
64
|
+
return a("Failed to switch network: " + c.error);
|
|
65
|
+
const _ = c.val.walletClient, u = await new E(t.val, r.orderBookUrl, i).init(_);
|
|
66
|
+
if (u.error) return a(u.error);
|
|
67
|
+
const d = {
|
|
68
|
+
...t.val,
|
|
69
|
+
source_swap: {
|
|
70
|
+
...t.val.source_swap,
|
|
71
|
+
initiate_tx_hash: u.val
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
return b(d);
|
|
75
|
+
}, I = async (e) => {
|
|
76
|
+
if (!n || !i)
|
|
77
|
+
return a("Orderbook or walletClient or auth not initialized");
|
|
78
|
+
let o = l;
|
|
79
|
+
if (!s || !o) {
|
|
80
|
+
const d = await O();
|
|
81
|
+
if (d.error) return a(d.error);
|
|
82
|
+
o = d.val;
|
|
83
|
+
}
|
|
84
|
+
if (F(e.source_swap.chain))
|
|
85
|
+
return a("Not an EVM order: sourceSwap.chain is Bitcoin");
|
|
86
|
+
const t = await W(
|
|
87
|
+
e.source_swap.chain,
|
|
88
|
+
n
|
|
30
89
|
);
|
|
31
|
-
if (
|
|
32
|
-
return
|
|
33
|
-
const k = await new
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
90
|
+
if (t.error)
|
|
91
|
+
return a("Failed to switch network: " + t.error);
|
|
92
|
+
const c = t.val.walletClient, k = await new E(e, r.orderBookUrl, i).init(c);
|
|
93
|
+
if (k.error) return a(k.error);
|
|
94
|
+
const u = {
|
|
95
|
+
...e,
|
|
96
|
+
source_swap: {
|
|
97
|
+
...e.source_swap,
|
|
98
|
+
initiate_tx_hash: k.val
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
return b(u);
|
|
102
|
+
}, T = async ({
|
|
103
|
+
fromAsset: e,
|
|
104
|
+
toAsset: o,
|
|
105
|
+
amount: t,
|
|
106
|
+
isExactOut: c = !1
|
|
107
|
+
}) => await m.getQuote(
|
|
108
|
+
Z(e, o),
|
|
109
|
+
t,
|
|
110
|
+
c
|
|
111
|
+
);
|
|
112
|
+
return f(() => {
|
|
113
|
+
if (!n) return;
|
|
114
|
+
const e = new X(new Y(r.orderBookUrl), n, {
|
|
39
115
|
store: r.store
|
|
40
116
|
});
|
|
41
|
-
|
|
42
|
-
}, [
|
|
43
|
-
if (!
|
|
44
|
-
const e =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
)
|
|
48
|
-
R(e);
|
|
49
|
-
}, [o, v]), u(() => {
|
|
50
|
-
if (!o || !t || !d || !a) return;
|
|
51
|
-
const e = new N({
|
|
117
|
+
q(e);
|
|
118
|
+
}, [n]), f(() => {
|
|
119
|
+
if (!s || !n || !v || !i) return;
|
|
120
|
+
const e = S.fromPrivateKey(
|
|
121
|
+
s.getMasterPrivKey(),
|
|
122
|
+
M
|
|
123
|
+
), o = new x({
|
|
52
124
|
orderbookURl: r.orderBookUrl,
|
|
53
|
-
secretManager:
|
|
125
|
+
secretManager: s,
|
|
54
126
|
quote: m,
|
|
55
|
-
auth:
|
|
127
|
+
auth: i,
|
|
56
128
|
wallets: {
|
|
57
|
-
evmWallet:
|
|
58
|
-
btcWallet:
|
|
59
|
-
}
|
|
129
|
+
evmWallet: n,
|
|
130
|
+
btcWallet: e
|
|
131
|
+
},
|
|
132
|
+
blockNumberFetcher: P
|
|
60
133
|
});
|
|
61
|
-
|
|
62
|
-
}, [
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
134
|
+
R(o);
|
|
135
|
+
}, [s, n, v, i]), f(() => {
|
|
136
|
+
if (!l) return;
|
|
137
|
+
const e = l.execute();
|
|
138
|
+
return l.on("onPendingOrdersChanged", (o) => {
|
|
139
|
+
C(o);
|
|
140
|
+
}), () => {
|
|
141
|
+
(async () => (await e)())();
|
|
142
|
+
};
|
|
143
|
+
}, [l]), /* @__PURE__ */ z.createElement(
|
|
144
|
+
g.Provider,
|
|
68
145
|
{
|
|
69
146
|
value: {
|
|
70
147
|
orderBookUrl: r.orderBookUrl,
|
|
71
|
-
initializeSecretManager:
|
|
72
|
-
orderBook:
|
|
73
|
-
swap:
|
|
74
|
-
|
|
148
|
+
initializeSecretManager: y,
|
|
149
|
+
orderBook: v,
|
|
150
|
+
swap: Q,
|
|
151
|
+
pendingOrders: U,
|
|
152
|
+
getQuote: T,
|
|
153
|
+
secretManager: s,
|
|
154
|
+
garden: l,
|
|
155
|
+
isExecuting: A,
|
|
156
|
+
evmInitiate: I,
|
|
75
157
|
quote: m
|
|
76
158
|
}
|
|
77
159
|
},
|
|
78
|
-
|
|
160
|
+
w
|
|
79
161
|
);
|
|
80
|
-
},
|
|
81
|
-
const
|
|
82
|
-
if (!
|
|
162
|
+
}, ue = () => {
|
|
163
|
+
const w = z.useContext(g);
|
|
164
|
+
if (!w)
|
|
83
165
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
84
|
-
return
|
|
166
|
+
return w;
|
|
85
167
|
};
|
|
86
168
|
export {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
169
|
+
g as GardenContext,
|
|
170
|
+
ce as GardenProvider,
|
|
171
|
+
ue as useGarden
|
|
90
172
|
};
|
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 i=require("@gardenfi/orderbook"),s=require("react"),b=require("wagmi"),l=(e,r,u)=>{const{data:t}=b.useWalletClient(),[a,c]=s.useState();return s.useEffect(()=>{if(!t||!e||!r)return;const n=new i.Orderbook({url:e,walletClient:t,auth:r});c(n),n.fetchOrders(!0,!0,{per_page:500}).then(o=>{!o.error&&o.val&&u(o.val.data)})},[t,e,r]),{orderbook:a}};exports.useOrderbook=l;
|
package/dist/index3.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
import { Orderbook as
|
|
2
|
-
import { useState as
|
|
3
|
-
import { useWalletClient as
|
|
4
|
-
const
|
|
5
|
-
const { data:
|
|
6
|
-
return
|
|
7
|
-
if (!
|
|
8
|
-
const
|
|
1
|
+
import { Orderbook as i } from "@gardenfi/orderbook";
|
|
2
|
+
import { useState as l, useEffect as u } from "react";
|
|
3
|
+
import { useWalletClient as c } from "wagmi";
|
|
4
|
+
const d = (e, t, a) => {
|
|
5
|
+
const { data: r } = c(), [f, s] = l();
|
|
6
|
+
return u(() => {
|
|
7
|
+
if (!r || !e || !t) return;
|
|
8
|
+
const n = new i({
|
|
9
9
|
url: e,
|
|
10
|
-
walletClient:
|
|
11
|
-
auth:
|
|
10
|
+
walletClient: r,
|
|
11
|
+
auth: t
|
|
12
12
|
});
|
|
13
|
-
n(
|
|
14
|
-
|
|
13
|
+
s(n), n.fetchOrders(!0, !0, {
|
|
14
|
+
per_page: 500
|
|
15
|
+
}).then((o) => {
|
|
16
|
+
!o.error && o.val && a(o.val.data);
|
|
17
|
+
});
|
|
18
|
+
}, [r, e, t]), { orderbook: f };
|
|
15
19
|
};
|
|
16
20
|
export {
|
|
17
|
-
|
|
21
|
+
d as useOrderbook
|
|
18
22
|
};
|
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 { IQuote, SecretManager, SwapParams } from '@gardenfi/core';
|
|
4
|
-
import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
|
|
3
|
+
import { IGardenJS, IQuote, 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,9 +14,13 @@ export type GardenContextType = {
|
|
|
14
14
|
* @returns {AsyncResult<string, string>} - create order ID.
|
|
15
15
|
*/
|
|
16
16
|
swap?: (params: SwapParams) => AsyncResult<MatchedOrder, string>;
|
|
17
|
-
pendingOrders?:
|
|
17
|
+
pendingOrders?: MatchedOrder[];
|
|
18
|
+
getQuote?: (params: QuoteParams) => AsyncResult<QuoteResponse, string>;
|
|
19
|
+
secretManager?: ISecretManager;
|
|
20
|
+
garden?: IGardenJS;
|
|
21
|
+
evmInitiate?: (order: MatchedOrder) => AsyncResult<MatchedOrder, string>;
|
|
22
|
+
isExecuting: boolean;
|
|
18
23
|
quote?: IQuote;
|
|
19
|
-
pendingOrdersCount?: number;
|
|
20
24
|
};
|
|
21
25
|
export type GardenProviderProps = {
|
|
22
26
|
children: React.ReactNode;
|
|
@@ -24,7 +28,14 @@ export type GardenProviderProps = {
|
|
|
24
28
|
orderBookUrl: string;
|
|
25
29
|
quoteUrl: string;
|
|
26
30
|
store: IStore;
|
|
27
|
-
|
|
31
|
+
network: BitcoinNetwork;
|
|
28
32
|
bitcoinRPCUrl?: string;
|
|
33
|
+
blockNumberFetcherUrl?: string;
|
|
29
34
|
};
|
|
30
35
|
};
|
|
36
|
+
export type QuoteParams = {
|
|
37
|
+
fromAsset: Asset;
|
|
38
|
+
toAsset: Asset;
|
|
39
|
+
amount: number;
|
|
40
|
+
isExactOut?: boolean;
|
|
41
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IOrderbook } from '@gardenfi/orderbook';
|
|
1
|
+
import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
|
|
2
2
|
import { IAuth } from '@gardenfi/utils';
|
|
3
3
|
|
|
4
|
-
export declare const useOrderbook: (orderBookUrl: string, auth: IAuth | undefined) => {
|
|
4
|
+
export declare const useOrderbook: (orderBookUrl: string, auth: IAuth | undefined, setPendingOrders: React.Dispatch<React.SetStateAction<MatchedOrder[] | undefined>>) => {
|
|
5
5
|
orderbook: IOrderbook | undefined;
|
|
6
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.80",
|
|
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.51",
|
|
31
|
+
"@gardenfi/core": "^0.2.0-beta.59",
|
|
32
|
+
"@gardenfi/orderbook": "^0.2.0-beta.14",
|
|
33
|
+
"@gardenfi/utils": "^0.0.1-beta.18",
|
|
34
34
|
"react": "^18.3.1",
|
|
35
35
|
"viem": "^2.21.15",
|
|
36
36
|
"wagmi": "^2.12.16"
|