forked from Disciplr-Org/Disciplr-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalletContext.tsx
More file actions
340 lines (302 loc) · 13.3 KB
/
Copy pathWalletContext.tsx
File metadata and controls
340 lines (302 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { createContext, useContext, useState, useEffect, useRef, ReactNode, useCallback } from 'react';
import { isAllowed, setAllowed, requestAccess, getAddress, getNetworkDetails } from '@stellar/freighter-api';
import { fetchUsdcBalance } from '../utils/horizon';
import { logger } from '../utils/logger';
import {
recordWalletTelemetry,
resolveConnectTimeoutMs,
type ConnectErrorCode,
} from '../utils/walletTelemetry';
export type WalletNetwork = 'TESTNET' | 'PUBLIC';
export type BalanceStatus = 'idle' | 'loading' | 'success' | 'no_trustline' | 'error';
export type WalletStatus = 'disconnected' | 'restoring' | 'connecting' | 'connected' | 'error';
interface WalletState {
status: WalletStatus;
address: string | null;
network: WalletNetwork | null;
balance: string | null;
balanceStatus: BalanceStatus;
balanceError: string | null;
error: string | null;
}
const initialState: WalletState = {
status: 'disconnected',
address: null,
network: null,
balance: null,
balanceStatus: 'idle',
balanceError: null,
error: null,
};
type Action =
| { type: 'RESTORE_START' }
| { type: 'RESTORE_ABORT' }
| { type: 'CONNECT_START' }
| { type: 'CONNECT_SUCCESS'; payload: { address: string; network: WalletNetwork } }
| { type: 'CONNECT_ERROR'; payload: { error: string } }
| { type: 'DISCONNECT' }
| { type: 'BALANCE_FETCH_START' }
| { type: 'BALANCE_FETCH_SUCCESS'; payload: { balance: string | null; status: BalanceStatus; network: WalletNetwork } }
| { type: 'BALANCE_FETCH_ERROR'; payload: { error: string } }
| { type: 'UPDATE_NETWORK'; payload: { network: WalletNetwork } }
| { type: 'UPDATE_ADDRESS'; payload: { address: string } };
function walletReducer(state: WalletState, action: Action): WalletState {
switch (action.type) {
case 'RESTORE_START':
return state.status === 'disconnected' || state.status === 'error' ? { ...state, status: 'restoring', error: null } : state;
case 'RESTORE_ABORT':
return state.status === 'restoring' ? { ...state, status: 'disconnected' } : state;
case 'CONNECT_START':
return { ...state, status: 'connecting', error: null };
case 'CONNECT_SUCCESS':
return { ...state, status: 'connected', address: action.payload.address, network: action.payload.network, error: null };
case 'CONNECT_ERROR':
return { ...state, status: 'error', error: action.payload.error };
case 'DISCONNECT':
return initialState;
case 'BALANCE_FETCH_START':
return { ...state, balanceStatus: 'loading', balanceError: null };
case 'BALANCE_FETCH_SUCCESS':
return { ...state, balance: action.payload.balance, balanceStatus: action.payload.status, network: action.payload.network };
case 'BALANCE_FETCH_ERROR':
return { ...state, balance: null, balanceStatus: 'error', balanceError: action.payload.error };
case 'UPDATE_NETWORK':
return { ...state, network: action.payload.network };
case 'UPDATE_ADDRESS':
return { ...state, address: action.payload.address };
default:
return state;
}
}
interface WalletContextType extends WalletState {
isConnecting: boolean;
connect: () => Promise<boolean>;
disconnect: () => void;
checkConnection: () => Promise<void>;
}
const WalletContext = createContext<WalletContextType | undefined>(undefined);
export const BALANCE_REFRESH_INTERVAL = 30_000;
export const ACCOUNT_POLL_INTERVAL = 2_000; // Check account explicitly every 2s
export const WALLET_DISCONNECTED_KEY = 'disciplr:wallet:userDisconnected';
export function WalletProvider({ children }: { children: ReactNode }) {
const [state, dispatch] = useReducer(walletReducer, initialState);
const abortControllerRef = useRef<AbortController | null>(null);
const lastKnownAddressRef = useRef<string | null>(null);
const lastKnownNetworkRef = useRef<WalletNetwork | null>(null);
const checkConnectionInProgress = useRef(false);
const normalizeNetwork = (networkName: string): WalletNetwork => {
return networkName === 'PUBLIC' ? 'PUBLIC' : 'TESTNET';
};
const fetchNetworkAndBalance = useCallback(async (pubKey: string) => {
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
abortControllerRef.current = new AbortController();
dispatch({ type: 'BALANCE_FETCH_START' });
try {
const netDetails = await getNetworkDetails();
if (seq !== operationSeqRef.current) return;
const activeNetwork = normalizeNetwork(netDetails.network);
lastKnownNetworkRef.current = activeNetwork;
const usdcBalance = await fetchUsdcBalance(pubKey, activeNetwork, fetch, {
signal: abortControllerRef.current.signal,
});
if (seq !== operationSeqRef.current) return;
dispatch({
type: 'BALANCE_FETCH_SUCCESS',
payload: {
balance: usdcBalance.balance,
status: usdcBalance.hasTrustline ? 'success' : 'no_trustline',
network: activeNetwork,
},
});
} catch (err) {
if (err instanceof Error && err.name === 'AbortError') return;
if (seq !== operationSeqRef.current) return;
logger.error('Failed to get network details', err);
const message = err instanceof Error ? err.message : 'Unable to load USDC balance.';
dispatch({ type: 'BALANCE_FETCH_ERROR', payload: { error: message } });
}
}, []);
const checkConnection = useCallback(async () => {
if (checkConnectionInProgress.current) return;
checkConnectionInProgress.current = true;
try {
if (localStorage.getItem(WALLET_DISCONNECTED_KEY) === 'true') {
return;
}
if ((await isAllowed()).isAllowed) {
const { address: pubKey, error: addrError } = await getAddress();
if (seq !== operationSeqRef.current) return;
if (pubKey && !addrError) {
setAddress(pubKey);
// Skip redundant fetch if address hasn't changed
if (pubKey !== lastKnownAddressRef.current) {
lastKnownAddressRef.current = pubKey;
await fetchNetworkAndBalance(pubKey);
}
}
} else {
dispatch({ type: 'RESTORE_ABORT' });
}
} catch (err) {
if (seq !== operationSeqRef.current) return;
logger.error('Check connection error', err);
} finally {
checkConnectionInProgress.current = false;
}
}, [fetchNetworkAndBalance]);
useEffect(() => {
checkConnection();
return () => {
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
operationSeqRef.current++;
};
}, [checkConnection]);
// Fast polling for account changes & standard polling for balance
useEffect(() => {
if (!state.address) return;
let lastBalanceCheck = Date.now();
const tick = async () => {
if (document.hidden) return;
const seq = ++operationSeqRef.current;
try {
const { address: currentAddr, error: addrError } = await getAddress();
if (seq !== operationSeqRef.current) return;
if (currentAddr && !addrError && currentAddr !== lastKnownAddressRef.current) {
// Account changed! Update state and fetch immediately
setAddress(currentAddr);
lastKnownAddressRef.current = currentAddr;
lastBalanceCheck = Date.now();
await fetchNetworkAndBalance(currentAddr);
} else if (Date.now() - lastBalanceCheck >= BALANCE_REFRESH_INTERVAL) {
// Refresh balance on the existing account
lastBalanceCheck = Date.now();
await fetchNetworkAndBalance(currentAddr || address);
}
} catch {
// If it fails, fallback
if (Date.now() - lastBalanceCheck >= BALANCE_REFRESH_INTERVAL) {
lastBalanceCheck = Date.now();
await fetchNetworkAndBalance(address);
}
}
};
const id = setInterval(tick, ACCOUNT_POLL_INTERVAL);
const onVisibilityChange = () => {
if (!document.hidden && address) {
lastBalanceCheck = Date.now();
fetchNetworkAndBalance(address);
}
};
document.addEventListener('visibilitychange', onVisibilityChange);
return () => {
clearInterval(id);
document.removeEventListener('visibilitychange', onVisibilityChange);
};
}, [address, fetchNetworkAndBalance]);
const connect = async (): Promise<boolean> => {
const seq = ++operationSeqRef.current;
dispatch({ type: 'CONNECT_START' });
try {
await setAllowed();
if (seq !== operationSeqRef.current) return false;
const access = await requestAccess();
if (seq !== operationSeqRef.current) return false;
if (access) {
const { address: pubKey, error: addrError } = await getAddress();
if (seq !== operationSeqRef.current) return false;
if (pubKey && !addrError) {
localStorage.removeItem(WALLET_DISCONNECTED_KEY);
lastKnownAddressRef.current = pubKey;
const netDetails = await getNetworkDetails();
if (seq !== operationSeqRef.current) return false;
const activeNetwork = normalizeNetwork(netDetails.network);
lastKnownNetworkRef.current = activeNetwork;
dispatch({ type: 'CONNECT_SUCCESS', payload: { address: pubKey, network: activeNetwork } });
await fetchNetworkAndBalance(pubKey, seq);
return true;
} else {
dispatch({ type: 'CONNECT_ERROR', payload: { error: addrError || 'Failed to get wallet address.' } });
}
} else {
dispatch({ type: 'CONNECT_ERROR', payload: { error: 'Wallet access denied.' } });
}
setError(result.message);
setIsConnecting(false);
recordWalletTelemetry({
event: 'wallet.connect.failure',
ts: Date.now(),
wallet: 'freighter',
durationMs: Date.now() - startedAt,
attempt,
errorCode: result.code,
});
return false;
} catch (err: unknown) {
if (seq !== operationSeqRef.current) return false;
logger.error('Connection error', err);
const message = err instanceof Error ? err.message : undefined;
dispatch({ type: 'CONNECT_ERROR', payload: { error: message || 'Failed to connect wallet. Make sure Freighter is installed and unlocked.' } });
}
};
const connect = (): Promise<boolean> => {
// Bounded concurrency: a second connect() call while one is already in
// flight returns the in-flight promise instead of prompting Freighter
// again, so rapid user interaction never stacks authorization prompts.
if (connectInFlightRef.current) {
recordWalletTelemetry({
event: 'wallet.connect.ignored',
ts: Date.now(),
wallet: 'freighter',
reason: 'already_in_flight',
});
return connectInFlightRef.current;
}
connectAttemptRef.current += 1;
const promise = performConnect(connectAttemptRef.current);
connectInFlightRef.current = promise;
void promise.then(
() => {
connectInFlightRef.current = null;
},
() => {
connectInFlightRef.current = null;
},
);
return promise;
};
const disconnect = useCallback(() => {
operationSeqRef.current++;
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
localStorage.setItem(WALLET_DISCONNECTED_KEY, 'true');
lastKnownAddressRef.current = null;
lastKnownNetworkRef.current = null;
dispatch({ type: 'DISCONNECT' });
}, []);
const isConnecting = state.status === 'connecting' || state.status === 'restoring';
return (
<WalletContext.Provider
value={{
...state,
isConnecting,
connect,
disconnect,
checkConnection,
}}
>
{children}
</WalletContext.Provider>
);
}
export function useWallet() {
const context = useContext(WalletContext);
if (context === undefined) {
throw new Error('useWallet must be used within a WalletProvider');
}
return context;
}