tornado-initiation-ui/store/deploy.js

189 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-10-21 16:42:50 +02:00
/* eslint-disable no-console */
2021-06-02 11:47:40 +02:00
import Web3 from 'web3'
2020-11-05 02:33:56 +01:00
import { hexToNumber, numberToHex } from 'web3-utils'
2020-10-21 16:42:50 +02:00
import deployerABI from '../abi/deployer.abi.json'
import deploymentActions from '../static/deploymentActions.json'
const state = () => {
return {}
}
const getters = {
2020-11-05 02:33:56 +01:00
deployerContract: (state, getters, rootState, rootGetters) => (isProxy) => {
2021-06-02 11:47:40 +02:00
const web3 = new Web3(rootGetters['provider/getNetwork'].rpcUrls.Infura.url)
2020-11-05 02:33:56 +01:00
return new web3.eth.Contract(
deployerABI,
isProxy
? deploymentActions.deployer
: deploymentActions.actions[0].expectedAddress
)
2020-10-21 16:42:50 +02:00
},
}
const mutations = {}
const actions = {
async deployContract(
{ state, dispatch, getters, rootGetters, commit, rootState },
2020-11-19 11:25:44 +01:00
{ action, index }
2020-10-21 16:42:50 +02:00
) {
try {
dispatch('loading/enable', {}, { root: true })
2020-11-11 20:07:00 +01:00
const isProxy = action.domain === 'deployer.contract.tornadocash.eth'
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-11-05 02:33:56 +01:00
const code = await web3.eth.getCode(action.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-11-05 02:33:56 +01:00
const data = getters
.deployerContract(isProxy)
.methods.deploy(action.bytecode, deploymentActions.salt)
2020-10-21 16:42:50 +02:00
.encodeABI()
2020-11-05 02:33:56 +01:00
const callParamsEstimate = {
method: 'eth_estimateGas',
params: [
{
from: ethAccount,
to: getters.deployerContract(isProxy)._address,
// gas: numberToHex(6e6),
gasPrice,
value: `0x0`,
data,
},
],
from: ethAccount,
}
2021-11-22 08:29:48 +01:00
const gasEstimate = await dispatch(
'provider/sendRequest',
callParamsEstimate,
{
root: true,
}
)
2020-11-05 02:33:56 +01:00
const gasWithBuffer = Math.ceil(hexToNumber(gasEstimate) * 1.1)
2020-10-21 16:42:50 +02:00
const callParams = {
method: 'eth_sendTransaction',
params: [
{
from: ethAccount,
2020-11-05 02:33:56 +01:00
to: getters.deployerContract(isProxy)._address,
gas: numberToHex(gasWithBuffer),
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 })
2020-11-19 11:25:44 +01:00
dispatch(
'steps/setPendingState',
{ status: true, stepIndex: index },
{ root: true }
)
const noticeId = await dispatch(
'notice/addNotice',
{
notice: {
title: 'sendingTransaction',
txHash,
type: 'loading',
},
},
{ root: true }
)
2020-11-03 14:37:17 +01:00
const success = await dispatch(
'txStorage/runTxWatcher',
{ txHash },
{ root: true }
)
2020-11-03 14:37:17 +01:00
if (success) {
dispatch(
'notice/updateNotice',
{
id: noticeId,
notice: {
title: 'contractDeployed',
type: 'success',
},
2020-11-19 11:25:44 +01:00
interval: 20000,
},
{ root: true }
)
2020-11-03 14:37:17 +01:00
dispatch('steps/fetchDeploymentStatus', {}, { root: true })
} else {
dispatch(
'notice/updateNotice',
{
id: noticeId,
notice: {
title: 'transactionFailed',
type: 'danger',
},
2020-11-19 11:25:44 +01:00
interval: 20000,
},
{ root: true }
)
}
2020-10-21 16:42:50 +02:00
} catch (e) {
console.error('deployContract', e.message)
2020-11-11 20:07:00 +01:00
await dispatch(
'notice/addNotice',
{
notice: {
title: 'cannotBeExecuted',
type: 'danger',
},
},
{ root: true }
)
} finally {
dispatch('loading/disable', {}, { root: true })
2020-11-19 11:25:44 +01:00
dispatch(
'steps/setPendingState',
{ status: false, stepIndex: index },
{ root: true }
)
2020-10-21 16:42:50 +02:00
}
},
}
export default {
namespaced: true,
state,
getters,
mutations,
actions,
}