2020-10-21 16:42:50 +02:00
|
|
|
import networkConfig from '@/networkConfig'
|
|
|
|
import {
|
|
|
|
SET_ACCOUNT,
|
|
|
|
SET_NETWORK,
|
2020-11-03 12:48:03 +01:00
|
|
|
CLEAR_STATE,
|
|
|
|
SET_BALANCE,
|
2020-10-21 16:42:50 +02:00
|
|
|
SET_NETWORK_NAME,
|
|
|
|
SET_PROVIDER_NAME,
|
|
|
|
} from './constant'
|
|
|
|
|
|
|
|
export default {
|
2020-11-03 12:48:03 +01:00
|
|
|
async initProvider({ commit, state, getters, dispatch }, { name, network }) {
|
2020-10-21 16:42:50 +02:00
|
|
|
try {
|
|
|
|
commit(SET_PROVIDER_NAME, name)
|
|
|
|
commit(SET_NETWORK_NAME, network)
|
|
|
|
|
2020-11-03 12:48:03 +01:00
|
|
|
const account = await this.$provider.initProvider(getters.getProvider)
|
|
|
|
await dispatch('checkNetworkVersion')
|
|
|
|
|
|
|
|
commit(SET_ACCOUNT, account)
|
2020-10-21 16:42:50 +02:00
|
|
|
|
2020-11-03 12:48:03 +01:00
|
|
|
await dispatch('getBalance', account)
|
2020-10-21 16:42:50 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message)
|
|
|
|
}
|
|
|
|
},
|
2020-11-03 12:48:03 +01:00
|
|
|
async checkNetworkVersion({ commit, state, dispatch }) {
|
2020-10-21 16:42:50 +02:00
|
|
|
try {
|
2020-11-03 12:48:03 +01:00
|
|
|
const id = await this.$provider.checkNetworkVersion()
|
|
|
|
commit(SET_NETWORK, { ...networkConfig[`netId${id}`], id: Number(id) })
|
2020-10-21 16:42:50 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message)
|
|
|
|
}
|
|
|
|
},
|
2020-11-03 12:48:03 +01:00
|
|
|
async sendRequest(_, params) {
|
2020-10-21 16:42:50 +02:00
|
|
|
try {
|
2020-11-03 12:48:03 +01:00
|
|
|
return await this.$provider.sendRequest(params)
|
2020-10-21 16:42:50 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message)
|
|
|
|
}
|
|
|
|
},
|
2020-11-03 12:48:03 +01:00
|
|
|
async contractRequest(_, params) {
|
2020-10-21 16:42:50 +02:00
|
|
|
try {
|
2020-11-03 12:48:03 +01:00
|
|
|
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 {
|
2020-11-03 12:48:03 +01:00
|
|
|
const balance = await this.$provider.getBalance({ address: account })
|
2020-10-21 16:42:50 +02:00
|
|
|
|
2020-11-03 12:48:03 +01:00
|
|
|
commit(SET_BALANCE, balance)
|
2020-10-21 16:42:50 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message)
|
|
|
|
}
|
|
|
|
},
|
2020-11-03 12:48:03 +01:00
|
|
|
clearState({ commit }) {
|
2020-10-21 16:42:50 +02:00
|
|
|
try {
|
2020-11-03 12:48:03 +01:00
|
|
|
window.localStorage.removeItem('provider')
|
2020-10-21 16:42:50 +02:00
|
|
|
|
2020-11-03 12:48:03 +01:00
|
|
|
commit(CLEAR_STATE)
|
2020-10-21 16:42:50 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message)
|
|
|
|
}
|
|
|
|
},
|
2020-11-03 12:48:03 +01:00
|
|
|
async waitForTxReceipt({ dispatch, getters }, { txHash }) {
|
2020-10-21 16:42:50 +02:00
|
|
|
try {
|
2020-11-03 12:48:03 +01:00
|
|
|
const tx = await this.$provider.waitForTxReceipt({ txHash })
|
2020-10-21 16:42:50 +02:00
|
|
|
|
2020-11-03 12:48:03 +01:00
|
|
|
return tx
|
2020-10-21 16:42:50 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|