|
1 |
| -export { |
2 |
| - useAccountCenter, |
3 |
| - useAppState, |
4 |
| - useConnectWallet, |
5 |
| - useNotifications, |
6 |
| - useSetChain, |
7 |
| - useSetLocale, |
8 |
| - useWallets |
9 |
| -} from './hooks' |
10 |
| -export { init, Web3OnboardProvider, Web3OnboardProviderProps } from './context' |
| 1 | +import { useState, useCallback } from 'react' |
| 2 | +import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js' |
11 | 3 |
|
| 4 | +import Web3Onboard from '@web3-onboard/core' |
| 5 | +import type { |
| 6 | + InitOptions, |
| 7 | + OnboardAPI, |
| 8 | + ConnectOptions, |
| 9 | + DisconnectOptions, |
| 10 | + WalletState, |
| 11 | + ConnectedChain, |
| 12 | + AccountCenter, |
| 13 | + AppState, |
| 14 | + CustomNotification, |
| 15 | + Notification, |
| 16 | + Notify, |
| 17 | + UpdateNotification, |
| 18 | + PreflightNotificationsOptions |
| 19 | +} from '@web3-onboard/core' |
| 20 | +import type { Chain, WalletInit } from '@web3-onboard/common' |
| 21 | + |
| 22 | +export let web3Onboard: OnboardAPI | null = null |
| 23 | + |
| 24 | +export const init = (options: InitOptions): OnboardAPI => { |
| 25 | + web3Onboard = Web3Onboard(options) |
| 26 | + return web3Onboard |
| 27 | +} |
| 28 | + |
| 29 | +const HOOK_ERROR_MESSAGE = 'Must initialize before using hooks.' |
| 30 | + |
| 31 | +const useAppState: { |
| 32 | + (): AppState |
| 33 | + <K extends keyof AppState>(stateKey?: K): AppState[K] |
| 34 | +} = (stateKey = undefined) => { |
| 35 | + if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE) |
| 36 | + |
| 37 | + const { select, get } = web3Onboard.state |
| 38 | + |
| 39 | + const subscribe = useCallback( |
| 40 | + (onStoreChange: () => void) => { |
| 41 | + const { unsubscribe } = stateKey |
| 42 | + ? select(stateKey).subscribe(onStoreChange) |
| 43 | + : select().subscribe(onStoreChange) |
| 44 | + |
| 45 | + return () => unsubscribe |
| 46 | + }, |
| 47 | + [stateKey] |
| 48 | + ) |
| 49 | + |
| 50 | + const getSnapshot = useCallback(() => { |
| 51 | + const snapshot = get() |
| 52 | + return stateKey ? snapshot[stateKey] : snapshot |
| 53 | + }, [stateKey]) |
| 54 | + |
| 55 | + const getServerSnapshot = () => get() || getSnapshot |
| 56 | + return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) |
| 57 | +} |
| 58 | + |
| 59 | +export const useConnectWallet = (): [ |
| 60 | + { wallet: WalletState | null; connecting: boolean }, |
| 61 | + (options?: ConnectOptions) => Promise<void>, |
| 62 | + (wallet: DisconnectOptions) => Promise<void>, |
| 63 | + (addresses?: string[]) => Promise<void>, |
| 64 | + (wallets: WalletInit[]) => void, |
| 65 | + (wallet: WalletState, address?: string) => void |
| 66 | +] => { |
| 67 | + if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE) |
| 68 | + |
| 69 | + const { connectWallet, disconnectWallet } = web3Onboard |
| 70 | + |
| 71 | + const wallets = useAppState('wallets') |
| 72 | + const wallet = wallets[0] || null |
| 73 | + |
| 74 | + const [connecting, setConnecting] = useState<boolean>(false) |
| 75 | + |
| 76 | + const connect = useCallback(async (options?: ConnectOptions) => { |
| 77 | + setConnecting(true) |
| 78 | + |
| 79 | + await connectWallet(options) |
| 80 | + |
| 81 | + setConnecting(false) |
| 82 | + }, []) |
| 83 | + |
| 84 | + const disconnect = useCallback(async ({ label }: DisconnectOptions) => { |
| 85 | + setConnecting(true) |
| 86 | + |
| 87 | + await disconnectWallet({ label }) |
| 88 | + |
| 89 | + setConnecting(false) |
| 90 | + }, []) |
| 91 | + |
| 92 | + const updateBalances = web3Onboard.state.actions.updateBalances |
| 93 | + const setWalletModules = web3Onboard.state.actions.setWalletModules |
| 94 | + const setPrimaryWallet = web3Onboard.state.actions.setPrimaryWallet |
| 95 | + |
| 96 | + return [ |
| 97 | + { wallet, connecting }, |
| 98 | + connect, |
| 99 | + disconnect, |
| 100 | + updateBalances, |
| 101 | + setWalletModules, |
| 102 | + setPrimaryWallet |
| 103 | + ] |
| 104 | +} |
| 105 | + |
| 106 | +type SetChainOptions = { |
| 107 | + chainId: string |
| 108 | + chainNamespace?: string |
| 109 | +} |
| 110 | + |
| 111 | +export const useSetChain = ( |
| 112 | + walletLabel?: string |
| 113 | +): [ |
| 114 | + { |
| 115 | + chains: Chain[] |
| 116 | + connectedChain: ConnectedChain | null |
| 117 | + settingChain: boolean |
| 118 | + }, |
| 119 | + (options: SetChainOptions) => Promise<boolean> |
| 120 | +] => { |
| 121 | + if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE) |
| 122 | + |
| 123 | + const { setChain } = web3Onboard |
| 124 | + |
| 125 | + const { wallets, chains } = useAppState() |
| 126 | + |
| 127 | + const getChain = () => { |
| 128 | + const wallet = walletLabel |
| 129 | + ? wallets.find(({ label }) => label === walletLabel) |
| 130 | + : wallets[0] |
| 131 | + return wallet && wallet.chains ? wallet.chains[0] : null |
| 132 | + } |
| 133 | + |
| 134 | + const connectedChain = getChain() |
| 135 | + |
| 136 | + const [settingChain, setInProgress] = useState<boolean>(false) |
| 137 | + |
| 138 | + const set = useCallback(async (options: SetChainOptions) => { |
| 139 | + setInProgress(true) |
| 140 | + |
| 141 | + const success = await setChain({ ...options, wallet: walletLabel }) |
| 142 | + |
| 143 | + setInProgress(false) |
| 144 | + |
| 145 | + return success |
| 146 | + }, []) |
| 147 | + |
| 148 | + return [{ chains, connectedChain, settingChain }, set] |
| 149 | +} |
| 150 | + |
| 151 | +export const useWallets = (): WalletState[] => { |
| 152 | + if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE) |
| 153 | + |
| 154 | + return useAppState('wallets') |
| 155 | +} |
| 156 | + |
| 157 | +export const useNotifications = (): [ |
| 158 | + Notification[], |
| 159 | + (updatedNotification: CustomNotification) => { |
| 160 | + dismiss: () => void |
| 161 | + update: UpdateNotification |
| 162 | + }, |
| 163 | + (update: Partial<Notify>) => void, |
| 164 | + (options: PreflightNotificationsOptions) => Promise<void | string> |
| 165 | +] => { |
| 166 | + if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE) |
| 167 | + |
| 168 | + const customNotification = web3Onboard.state.actions.customNotification |
| 169 | + const updateNotify = web3Onboard.state.actions.updateNotify |
| 170 | + const preflightNotifications = |
| 171 | + web3Onboard.state.actions.preflightNotifications |
| 172 | + |
| 173 | + return [ |
| 174 | + useAppState('notifications'), |
| 175 | + customNotification, |
| 176 | + updateNotify, |
| 177 | + preflightNotifications |
| 178 | + ] |
| 179 | +} |
| 180 | + |
| 181 | +export const useSetLocale = (): ((locale: string) => void) => { |
| 182 | + if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE) |
| 183 | + |
| 184 | + return web3Onboard.state.actions.setLocale |
| 185 | +} |
| 186 | + |
| 187 | +export const useAccountCenter = (): (( |
| 188 | + update: AccountCenter | Partial<AccountCenter> |
| 189 | +) => void) => { |
| 190 | + if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE) |
| 191 | + |
| 192 | + return web3Onboard.state.actions.updateAccountCenter |
| 193 | +} |
0 commit comments