@fedimint/react 0.0.3 → 0.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.
@@ -3,13 +3,18 @@ type FedimintWalletConfig = {
3
3
  lazy?: boolean;
4
4
  debug?: boolean;
5
5
  };
6
+ export type WalletStatus = 'open' | 'closed' | 'opening';
6
7
  export declare const setupFedimintWallet: (config: FedimintWalletConfig) => void;
7
8
  export declare const FedimintWalletContext: import('react').Context<{
8
9
  wallet: FedimintWallet;
10
+ walletStatus: WalletStatus;
11
+ setWalletStatus: (status: WalletStatus) => void;
9
12
  } | undefined>;
10
13
  export type FedimintWalletProviderProps = {};
11
14
  export declare const FedimintWalletProvider: (parameters: React.PropsWithChildren<FedimintWalletProviderProps>) => import('react').FunctionComponentElement<import('react').ProviderProps<{
12
15
  wallet: FedimintWallet;
16
+ walletStatus: WalletStatus;
17
+ setWalletStatus: (status: WalletStatus) => void;
13
18
  } | undefined>>;
14
19
  export {};
15
20
  //# sourceMappingURL=FedimintWalletContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"FedimintWalletContext.d.ts","sourceRoot":"","sources":["../../lib/contexts/FedimintWalletContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAKnD,KAAK,oBAAoB,GAAG;IAC1B,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,eAAO,MAAM,mBAAmB,WAAY,oBAAoB,SAK/D,CAAA;AAED,eAAO,MAAM,qBAAqB;YACtB,cAAc;cACd,CAAA;AAEZ,MAAM,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAE5C,eAAO,MAAM,sBAAsB,eACrB,KAAK,CAAC,iBAAiB,CAAC,2BAA2B,CAAC;YANtD,cAAc;eAkBzB,CAAA"}
