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

79 lines
1.9 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'
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 {
commit(SET_PROVIDER_NAME, name)
commit(SET_NETWORK_NAME, network)
2020-11-03 13:27:23 +01:00
localStorage.setItem('provider', { name, network })
const account = await this.$provider.initProvider(getters.getProvider)
await dispatch('checkNetworkVersion')
commit(SET_ACCOUNT, account)
2020-10-21 16:42:50 +02:00
await dispatch('getBalance', account)
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-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)
}
},
}