-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathuseProvider.ts
More file actions
61 lines (55 loc) · 1.96 KB
/
useProvider.ts
File metadata and controls
61 lines (55 loc) · 1.96 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
/* eslint-disable valtio/state-snapshot-rule */
import { useMemo } from 'react';
import { useSnapshot } from 'valtio';
import { ConnectionsController, LogController } from '@reown/appkit-core-react-native';
import type { Provider, ChainNamespace } from '@reown/appkit-common-react-native';
/**
* Interface representing the result of the useProvider hook
*/
interface ProviderResult {
/** The active connection provider instance */
provider?: Provider;
/** The chain namespace supported by the provider */
providerType?: ChainNamespace;
}
/**
* Hook that returns the active connection provider and its supported chain namespace.
*
* This hook accesses the current connection and returns the provider instance along with its supported namespace.
*
* @returns {ProviderResult} An object containing:
* - `provider`: The active connection provider instance, or undefined if no connection exists
* - `providerType`: The chain namespace supported by the provider, or undefined if no connection exists
*
* @example
* ```typescript
* const { provider, providerType } = useProvider();
*
* if (provider) {
* // Use the provider for blockchain operations
* const balance = await provider.request({
* method: 'eth_getBalance',
* params: [address, 'latest']
* });
* }
* ```
*/
export function useProvider(): ProviderResult {
const { connection } = useSnapshot(ConnectionsController.state);
const returnValue = useMemo(() => {
if (!connection || !connection.adapter) {
return { provider: undefined, providerType: undefined };
}
try {
return {
provider: connection.adapter.getProvider(),
providerType: connection.adapter.getSupportedNamespace()
};
} catch (error) {
LogController.sendError(error, 'useProvider', 'useProvider');
// Provider not initialized yet during session restoration
return { provider: undefined, providerType: undefined };
}
}, [connection]);
return returnValue;
}