tornado-initiation-ui/store/deploy.js

147 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-10-21 16:42:50 +02:00
/* eslint-disable no-console */
import Web3 from 'web3'
import { numberToHex } from 'web3-utils'
import deployerABI from '../abi/deployer.abi.json'
import deploymentActions from '../static/deploymentActions.json'
const state = () => {
return {}
}
const getters = {
deployerContract: (state, getters, rootState, rootGetters) => {
const { deployerContract, rpcUrls } = rootGetters['provider/getNetwork']
const web3 = new Web3(rpcUrls.Infura.url)
return new web3.eth.Contract(deployerABI, deployerContract)
},
}
const mutations = {}
const actions = {
async deployContract(
{ state, dispatch, getters, rootGetters, commit, rootState },
{ domain }
) {
try {
dispatch('loading/enable', {}, { root: true })
2020-10-21 16:42:50 +02:00
const ethAccount = rootGetters['provider/getAccount']
2020-10-21 21:45:22 +02:00
const web3 = rootGetters['provider/getWeb3']
2020-10-21 16:42:50 +02:00
const { salt } = deploymentActions
2020-10-21 21:45:22 +02:00
const { bytecode, expectedAddress } = deploymentActions.actions.filter(
(action) => {
return action.domain === domain
}
)[0]
const code = await web3.eth.getCode(expectedAddress)
2020-10-30 09:01:41 +01:00
console.log('code', code)
2020-10-21 21:45:22 +02:00
if (code !== '0x') {
dispatch(
'notice/addNoticeWithInterval',
{
notice: {
title: 'alreadyDeployed',
type: 'danger',
},
},
{ root: true }
)
2020-10-21 21:45:22 +02:00
throw new Error('Already deployed')
}
2020-11-03 13:49:11 +01:00
const gasPrice = rootGetters['gasPrice/fastGasPrice']
2020-10-21 16:42:50 +02:00
const data = getters.deployerContract.methods
.deploy(bytecode, salt)
.encodeABI()
const callParams = {
method: 'eth_sendTransaction',
params: [
{
from: ethAccount,
2020-10-21 21:45:22 +02:00
to: getters.deployerContract._address,
gas: numberToHex(6e6),
2020-11-03 13:49:11 +01:00
gasPrice,
2020-10-21 16:42:50 +02:00
value: 0,
data,
},
],
from: ethAccount,
}
dispatch(
'loading/changeText',
{
message: this.app.i18n.t('pleaseConfirmTransactionInWallet', {
wallet: rootGetters['provider/getProviderName'],
}),
},
{ root: true }
)
2020-10-21 16:42:50 +02:00
const txHash = await dispatch('provider/sendRequest', callParams, {
root: true,
})
console.log('txHash', txHash)
dispatch('loading/disable', {}, { root: true })
const noticeId = await dispatch(
'notice/addNotice',
{
notice: {
title: 'sendingTransaction',
txHash,
type: 'loading',
},
},
{ root: true }
)
const result = await dispatch(
'txStorage/runTxWatcher',
{ txHash },
{ root: true }
)
if (result) {
dispatch(
'notice/updateNotice',
{
id: noticeId,
notice: {
title: 'contractDeployed',
type: 'success',
},
interval: true,
},
{ root: true }
)
} else {
dispatch(
'notice/updateNotice',
{
id: noticeId,
notice: {
title: 'transactionFailed',
type: 'danger',
},
interval: true,
},
{ root: true }
)
}
2020-10-21 16:42:50 +02:00
} catch (e) {
console.error('deployContract', e.message)
} finally {
dispatch('loading/disable', {}, { root: true })
2020-10-21 16:42:50 +02:00
}
},
}
export default {
namespaced: true,
state,
getters,
mutations,
actions,
}