From 893620c76b4f611e47841cfd2c0cee9b1114ac59 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 4 Dec 2019 10:28:53 +0300 Subject: [PATCH 1/3] Remove subscriber for MetaMask legacy account switcher --- src/utils/getWeb3.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/utils/getWeb3.js b/src/utils/getWeb3.js index d69a135..a7d2294 100644 --- a/src/utils/getWeb3.js +++ b/src/utils/getWeb3.js @@ -37,14 +37,16 @@ export default async function getWeb3(netId = defaultNetId, onAccountChange) { console.error('Unlock your wallet') } - let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : '' - web3.currentProvider.publicConfigStore.on('update', function(obj) { - const account = obj.selectedAddress - if (account && account !== currentAccount) { - currentAccount = account - onAccountChange(account) - } - }) + if (web3.currentProvider.publicConfigStore) { + let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : '' + web3.currentProvider.publicConfigStore.on('update', function(obj) { + const account = obj.selectedAddress + if (account && account !== currentAccount) { + currentAccount = account + onAccountChange(account) + } + }) + } const web3NetId = await web3.eth.net.getId() if (web3NetId === netId) { From b314def1a9cae5a7943afad1a622e37ea9cf04a2 Mon Sep 17 00:00:00 2001 From: Max Alekseenko Date: Mon, 30 Dec 2019 13:45:14 +0300 Subject: [PATCH 2/3] Select the network from metamask when first visiting (#212) * select the network from metamask when first visiting * Keep netId in browser's storage and use it when page first loading if possible Co-authored-by: varasev <33550681+varasev@users.noreply.github.com> --- src/index.js | 3 ++- src/utils/getWeb3.js | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index abc3853..4025217 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,7 @@ class AppMainRouter extends Component { } initChain = () => { - const netId = window.localStorage.netId + const netId = window.sessionStorage.netId getWeb3(netId, this.onAccountChange) .then(async web3Config => { await this.initialize(web3Config) @@ -103,6 +103,7 @@ class AppMainRouter extends Component { onNetworkChange = e => { commonStore.showLoading(getNetworkBranch(e.value)) window.localStorage.netId = e.value + window.sessionStorage.netId = e.value contractsStore.resetContracts() ballotsStore.reset() this.initChain() diff --git a/src/utils/getWeb3.js b/src/utils/getWeb3.js index a7d2294..9553b48 100644 --- a/src/utils/getWeb3.js +++ b/src/utils/getWeb3.js @@ -4,9 +4,8 @@ import { constants } from './constants' const defaultNetId = helpers.netIdByBranch(constants.CORE) -export default async function getWeb3(netId = defaultNetId, onAccountChange) { +export default async function getWeb3(netId, onAccountChange) { let web3 = null - netId = Number(netId) // Checking if Web3 has been injected by the browser (Mist/MetaMask) if (window.ethereum) { @@ -23,6 +22,32 @@ export default async function getWeb3(netId = defaultNetId, onAccountChange) { console.log('Injected web3 detected.') } + if (!netId) { + // Load for the first time in the current browser's session + if (web3) { + // MetaMask (or another plugin) is injected + netId = await web3.eth.net.getId() + if (!(netId in constants.NETWORKS)) { + // If plugin's netId is unsupported, try to use + // the previously chosen netId + netId = window.localStorage.netId + } + } else { + // MetaMask (or another plugin) is not injected, + // so try to use the previously chosen netId + netId = window.localStorage.netId + } + if (!(netId in constants.NETWORKS)) { + // If plugin's netId and/or previously chosen netId are not supported, + // fallback to default netId + netId = defaultNetId + } + window.localStorage.netId = netId + window.sessionStorage.netId = netId + } + + netId = Number(netId) + const network = constants.NETWORKS[netId] const injectedWeb3 = web3 !== null let netIdName = network.NAME From 8248992d46e6950136cbf12e29dcea0d753b7eb1 Mon Sep 17 00:00:00 2001 From: Max Alekseenko Date: Mon, 30 Dec 2019 14:58:01 +0300 Subject: [PATCH 3/3] Fetch the threshold for each ballot (#214) * load threshold for each ballot * Fix minThreshold for ballot Co-authored-by: varasev <33550681+varasev@users.noreply.github.com> --- src/components/BallotCard/index.js | 35 +++++++------------ src/components/BallotInfoContainer/index.js | 4 +-- src/contracts/VotingToChangeKeys.contract.js | 4 +++ .../VotingToChangeMinThreshold.contract.js | 4 +++ src/contracts/VotingToChangeProxy.contract.js | 4 +++ .../VotingToManageEmissionFunds.contract.js | 4 +++ 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/components/BallotCard/index.js b/src/components/BallotCard/index.js index 5a219ac..9db8426 100644 --- a/src/components/BallotCard/index.js +++ b/src/components/BallotCard/index.js @@ -59,6 +59,7 @@ export class BallotCard extends React.Component { @observable memo @observable quorumState @observable minBallotDuration + @observable minThreshold @computed get cancelOrFinalizeButtonDisplayName() { @@ -302,6 +303,9 @@ export class BallotCard extends React.Component { this.getHasAlreadyVoted() } + // minThreshold + this.getMinThreshold() + if (votingType === 'votingToManageEmissionFunds') { this.getQuorumState() } @@ -317,6 +321,12 @@ export class BallotCard extends React.Component { return formattedMs } + @action('ballot min threshold of voters') + getMinThreshold = async () => { + const { contractsStore, id, votingType } = this.props + this.minThreshold = await this.getContract(contractsStore, votingType).getMinThresholdOfVoters(id) + } + @action('validator has already voted') getHasAlreadyVoted = async () => { const { contractsStore, id, votingType } = this.props @@ -610,21 +620,6 @@ export class BallotCard extends React.Component { } } - getThreshold(contractsStore, votingType) { - switch (votingType) { - case 'votingToChangeKeys': - return contractsStore.keysBallotThreshold - case 'votingToChangeMinThreshold': - return contractsStore.minThresholdBallotThreshold - case 'votingToChangeProxy': - return contractsStore.proxyBallotThreshold - case 'votingToManageEmissionFunds': - return contractsStore.emissionFundsBallotThreshold - default: - return contractsStore.keysBallotThreshold - } - } - getMinBallotDuration(contractsStore, votingType) { switch (votingType) { case 'votingToChangeKeys': @@ -682,10 +677,9 @@ export class BallotCard extends React.Component { } render() { - let { contractsStore, votingType, children } = this.props + let { votingType, children } = this.props let votes - const threshold = this.getThreshold(contractsStore, votingType) const networkBranch = this.getVotingNetworkBranch() if (votingType === 'votingToManageEmissionFunds') { @@ -744,12 +738,7 @@ export class BallotCard extends React.Component { /> - + MAX_DETAILS_LENGTH ? (
- Minimum {threshold} from {validatorsLength} validators are required to pass the proposal + Minimum {threshold} validators are required to pass the proposal