@ -158,8 +158,6 @@ if ($stakesPage.length) {
}
}
updating = true
updating = true
const firstMsg = ( state . currentBlockNumber === 0 )
store . dispatch ( { type : 'BLOCK_CREATED' , currentBlockNumber : msg . block _number } )
store . dispatch ( { type : 'BLOCK_CREATED' , currentBlockNumber : msg . block _number } )
// hide tooltip on tooltip triggering element reloading
// hide tooltip on tooltip triggering element reloading
@ -179,13 +177,9 @@ if ($stakesPage.length) {
msg . epoch _number > state . lastEpochNumber ||
msg . epoch _number > state . lastEpochNumber ||
msg . validator _set _apply _block !== state . validatorSetApplyBlock ||
msg . validator _set _apply _block !== state . validatorSetApplyBlock ||
( state . refreshInterval && msg . block _number >= state . refreshBlockNumber + state . refreshInterval ) ||
( state . refreshInterval && msg . block _number >= state . refreshBlockNumber + state . refreshInterval ) ||
accountChanged ( msg . account , state ) || msg . by _set _account
accountChanged ( msg . account , state ) ||
msg . by _set _account
) {
) {
if ( firstMsg ) {
// Don't refresh the page for the first load
// as it is already refreshed by the `initialize` function.
msg . dont _refresh _page = true
}
await reloadPoolList ( msg , store )
await reloadPoolList ( msg , store )
}
}
@ -205,7 +199,6 @@ if ($stakesPage.length) {
if ( ! store . getState ( ) . finishRequestResolve ) {
if ( ! store . getState ( ) . finishRequestResolve ) {
$refreshInformer . hide ( )
$refreshInformer . hide ( )
$stakesPage . fadeTo ( 0 , 0.5 )
$stakesPage . fadeTo ( 0 , 0.5 )
delete msg . dont _refresh _page // refresh anyway
await reloadPoolList ( msg , store )
await reloadPoolList ( msg , store )
}
}
} )
} )
@ -308,6 +301,28 @@ function accountChanged (account, state) {
return account !== state . account
return account !== state . account
}
}
async function getAccounts ( ) {
let accounts = [ ]
try {
accounts = await window . ethereum . request ( { method : 'eth_accounts' } )
} catch ( e ) {
console . error ( 'eth_accounts request failed. Make sure you are using the latest version of MetaMask' )
openErrorModal ( 'Get account' , 'Cannot get your account address. Make sure you are using the latest version of MetaMask' )
}
return accounts
}
async function getNetId ( web3 ) {
let netId = null
if ( ! window . ethereum . chainId ) {
console . error ( 'Cannot get chainId. Make sure you are using the latest MetaMask version' )
} else {
const { chainId } = window . ethereum
netId = web3 . utils . isHex ( chainId ) ? web3 . utils . hexToNumber ( chainId ) : chainId
}
return netId
}
function hideCurrentModal ( ) {
function hideCurrentModal ( ) {
const $modal = currentModal ( )
const $modal = currentModal ( )
if ( $modal ) $modal . modal ( 'hide' )
if ( $modal ) $modal . modal ( 'hide' )
@ -316,51 +331,63 @@ function hideCurrentModal () {
function initialize ( store ) {
function initialize ( store ) {
if ( window . ethereum ) {
if ( window . ethereum ) {
const web3 = new Web3 ( window . ethereum )
const web3 = new Web3 ( window . ethereum )
if ( window . ethereum . autoRefreshOnNetworkChange ) {
window . ethereum . autoRefreshOnNetworkChange = false
window . ethereum . autoRefreshOnNetworkChange = false
}
store . dispatch ( { type : 'WEB3_DETECTED' , web3 } )
store . dispatch ( { type : 'WEB3_DETECTED' , web3 } )
checkNetworkAndAccount ( store , web3 )
initNetworkAndAccount ( store , web3 )
window . ethereum . on ( 'chainChanged' , async ( chainId ) => {
const newNetId = web3 . utils . isHex ( chainId ) ? web3 . utils . hexToNumber ( chainId ) : chainId
setNetwork ( newNetId , store )
} )
window . ethereum . on ( 'accountsChanged' , async ( accs ) => {
const newAccount = accs && accs . length > 0 ? accs [ 0 ] . toLowerCase ( ) : null
if ( accountChanged ( newAccount , store . getState ( ) ) ) {
await setAccount ( newAccount , store )
}
} )
$stakesTop . on ( 'click' , '[data-selector="login-button"]' , loginByMetamask )
$stakesTop . on ( 'click' , '[data-selector="login-button"]' , loginByMetamask )
} else {
} else {
// We do the first load immediately if the latest version of MetaMask is not installed
refreshPageWrapper ( store )
refreshPageWrapper ( store )
}
}
}
}
async function checkNetworkAndAccount ( store , web3 ) {
async function initNetworkAndAccount ( store , web3 ) {
const networkId = await web3 . eth . net . getId ( )
const state = store . getState ( )
const state = store . getState ( )
let refresh = false
const networkId = await getNetId ( web3 )
if ( ! state . network || ( networkId !== state . network . id ) ) {
if ( ! state . network || ( networkId !== state . network . id ) ) {
setNetwork ( networkId , store )
setNetwork ( networkId , store )
refresh = true
}
}
const accounts = await web3 . eth . getAccounts ( )
const accounts = await getAccounts ( )
const account = accounts [ 0 ] ? accounts [ 0 ] . toLowerCase ( ) : null
const account = accounts [ 0 ] ? accounts [ 0 ] . toLowerCase ( ) : null
if ( accountChanged ( account , state ) && await setAccount ( account , store ) ) {
if ( accountChanged ( account , state ) ) {
refresh = false // because refreshing will be done by `onStakingUpdate`
await setAccount ( account , store )
}
// We don't call `refreshPageWrapper` in this case because it will be called
// by the `onStakingUpdate` function
if ( refresh ) {
} else {
await refreshPageWrapper ( store )
await refreshPageWrapper ( store )
}
}
setTimeout ( ( ) => {
checkNetworkAndAccount ( store , web3 )
} , 100 )
}
}
async function loginByMetamask ( ) {
async function loginByMetamask ( ) {
event . stopPropagation ( )
event . stopPropagation ( )
event . preventDefault ( )
event . preventDefault ( )
try {
try {
await window . ethereum . enable ( )
await window . ethereum . request ( { method : 'eth_requestAccounts' } )
} catch ( e ) {
} catch ( e ) {
console . log ( e )
console . log ( e )
console . error ( 'User denied account access' )
if ( e . code !== 4001 ) {
console . error ( 'eth_requestAccounts failed. Make sure you are using the latest MetaMask version' )
openErrorModal ( 'Request account access' , 'Cannot request access to your account in MetaMask. Make sure you are using the latest version of MetaMask' )
}
}
}
}
}
@ -394,9 +421,7 @@ async function reloadPoolList (msg, store) {
stakingTokenDefined : msg . staking _token _defined ,
stakingTokenDefined : msg . staking _token _defined ,
validatorSetApplyBlock : msg . validator _set _apply _block
validatorSetApplyBlock : msg . validator _set _apply _block
} )
} )
if ( ! msg . dont _refresh _page ) {
await refreshPageWrapper ( store )
await refreshPageWrapper ( store )
}
}
}
function resetFilterMy ( store ) {
function resetFilterMy ( store ) {
@ -417,11 +442,13 @@ function setAccount (account, store) {
store . getState ( ) . channel . push (
store . getState ( ) . channel . push (
'set_account' , account
'set_account' , account
) . receive ( 'ok' , ( ) => {
) . receive ( 'ok' , ( ) => {
if ( account ) {
$addressField . html ( `
$addressField . html ( `
< div data - placement = "bottom" data - toggle = "tooltip" title = "${account}" >
< div data - placement = "bottom" data - toggle = "tooltip" title = "${account}" >
$ { account }
$ { account }
< / d i v >
< / d i v >
` )
` )
}
hideCurrentModal ( )
hideCurrentModal ( )
resolve ( true )
resolve ( true )
} ) . receive ( 'error' , ( ) => {
} ) . receive ( 'error' , ( ) => {
@ -468,7 +495,7 @@ function updateFilters (store, filterType) {
if ( filterType === 'my' && ! state . account ) {
if ( filterType === 'my' && ! state . account ) {
filterMy . prop ( 'checked' , false )
filterMy . prop ( 'checked' , false )
openWarningModal ( 'Unauthorized' , 'Please login with MetaMask' )
openWarningModal ( 'Unauthorized' , 'You are not logged in. Please login with the latest version of MetaMask' )
return
return
}
}
store . dispatch ( {
store . dispatch ( {