2022-04-21 23:05:56 -04:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
import Web3 from 'web3'
|
|
|
|
|
|
|
|
import networkConfig from '@/networkConfig'
|
|
|
|
|
|
|
|
const getFirstRpcs = (acc, [netId, { rpcUrls }]) => {
|
|
|
|
const [rpc] = Object.values(rpcUrls)
|
|
|
|
|
|
|
|
acc[netId] = {
|
|
|
|
rpc
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
const rpcData = Object.entries(networkConfig).reduce(getFirstRpcs, {})
|
|
|
|
|
|
|
|
export const state = () => {
|
|
|
|
return {
|
|
|
|
...rpcData,
|
|
|
|
isActiveNotification: {
|
|
|
|
first: true,
|
2022-06-06 10:26:00 -04:00
|
|
|
second: true,
|
|
|
|
third: true
|
2022-04-21 23:05:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getRpc: (state) => (netId) => {
|
|
|
|
return state[`netId${netId}`].rpc
|
|
|
|
},
|
|
|
|
currentRpc: (state, getters, rootState) => {
|
|
|
|
const netId = rootState.metamask.netId
|
|
|
|
return state[`netId${netId}`].rpc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
SAVE_RPC(state, { netId, name, url }) {
|
|
|
|
this._vm.$set(state[`netId${netId}`], 'rpc', { name, url })
|
|
|
|
},
|
|
|
|
DISABLE_NOTIFICATION(state, { key }) {
|
|
|
|
this._vm.$set(state, 'isActiveNotification', { ...state.isActiveNotification, [key]: false })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
disableNotification({ commit }, params) {
|
|
|
|
commit('DISABLE_NOTIFICATION', params)
|
|
|
|
},
|
|
|
|
async checkCurrentRpc({ dispatch, getters, rootGetters }) {
|
|
|
|
const netId = rootGetters['metamask/netId']
|
2022-12-03 11:02:03 -05:00
|
|
|
if (netId !== 1) {
|
|
|
|
await dispatch('preselectRpc', { netId: 1, isEthRpc: true })
|
|
|
|
}
|
2022-04-21 23:05:56 -04:00
|
|
|
await dispatch('preselectRpc', { netId })
|
|
|
|
},
|
2022-12-03 11:02:03 -05:00
|
|
|
async preselectRpc({ getters, commit, dispatch }, { netId, isEthRpc = false }) {
|
2022-04-21 23:05:56 -04:00
|
|
|
const savedRpc = getters.getRpc(netId)
|
2022-12-03 11:02:03 -05:00
|
|
|
const { isValid } = await dispatch('checkRpc', { ...savedRpc, netId, isEthRpc })
|
2022-04-21 23:05:56 -04:00
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const { rpcUrls } = networkConfig[`netId${netId}`]
|
|
|
|
|
|
|
|
for (const [, { name, url }] of Object.entries(rpcUrls)) {
|
2022-12-03 11:02:03 -05:00
|
|
|
const { isValid, error } = await dispatch('checkRpc', { url, netId, isEthRpc })
|
2022-04-21 23:05:56 -04:00
|
|
|
if (isValid) {
|
|
|
|
commit('SAVE_RPC', { netId, name, url })
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
console.error('preselectRpc', url, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error(this.app.i18n.t('rpcSelectError'))
|
|
|
|
},
|
2022-12-03 11:02:03 -05:00
|
|
|
async checkRpc(_, { url, netId, isEthRpc = false }) {
|
2022-04-21 23:05:56 -04:00
|
|
|
try {
|
|
|
|
const web3 = new Web3(url)
|
|
|
|
const chainId = await web3.eth.getChainId()
|
|
|
|
const isCurrent = Number(chainId) === Number(netId)
|
2022-12-03 11:02:03 -05:00
|
|
|
|
|
|
|
if (isEthRpc || isCurrent) {
|
2022-04-21 23:05:56 -04:00
|
|
|
return { isValid: true }
|
|
|
|
} else {
|
|
|
|
return { isValid: false, error: this.app.i18n.t('thisRpcIsForDifferentNetwork') }
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error('checkRpc', e)
|
|
|
|
return { isValid: false, error: this.app.i18n.t('rpcIsDown') }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|