Injecting provider selectors in Header's layout

This commit is contained in:
apanizo 2018-08-28 14:22:13 +02:00
parent b1244a22fd
commit 66792477bb
3 changed files with 59 additions and 5 deletions

View File

@ -8,6 +8,9 @@ import selector from './selector'
type Props = { type Props = {
provider: string, provider: string,
fetchProvider: Function, fetchProvider: Function,
userAddress: string,
network: string,
connected: boolean,
} }
class Header extends React.PureComponent<Props> { class Header extends React.PureComponent<Props> {
@ -20,9 +23,18 @@ class Header extends React.PureComponent<Props> {
} }
render() { render() {
const { provider } = this.props const {
provider, userAddress, network, connected,
} = this.props
return ( return (
<Layout provider={provider} reloadWallet={this.reloadWallet} /> <Layout
provider={provider}
reloadWallet={this.reloadWallet}
userAddress={userAddress}
network={network}
connected={connected}
/>
) )
} }
} }

View File

@ -1,7 +1,10 @@
// @flow // @flow
import { createStructuredSelector } from 'reselect' import { createStructuredSelector } from 'reselect'
import { providerNameSelector } from '~/logic/wallets/store/selectors' import { providerNameSelector, userAccountSelector, networkSelector, connectedSelector } from '~/logic/wallets/store/selectors'
export default createStructuredSelector({ export default createStructuredSelector({
provider: providerNameSelector, provider: providerNameSelector,
userAddress: userAccountSelector,
network: networkSelector,
connected: connectedSelector,
}) })

View File

@ -4,6 +4,36 @@ import Web3 from 'web3'
import type { ProviderProps } from '~/logic/wallets/store/model/provider' import type { ProviderProps } from '~/logic/wallets/store/model/provider'
import { promisify } from '~/utils/promisify' import { promisify } from '~/utils/promisify'
export const ETHEREUM_NETWORK = {
MAIN: 'MAIN',
MORDEN: 'MORDEN',
ROPSTEN: 'ROPSTEN',
RINKEBY: 'RINKEBY',
KOVAN: 'KOVAN',
UNKNOWN: 'UNKNOWN',
}
export const WALLET_PROVIDER = {
METAMASK: 'METAMASK',
PARITY: 'PARITY',
REMOTE: 'REMOTE',
UPORT: 'UPORT',
}
export const ETHEREUM_NETWORK_IDS = {
// $FlowFixMe
1: ETHEREUM_NETWORK.MAIN,
// $FlowFixMe
2: ETHEREUM_NETWORK.MORDEN,
// $FlowFixMe
3: ETHEREUM_NETWORK.ROPSTEN,
// $FlowFixMe
4: ETHEREUM_NETWORK.RINKEBY,
// $FlowFixMe
42: ETHEREUM_NETWORK.KOVAN,
}
let web3 let web3
export const getWeb3 = () => web3 || new Web3(window.web3.currentProvider) export const getWeb3 = () => web3 || new Web3(window.web3.currentProvider)
@ -19,10 +49,16 @@ const getAccountFrom: Function = async (web3Provider): Promise<string | null> =>
return accounts && accounts.length > 0 ? accounts[0] : null return accounts && accounts.length > 0 ? accounts[0] : null
} }
const getNetworkIdFrom = async (web3Provider) => {
const networkId = await web3Provider.version.network
return networkId
}
export const getProviderInfo: Function = async (): Promise<ProviderProps> => { export const getProviderInfo: Function = async (): Promise<ProviderProps> => {
if (typeof window.web3 === 'undefined') { if (typeof window.web3 === 'undefined') {
return { return {
name: '', available: false, loaded: false, account: '', name: '', available: false, loaded: false, account: '', network: 0,
} }
} }
@ -34,8 +70,10 @@ export const getProviderInfo: Function = async (): Promise<ProviderProps> => {
console.log('Injected web3 detected.') console.log('Injected web3 detected.')
} }
const name = isMetamask(web3) ? 'METAMASK' : 'UNKNOWN' const name = isMetamask(web3) ? WALLET_PROVIDER.METAMASK : 'UNKNOWN'
const account = await getAccountFrom(web3) const account = await getAccountFrom(web3)
const network = await getNetworkIdFrom(web3)
const available = account !== null const available = account !== null
return { return {
@ -43,6 +81,7 @@ export const getProviderInfo: Function = async (): Promise<ProviderProps> => {
available, available,
loaded: true, loaded: true,
account, account,
network,
} }
} }