tornado-initiation-ui/store/deploy.js

75 lines
1.9 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 {
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') {
throw new Error('Already deployed')
}
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-10-21 16:42:50 +02:00
gasPrice: '0x100000000',
value: 0,
data,
},
],
from: ethAccount,
}
const txHash = await dispatch('provider/sendRequest', callParams, {
root: true,
})
console.log('txHash', txHash)
} catch (e) {
console.error('deployContract', e.message)
}
},
}
export default {
namespaced: true,
state,
getters,
mutations,
actions,
}