Merge pull request #422 from NoahZinsmeister/beta

make web3 connection logic more robust
This commit is contained in:
Noah Zinsmeister 2019-08-20 17:19:03 +02:00 committed by GitHub
commit 81bc6926ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 21 deletions

@ -32,8 +32,10 @@ const SpinnerWrapper = styled(Spinner)`
`
function tryToSetConnector(setConnector, setError) {
setConnector('Injected', { suppressAndThrowErrors: true }).catch(error => {
setConnector('Network')
setConnector('Injected', { suppressAndThrowErrors: true }).catch(() => {
setConnector('Network', { suppressAndThrowErrors: true }).catch(error => {
setError(error)
})
})
}
@ -54,25 +56,31 @@ export default function Web3ReactManager({ children }) {
if (accounts.length >= 1) {
tryToSetConnector(setConnector, setError)
} else {
setConnector('Network')
setConnector('Network', { suppressAndThrowErrors: true }).catch(error => {
setError(error)
})
}
})
}
} else {
setConnector('Network')
setConnector('Network', { suppressAndThrowErrors: true }).catch(error => {
setError(error)
})
}
}
}, [active, error, setConnector, setError])
})
// parse the error
useEffect(() => {
if (error) {
// if the user changes to the wrong network, unset the connector
if (error.code === Connector.errorCodes.UNSUPPORTED_NETWORK) {
setConnector('Network')
setConnector('Network', { suppressAndThrowErrors: true }).catch(error => {
setError(error)
})
}
}
}, [error, setConnector])
})
const [showLoader, setShowLoader] = useState(false)
useEffect(() => {

@ -77,7 +77,7 @@ export function Updater() {
useEffect(() => {
if (library) {
if (connectorName === 'Network') {
library.pollingInterval = 15
library.polling = false
} else {
library.pollingInterval = 5
}
@ -86,24 +86,26 @@ export function Updater() {
// update usd price
useEffect(() => {
let stale = false
if (library) {
let stale = false
getUSDPrice(library)
.then(([price]) => {
if (!stale) {
updateUSDPrice(networkId, price)
}
})
.catch(() => {
if (!stale) {
updateUSDPrice(networkId, null)
}
})
getUSDPrice(library)
.then(([price]) => {
if (!stale) {
updateUSDPrice(networkId, price)
}
})
.catch(() => {
if (!stale) {
updateUSDPrice(networkId, null)
}
})
}
}, [globalBlockNumber, library, networkId, updateUSDPrice])
// update block number
useEffect(() => {
if ((networkId || networkId === 0) && library) {
if (library) {
let stale = false
function update() {