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-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-04-20 07:09:25 -07:00
|
|
|
import useInterval from './useInterval'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
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'
|
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: SlopeWalletAdapter,
|
|
|
|
},
|
2021-03-30 15:47:08 -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
|
|
|
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-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(
|
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-04-11 21:17:23 -07:00
|
|
|
notify({
|
2021-10-20 05:42:40 -07:00
|
|
|
title: t('wallet-connected'),
|
2021-04-11 21:17:23 -07:00
|
|
|
description:
|
2021-10-20 05:42:40 -07:00
|
|
|
t('connected-to') +
|
2021-04-11 21:17:23 -07:00
|
|
|
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
|
|
|
})
|
|
|
|
return () => {
|
2021-04-13 22:23:50 -07:00
|
|
|
if (wallet && wallet.connected) {
|
|
|
|
console.log('DISCONNECTING')
|
|
|
|
|
|
|
|
wallet.disconnect()
|
|
|
|
}
|
2021-04-05 07:32:11 -07:00
|
|
|
setMangoStore((state) => {
|
2021-04-14 15:46:36 -07:00
|
|
|
state.wallet.connected = false
|
2021-04-02 11:26:21 -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-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-09-02 23:39:37 -07:00
|
|
|
}
|
|
|
|
}, 90 * SECONDS)
|
|
|
|
|
2021-08-16 15:10:06 -07:00
|
|
|
useInterval(() => {
|
|
|
|
if (connected && mangoAccount) {
|
2021-08-31 10:47:03 -07:00
|
|
|
actions.reloadMangoAccount()
|
2021-04-20 07:09:25 -07:00
|
|
|
}
|
2021-09-08 10:52:52 -07:00
|
|
|
}, 20 * SECONDS)
|
2021-04-20 07:09:25 -07:00
|
|
|
|
2021-04-13 22:23:50 -07:00
|
|
|
return { connected, wallet }
|
2021-03-30 15:47:08 -07:00
|
|
|
}
|