2021-04-14 15:46:36 -07:00
|
|
|
import { useEffect, useMemo } from 'react'
|
2021-03-30 15:47:08 -07:00
|
|
|
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'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-01-26 10:37:02 -08:00
|
|
|
import { DEFAULT_PROVIDER, WALLET_PROVIDERS } from '../utils/wallet-adapters'
|
2021-04-13 22:23:50 -07:00
|
|
|
|
2021-08-04 14:47:05 -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() {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-04-05 07:32:11 -07:00
|
|
|
const setMangoStore = useMangoStore((state) => state.set)
|
2021-04-14 15:46:36 -07:00
|
|
|
const {
|
|
|
|
current: wallet,
|
|
|
|
connected,
|
|
|
|
providerUrl: selectedProviderUrl,
|
|
|
|
} = useMangoStore((state) => state.wallet)
|
2021-04-05 07:32:11 -07:00
|
|
|
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(
|
2021-08-04 14:47:05 -07:00
|
|
|
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-03-30 15:47:08 -07:00
|
|
|
)
|
|
|
|
|
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-03-30 15:47:08 -07:00
|
|
|
|
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])
|
2021-03-30 15:47:08 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-04-02 11:26:21 -07:00
|
|
|
if (!wallet) return
|
2021-04-20 07:09:25 -07:00
|
|
|
wallet.on('connect', async () => {
|
2021-05-04 17:00:37 -07:00
|
|
|
setMangoStore((state) => {
|
|
|
|
state.wallet.connected = true
|
|
|
|
})
|
|
|
|
// set connected before fetching data
|
2021-09-29 15:09:35 -07:00
|
|
|
await actions.fetchAllMangoAccounts()
|
2022-01-18 10:26:13 -08:00
|
|
|
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
|
|
|
|
2021-11-15 10:27:57 -08:00
|
|
|
// notify({
|
|
|
|
// title: t('wallet-connected'),
|
|
|
|
// description:
|
|
|
|
// t('connected-to') +
|
|
|
|
// wallet.publicKey.toString().substr(0, 5) +
|
|
|
|
// '...' +
|
|
|
|
// wallet.publicKey.toString().substr(-5),
|
|
|
|
// })
|
2021-03-30 15:47:08 -07:00
|
|
|
})
|
|
|
|
wallet.on('disconnect', () => {
|
2021-04-29 10:42:53 -07:00
|
|
|
console.log('disconnecting wallet')
|
2021-04-05 07:32:11 -07:00
|
|
|
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',
|
2021-10-20 05:42:40 -07:00
|
|
|
title: t('wallet-disconnected'),
|
2021-04-11 21:17:23 -07:00
|
|
|
})
|
2021-03-30 15:47:08 -07:00
|
|
|
})
|
2021-04-13 22:23:50 -07:00
|
|
|
}, [wallet, setMangoStore])
|
2021-03-30 15:47:08 -07:00
|
|
|
|
2021-04-13 22:23:50 -07:00
|
|
|
return { connected, wallet }
|
2021-03-30 15:47:08 -07:00
|
|
|
}
|