tornado-initiation-ui/store/provider/actions.js

180 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-10-21 16:42:50 +02:00
import networkConfig from '@/networkConfig'
2020-11-03 13:27:23 +01:00
import { localStorage } from '@/utillites'
2021-08-21 04:42:17 +02:00
import { numberToHex } from 'web3-utils'
2020-10-21 16:42:50 +02:00
import {
SET_ACCOUNT,
SET_NETWORK,
CLEAR_STATE,
SET_BALANCE,
2020-10-21 16:42:50 +02:00
SET_NETWORK_NAME,
SET_PROVIDER_NAME,
} from './constant'
export default {
async initProvider({ commit, state, getters, dispatch }, { name, network }) {
2020-10-21 16:42:50 +02:00
try {
2021-10-27 17:41:14 +02:00
const account = await this.$provider.initProvider(getters.getProvider, {})
2021-10-25 11:12:11 +02:00
const supportedNetworks = [numberToHex(1), numberToHex(100)]
if (!supportedNetworks.includes(window.ethereum.chainId)) {
2021-06-02 11:47:40 +02:00
await dispatch(
'notice/addNotice',
{
notice: {
2021-10-25 11:12:11 +02:00
title: 'switchToL1',
type: 'danger',
callback: () => dispatch('switchNetwork', { netId: 1 }),
message: 'switchNetworkToL1',
},
},
{ root: true }
)
await dispatch(
'notice/addNotice',
{
notice: {
title: 'switchToL2',
2021-06-02 11:47:40 +02:00
type: 'danger',
2021-08-21 04:42:17 +02:00
callback: () => dispatch('switchNetwork', { netId: 100 }),
2021-10-25 11:12:11 +02:00
message: 'switchNetworkToL2',
2021-06-02 11:47:40 +02:00
},
},
{ root: true }
)
2021-10-25 11:12:11 +02:00
throw new Error('Connect to L1 or L2')
2021-06-02 11:47:40 +02:00
}
2020-10-21 16:42:50 +02:00
commit(SET_PROVIDER_NAME, name)
commit(SET_NETWORK_NAME, network)
2020-11-03 13:27:23 +01:00
localStorage.setItem('provider', { name, network })
2020-11-19 11:25:44 +01:00
const netId = await dispatch('checkNetworkVersion')
this.$provider.initWeb3(networkConfig[`netId${netId}`].rpcUrls.Infura.url)
commit(SET_ACCOUNT, account)
2020-10-21 16:42:50 +02:00
await dispatch('getBalance', account)
2020-11-21 11:46:25 +01:00
dispatch('airdrop/checkAddress', {}, { root: true })
2020-10-21 16:42:50 +02:00
} catch (err) {
throw new Error(err.message)
}
},
async checkNetworkVersion({ commit, state, dispatch }) {
2020-10-21 16:42:50 +02:00
try {
const id = await this.$provider.checkNetworkVersion()
commit(SET_NETWORK, { ...networkConfig[`netId${id}`], id: Number(id) })
2020-11-19 11:25:44 +01:00
return id
2020-10-21 16:42:50 +02:00
} catch (err) {
throw new Error(err.message)
}
},
async sendRequest(_, params) {
2020-10-21 16:42:50 +02:00
try {
return await this.$provider.sendRequest(params)
2020-10-21 16:42:50 +02:00
} catch (err) {
throw new Error(err.message)
}
},
async contractRequest(_, params) {
2020-10-21 16:42:50 +02:00
try {
return await this.$provider.contractRequest(params)
2020-10-21 16:42:50 +02:00
} catch (err) {
throw new Error(err.message)
}
},
async getBalance({ dispatch, commit, getters }, account) {
try {
const balance = await this.$provider.getBalance({ address: account })
2020-10-21 16:42:50 +02:00
commit(SET_BALANCE, balance)
2020-10-21 16:42:50 +02:00
} catch (err) {
throw new Error(err.message)
}
},
clearState({ commit }) {
2020-10-21 16:42:50 +02:00
try {
2020-11-03 13:27:23 +01:00
localStorage.removeItem('provider')
commit(CLEAR_STATE)
2020-10-21 16:42:50 +02:00
} catch (err) {
throw new Error(err.message)
}
},
async waitForTxReceipt({ dispatch, getters }, { txHash }) {
2020-10-21 16:42:50 +02:00
try {
const tx = await this.$provider.waitForTxReceipt({ txHash })
2020-10-21 16:42:50 +02:00
return tx
2020-10-21 16:42:50 +02:00
} catch (err) {
throw new Error(err.message)
}
},
2021-08-21 04:42:17 +02:00
async switchNetwork({ dispatch }, { netId }) {
try {
await this.$provider.sendRequest({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(netId) }],
})
} catch (err) {
// This error indicates that the chain has not been added to MetaMask.
if (err.message.includes('wallet_addEthereumChain')) {
return dispatch('addNetwork', { netId })
}
throw new Error(err.message)
}
},
2021-06-02 11:47:40 +02:00
async addNetwork(_, { netId }) {
const METAMASK_LIST = {
2021-08-21 04:42:17 +02:00
100: {
chainId: '0x64',
chainName: 'xDAI Chain',
rpcUrls: ['https://rpc.xdaichain.com'],
2021-06-02 11:47:40 +02:00
nativeCurrency: {
2021-08-21 04:42:17 +02:00
name: 'xDAI',
symbol: 'xDAI',
2021-06-02 11:47:40 +02:00
decimals: 18,
},
2021-08-21 04:42:17 +02:00
blockExplorerUrls: ['https://blockscout.com/xdai/mainnet'],
2021-06-02 11:47:40 +02:00
},
}
if (METAMASK_LIST[netId]) {
await this.$provider.sendRequest({
method: 'wallet_addEthereumChain',
params: [METAMASK_LIST[netId]],
})
}
},
2021-11-15 14:21:04 +01:00
async sendTransaction(
{ dispatch, rootGetters },
{ method, params, eipDisable = false }
) {
try {
const gasParams = rootGetters['gasPrice/txGasParams'](eipDisable)
const txHash = await dispatch('sendRequest', {
method,
params: [
{
...params,
...gasParams,
},
],
})
return txHash
} catch (err) {
if (err.message.includes('EIP-1559')) {
return await dispatch('sendTransaction', {
method,
params,
eipDisable: true,
})
}
throw new Error(err.message)
}
},
2020-10-21 16:42:50 +02:00
}