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-13 22:23:50 -07:00
|
|
|
import {
|
|
|
|
PhantomWalletAdapter,
|
2021-09-21 03:08:13 -07:00
|
|
|
SlopeWalletAdapter,
|
2021-04-13 22:23:50 -07:00
|
|
|
SolletExtensionAdapter,
|
2021-11-10 03:46:54 -08:00
|
|
|
SolflareExtensionWalletAdapter,
|
2021-11-23 04:25:31 -08:00
|
|
|
BitpieWalletAdapter,
|
2021-04-13 22:23:50 -07:00
|
|
|
} from '../utils/wallet-adapters'
|
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'
|
2021-04-13 22:23:50 -07:00
|
|
|
|
|
|
|
const ASSET_URL =
|
|
|
|
'https://cdn.jsdelivr.net/gh/solana-labs/oyster@main/assets/wallets'
|
2021-03-30 15:47:08 -07:00
|
|
|
|
|
|
|
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-11-10 03:46:54 -08:00
|
|
|
{
|
|
|
|
name: 'Solflare',
|
|
|
|
url: 'https://solflare.com',
|
|
|
|
icon: `${ASSET_URL}/solflare.svg`,
|
|
|
|
adapter: SolflareExtensionWalletAdapter,
|
|
|
|
},
|
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,
|
|
|
|
},
|
2021-09-21 03:08:13 -07:00
|
|
|
{
|
|
|
|
name: 'Slope',
|
|
|
|
url: 'https://www.slope.finance/#/wallet',
|
|
|
|
icon: 'data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIHdpZHRoPSIxMjgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGNpcmNsZSBjeD0iNjQiIGN5PSI2NCIgZmlsbD0iIzZlNjZmYSIgcj0iNjQiLz48ZyBmaWxsPSIjZmZmIj48cGF0aCBkPSJtMzUuMTk2MyA1NC4zOTk4aDE5LjJ2MTkuMmgtMTkuMnoiLz48cGF0aCBkPSJtNzMuNTk3IDU0LjM5OTgtMTkuMiAxOS4ydi0xOS4ybDE5LjItMTkuMnoiIGZpbGwtb3BhY2l0eT0iLjQiLz48cGF0aCBkPSJtNzMuNTk3IDczLjU5OTgtMTkuMiAxOS4ydi0xOS4ybDE5LjItMTkuMnoiIGZpbGwtb3BhY2l0eT0iLjc1Ii8+PHBhdGggZD0ibTczLjYwNCA1NC4zOTk4aDE5LjJ2MTkuMmgtMTkuMnoiLz48cGF0aCBkPSJtNTQuMzk2OCAzNS4yIDE5LjItMTkuMnYxOS4ybC0xOS4yIDE5LjJoLTE5LjJ6IiBmaWxsLW9wYWNpdHk9Ii43NSIvPjxwYXRoIGQ9Im03My41OTE1IDkyLjgtMTkuMiAxOS4ydi0xOS4ybDE5LjItMTkuMmgxOS4yeiIgZmlsbC1vcGFjaXR5PSIuNCIvPjwvZz48L3N2Zz4=',
|
|
|
|
adapter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|
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
|
|
|
export const DEFAULT_PROVIDER = WALLET_PROVIDERS[0]
|
|
|
|
|
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()
|
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
|
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',
|
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
|
|
|
}
|