mango-ui-v3/hooks/useWallet.tsx

104 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-04-14 15:46:36 -07:00
import { useEffect, useMemo } from 'react'
import Wallet from '@project-serum/sol-wallet-adapter'
import useLocalStorageState from './useLocalStorageState'
2021-04-14 15:46:36 -07:00
import useMangoStore from '../stores/useMangoStore'
2021-04-11 21:17:23 -07:00
import { notify } from '../utils/notifications'
2021-04-14 15:46:36 -07:00
import { WalletAdapter } from '../@types/types'
import { useTranslation } from 'next-i18next'
import { DEFAULT_PROVIDER, WALLET_PROVIDERS } from '../utils/wallet-adapters'
2021-04-13 22:23:50 -07:00
export const PROVIDER_LOCAL_STORAGE_KEY = 'walletProvider-0.1'
2021-04-14 15:46:36 -07:00
2021-04-02 11:26:21 -07:00
export default function useWallet() {
const { t } = useTranslation('common')
const setMangoStore = useMangoStore((state) => state.set)
2021-04-14 15:46:36 -07:00
const {
current: wallet,
connected,
providerUrl: selectedProviderUrl,
} = useMangoStore((state) => state.wallet)
const endpoint = useMangoStore((state) => state.connection.endpoint)
2021-04-14 15:46:36 -07:00
const actions = useMangoStore((s) => s.actions)
2021-04-13 22:23:50 -07:00
const [savedProviderUrl, setSavedProviderUrl] = useLocalStorageState(
PROVIDER_LOCAL_STORAGE_KEY,
2021-04-14 15:46:36 -07:00
DEFAULT_PROVIDER.url
)
const provider = useMemo(
() => WALLET_PROVIDERS.find(({ url }) => url === savedProviderUrl),
[savedProviderUrl]
)
2021-04-02 11:26:21 -07:00
useEffect(() => {
2021-04-14 15:46:36 -07:00
if (selectedProviderUrl) {
setSavedProviderUrl(selectedProviderUrl)
2021-04-13 22:23:50 -07:00
}
2021-04-14 15:46:36 -07:00
}, [selectedProviderUrl])
2021-04-13 22:23:50 -07:00
useEffect(() => {
2021-04-14 15:46:36 -07:00
if (provider) {
const updateWallet = () => {
// hack to also update wallet synchronously in case it disconnects
// eslint-disable-next-line react-hooks/exhaustive-deps
const wallet = new (provider.adapter || Wallet)(
savedProviderUrl,
endpoint
) as WalletAdapter
setMangoStore((state) => {
state.wallet.current = wallet
})
}
2021-04-14 15:46:36 -07:00
if (document.readyState !== 'complete') {
// wait to ensure that browser extensions are loaded
const listener = () => {
updateWallet()
window.removeEventListener('load', listener)
}
window.addEventListener('load', listener)
return () => window.removeEventListener('load', listener)
} else {
updateWallet()
}
}
}, [provider, savedProviderUrl, endpoint])
useEffect(() => {
2021-04-02 11:26:21 -07:00
if (!wallet) return
2021-04-20 07:09:25 -07:00
wallet.on('connect', async () => {
setMangoStore((state) => {
state.wallet.connected = true
})
// set connected before fetching data
await actions.fetchAllMangoAccounts()
actions.fetchProfilePicture()
2021-09-23 13:52:16 -07:00
actions.reloadOrders()
2021-08-03 10:59:16 -07:00
actions.fetchTradeHistory()
2021-06-17 11:03:47 -07:00
actions.fetchWalletTokens()
2021-12-03 08:03:51 -08:00
// notify({
// title: t('wallet-connected'),
// description:
// t('connected-to') +
// wallet.publicKey.toString().substr(0, 5) +
// '...' +
// wallet.publicKey.toString().substr(-5),
// })
})
wallet.on('disconnect', () => {
2021-04-29 10:42:53 -07:00
console.log('disconnecting wallet')
setMangoStore((state) => {
2021-04-14 15:46:36 -07:00
state.wallet.connected = false
2021-06-23 08:32:33 -07:00
state.mangoAccounts = []
state.selectedMangoAccount.current = null
2022-02-10 09:49:59 -08:00
state.tradeHistory = { spot: [], perp: [] }
2021-04-02 11:26:21 -07:00
})
2021-04-11 21:17:23 -07:00
notify({
type: 'info',
title: t('wallet-disconnected'),
2021-04-11 21:17:23 -07:00
})
})
2021-04-13 22:23:50 -07:00
}, [wallet, setMangoStore])
2021-04-13 22:23:50 -07:00
return { connected, wallet }
}