enable wallet only when it's needed
This commit is contained in:
parent
795472ec57
commit
9559fe4273
44
src/App.js
44
src/App.js
|
@ -6,6 +6,7 @@ import { constants } from './utils/constants'
|
||||||
import { getNetworkBranch } from './utils/utils'
|
import { getNetworkBranch } from './utils/utils'
|
||||||
import { inject, observer } from 'mobx-react'
|
import { inject, observer } from 'mobx-react'
|
||||||
import messages from './utils/messages'
|
import messages from './utils/messages'
|
||||||
|
import { enableWallet } from './utils/getWeb3'
|
||||||
|
|
||||||
import './assets/stylesheets/index.css'
|
import './assets/stylesheets/index.css'
|
||||||
|
|
||||||
|
@ -42,28 +43,31 @@ class App extends Component {
|
||||||
const { commonStore, contractsStore } = this.props
|
const { commonStore, contractsStore } = this.props
|
||||||
|
|
||||||
if (!commonStore.loading) {
|
if (!commonStore.loading) {
|
||||||
if (!contractsStore.injectedWeb3) {
|
enableWallet(contractsStore.setKeys)
|
||||||
commonStore.hideLoading()
|
.then(() => {
|
||||||
swal({
|
if (!contractsStore.injectedWeb3) {
|
||||||
title: 'Error',
|
swal({
|
||||||
html: messages.NO_METAMASK_MSG,
|
title: 'Error',
|
||||||
type: 'error'
|
html: messages.NO_METAMASK_MSG,
|
||||||
|
type: 'error'
|
||||||
|
})
|
||||||
|
} else if (!contractsStore.networkMatch) {
|
||||||
|
swal({
|
||||||
|
title: 'Warning!',
|
||||||
|
html: messages.networkMatchError(contractsStore.netId),
|
||||||
|
type: 'warning'
|
||||||
|
})
|
||||||
|
} else if (contractsStore.votingKey && !contractsStore.isValidVotingKey) {
|
||||||
|
swal({
|
||||||
|
title: 'Warning!',
|
||||||
|
html: messages.invalidVotingKeyMsg(contractsStore.votingKey),
|
||||||
|
type: 'warning'
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
} else if (!contractsStore.networkMatch) {
|
.catch(error => {
|
||||||
commonStore.hideLoading()
|
swal('Error', error.message, 'error')
|
||||||
swal({
|
|
||||||
title: 'Warning!',
|
|
||||||
html: messages.networkMatchError(contractsStore.netId),
|
|
||||||
type: 'warning'
|
|
||||||
})
|
})
|
||||||
} else if (contractsStore.votingKey && !contractsStore.isValidVotingKey) {
|
|
||||||
commonStore.hideLoading()
|
|
||||||
swal({
|
|
||||||
title: 'Warning!',
|
|
||||||
html: messages.invalidVotingKeyMsg(contractsStore.votingKey),
|
|
||||||
type: 'warning'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return <NewBallot networkBranch={this.getVotingNetworkBranch()} />
|
return <NewBallot networkBranch={this.getVotingNetworkBranch()} />
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { inject, observer } from 'mobx-react'
|
||||||
import messages from '../../utils/messages'
|
import messages from '../../utils/messages'
|
||||||
import { observable, action, computed } from 'mobx'
|
import { observable, action, computed } from 'mobx'
|
||||||
import { sendTransactionByVotingKey } from '../../utils/helpers'
|
import { sendTransactionByVotingKey } from '../../utils/helpers'
|
||||||
|
import { enableWallet } from '../../utils/getWeb3'
|
||||||
|
|
||||||
const ACCEPT = 1
|
const ACCEPT = 1
|
||||||
const REJECT = 2
|
const REJECT = 2
|
||||||
|
@ -373,7 +374,16 @@ export class BallotCard extends React.Component {
|
||||||
swal('Warning!', messages.ballotIsNotActiveMsg(this.timeTo.displayValue), 'warning')
|
swal('Warning!', messages.ballotIsNotActiveMsg(this.timeTo.displayValue), 'warning')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
|
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
|
||||||
|
|
||||||
|
try {
|
||||||
|
await enableWallet(contractsStore.setKeys)
|
||||||
|
} catch (error) {
|
||||||
|
swal('Error', error.message, 'error')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const { push } = this.props.routing
|
const { push } = this.props.routing
|
||||||
if (!contractsStore.votingKey) {
|
if (!contractsStore.votingKey) {
|
||||||
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
|
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
|
||||||
|
@ -463,6 +473,25 @@ export class BallotCard extends React.Component {
|
||||||
const { votingState, contractsStore, commonStore, ballotsStore, votingType, id, pos } = this.props
|
const { votingState, contractsStore, commonStore, ballotsStore, votingType, id, pos } = this.props
|
||||||
const { push } = this.props.routing
|
const { push } = this.props.routing
|
||||||
const contract = this.getContract(contractsStore, votingType)
|
const contract = this.getContract(contractsStore, votingType)
|
||||||
|
|
||||||
|
try {
|
||||||
|
await enableWallet(contractsStore.setKeys)
|
||||||
|
} 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')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let canCancel = true
|
let canCancel = true
|
||||||
|
|
||||||
if (!this.timeToCancel.val) {
|
if (!this.timeToCancel.val) {
|
||||||
|
@ -515,6 +544,14 @@ export class BallotCard extends React.Component {
|
||||||
}
|
}
|
||||||
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
|
const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props
|
||||||
const { push } = this.props.routing
|
const { push } = this.props.routing
|
||||||
|
|
||||||
|
try {
|
||||||
|
await enableWallet(contractsStore.setKeys)
|
||||||
|
} catch (error) {
|
||||||
|
swal('Error', error.message, 'error')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!contractsStore.votingKey) {
|
if (!contractsStore.votingKey) {
|
||||||
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
|
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
|
||||||
return
|
return
|
||||||
|
|
|
@ -17,6 +17,7 @@ import { getNetworkBranch } from '../../utils/utils'
|
||||||
import { inject, observer } from 'mobx-react'
|
import { inject, observer } from 'mobx-react'
|
||||||
import messages from '../../utils/messages'
|
import messages from '../../utils/messages'
|
||||||
import { sendTransactionByVotingKey } from '../../utils/helpers'
|
import { sendTransactionByVotingKey } from '../../utils/helpers'
|
||||||
|
import { enableWallet } from '../../utils/getWeb3'
|
||||||
|
|
||||||
@inject('commonStore', 'ballotStore', 'validatorStore', 'contractsStore', 'routing', 'ballotsStore')
|
@inject('commonStore', 'ballotStore', 'validatorStore', 'contractsStore', 'routing', 'ballotsStore')
|
||||||
@observer
|
@observer
|
||||||
|
@ -253,6 +254,13 @@ export class NewBallot extends React.Component {
|
||||||
const { commonStore, contractsStore, ballotStore, ballotsStore } = this.props
|
const { commonStore, contractsStore, ballotStore, ballotsStore } = this.props
|
||||||
const { push } = this.props.routing
|
const { push } = this.props.routing
|
||||||
|
|
||||||
|
try {
|
||||||
|
await enableWallet(contractsStore.setKeys)
|
||||||
|
} catch (error) {
|
||||||
|
swal('Error', error.message, 'error')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!contractsStore.votingKey) {
|
if (!contractsStore.votingKey) {
|
||||||
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
|
swal('Warning!', messages.NO_METAMASK_MSG, 'warning')
|
||||||
return
|
return
|
||||||
|
|
|
@ -113,9 +113,7 @@ class AppMainRouter extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
setKeys = async account => {
|
setKeys = async account => {
|
||||||
await contractsStore.setMiningKey(account)
|
await contractsStore.setKeys(account)
|
||||||
await contractsStore.setVotingKey(account)
|
|
||||||
|
|
||||||
console.log('votingKey', contractsStore.votingKey)
|
console.log('votingKey', contractsStore.votingKey)
|
||||||
console.log('miningKey', contractsStore.miningKey)
|
console.log('miningKey', contractsStore.miningKey)
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,6 +214,12 @@ class ContractsStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@action('Set keys')
|
||||||
|
setKeys = async account => {
|
||||||
|
this.setVotingKey(account)
|
||||||
|
await this.setMiningKey(account)
|
||||||
|
}
|
||||||
|
|
||||||
@action('Get all keys ballots')
|
@action('Get all keys ballots')
|
||||||
getAllBallots = async () => {
|
getAllBallots = async () => {
|
||||||
let keysNextBallotId = 0,
|
let keysNextBallotId = 0,
|
||||||
|
|
|
@ -1,9 +1,27 @@
|
||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import helpers from './helpers'
|
import helpers from './helpers'
|
||||||
import { constants } from './constants'
|
import { constants } from './constants'
|
||||||
|
import messages from './messages'
|
||||||
|
|
||||||
const defaultNetId = helpers.netIdByBranch(constants.CORE)
|
const defaultNetId = helpers.netIdByBranch(constants.CORE)
|
||||||
|
|
||||||
|
export async function enableWallet(setKeys) {
|
||||||
|
if (window.ethereum) {
|
||||||
|
try {
|
||||||
|
await window.ethereum.enable()
|
||||||
|
} catch (e) {
|
||||||
|
throw Error(messages.USER_DENIED_ACCOUNT_ACCESS)
|
||||||
|
}
|
||||||
|
|
||||||
|
const web3 = new Web3(window.ethereum)
|
||||||
|
const accounts = await web3.eth.getAccounts()
|
||||||
|
|
||||||
|
if (accounts[0]) {
|
||||||
|
await setKeys(accounts[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default async function getWeb3(netId = defaultNetId, onAccountChange) {
|
export default async function getWeb3(netId = defaultNetId, onAccountChange) {
|
||||||
let web3 = null
|
let web3 = null
|
||||||
netId = Number(netId)
|
netId = Number(netId)
|
||||||
|
@ -12,11 +30,6 @@ export default async function getWeb3(netId = defaultNetId, onAccountChange) {
|
||||||
if (window.ethereum) {
|
if (window.ethereum) {
|
||||||
web3 = new Web3(window.ethereum)
|
web3 = new Web3(window.ethereum)
|
||||||
console.log('Injected web3 detected.')
|
console.log('Injected web3 detected.')
|
||||||
try {
|
|
||||||
await window.ethereum.enable()
|
|
||||||
} catch (e) {
|
|
||||||
throw Error('You have denied access to your accounts')
|
|
||||||
}
|
|
||||||
window.ethereum.autoRefreshOnNetworkChange = true
|
window.ethereum.autoRefreshOnNetworkChange = true
|
||||||
} else if (window.web3) {
|
} else if (window.web3) {
|
||||||
web3 = new Web3(window.web3.currentProvider)
|
web3 = new Web3(window.web3.currentProvider)
|
||||||
|
|
Loading…
Reference in New Issue