tornado-initiation-ui/store/steps.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-10-21 21:45:22 +02:00
/* eslint-disable no-console */
2020-11-03 14:33:33 +01:00
import deploymentActions from '../static/deploymentActions.json'
2020-10-21 21:45:22 +02:00
2021-10-25 11:12:11 +02:00
const l1Steps = deploymentActions.actions.filter(
({ isL1Contract }) => isL1Contract
)
const l2Steps = deploymentActions.actions.filter(
({ isL1Contract }) => !isL1Contract
)
2020-10-21 21:45:22 +02:00
const state = () => {
return {
2021-10-25 11:12:11 +02:00
l1Steps,
l2Steps,
2020-10-21 21:45:22 +02:00
}
}
2020-11-03 14:13:09 +01:00
const getters = {
2021-10-25 11:12:11 +02:00
deployedL1Count: (state, getters) => {
return getters.deployedCount(true)
},
deployedL2Count: (state, getters) => {
return getters.deployedCount(false)
},
steps: (state) => (isL1) => {
return isL1 ? state.l1Steps : state.l2Steps
},
deployedCount: (state, getters) => (isL1) => {
const steps = getters.steps(isL1)
const deployed = steps.filter((step) => !!step.deployerAddress).length
const all = steps.length
2020-11-03 14:13:09 +01:00
return `${deployed}/${all}`
},
2021-10-25 11:12:11 +02:00
canDeploy: (state, getters) => (domain, isL1) => {
const steps = getters.steps(isL1)
const { dependsOn } = steps.find((s) => s.domain === domain)
return dependsOn.every((d) => {
return Boolean(steps.find((s) => s.domain === d)?.deployerAddress)
})
2020-11-05 02:33:56 +01:00
},
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 = {
2021-10-25 11:12:11 +02:00
[SET_DEPLOYER](
state,
{ stepIndex, deployerAddress, deployTransaction, isL1 }
) {
const steps = isL1 ? 'l1Steps' : 'l2Steps'
this._vm.$set(state[steps][stepIndex], 'deployerAddress', deployerAddress)
2020-11-05 02:33:56 +01:00
this._vm.$set(
2021-10-25 11:12:11 +02:00
state[steps][stepIndex],
2020-11-05 02:33:56 +01:00
'deployTransaction',
deployTransaction
)
2020-10-21 21:45:22 +02:00
},
2021-10-25 11:12:11 +02:00
[SET_PENDING_STATE](state, { status, stepIndex, isL1 }) {
const steps = isL1 ? 'l1Steps' : 'l2Steps'
this._vm.$set(state[steps][stepIndex], 'isPending', status)
2020-11-19 11:25:44 +01:00
},
2020-10-21 21:45:22 +02:00
}
const actions = {
2021-10-25 11:12:11 +02:00
async fetchDeploymentStatus({
state,
getters,
dispatch,
commit,
rootGetters,
}) {
const { isL1 } = rootGetters['provider/getNetwork']
const steps = getters.steps(isL1)
2020-11-05 02:33:56 +01:00
const deployContract = rootGetters['deploy/deployerContract'](false)
2021-08-21 04:42:17 +02:00
const events = await deployContract.getPastEvents('Deployed', {
fromBlock: 0,
toBlock: 'latest',
})
2020-11-03 14:33:33 +01:00
2021-08-21 04:42:17 +02:00
for (const event of events) {
2021-10-25 11:12:11 +02:00
const step = steps.find(
2021-08-21 04:42:17 +02:00
(s) => s.expectedAddress === event.returnValues.addr
)
2021-06-02 11:47:40 +02:00
2021-08-21 04:42:17 +02:00
if (!step) {
continue
2021-06-02 11:47:40 +02:00
}
2021-08-21 04:42:17 +02:00
commit(SET_DEPLOYER, {
2021-10-25 11:12:11 +02:00
stepIndex: steps.indexOf(step),
2021-08-21 04:42:17 +02:00
deployerAddress: event.returnValues.sender,
deployTransaction: event.transactionHash,
2021-10-25 11:12:11 +02:00
isL1,
2021-08-21 04:42:17 +02:00
})
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)
},
2021-10-25 11:12:11 +02:00
setPendingState({ commit }, payload) {
commit(SET_PENDING_STATE, payload)
2020-11-19 11:25:44 +01:00
},
2020-10-21 21:45:22 +02:00
}
export default {
namespaced: true,
state,
getters,
mutations,
actions,
}