-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathreact.ts
58 lines (50 loc) · 1.84 KB
/
react.ts
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
import { useSnapshot } from 'valtio'
import { AccountController } from '../src/controllers/AccountController.js'
import { CoreHelperUtil } from '../src/utils/CoreHelperUtil.js'
import { ChainController } from '../src/controllers/ChainController.js'
import { ConnectionController } from '../src/controllers/ConnectionController.js'
import type {
AccountType,
UseAppKitAccountReturn,
UseAppKitNetworkReturn
} from '../src/utils/TypeUtil.js'
import { ConnectorController } from '../src/controllers/ConnectorController.js'
// -- Hooks ------------------------------------------------------------
export function useAppKitNetworkCore(): Pick<
UseAppKitNetworkReturn,
'caipNetwork' | 'chainId' | 'caipNetworkId'
> {
const { activeCaipNetwork } = useSnapshot(ChainController.state)
return {
caipNetwork: activeCaipNetwork,
chainId: activeCaipNetwork?.id,
caipNetworkId: activeCaipNetwork?.caipNetworkId
}
}
export function useAppKitAccount(): UseAppKitAccountReturn {
const { status, user, preferredAccountType, smartAccountDeployed, allAccounts, socialProvider } =
useSnapshot(AccountController.state)
const { activeCaipAddress } = useSnapshot(ChainController.state)
const authConnector = ConnectorController.getAuthConnector()
return {
allAccounts: allAccounts as AccountType[],
caipAddress: activeCaipAddress,
address: CoreHelperUtil.getPlainAddress(activeCaipAddress),
isConnected: Boolean(activeCaipAddress),
status,
embeddedWalletInfo: authConnector
? {
user,
authProvider: socialProvider || 'email',
accountType: preferredAccountType,
isSmartAccountDeployed: Boolean(smartAccountDeployed)
}
: undefined
}
}
export function useDisconnect() {
async function disconnect() {
await ConnectionController.disconnect()
}
return { disconnect }
}