From c8047f9520d509b76a7416c788df226ab0a029b7 Mon Sep 17 00:00:00 2001 From: Max Alekseenko Date: Tue, 19 Nov 2019 14:38:53 +0300 Subject: [PATCH] getBallotsLimits after the wallet was enabled --- src/App.js | 2 +- src/components/BallotCard/index.js | 6 +++--- src/components/NewBallot/index.js | 2 +- src/index.js | 10 ++++------ src/stores/ContractsStore.js | 12 ++++++++++-- src/utils/getWeb3.js | 8 ++++---- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/App.js b/src/App.js index 84dcc16..3d7f82d 100644 --- a/src/App.js +++ b/src/App.js @@ -43,7 +43,7 @@ class App extends Component { const { commonStore, contractsStore } = this.props if (!commonStore.loading) { - enableWallet(contractsStore.setKeys) + enableWallet(contractsStore.updateKeys) .then(() => { if (!contractsStore.injectedWeb3) { swal({ diff --git a/src/components/BallotCard/index.js b/src/components/BallotCard/index.js index c6fbbc1..b608b70 100644 --- a/src/components/BallotCard/index.js +++ b/src/components/BallotCard/index.js @@ -378,7 +378,7 @@ export class BallotCard extends React.Component { const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props try { - await enableWallet(contractsStore.setKeys) + await enableWallet(contractsStore.updateKeys) } catch (error) { swal('Error', error.message, 'error') return @@ -475,7 +475,7 @@ export class BallotCard extends React.Component { const contract = this.getContract(contractsStore, votingType) try { - await enableWallet(contractsStore.setKeys) + await enableWallet(contractsStore.updateKeys) } catch (error) { swal('Error', error.message, 'error') return @@ -546,7 +546,7 @@ export class BallotCard extends React.Component { const { push } = this.props.routing try { - await enableWallet(contractsStore.setKeys) + await enableWallet(contractsStore.updateKeys) } catch (error) { swal('Error', error.message, 'error') return diff --git a/src/components/NewBallot/index.js b/src/components/NewBallot/index.js index 93e987d..98f0594 100644 --- a/src/components/NewBallot/index.js +++ b/src/components/NewBallot/index.js @@ -255,7 +255,7 @@ export class NewBallot extends React.Component { const { push } = this.props.routing try { - await enableWallet(contractsStore.setKeys) + await enableWallet(contractsStore.updateKeys) } catch (error) { swal('Error', error.message, 'error') return diff --git a/src/index.js b/src/index.js index 0513867..1c0ab2b 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,7 @@ class AppMainRouter extends Component { initChain = () => { const netId = window.localStorage.netId - getWeb3(netId, this.onAccountChange) + getWeb3(netId, contractsStore.updateKeys) .then(async web3Config => { await this.initialize(web3Config) commonStore.hideLoading() @@ -108,12 +108,10 @@ class AppMainRouter extends Component { this.initChain() } - onAccountChange = account => { - this.setKeys(account) - } - setKeys = async account => { - await contractsStore.setKeys(account) + await contractsStore.setMiningKey(account) + await contractsStore.setVotingKey(account) + console.log('votingKey', contractsStore.votingKey) console.log('miningKey', contractsStore.miningKey) } diff --git a/src/stores/ContractsStore.js b/src/stores/ContractsStore.js index 7a4100b..be3f122 100644 --- a/src/stores/ContractsStore.js +++ b/src/stores/ContractsStore.js @@ -214,10 +214,17 @@ class ContractsStore { } } - @action('Set keys') - setKeys = async account => { + @action('Update keys') + updateKeys = async account => { + if (this.votingKey === account) return + this.setVotingKey(account) await this.setMiningKey(account) + + console.log('votingKey', this.votingKey) + console.log('miningKey', this.miningKey) + + await this.getBallotsLimits() } @action('Get all keys ballots') @@ -415,6 +422,7 @@ class ContractsStore { @action async getBallotsLimits() { return new Promise(async resolve => { + console.log('>>>>>>>>>>>>>>>>>>>>>>>') if (this.web3Instance && this.netId) { const limitPerValidator = await this.ballotsStorage.instance.methods.getBallotLimitPerValidator().call() diff --git a/src/utils/getWeb3.js b/src/utils/getWeb3.js index 888a172..6816bfa 100644 --- a/src/utils/getWeb3.js +++ b/src/utils/getWeb3.js @@ -5,7 +5,7 @@ import messages from './messages' const defaultNetId = helpers.netIdByBranch(constants.CORE) -export async function enableWallet(setKeys) { +export async function enableWallet(updateKeys) { if (window.ethereum) { try { await window.ethereum.enable() @@ -17,12 +17,12 @@ export async function enableWallet(setKeys) { const accounts = await web3.eth.getAccounts() if (accounts[0]) { - await setKeys(accounts[0]) + await updateKeys(accounts[0]) } } } -export default async function getWeb3(netId = defaultNetId, onAccountChange) { +export default async function getWeb3(netId = defaultNetId, updateKeys) { let web3 = null netId = Number(netId) @@ -55,7 +55,7 @@ export default async function getWeb3(netId = defaultNetId, onAccountChange) { const account = obj.selectedAddress if (account && account !== currentAccount) { currentAccount = account - onAccountChange(account) + updateKeys(account) } })