Improvements and optimizations

This commit is contained in:
Vadim 2020-01-09 16:02:58 +03:00
parent 557bc54778
commit 8f7279519e
11 changed files with 109 additions and 126 deletions

View File

@ -57,7 +57,7 @@ class App extends Component {
html: messages.networkMatchError(contractsStore.netId),
type: 'warning'
})
} else if (contractsStore.votingKey && !contractsStore.isValidVotingKey) {
} else if (!contractsStore.isValidVotingKey) {
swal({
title: 'Warning!',
html: messages.invalidVotingKeyMsg(contractsStore.votingKey),

View File

@ -375,6 +375,27 @@ export class BallotCard extends React.Component {
this.quorumState = await this.repeatGetProperty(contractsStore, votingType, id, 'getQuorumState', 0)
}
networkAndKeyValidation = async () => {
const { contractsStore } = this.props
try {
await enableWallet(contractsStore.updateKeys)
} catch (error) {
swal('Error', error.message, 'error')
return false
}
if (contractsStore.isEmptyVotingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return false
} else if (!contractsStore.networkMatch) {
swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning')
return false
} else if (!contractsStore.isValidVotingKey) {
swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning')
return false
}
return true
}
vote = async ({ choice }) => {
if (this.isCanceled) {
swal('Warning!', messages.INVALID_VOTE_MSG, 'warning')
@ -385,26 +406,12 @@ export class BallotCard extends React.Component {
return
}
if (!(await this.networkAndKeyValidation())) {
return
}
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
try {
await enableWallet(contractsStore.updateKeys)
} catch (error) {
swal('Error', error.message, 'error')
return
}
const { push } = this.props.routing
if (!contractsStore.votingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return
} else if (!contractsStore.networkMatch) {
swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning')
return
} else if (!contractsStore.isValidVotingKey) {
swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning')
return
}
commonStore.showLoading()
let isValidVote = await this.isValidVote()
if (!isValidVote) {
@ -484,21 +491,7 @@ export class BallotCard extends React.Component {
const { push } = this.props.routing
const contract = this.getContract(contractsStore, votingType)
try {
await enableWallet(contractsStore.updateKeys)
} catch (error) {
swal('Error', error.message, 'error')
return
}
if (!contractsStore.votingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return
} else if (!contractsStore.networkMatch) {
swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning')
return
} else if (!contractsStore.isValidVotingKey) {
swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning')
if (!(await this.networkAndKeyValidation())) {
return
}
@ -555,23 +548,10 @@ export class BallotCard extends React.Component {
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
const { push } = this.props.routing
try {
await enableWallet(contractsStore.updateKeys)
} catch (error) {
swal('Error', error.message, 'error')
if (!(await this.networkAndKeyValidation())) {
return
}
if (!contractsStore.votingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return
} else if (!contractsStore.networkMatch) {
swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning')
return
} else if (!contractsStore.isValidVotingKey) {
swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning')
return
}
if (this.isFinalized) {
swal('Warning!', messages.ALREADY_FINALIZED_MSG, 'warning')
return

View File

@ -45,7 +45,7 @@ export class BallotKeysMetadata extends React.Component {
<FormSelect
disabled={ballotStore.isNewValidatorPersonalData}
hint="Mining key address of validator to vote for.<br />Example: 0xc70760D23557A4FDE612C0bE63b26EBD023C51Ee."
id="mining-key"
id="mining-key-select"
name="form-field-name"
networkBranch={networkBranch}
onChange={ballotStore.setMiningKey}

View File

@ -261,7 +261,7 @@ export class NewBallot extends React.Component {
return
}
if (!contractsStore.votingKey) {
if (contractsStore.isEmptyVotingKey) {
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
return
} else if (!contractsStore.networkMatch) {

View File

@ -8,11 +8,11 @@ export const NewBallotMenuInfo = ({ minThreshold, validatorsLength, keys, valida
<li className="mn-NewBallotMenuInfo_ListItem">
Minimum {minThreshold} from {validatorsLength} validators are required to pass the&nbsp; proposal
</li>
<li className="mn-NewBallotMenuInfo_ListItem">You can create {keys} ballot(s) for keys</li>
<li className="mn-NewBallotMenuInfo_ListItem">You can create {Number(keys)} ballot(s) for keys</li>
<li className="mn-NewBallotMenuInfo_ListItem">
You can create {validatorLimitsMinThreshold} ballot(s) for consensus
You can create {Number(validatorLimitsMinThreshold)} ballot(s) for consensus
</li>
<li className="mn-NewBallotMenuInfo_ListItem">You can create {proxy} ballot(s) for proxy</li>
<li className="mn-NewBallotMenuInfo_ListItem">You can create {Number(proxy)} ballot(s) for proxy</li>
</ul>
</div>
)

View File

@ -90,7 +90,7 @@ export default class VotingToChangeKeys {
return _limitPerValidator - _activeBallots
}
async minBallotDuration() {
return await this.instance.methods.minBallotDuration().call()
minBallotDuration() {
return this.instance.methods.minBallotDuration().call()
}
}

View File

@ -82,7 +82,7 @@ export default class VotingToChangeMinThreshold {
return _limitPerValidator - _activeBallots
}
async minBallotDuration() {
return await this.instance.methods.minBallotDuration().call()
minBallotDuration() {
return this.instance.methods.minBallotDuration().call()
}
}

View File

@ -78,7 +78,7 @@ export default class VotingToChangeProxy {
return _limitPerValidator - _activeBallots
}
async minBallotDuration() {
return await this.instance.methods.minBallotDuration().call()
minBallotDuration() {
return this.instance.methods.minBallotDuration().call()
}
}

View File

@ -88,13 +88,9 @@ class AppMainRouter extends Component {
await Promise.all(promises)
await this.setKeys(web3Config.defaultAccount)
await contractsStore.updateKeys(web3Config.defaultAccount)
contractsStore.getKeysBallotThreshold()
contractsStore.getProxyBallotThreshold()
contractsStore.getBallotCancelingThreshold()
await contractsStore.getBallotsLimits()
await contractsStore.getMinBallotDurationsAndThresholds()
await contractsStore.getAllValidatorMetadata()
await contractsStore.getAllBallots()
@ -109,14 +105,6 @@ class AppMainRouter extends Component {
this.initChain()
}
setKeys = async account => {
await contractsStore.setMiningKey(account)
await contractsStore.setVotingKey(account)
console.log('votingKey', contractsStore.votingKey)
console.log('miningKey', contractsStore.miningKey)
}
render() {
return (
<Provider {...stores}>

View File

@ -49,39 +49,26 @@ class ContractsStore {
@observable injectedWeb3
constructor() {
this.votingKey = null
this.miningKey = null
this.votingKey = '0x0000000000000000000000000000000000000000'
this.miningKey = '0x0000000000000000000000000000000000000000'
this.validatorsMetadata = {}
this.validatorLimits = { keys: null, minThreshold: null, proxy: null }
this.minBallotDuration = { keys: 0, minThreshold: 0, proxy: 0 }
this.injectedWeb3 = false
}
@computed
get isEmptyVotingKey() {
return !this.votingKey || this.votingKey === '0x0000000000000000000000000000000000000000'
}
@computed
get isValidVotingKey() {
if (this.isEmptyVotingKey) return false
if (this.miningKey && this.miningKey !== '0x0000000000000000000000000000000000000000') return true
return false
}
@action('Get keys ballot threshold')
getKeysBallotThreshold = async () => {
this.keysBallotThreshold = await this.ballotsStorage.instance.methods.getBallotThreshold(1).call()
this.minThresholdBallotThreshold = this.keysBallotThreshold
}
@action('Get proxy ballot threshold')
getProxyBallotThreshold = async () => {
this.proxyBallotThreshold = await this.ballotsStorage.instance.methods.getProxyThreshold().call()
this.emissionFundsBallotThreshold = this.proxyBallotThreshold
}
@action('Get ballot canceling threshold')
getBallotCancelingThreshold = async () => {
this.ballotCancelingThreshold = this.votingToManageEmissionFunds
? Number(await this.votingToManageEmissionFunds.ballotCancelingThreshold())
: 0
}
@action('Set web3Instance')
setWeb3Instance = web3Config => {
this.web3Instance = web3Config.web3Instance
@ -206,17 +193,24 @@ class ContractsStore {
@action('Set mining key')
setMiningKey = async account => {
try {
this.miningKey = await this.keysManager.instance.methods.miningKeyByVoting(account).call()
} catch (e) {
console.log(e)
this.miningKey = '0x0000000000000000000000000000000000000000'
let miningKey = '0x0000000000000000000000000000000000000000'
if (account && account !== '0x0000000000000000000000000000000000000000') {
try {
miningKey = await this.keysManager.instance.methods.miningKeyByVoting(account).call()
} catch (e) {
console.log(e)
}
}
this.miningKey = miningKey
}
@action('Update keys')
updateKeys = async account => {
if (this.votingKey === account) return
account = account || '0x0000000000000000000000000000000000000000'
if (this.votingKey && this.votingKey.toLowerCase() === account.toLowerCase()) {
return
}
this.setVotingKey(account)
await this.setMiningKey(account)
@ -423,41 +417,63 @@ class ContractsStore {
async getBallotsLimits() {
return new Promise(async resolve => {
if (this.web3Instance && this.netId) {
const limitPerValidator = await this.ballotsStorage.instance.methods.getBallotLimitPerValidator().call()
let keysLimit = 0
let minThresholdLimit = 0
let proxyLimit = 0
const getKeysLimit = await this.votingToChangeKeys.getBallotLimit(this.miningKey, limitPerValidator)
const getMinThresholdLimit = await this.votingToChangeMinThreshold.getBallotLimit(
this.miningKey,
limitPerValidator
)
const getProxyLimit = await this.votingToChangeProxy.getBallotLimit(this.miningKey, limitPerValidator)
if (this.isValidVotingKey) {
const limitPerValidator = await this.ballotsStorage.instance.methods.getBallotLimitPerValidator().call()
keysLimit = await this.votingToChangeKeys.getBallotLimit(this.miningKey, limitPerValidator)
minThresholdLimit = await this.votingToChangeMinThreshold.getBallotLimit(this.miningKey, limitPerValidator)
proxyLimit = await this.votingToChangeProxy.getBallotLimit(this.miningKey, limitPerValidator)
}
const getKeysMinBallotDuration = await this.votingToChangeKeys.minBallotDuration()
const getMinThresholdMinBallotDuration = await this.votingToChangeMinThreshold.minBallotDuration()
const getProxyMinBallotDuration = await this.votingToChangeProxy.minBallotDuration()
this.validatorLimits.keys = keysLimit
this.validatorLimits.minThreshold = minThresholdLimit
this.validatorLimits.proxy = proxyLimit
}
resolve()
})
}
@action
async getMinBallotDurationsAndThresholds() {
return new Promise(async resolve => {
if (this.web3Instance && this.netId) {
const getKeysMinBallotDuration = this.votingToChangeKeys.minBallotDuration()
const getMinThresholdMinBallotDuration = this.votingToChangeMinThreshold.minBallotDuration()
const getProxyMinBallotDuration = this.votingToChangeProxy.minBallotDuration()
const getBallotThreshold = this.ballotsStorage.instance.methods.getBallotThreshold(1).call()
const getProxyThreshold = this.ballotsStorage.instance.methods.getProxyThreshold().call()
const getBallotCancelingThreshold = this.votingToManageEmissionFunds
? this.votingToManageEmissionFunds.ballotCancelingThreshold()
: 0
await Promise.all([
getKeysLimit,
getMinThresholdLimit,
getProxyLimit,
getKeysMinBallotDuration,
getMinThresholdMinBallotDuration,
getProxyMinBallotDuration
getProxyMinBallotDuration,
getBallotThreshold,
getProxyThreshold,
getBallotCancelingThreshold
]).then(
([
keysLimit,
minThresholdLimit,
proxyLimit,
keysMinBallotDuration,
minThresholdMinBallotDuration,
proxyMinBallotDuration
proxyMinBallotDuration,
keysBallotThreshold,
proxyBallotThreshold,
cancelingThreshold
]) => {
this.validatorLimits.keys = keysLimit
this.validatorLimits.minThreshold = minThresholdLimit
this.validatorLimits.proxy = proxyLimit
this.minBallotDuration.keys = keysMinBallotDuration
this.minBallotDuration.minThreshold = minThresholdMinBallotDuration
this.minBallotDuration.proxy = proxyMinBallotDuration
this.keysBallotThreshold = keysBallotThreshold
this.minThresholdBallotThreshold = keysBallotThreshold
this.proxyBallotThreshold = proxyBallotThreshold
this.emissionFundsBallotThreshold = proxyBallotThreshold
this.ballotCancelingThreshold = cancelingThreshold
resolve()
}
)

View File

@ -10,15 +10,14 @@ export async function enableWallet(updateKeys) {
try {
await window.ethereum.enable()
} catch (e) {
await updateKeys(null)
throw Error(messages.USER_DENIED_ACCOUNT_ACCESS)
}
const web3 = new Web3(window.ethereum)
const accounts = await web3.eth.getAccounts()
if (accounts[0]) {
await updateKeys(accounts[0])
}
await updateKeys(accounts[0])
}
}
@ -76,10 +75,10 @@ export default async function getWeb3(netId, updateKeys) {
}
if (web3.currentProvider.publicConfigStore) {
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : ''
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null
web3.currentProvider.publicConfigStore.on('update', function(obj) {
const account = obj.selectedAddress
if (account && account !== currentAccount) {
if (account !== currentAccount) {
currentAccount = account
updateKeys(account)
}