init
This commit is contained in:
commit
44f31f8b9f
402 changed files with 47865 additions and 0 deletions
8
middleware/README.md
Normal file
8
middleware/README.md
Normal file
|
@ -0,0 +1,8 @@
|
|||
# MIDDLEWARE
|
||||
|
||||
**This directory is not required, you can delete it if you don't want to use it.**
|
||||
|
||||
This directory contains your application middleware.
|
||||
Middleware let you define custom functions that can be run before rendering either a page or a group of pages.
|
||||
|
||||
More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/routing#middleware).
|
48
middleware/provider.js
Normal file
48
middleware/provider.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
import networkConfig from '@/networkConfig'
|
||||
const { hexToNumber } = require('web3-utils')
|
||||
|
||||
const checkProvider = async ({ store, accounts, chainId, providerName }) => {
|
||||
if (accounts.length === 0) {
|
||||
// MetaMask is locked or the user has not connected any accounts
|
||||
return
|
||||
}
|
||||
|
||||
if (!networkConfig[`netId${chainId}`]) {
|
||||
await store.dispatch('metamask/checkMismatchNetwork', chainId)
|
||||
return
|
||||
}
|
||||
|
||||
await store.dispatch('metamask/initialize', { providerName })
|
||||
}
|
||||
|
||||
const providerMiddleware = async ({ store }) => {
|
||||
try {
|
||||
const providerName = window.localStorage.getItem('provider')
|
||||
|
||||
if (providerName === 'walletConnect') {
|
||||
let providerData = window.localStorage.getItem('walletconnect')
|
||||
if (!providerData) {
|
||||
return
|
||||
}
|
||||
|
||||
const { accounts, chainId } = ({ providerData } = JSON.parse(providerData))
|
||||
|
||||
await checkProvider({ store, accounts, chainId, providerName })
|
||||
return
|
||||
}
|
||||
|
||||
if (providerName) {
|
||||
const provider = await store.getters['metamask/getEthereumProvider']()
|
||||
|
||||
const accounts = await provider.request({ method: 'eth_accounts' })
|
||||
|
||||
const chainId = hexToNumber(await provider.request({ method: 'eth_chainId' }))
|
||||
|
||||
await checkProvider({ store, accounts, chainId, providerName })
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Provider container has error: ${err.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
export default providerMiddleware
|
Loading…
Add table
Add a link
Reference in a new issue