1
+ {"version":3,"file":"FedimintWalletContext.d.ts","sourceRoot":"","sources":["../../lib/contexts/FedimintWalletContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAWnD,KAAK,oBAAoB,GAAG;IAC1B,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;AAExD,eAAO,MAAM,mBAAmB,GAAI,QAAQ,oBAAoB,SAK/D,CAAA;AAED,eAAO,MAAM,qBAAqB;YAEpB,cAAc;kBACR,YAAY;qBACT,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI;cAGzC,CAAA;AAEZ,MAAM,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAE5C,eAAO,MAAM,sBAAsB,GACjC,YAAY,KAAK,CAAC,iBAAiB,CAAC,2BAA2B,CAAC;YAVpD,cAAc;kBACR,YAAY;qBACT,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI;eAsCpD,CAAA"}
@@ -3,4 +3,6 @@ export { useOpenWallet } from './useOpenWallet';
3
3
  export { useFedimintWallet } from './useFedimintWallet';
4
4
  export { useReceiveLightning } from './useReceiveLightning';
5
5
  export { useSendLightning } from './useSendLightning';
6
+ export { useSpendEcash } from './useSpendEcash';
7
+ export { useReceiveEcash } from './useReceiveEcash';
6
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA"}
@@ -1,8 +1,6 @@
1
- type WalletStatus = 'open' | 'closed' | 'opening';
2
1
  export declare const useOpenWallet: () => {
3
- walletStatus: WalletStatus | undefined;
2
+ walletStatus: import('../contexts/FedimintWalletContext').WalletStatus;
4
3
  openWallet: () => void;
5
4
  joinFederation: (invite: string) => Promise<void>;
6
5
  };
7
- export {};
8
6
  //# sourceMappingURL=useOpenWallet.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useOpenWallet.d.ts","sourceRoot":"","sources":["../../lib/hooks/useOpenWallet.ts"],"names":[],"mappings":"AAGA,KAAK,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;AAEjD,eAAO,MAAM,aAAa;;;6BAcP,MAAM;CAsBxB,CAAA"}
1
+ {"version":3,"file":"useOpenWallet.d.ts","sourceRoot":"","sources":["../../lib/hooks/useOpenWallet.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,aAAa;;;6BAqBP,MAAM;CAaxB,CAAA"}
@@ -0,0 +1,7 @@
1
+ import { ReissueExternalNotesState } from '@fedimint/core-web';
2
+ export declare const useReceiveEcash: () => {
3
+ redeemEcash: (notes: string) => Promise<void>;
4
+ state: "Error" | ReissueExternalNotesState | undefined;
5
+ error: string | undefined;
6
+ };
7
+ //# sourceMappingURL=useReceiveEcash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useReceiveEcash.d.ts","sourceRoot":"","sources":["../../lib/hooks/useReceiveEcash.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAA;AAE9D,eAAO,MAAM,eAAe;yBA2BV,MAAM;;;CAkBvB,CAAA"}
@@ -0,0 +1,8 @@
1
+ import { SpendNotesState } from '@fedimint/core-web';
2
+ export declare const useSpendEcash: () => {
3
+ spendEcash: (amountSats: number, reclaimAfter?: number) => Promise<string>;
4
+ notes: string | undefined;
5
+ state: SpendNotesState | "Error" | undefined;
6
+ error: string | undefined;
7
+ };
8
+ //# sourceMappingURL=useSpendEcash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSpendEcash.d.ts","sourceRoot":"","sources":["../../lib/hooks/useSpendEcash.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEzD,eAAO,MAAM,aAAa;6BA8BH,MAAM,iBAAiB,MAAM;;;;CAmBnD,CAAA"}
package/dist/index.js CHANGED
@@ -1,121 +1,202 @@
1
- import { useState as s, useEffect as d, useCallback as u, createContext as y, createElement as f, useContext as S } from "react";
2
- import { FedimintWallet as W } from "@fedimint/core-web";
3
- const I = () => {
4
- const e = w(), { walletStatus: n } = b(), [t, i] = s();
5
- return d(() => {
1
+ import { useState as r, useEffect as h, createContext as y, useMemo as W, createElement as F, useContext as f, useCallback as m } from "react";
2
+ import { FedimintWallet as I } from "@fedimint/core-web";
3
+ const L = () => {
4
+ const e = g(), { walletStatus: n } = E(), [t, o] = r();
5
+ return h(() => {
6
6
  if (n !== "open") return;
7
- const a = e.balance.subscribeBalance((r) => {
8
- i(r);
7
+ const a = e.balance.subscribeBalance((c) => {
8
+ o(c);
9
9
  });
10
10
  return () => {
11
11
  a();
12
12
  };
13
13
  }, [n]), t;
14
- }, b = () => {
15
- const e = w(), [n, t] = s(), i = u(() => {
16
- n !== "open" && (t("opening"), e.open().then((r) => {
17
- t(r ? "open" : "closed");
18
- }));
19
- }, [e]), a = u(
20
- async (r) => {
21
- if (n === "open") return;
22
- t("opening");
23
- const l = await e.joinFederation(r);
24
- t(l ? "open" : "closed");
25
- },
26
- [e]
27
- );
28
- return d(() => (e.waitForOpen().then(() => {
29
- t("open");
30
- }), () => {
31
- t("closed");
32
- }), [e]), { walletStatus: n, openWallet: i, joinFederation: a };
33
14
  };
34
- let p;
35
- const L = (e) => {
36
- p = new W(!!e.lazy), e.debug && p.setLogLevel("debug");
37
- }, h = y(void 0), P = (e) => {
38
- const { children: n } = e;
39
- if (!p)
15
+ let w;
16
+ const R = (e) => {
17
+ w = new I(!!e.lazy), e.debug && w.setLogLevel("debug");
18
+ }, v = y(void 0), O = (e) => {
19
+ const [n, t] = r("closed"), { children: o } = e;
20
+ if (!w)
40
21
  throw new Error(
41
22
  "You must call setupFedimintWallet() first. See the getting started guide."
42
23
  );
43
- const t = { value: { wallet: p } };
44
- return f(h.Provider, t, n);
45
- }, w = () => {
46
- const e = S(h);
24
+ const a = W(
25
+ () => ({
26
+ wallet: w,
27
+ walletStatus: n,
28
+ setWalletStatus: t
29
+ }),
30
+ [n]
31
+ );
32
+ return h(() => {
33
+ w.waitForOpen().then(() => {
34
+ t("open");
35
+ });
36
+ }, [w]), F(
37
+ v.Provider,
38
+ { value: a },
39
+ o
40
+ );
41
+ }, E = () => {
42
+ const e = f(v);
43
+ if (!e)
44
+ throw new Error(
45
+ "useOpenWallet must be used within a FedimintWalletProvider"
46
+ );
47
+ const { wallet: n, walletStatus: t, setWalletStatus: o } = e, a = m(() => {
48
+ t !== "open" && (o("opening"), n.open().then((l) => {
49
+ o(l ? "open" : "closed");
50
+ }));
51
+ }, [n]), c = m(
52
+ async (l) => {
53
+ t !== "open" && (o("opening"), await n.joinFederation(l).then((u) => {
54
+ o(u ? "open" : "closed");
55
+ }));
56
+ },
57
+ [n]
58
+ );
59
+ return { walletStatus: t, openWallet: a, joinFederation: c };
60
+ }, g = () => {
61
+ const e = f(v);
47
62
  if (!(e != null && e.wallet))
48
63
  throw new Error(
49
64
  "useFedimintWallet must be used within a FedimintWalletProvider"
50
65
  );
51
66
  return e.wallet;
52
- }, x = () => {
53
- const e = w(), { walletStatus: n } = b(), [t, i] = s(), [a, r] = s(), [l, g] = s(), m = u(
54
- async (c, o) => {
67
+ }, C = () => {
68
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), p = m(
69
+ async (i, s) => {
55
70
  if (n !== "open") throw new Error("Wallet is not open");
56
- const v = await e.lightning.createInvoice(c, o);
57
- return i(v), v.invoice;
71
+ const d = await e.lightning.createInvoice(i, s);
72
+ return o(d), d.invoice;
58
73
  },
59
74
  [e, n]
60
75
  );
61
- return d(() => {
76
+ return h(() => {
62
77
  if (n !== "open" || !t) return;
63
- const c = e.lightning.subscribeLnReceive(
78
+ const i = e.lightning.subscribeLnReceive(
64
79
  t.operation_id,
65
- (o) => {
66
- r(o);
80
+ (s) => {
81
+ c(s);
67
82
  },
68
- (o) => {
69
- g(o);
83
+ (s) => {
84
+ u(s);
70
85
  }
71
86
  );
72
87
  return () => {
73
- c();
88
+ i();
74
89
  };
75
90
  }, [n, t]), {
76
- generateInvoice: m,
91
+ generateInvoice: p,
77
92
  bolt11: t == null ? void 0 : t.invoice,
78
93
  invoiceStatus: a,
79
94
  error: l
80
95
  };
81
- }, C = () => {
82
- const e = w(), { walletStatus: n } = b(), [t, i] = s(), [a, r] = s(), [l, g] = s(), m = u(
83
- async (c) => {
96
+ }, N = () => {
97
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), p = m(
98
+ async (i) => {
84
99
  if (n !== "open") throw new Error("Wallet is not open");
85
- const o = await e.lightning.payInvoice(c);
86
- return i(o), o;
100
+ const s = await e.lightning.payInvoice(i);
101
+ return o(s), s;
87
102
  },
88
103
  [e, n]
89
104
  );
90
- return d(() => {
105
+ return h(() => {
91
106
  if (n !== "open" || !t) return;
92
- const c = e.lightning.subscribeLnPay(
107
+ const i = e.lightning.subscribeLnPay(
93
108
  // @ts-ignore
94
109
  t.payment_type.lightning,
95
- (o) => {
96
- r(o);
110
+ (s) => {
111
+ c(s);
97
112
  },
98
- (o) => {
99
- g(o);
113
+ (s) => {
114
+ u(s);
100
115
  }
101
116
  );
102
117
  return () => {
103
- c();
118
+ i();
104
119
  };
105
120
  }, [n, t]), {
106
- payInvoice: m,
121
+ payInvoice: p,
107
122
  payment: t,
108
123
  paymentStatus: a,
109
124
  paymentError: l
110
125
  };
126
+ }, B = () => {
127
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), [p, i] = r();
128
+ return h(() => {
129
+ if (!t) return;
130
+ const d = e.mint.subscribeSpendNotes(
131
+ t,
132
+ (b) => {
133
+ u(b);
134
+ },
135
+ (b) => {
136
+ u("Error"), i(b);
137
+ }
138
+ );
139
+ return () => {
140
+ d();
141
+ };
142
+ }, [t]), {
143
+ spendEcash: m(
144
+ async (d, b) => {
145
+ if (n !== "open") throw new Error("Wallet is not open");
146
+ const S = await e.mint.spendNotes(
147
+ d * 1e3,
148
+ b
149
+ );
150
+ return o(S.operation_id), c(S.notes), S.notes;
151
+ },
152
+ [e, n]
153
+ ),
154
+ notes: a,
155
+ state: l,
156
+ error: p
157
+ };
158
+ }, _ = () => {
159
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r();
160
+ return h(() => {
161
+ if (!t) return;
162
+ const i = e.mint.subscribeReissueExternalNotes(
163
+ t,
164
+ (s) => {
165
+ c(s);
166
+ },
167
+ (s) => {
168
+ u(s);
169
+ }
170
+ );
171
+ return () => {
172
+ i();
173
+ };
174
+ }, [t]), {
175
+ redeemEcash: m(
176
+ async (i) => {
177
+ if (n !== "open") throw new Error("Wallet is not open");
178
+ try {
179
+ const s = await e.mint.redeemEcash(i);
180
+ o(s);
181
+ } catch (s) {
182
+ c("Error"), u(s);
183
+ }
184
+ },
185
+ [e, n]
186
+ ),
187
+ state: a,
188
+ error: l
189
+ };
111
190
  };
112
191
  export {
113
- h as FedimintWalletContext,
114
- P as FedimintWalletProvider,
115
- L as setupFedimintWallet,
116
- I as useBalance,
117
- w as useFedimintWallet,
118
- b as useOpenWallet,
119
- x as useReceiveLightning,
120
- C as useSendLightning
192
+ v as FedimintWalletContext,
193
+ O as FedimintWalletProvider,
194
+ R as setupFedimintWallet,
195
+ L as useBalance,
196
+ g as useFedimintWallet,
197
+ E as useOpenWallet,
198
+ _ as useReceiveEcash,
199
+ C as useReceiveLightning,
200
+ N as useSendLightning,
201
+ B as useSpendEcash
121
202
  };
@@ -1 +1 @@
1
- {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../lib/init.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,iBAAiB,UAAU,OAAO,OAM9C,CAAA"}
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../lib/init.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,iBAAiB,GAAI,OAAM,OAAe,OAMtD,CAAA"}
@@ -1,5 +1,11 @@
1
1
  import { FedimintWallet } from '@fedimint/core-web'
2
- import { createContext, createElement } from 'react'
2
+ import {
3
+ createContext,
4
+ createElement,
5
+ useEffect,
6
+ useMemo,
7
+ useState,
8
+ } from 'react'
3
9
 
4
10
  let wallet: FedimintWallet
5
11
 
@@ -8,6 +14,8 @@ type FedimintWalletConfig = {
8
14
  debug?: boolean
9
15
  }
10
16
 
17
+ export type WalletStatus = 'open' | 'closed' | 'opening'
18
+
11
19
  export const setupFedimintWallet = (config: FedimintWalletConfig) => {
12
20
  wallet = new FedimintWallet(!!config.lazy)
13
21
  if (config.debug) {
@@ -16,7 +24,12 @@ export const setupFedimintWallet = (config: FedimintWalletConfig) => {
16
24
  }
17
25
 
18
26
  export const FedimintWalletContext = createContext<
19
- { wallet: FedimintWallet } | undefined
27
+ | {
28
+ wallet: FedimintWallet
29
+ walletStatus: WalletStatus
30
+ setWalletStatus: (status: WalletStatus) => void
31
+ }
32
+ | undefined
20
33
  >(undefined)
21
34
 
22
35
  export type FedimintWalletProviderProps = {}
@@ -24,6 +37,7 @@ export type FedimintWalletProviderProps = {}
24
37
  export const FedimintWalletProvider = (
25
38
  parameters: React.PropsWithChildren<FedimintWalletProviderProps>,
26
39
  ) => {
40
+ const [walletStatus, setWalletStatus] = useState<WalletStatus>('closed')
27
41
  const { children } = parameters
28
42
 
29
43
  if (!wallet)
@@ -31,7 +45,24 @@ export const FedimintWalletProvider = (
31
45
  'You must call setupFedimintWallet() first. See the getting started guide.',
32
46
  )
33
47
 
34
- const props = { value: { wallet } }
48
+ const contextValue = useMemo(
49
+ () => ({
50
+ wallet,
51
+ walletStatus,
52
+ setWalletStatus,
53
+ }),
54
+ [walletStatus],
55
+ )
56
+
57
+ useEffect(() => {
58
+ wallet.waitForOpen().then(() => {
59
+ setWalletStatus('open')
60
+ })
61
+ }, [wallet])
35
62
 
36
- return createElement(FedimintWalletContext.Provider, props, children)
63
+ return createElement(
64
+ FedimintWalletContext.Provider,
65
+ { value: contextValue },
66
+ children,
67
+ )
37
68
  }
@@ -3,3 +3,5 @@ export { useOpenWallet } from './useOpenWallet'
3
3
  export { useFedimintWallet } from './useFedimintWallet'
4
4
  export { useReceiveLightning } from './useReceiveLightning'
5
5
  export { useSendLightning } from './useSendLightning'
6
+ export { useSpendEcash } from './useSpendEcash'
7
+ export { useReceiveEcash } from './useReceiveEcash'
@@ -1,11 +1,16 @@
1
- import { useCallback, useEffect, useState } from 'react'
2
- import { useFedimintWallet } from '.'
3
-
4
- type WalletStatus = 'open' | 'closed' | 'opening'
1
+ import { useCallback, useContext } from 'react'
2
+ import { FedimintWalletContext } from '../contexts/FedimintWalletContext'
5
3
 
6
4
  export const useOpenWallet = () => {
7
- const wallet = useFedimintWallet()
8
- const [walletStatus, setWalletStatus] = useState<WalletStatus>()
5
+ const value = useContext(FedimintWalletContext)
6
+
7
+ if (!value) {
8
+ throw new Error(
9
+ 'useOpenWallet must be used within a FedimintWalletProvider',
10
+ )
11
+ }
12
+
13
+ const { wallet, walletStatus, setWalletStatus } = value
9
14
 
10
15
  const openWallet = useCallback(() => {
11
16
  if (walletStatus === 'open') return
@@ -22,21 +27,12 @@ export const useOpenWallet = () => {
22
27
 
23
28
  setWalletStatus('opening')
24
29
 
25
- const res = await wallet.joinFederation(invite)
26
- setWalletStatus(res ? 'open' : 'closed')
30
+ await wallet.joinFederation(invite).then((res) => {
31
+ setWalletStatus(res ? 'open' : 'closed')
32
+ })
27
33
  },
28
34
  [wallet],
29
35
  )
30
36
 
31
- useEffect(() => {
32
- wallet.waitForOpen().then(() => {
33
- setWalletStatus('open')
34
- })
35
-
36
- return () => {
37
- setWalletStatus('closed')
38
- }
39
- }, [wallet])
40
-
41
37
  return { walletStatus, openWallet, joinFederation }
42
38
  }
@@ -0,0 +1,50 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { useFedimintWallet, useOpenWallet } from '.'
3
+ import { ReissueExternalNotesState } from '@fedimint/core-web'
4
+
5
+ export const useReceiveEcash = () => {
6
+ const wallet = useFedimintWallet()
7
+ const { walletStatus } = useOpenWallet()
8
+
9
+ const [operationId, setOperationId] = useState<string>()
10
+ const [state, setState] = useState<ReissueExternalNotesState | 'Error'>()
11
+ const [error, setError] = useState<string>()
12
+
13
+ useEffect(() => {
14
+ if (!operationId) return
15
+
16
+ const unsubscribe = wallet.mint.subscribeReissueExternalNotes(
17
+ operationId,
18
+ (_state) => {
19
+ setState(_state)
20
+ },
21
+ (error) => {
22
+ setError(error)
23
+ },
24
+ )
25
+
26
+ return () => {
27
+ unsubscribe()
28
+ }
29
+ }, [operationId])
30
+
31
+ const redeemEcash = useCallback(
32
+ async (notes: string) => {
33
+ if (walletStatus !== 'open') throw new Error('Wallet is not open')
34
+ try {
35
+ const response = await wallet.mint.redeemEcash(notes)
36
+ setOperationId(response)
37
+ } catch (e) {
38
+ setState('Error')
39
+ setError(e as string)
40
+ }
41
+ },
42
+ [wallet, walletStatus],
43
+ )
44
+
45
+ return {
46
+ redeemEcash,
47
+ state,
48
+ error,
49
+ }
50
+ }
@@ -0,0 +1,54 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { useFedimintWallet, useOpenWallet } from '.'
3
+ import { type SpendNotesState } from '@fedimint/core-web'
4
+
5
+ export const useSpendEcash = () => {
6
+ const wallet = useFedimintWallet()
7
+ const { walletStatus } = useOpenWallet()
8
+
9
+ const [operationId, setOperationId] = useState<string>()
10
+ const [notes, setNotes] = useState<string>()
11
+
12
+ const [state, setState] = useState<SpendNotesState | 'Error'>()
13
+ const [error, setError] = useState<string>()
14
+
15
+ useEffect(() => {
16
+ if (!operationId) return
17
+
18
+ const unsubscribe = wallet.mint.subscribeSpendNotes(
19
+ operationId,
20
+ (_state) => {
21
+ setState(_state)
22
+ },
23
+ (error) => {
24
+ setState('Error')
25
+ setError(error)
26
+ },
27
+ )
28
+
29
+ return () => {
30
+ unsubscribe()
31
+ }
32
+ }, [operationId])
33
+
34
+ const spendEcash = useCallback(
35
+ async (amountSats: number, reclaimAfter?: number) => {
36
+ if (walletStatus !== 'open') throw new Error('Wallet is not open')
37
+ const response = await wallet.mint.spendNotes(
38
+ amountSats * 1000,
39
+ reclaimAfter,
40
+ )
41
+ setOperationId(response.operation_id)
42
+ setNotes(response.notes)
43
+ return response.notes
44
+ },
45
+ [wallet, walletStatus],
46
+ )
47
+
48
+ return {
49
+ spendEcash,
50
+ notes,
51
+ state,
52
+ error,
53
+ }
54
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedimint/react",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -10,22 +10,22 @@
10
10
  "types": "./dist/index.d.ts",
11
11
  "peerDependencies": {
12
12
  "react": "^18.3.1",
13
- "@fedimint/core-web": "0.0.10"
13
+ "@fedimint/core-web": "0.0.11"
14
14
  },
15
15
  "devDependencies": {
16
- "@types/react": ">=18.3.11",
17
- "@types/react-dom": "^18.3.0",
16
+ "@types/react": ">=19.1.4",
17
+ "@types/react-dom": "^18.3.7",
18
18
  "@typescript-eslint/eslint-plugin": "^7.18.0",
19
19
  "@typescript-eslint/parser": "^7.18.0",
20
- "@vitejs/plugin-react": "^4.3.2",
21
- "eslint": "^9.11.1",
20
+ "@vitejs/plugin-react": "^4.4.1",
21
+ "eslint": "^9.27.0",
22
22
  "eslint-plugin-react-hooks": "^4.6.2",
23
- "eslint-plugin-react-refresh": "^0.4.12",
24
- "react-dom": ">=18.3.1",
25
- "typescript": "^5.6.2",
26
- "vite": "^5.4.8",
27
- "vite-plugin-dts": "^4.2.4",
28
- "vite-plugin-wasm": "^3.3.0"
23
+ "eslint-plugin-react-refresh": "^0.4.20",
24
+ "react-dom": ">=19.1.0",
25
+ "typescript": "^5.8.3",
26
+ "vite": "^5.4.19",
27
+ "vite-plugin-dts": "^4.5.4",
28
+ "vite-plugin-wasm": "^3.4.1"
29
29
  },
30
30
  "scripts": {
31
31
  "dev": "vite",