tornado-initiation-ui/store/steps.js

102 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-10-21 21:45:22 +02:00
/* eslint-disable no-console */
2021-06-02 11:47:40 +02:00
import Web3 from 'web3'
2020-11-03 14:33:33 +01:00
import deploymentActions from '../static/deploymentActions.json'
2020-10-21 21:45:22 +02:00
const state = () => {
return {
2020-11-05 02:33:56 +01:00
steps: deploymentActions.actions,
2020-10-21 21:45:22 +02:00
}
}
2020-11-03 14:13:09 +01:00
const getters = {
deployedCount: (state) => {
2020-11-05 02:33:56 +01:00
const deployed = state.steps.filter((step) => !!step.deployerAddress).length
2020-11-03 14:13:09 +01:00
const all = state.steps.length
return `${deployed}/${all}`
},
2020-11-05 02:33:56 +01:00
canDeploy: (state) => (domain) => {
const { dependsOn } = state.steps.find((s) => s.domain === domain)
return dependsOn.every(
(d) => !!state.steps.find((s) => s.domain === d).deployerAddress
)
},
2020-11-03 14:13:09 +01:00
}
2020-10-21 21:45:22 +02:00
const SET_DEPLOYER = 'SET_DEPLOYER'
2020-11-19 11:25:44 +01:00
const SET_PENDING_STATE = 'SET_PENDING_STATE'
2020-10-21 21:45:22 +02:00
const mutations = {
2020-11-05 02:33:56 +01:00
[SET_DEPLOYER](state, { stepIndex, deployerAddress, deployTransaction }) {
2020-10-21 21:45:22 +02:00
this._vm.$set(state.steps[stepIndex], 'deployerAddress', deployerAddress)
2020-11-05 02:33:56 +01:00
this._vm.$set(
state.steps[stepIndex],
'deployTransaction',
deployTransaction
)
2020-10-21 21:45:22 +02:00
},
2020-11-19 11:25:44 +01:00
[SET_PENDING_STATE](state, { status, stepIndex }) {
this._vm.$set(state.steps[stepIndex], 'isPending', status)
},
2020-10-21 21:45:22 +02:00
}
const actions = {
2020-11-03 14:33:33 +01:00
async fetchDeploymentStatus({ state, dispatch, commit, rootGetters }) {
2020-11-05 02:33:56 +01:00
const deployContract = rootGetters['deploy/deployerContract'](false)
2021-06-02 11:47:40 +02:00
const web3 = new Web3(rootGetters['provider/getNetwork'].rpcUrls.Infura.url)
const code = await web3.eth.getCode(deployContract._address)
if (code === '0x') {
return
}
2020-11-03 14:33:33 +01:00
2021-06-02 11:47:40 +02:00
const latestBlock = await web3.eth.getBlock('latest')
let number = latestBlock.number
let events = []
while (true) {
2021-06-23 14:18:14 +02:00
const fromBlock = number - 99500
2021-06-02 11:47:40 +02:00
events = await deployContract.getPastEvents('Deployed', {
fromBlock,
toBlock: number,
2020-11-05 02:33:56 +01:00
})
2021-06-02 11:47:40 +02:00
number = fromBlock
if (events.length > 0) {
for (const event of events) {
const step = state.steps.find(
(s) => s.expectedAddress === event.returnValues.addr
)
if (!step) {
continue
}
commit(SET_DEPLOYER, {
stepIndex: state.steps.indexOf(step),
deployerAddress: event.returnValues.sender,
deployTransaction: event.transactionHash,
})
}
if (events[0].returnValues.addr === deployContract._address) {
break
}
}
2020-10-21 21:45:22 +02:00
}
},
2020-11-19 11:25:44 +01:00
statusPooling({ dispatch }) {
setTimeout(async () => {
try {
console.log('Fetching deployment status...')
await dispatch('fetchDeploymentStatus')
} finally {
dispatch('statusPooling')
}
}, 15000)
},
setPendingState({ commit }, { status, stepIndex }) {
commit(SET_PENDING_STATE, { status, stepIndex })
},
2020-10-21 21:45:22 +02:00
}
export default {
namespaced: true,
state,
getters,
mutations,
actions,
}