@gardenfi/react-hooks 2.0.2 → 2.0.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 s=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react"),x=require("./index3.cjs"),a=require("@gardenfi/core"),i=require("@catalogfi/utils"),v=require("@gardenfi/orderbook"),S=require("./index5.cjs"),w=s.createContext({isExecuting:!1,isExecutorRequired:!1}),_=({children:d,config:t})=>{const[n,h]=s.useState(),[O,C]=s.useState(),E=s.useMemo(()=>new a.Quote(t.quoteUrl||a.API[t.environment].quote),[t.quoteUrl,t.environment]),{pendingOrders:c,isExecuting:p}=x.useOrderbook(n),m=s.useMemo(()=>!!c.find(r=>{const e=r.status;return e===a.OrderStatus.InitiateDetected||e===a.OrderStatus.Initiated||e===a.OrderStatus.CounterPartyInitiateDetected||e===a.OrderStatus.CounterPartyInitiated||e===a.OrderStatus.RedeemDetected||e===a.OrderStatus.Expired}),[c]),q=async r=>{if(!n||!t.walletClient)return i.Err("Garden not initialized");const e=await n.swap(r);if(e.error)return i.Err(e.error);if(v.isBitcoin(e.val.source_swap.chain))return i.Ok(e.val);const o=await a.switchOrAddNetwork(r.fromAsset.chain,t.walletClient);if(o.error)return i.Err("Failed to switch network: "+o.error);const u=o.val.walletClient,l=await n.evmRelay.init(u,e.val);if(l.error)return i.Err(l.error);const G={...e.val,source_swap:{...e.val.source_swap,initiate_tx_hash:l.val}};return i.Ok(G)},k=async r=>{if(!n||!t.walletClient)return i.Err("garden not initialized");if(v.isBitcoin(r.source_swap.chain))return i.Err("Not an EVM order: sourceSwap.chain is Bitcoin");const e=await a.switchOrAddNetwork(r.source_swap.chain,t.walletClient);if(e.error)return i.Err("Failed to switch network: "+e.error);const o=e.val.walletClient,u=await n.evmRelay.init(o,r);if(u.error)return i.Err(u.error);const l={...r,source_swap:{...r.source_swap,initiate_tx_hash:u.val}};return i.Ok(l)};return s.useEffect(()=>{var r;if(t.walletClient){if(!((r=t.walletClient.account)!=null&&r.address))throw new Error("WalletClient doesn't have an account");h(new a.Garden({environment:t.environment,evmWallet:t.walletClient,siweOpts:t.siweOpts??{domain:window.location.hostname,store:t.store}}))}},[t.walletClient]),s.useEffect(()=>{n&&C(()=>async({fromAsset:r,toAsset:e,amount:o,isExactOut:u=!1})=>await n.quote.getQuote(S.constructOrderpair(r,e),o,u))},[n]),s.createElement(w.Provider,{value:{orderBook:n==null?void 0:n.orderbook,quote:E,swapAndInitiate:q,pendingOrders:c,getQuote:O,garden:n,isExecuting:p,isExecutorRequired:m,evmInitiate:k}},d)},R=()=>{const d=s.useContext(w);if(!d)throw new Error("useGarden must be used within a GardenProvider");return d};exports.GardenContext=w;exports.GardenProvider=_;exports.useGarden=R;
|
package/dist/index2.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useOrderbook as
|
|
3
|
-
import { OrderStatus as s, Garden as
|
|
4
|
-
import { Err as
|
|
5
|
-
import { isBitcoin as
|
|
6
|
-
import { constructOrderpair as
|
|
7
|
-
const
|
|
1
|
+
import C, { createContext as I, useState as d, useMemo as m, useEffect as p } from "react";
|
|
2
|
+
import { useOrderbook as P } from "./index3.js";
|
|
3
|
+
import { Quote as A, API as Q, OrderStatus as s, Garden as W, switchOrAddNetwork as v } from "@gardenfi/core";
|
|
4
|
+
import { Err as a, Ok as w } from "@catalogfi/utils";
|
|
5
|
+
import { isBitcoin as h } from "@gardenfi/orderbook";
|
|
6
|
+
import { constructOrderpair as b } from "./index5.js";
|
|
7
|
+
const f = I({
|
|
8
8
|
isExecuting: !1,
|
|
9
9
|
isExecutorRequired: !1
|
|
10
|
-
}),
|
|
10
|
+
}), N = ({
|
|
11
11
|
children: u,
|
|
12
|
-
config:
|
|
12
|
+
config: t
|
|
13
13
|
}) => {
|
|
14
|
-
const [
|
|
14
|
+
const [n, x] = d(), [E, _] = d(), O = m(() => new A(t.quoteUrl || Q[t.environment].quote), [t.quoteUrl, t.environment]), { pendingOrders: c, isExecuting: R } = P(n), G = m(() => !!c.find((r) => {
|
|
15
15
|
const e = r.status;
|
|
16
16
|
return e === s.InitiateDetected || e === s.Initiated || e === s.CounterPartyInitiateDetected || e === s.CounterPartyInitiated || e === s.RedeemDetected || e === s.Expired;
|
|
17
|
-
}), [c]),
|
|
18
|
-
if (!
|
|
19
|
-
const e = await
|
|
20
|
-
if (e.error) return
|
|
21
|
-
if (
|
|
22
|
-
const
|
|
17
|
+
}), [c]), k = async (r) => {
|
|
18
|
+
if (!n || !t.walletClient) return a("Garden not initialized");
|
|
19
|
+
const e = await n.swap(r);
|
|
20
|
+
if (e.error) return a(e.error);
|
|
21
|
+
if (h(e.val.source_swap.chain)) return w(e.val);
|
|
22
|
+
const i = await v(
|
|
23
23
|
r.fromAsset.chain,
|
|
24
|
-
|
|
24
|
+
t.walletClient
|
|
25
25
|
);
|
|
26
|
-
if (
|
|
27
|
-
return
|
|
28
|
-
const o =
|
|
29
|
-
if (l.error) return
|
|
30
|
-
const
|
|
26
|
+
if (i.error)
|
|
27
|
+
return a("Failed to switch network: " + i.error);
|
|
28
|
+
const o = i.val.walletClient, l = await n.evmRelay.init(o, e.val);
|
|
29
|
+
if (l.error) return a(l.error);
|
|
30
|
+
const y = {
|
|
31
31
|
...e.val,
|
|
32
32
|
source_swap: {
|
|
33
33
|
...e.val.source_swap,
|
|
34
34
|
initiate_tx_hash: l.val
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
return w(
|
|
38
|
-
},
|
|
39
|
-
if (!
|
|
40
|
-
if (
|
|
41
|
-
return
|
|
42
|
-
const e = await
|
|
37
|
+
return w(y);
|
|
38
|
+
}, q = async (r) => {
|
|
39
|
+
if (!n || !t.walletClient) return a("garden not initialized");
|
|
40
|
+
if (h(r.source_swap.chain))
|
|
41
|
+
return a("Not an EVM order: sourceSwap.chain is Bitcoin");
|
|
42
|
+
const e = await v(
|
|
43
43
|
r.source_swap.chain,
|
|
44
|
-
|
|
44
|
+
t.walletClient
|
|
45
45
|
);
|
|
46
46
|
if (e.error)
|
|
47
|
-
return
|
|
48
|
-
const
|
|
49
|
-
if (o.error) return
|
|
47
|
+
return a("Failed to switch network: " + e.error);
|
|
48
|
+
const i = e.val.walletClient, o = await n.evmRelay.init(i, r);
|
|
49
|
+
if (o.error) return a(o.error);
|
|
50
50
|
const l = {
|
|
51
51
|
...r,
|
|
52
52
|
source_swap: {
|
|
@@ -56,60 +56,60 @@ const C = y({
|
|
|
56
56
|
};
|
|
57
57
|
return w(l);
|
|
58
58
|
};
|
|
59
|
-
return
|
|
59
|
+
return p(() => {
|
|
60
60
|
var r;
|
|
61
|
-
if (
|
|
62
|
-
if (!((r =
|
|
61
|
+
if (t.walletClient) {
|
|
62
|
+
if (!((r = t.walletClient.account) != null && r.address))
|
|
63
63
|
throw new Error("WalletClient doesn't have an account");
|
|
64
|
-
|
|
65
|
-
new
|
|
66
|
-
environment:
|
|
67
|
-
evmWallet:
|
|
68
|
-
siweOpts: {
|
|
64
|
+
x(
|
|
65
|
+
new W({
|
|
66
|
+
environment: t.environment,
|
|
67
|
+
evmWallet: t.walletClient,
|
|
68
|
+
siweOpts: t.siweOpts ?? {
|
|
69
69
|
domain: window.location.hostname,
|
|
70
|
-
store:
|
|
70
|
+
store: t.store
|
|
71
71
|
}
|
|
72
72
|
})
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
|
-
}, [
|
|
76
|
-
|
|
75
|
+
}, [t.walletClient]), p(() => {
|
|
76
|
+
n && _(
|
|
77
77
|
() => async ({
|
|
78
78
|
fromAsset: r,
|
|
79
79
|
toAsset: e,
|
|
80
|
-
amount:
|
|
80
|
+
amount: i,
|
|
81
81
|
isExactOut: o = !1
|
|
82
|
-
}) => await
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
}) => await n.quote.getQuote(
|
|
83
|
+
b(r, e),
|
|
84
|
+
i,
|
|
85
85
|
o
|
|
86
86
|
)
|
|
87
87
|
);
|
|
88
|
-
}, [
|
|
89
|
-
|
|
88
|
+
}, [n]), /* @__PURE__ */ C.createElement(
|
|
89
|
+
f.Provider,
|
|
90
90
|
{
|
|
91
91
|
value: {
|
|
92
|
-
orderBook:
|
|
93
|
-
quote:
|
|
94
|
-
swapAndInitiate:
|
|
92
|
+
orderBook: n == null ? void 0 : n.orderbook,
|
|
93
|
+
quote: O,
|
|
94
|
+
swapAndInitiate: k,
|
|
95
95
|
pendingOrders: c,
|
|
96
|
-
getQuote:
|
|
97
|
-
garden:
|
|
98
|
-
isExecuting:
|
|
99
|
-
isExecutorRequired:
|
|
100
|
-
evmInitiate:
|
|
96
|
+
getQuote: E,
|
|
97
|
+
garden: n,
|
|
98
|
+
isExecuting: R,
|
|
99
|
+
isExecutorRequired: G,
|
|
100
|
+
evmInitiate: q
|
|
101
101
|
}
|
|
102
102
|
},
|
|
103
103
|
u
|
|
104
104
|
);
|
|
105
|
-
},
|
|
106
|
-
const u =
|
|
105
|
+
}, U = () => {
|
|
106
|
+
const u = C.useContext(f);
|
|
107
107
|
if (!u)
|
|
108
108
|
throw new Error("useGarden must be used within a GardenProvider");
|
|
109
109
|
return u;
|
|
110
110
|
};
|
|
111
111
|
export {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
f as GardenContext,
|
|
113
|
+
N as GardenProvider,
|
|
114
|
+
U as useGarden
|
|
115
115
|
};
|
|
@@ -2,7 +2,7 @@ import { WalletClient } from 'viem';
|
|
|
2
2
|
import { OrderWithStatus, IGardenJS, IQuote, QuoteResponse, SwapParams } from '@gardenfi/core';
|
|
3
3
|
import { AsyncResult, Result } from '@catalogfi/utils';
|
|
4
4
|
import { Asset, IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
|
|
5
|
-
import { Environment, IStore } from '@gardenfi/utils';
|
|
5
|
+
import { Environment, IStore, SiweOpts } from '@gardenfi/utils';
|
|
6
6
|
|
|
7
7
|
export type GardenContextType = {
|
|
8
8
|
/**
|
|
@@ -64,6 +64,7 @@ export type GardenProviderProps = {
|
|
|
64
64
|
quoteUrl?: string;
|
|
65
65
|
bitcoinRPCUrl?: string;
|
|
66
66
|
blockNumberFetcherUrl?: string;
|
|
67
|
+
siweOpts?: SiweOpts;
|
|
67
68
|
};
|
|
68
69
|
};
|
|
69
70
|
export type QuoteParams = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/react-hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
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.51",
|
|
31
|
-
"@gardenfi/core": "^2.0.
|
|
31
|
+
"@gardenfi/core": "^2.0.3",
|
|
32
32
|
"@gardenfi/orderbook": "^2.0.0",
|
|
33
33
|
"@gardenfi/utils": "^2.0.0",
|
|
34
34
|
"react": "^18.3.1",
|