@ -15,11 +15,13 @@ import { openMakeStakeModal } from './stakes/make_stake'
import { openMoveStakeModal } from './stakes/move_stake'
import { openMoveStakeModal } from './stakes/move_stake'
import { openWithdrawStakeModal } from './stakes/withdraw_stake'
import { openWithdrawStakeModal } from './stakes/withdraw_stake'
import { openClaimWithdrawalModal } from './stakes/claim_withdrawal'
import { openClaimWithdrawalModal } from './stakes/claim_withdrawal'
import { openWarningModal } from '../lib/modals'
export const initialState = {
export const initialState = {
channel : null ,
channel : null ,
web3 : null ,
web3 : null ,
account : null ,
account : null ,
network : null ,
stakingContract : null ,
stakingContract : null ,
blockRewardContract : null ,
blockRewardContract : null ,
tokenDecimals : 0 ,
tokenDecimals : 0 ,
@ -30,6 +32,9 @@ export const initialState = {
stakingAllowed : false
stakingAllowed : false
}
}
// 100 - id of xDai network, 101 - id of xDai test network
export const allowedNetworkIds = [ 100 , 101 ]
export function reducer ( state = initialState , action ) {
export function reducer ( state = initialState , action ) {
switch ( action . type ) {
switch ( action . type ) {
case 'PAGE_LOAD' :
case 'PAGE_LOAD' :
@ -50,6 +55,14 @@ export function reducer (state = initialState, action) {
} )
} )
} )
} )
}
}
case 'NETWORK_UPDATED' : {
return Object . assign ( { } , state , {
network : action . network ,
additionalParams : Object . assign ( { } , state . additionalParams , {
network : action . network
} )
} )
}
case 'FILTERS_UPDATED' : {
case 'FILTERS_UPDATED' : {
return Object . assign ( { } , state , {
return Object . assign ( { } , state , {
additionalParams : Object . assign ( { } , state . additionalParams , {
additionalParams : Object . assign ( { } , state . additionalParams , {
@ -173,6 +186,11 @@ function initializeWeb3 (store) {
store . dispatch ( { type : 'WEB3_DETECTED' , web3 } )
store . dispatch ( { type : 'WEB3_DETECTED' , web3 } )
setInterval ( async function ( ) {
setInterval ( async function ( ) {
const networkId = await web3 . eth . net . getId ( )
if ( ! store . getState ( ) . network || ( networkId !== store . getState ( ) . network . id ) ) {
setNetwork ( networkId , store )
}
const accounts = await web3 . eth . getAccounts ( )
const accounts = await web3 . eth . getAccounts ( )
const account = accounts [ 0 ] ? accounts [ 0 ] . toLowerCase ( ) : null
const account = accounts [ 0 ] ? accounts [ 0 ] . toLowerCase ( ) : null
@ -191,6 +209,22 @@ function setAccount (account, store) {
refreshPage ( store )
refreshPage ( store )
}
}
function setNetwork ( networkId , store ) {
let network = {
id : networkId ,
authorized : false
}
if ( allowedNetworkIds . includes ( networkId ) ) {
network . authorized = true
} else {
openWarningModal ( 'Unauthorized' , 'Connect to the xDai Chain for staking.<br /> <a href="https://docs.xdaichain.com" target="_blank">Instructions</a>' )
}
store . dispatch ( { type : 'NETWORK_UPDATED' , network } )
refreshPage ( store )
}
async function loginByMetamask ( event ) {
async function loginByMetamask ( event ) {
event . stopPropagation ( )
event . stopPropagation ( )
event . preventDefault ( )
event . preventDefault ( )