mango-ui-v3/hooks/useWallet.tsx

155 lines
4.3 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-13 22:23:50 -07:00
import {
PhantomWalletAdapter,
SolletExtensionAdapter,
} from '../utils/wallet-adapters'
2021-04-14 15:46:36 -07:00
import { WalletAdapter } from '../@types/types'
2021-04-20 07:09:25 -07:00
import useInterval from './useInterval'
2021-04-13 22:23:50 -07:00
2021-04-21 08:21:00 -07:00
const SECONDS = 1000
2021-04-13 22:23:50 -07:00
const ASSET_URL =
'https://cdn.jsdelivr.net/gh/solana-labs/oyster@main/assets/wallets'
export const WALLET_PROVIDERS = [
2021-08-04 11:51:17 -07:00
{
name: 'Phantom',
url: 'https://www.phantom.app',
icon: `https://www.phantom.app/img/logo.png`,
adapter: PhantomWalletAdapter,
},
2021-04-13 22:23:50 -07:00
{
name: 'Sollet.io',
url: 'https://www.sollet.io',
icon: `${ASSET_URL}/sollet.svg`,
},
{
name: 'Sollet Extension',
url: 'https://www.sollet.io/extension',
icon: `${ASSET_URL}/sollet.svg`,
adapter: SolletExtensionAdapter as any,
},
]
export const PROVIDER_LOCAL_STORAGE_KEY = 'walletProvider-0.1'
2021-04-14 15:46:36 -07:00
export const DEFAULT_PROVIDER = WALLET_PROVIDERS[0]
2021-04-02 11:26:21 -07:00
export default function useWallet() {
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-06-23 08:32:33 -07:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
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
notify({
title: 'Connecting wallet...',
type: 'info',
})
2021-08-20 02:53:44 -07:00
await actions.fetchMangoAccounts()
2021-08-03 10:59:16 -07:00
actions.fetchTradeHistory()
2021-06-17 11:03:47 -07:00
actions.fetchWalletTokens()
2021-04-11 21:17:23 -07:00
notify({
title: 'Wallet connected',
2021-04-11 21:17:23 -07:00
description:
'Connected to wallet ' +
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
2021-04-14 15:46:36 -07:00
state.tradeHistory = []
2021-04-02 11:26:21 -07:00
})
2021-04-11 21:17:23 -07:00
notify({
type: 'info',
title: 'Disconnected from wallet',
2021-04-11 21:17:23 -07:00
})
})
return () => {
2021-04-13 22:23:50 -07:00
if (wallet && wallet.connected) {
console.log('DISCONNECTING')
wallet.disconnect()
}
setMangoStore((state) => {
2021-04-14 15:46:36 -07:00
state.wallet.connected = false
2021-04-02 11:26:21 -07:00
})
}
2021-04-13 22:23:50 -07:00
}, [wallet, setMangoStore])
2021-04-20 07:09:25 -07:00
useInterval(() => {
2021-06-23 08:32:33 -07:00
if (connected && mangoAccount) {
2021-06-17 11:03:47 -07:00
actions.fetchWalletTokens()
2021-08-16 17:05:57 -07:00
actions.fetchTradeHistory()
2021-08-16 15:10:06 -07:00
}
2021-08-23 08:21:06 -07:00
}, 60 * SECONDS)
2021-08-16 15:10:06 -07:00
useInterval(() => {
if (connected && mangoAccount) {
actions.fetchMangoAccounts()
2021-04-20 07:09:25 -07:00
}
2021-08-16 15:10:06 -07:00
}, 30 * SECONDS)
2021-04-20 07:09:25 -07:00
2021-04-13 22:23:50 -07:00
return { connected, wallet }
}