1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

Ocean network detection, remove isOceanNetwork

This commit is contained in:
Matthias Kretschmann 2019-07-11 15:45:49 +02:00
parent 4f051deefa
commit e488b9f001
Signed by: m
GPG Key ID: 606EEEF3C479A91F
15 changed files with 108 additions and 75 deletions

View File

@ -4,7 +4,6 @@ const userMock = {
isLogged: false, isLogged: false,
isLoading: false, isLoading: false,
isBurner: false, isBurner: false,
isOceanNetwork: false,
account: '', account: '',
web3: {}, web3: {},
...oceanMock, ...oceanMock,
@ -20,7 +19,6 @@ const userMockConnected = {
isLogged: true, isLogged: true,
isLoading: false, isLoading: false,
isBurner: false, isBurner: false,
isOceanNetwork: true,
account: '0xxxxxx', account: '0xxxxxx',
web3: {}, web3: {},
...oceanMock, ...oceanMock,

View File

@ -1,6 +1,6 @@
import React from 'react' import React from 'react'
import cx from 'classnames' import cx from 'classnames'
import { User } from '../../../context' import { User, Market } from '../../../context'
import styles from './Indicator.module.scss' import styles from './Indicator.module.scss'
const Indicator = ({ const Indicator = ({
@ -19,13 +19,19 @@ const Indicator = ({
ref={forwardedRef} ref={forwardedRef}
> >
<User.Consumer> <User.Consumer>
{states => {user => (
!states.isLogged || !states.isOceanNetwork ? ( <Market.Consumer>
<span className={styles.statusIndicatorCloseEnough} /> {market =>
) : states.isLogged ? ( !user.isLogged || !market.networkMatch ? (
<span className={styles.statusIndicatorActive} /> <span
) : null className={styles.statusIndicatorCloseEnough}
} />
) : user.isLogged ? (
<span className={styles.statusIndicatorActive} />
) : null
}
</Market.Consumer>
)}
</User.Consumer> </User.Consumer>
</div> </div>
) )

View File

@ -38,7 +38,6 @@ describe('Popover', () => {
<User.Provider <User.Provider
value={{ value={{
...userMockConnected, ...userMockConnected,
isOceanNetwork: false,
network: '1' network: '1'
}} }}
> >

View File

@ -1,14 +1,16 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import Account from '../../atoms/Account' import Account from '../../atoms/Account'
import { User } from '../../../context' import { User, Market } from '../../../context'
import styles from './Popover.module.scss' import styles from './Popover.module.scss'
export default class Popover extends PureComponent<{ export default class Popover extends PureComponent<{
forwardedRef?: (ref: HTMLElement | null) => void forwardedRef?: (ref: HTMLElement | null) => void
style?: React.CSSProperties style?: React.CSSProperties
}> { }> {
public static contextType = User
public render() { public render() {
const { account, balance, network, isOceanNetwork } = this.context const { account, balance, network } = this.context
return ( return (
<div <div
@ -41,16 +43,28 @@ export default class Popover extends PureComponent<{
</div> </div>
)} )}
<div className={styles.popoverInfoline}> <Market.Consumer>
{network && !isOceanNetwork {market => (
? 'Please connect to Custom RPC\n https://pacific.oceanprotocol.com' <div className={styles.popoverInfoline}>
: network && `Connected to ${network} network`} {network && !market.networkMatch
</div> ? `Please connect to Custom RPC
${
market.network === 'Pacific'
? 'https://pacific.oceanprotocol.com'
: market.network === 'Nile'
? 'https://nile.dev-ocean.com'
: market.network === 'Duero'
? 'https://duero.dev-ocean.com'
: 'http://localhost:8545'
}`
: network &&
`Connected to ${network} network`}
</div>
)}
</Market.Consumer>
</> </>
} }
</div> </div>
) )
} }
} }
Popover.contextType = User

View File

@ -57,10 +57,9 @@ export default class AssetsUser extends PureComponent<
} }
public render() { public render() {
const { account, isOceanNetwork } = this.context const { account } = this.context
return ( return (
isOceanNetwork &&
account && ( account && (
<div className={styles.assetsUser}> <div className={styles.assetsUser}>
{this.props.recent && ( {this.props.recent && (

View File

@ -16,9 +16,7 @@ describe('Web3message', () => {
it('renders with wrongNetwork message', () => { it('renders with wrongNetwork message', () => {
const { container } = render( const { container } = render(
<User.Provider <User.Provider value={{ ...userMockConnected }}>
value={{ ...userMockConnected, isOceanNetwork: false }}
>
<Web3message extended /> <Web3message extended />
</User.Provider> </User.Provider>
) )
@ -29,7 +27,7 @@ describe('Web3message', () => {
it('renders with noAccount message', () => { it('renders with noAccount message', () => {
const { container } = render( const { container } = render(
<User.Provider value={{ ...userMock, isOceanNetwork: true }}> <User.Provider value={{ ...userMock }}>
<Web3message extended /> <Web3message extended />
</User.Provider> </User.Provider>
) )

View File

@ -2,28 +2,51 @@ import React, { PureComponent } from 'react'
import Account from '../atoms/Account' import Account from '../atoms/Account'
import AccountStatus from '../molecules/AccountStatus' import AccountStatus from '../molecules/AccountStatus'
import styles from './Web3message.module.scss' import styles from './Web3message.module.scss'
import { User } from '../../context' import { User, Market } from '../../context'
import content from '../../data/web3message.json' import content from '../../data/web3message.json'
export default class Web3message extends PureComponent<{ extended?: boolean }> { export default class Web3message extends PureComponent<{ extended?: boolean }> {
public static contextType = User public static contextType = Market
private message = () => { private messageOceanNetwork = () =>
const { isOceanNetwork, isLogged, isBurner, network } = this.context this.context.network === 'Pacific'
return !isOceanNetwork && !isBurner
? content.wrongNetworkPacific ? content.wrongNetworkPacific
: !isLogged : this.context.network === 'Nile'
? content.noAccount ? content.wrongNetworkNile
: isBurner : this.context.network === 'Duero'
? content.hasBurnerWallet ? content.wrongNetworkDuero
: isLogged : content.wrongNetworkSpree
? content.hasMetaMaskWallet.replace('NETWORK', network)
: '' private Message = () => {
const { networkMatch, network } = this.context
return (
<User.Consumer>
{user => (
<em
dangerouslySetInnerHTML={{
__html:
!networkMatch && !user.isBurner
? this.messageOceanNetwork()
: !user.isLogged
? content.noAccount
: user.isBurner
? content.hasBurnerWallet
: user.isLogged
? content.hasMetaMaskWallet.replace(
'NETWORK',
network
)
: ''
}}
/>
)}
</User.Consumer>
)
} }
public render() { public render() {
const { network } = this.context const { networkMatch } = this.context
return ( return (
<div className={styles.message}> <div className={styles.message}>
@ -31,14 +54,10 @@ export default class Web3message extends PureComponent<{ extended?: boolean }> {
<Account /> <Account />
</div> </div>
{(network !== 'Pacific' || this.props.extended) && ( {(!networkMatch || this.props.extended) && (
<div className={styles.text}> <div className={styles.text}>
<AccountStatus className={styles.status} /> <AccountStatus className={styles.status} />
<em <this.Message />
dangerouslySetInnerHTML={{
__html: this.message()
}}
/>
</div> </div>
)} )}
</div> </div>

View File

@ -109,7 +109,7 @@ export default class AssetFile extends PureComponent<
public render() { public render() {
const { ddo, file } = this.props const { ddo, file } = this.props
const { isLoading, error, step } = this.state const { isLoading, error, step } = this.state
const { isLogged, isOceanNetwork } = this.context const { isLogged } = this.context
const { index, contentType, contentLength } = file const { index, contentType, contentLength } = file
return ( return (
@ -145,7 +145,7 @@ export default class AssetFile extends PureComponent<
className={styles.buttonMain} className={styles.buttonMain}
// weird 0 hack so TypeScript is happy // weird 0 hack so TypeScript is happy
onClick={() => this.purchaseAsset(ddo, index || 0)} onClick={() => this.purchaseAsset(ddo, index || 0)}
disabled={!isLogged || !isOceanNetwork} disabled={!isLogged}
name="Download" name="Download"
> >
Get file Get file

View File

@ -19,9 +19,7 @@ export default class AssetFilesDetails extends PureComponent<{
<AssetFile key={file.index} ddo={ddo} file={file} /> <AssetFile key={file.index} ddo={ddo} file={file} />
))} ))}
</div> </div>
{(!this.context.isOceanNetwork || !this.context.isLogged) && ( <Web3message />
<Web3message />
)}
</> </>
) : ( ) : (
<div>No files attached.</div> <div>No files attached.</div>

View File

@ -1,6 +1,6 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { Logger, Ocean } from '@oceanprotocol/squid' import { Logger, Ocean } from '@oceanprotocol/squid'
import { Market } from '.' import { Market, User } from '.'
import formPublish from '../data/form-publish.json' import formPublish from '../data/form-publish.json'
const categories = const categories =
@ -17,19 +17,25 @@ interface MarketProviderState {
totalAssets: number totalAssets: number
categories: string[] categories: string[]
network: string network: string
networkMatch: boolean
} }
export default class MarketProvider extends PureComponent< export default class MarketProvider extends PureComponent<
MarketProviderProps, MarketProviderProps,
MarketProviderState MarketProviderState
> { > {
public static contextType = User
public state = { public state = {
totalAssets: 0, totalAssets: 0,
categories, categories,
network: 'Pacific' network: 'Pacific',
networkMatch: false
} }
public async componentDidMount() {} public async componentDidMount() {
await this.checkCorrectUserNetwork()
}
public async componentDidUpdate(prevProps: any) { public async componentDidUpdate(prevProps: any) {
// Using ocean prop instead of getting it from context to be able to compare. // Using ocean prop instead of getting it from context to be able to compare.
@ -37,6 +43,7 @@ export default class MarketProvider extends PureComponent<
if (prevProps.ocean !== this.props.ocean) { if (prevProps.ocean !== this.props.ocean) {
await this.getTotalAssets() await this.getTotalAssets()
await this.getMarketNetwork() await this.getMarketNetwork()
await this.checkCorrectUserNetwork()
} }
} }
@ -72,6 +79,14 @@ export default class MarketProvider extends PureComponent<
} }
} }
private async checkCorrectUserNetwork() {
if (this.context.network === this.state.network) {
this.setState({ networkMatch: true })
} else {
this.setState({ networkMatch: false })
}
}
public render() { public render() {
return ( return (
<Market.Provider value={this.state}> <Market.Provider value={this.state}>

View File

@ -51,7 +51,6 @@ interface UserProviderState {
isBurner: boolean isBurner: boolean
isWeb3Capable: boolean isWeb3Capable: boolean
isLoading: boolean isLoading: boolean
isOceanNetwork: boolean
account: string account: string
balance: { balance: {
eth: number eth: number
@ -110,7 +109,6 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
isBurner: false, isBurner: false,
isWeb3Capable: Boolean(window.web3 || window.ethereum), isWeb3Capable: Boolean(window.web3 || window.ethereum),
isLoading: true, isLoading: true,
isOceanNetwork: false,
balance: { balance: {
eth: 0, eth: 0,
ocn: 0 ocn: 0
@ -205,13 +203,13 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
} }
private fetchAccounts = async () => { private fetchAccounts = async () => {
const { ocean, isLogged, isOceanNetwork } = this.state const { ocean, isLogged } = this.state
if (isLogged) { if (isLogged) {
let accounts let accounts
// Modern dapp browsers // Modern dapp browsers
if (window.ethereum && !isLogged && isOceanNetwork) { if (window.ethereum && !isLogged) {
// simply set to empty, and have user click a button somewhere // simply set to empty, and have user click a button somewhere
// to initiate account unlocking // to initiate account unlocking
accounts = [] accounts = []
@ -250,16 +248,8 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
private fetchNetwork = async () => { private fetchNetwork = async () => {
const { ocean } = this.state const { ocean } = this.state
const network = await ocean.keeper.getNetworkName() const network = await ocean.keeper.getNetworkName()
const isPacific = network === 'Pacific' network !== this.state.network && this.setState({ network })
const isNile = network === 'Nile'
const isDuero = network === 'Duero'
const isSpree = network === 'Spree'
const isOceanNetwork = isPacific || isNile || isDuero || isSpree
network !== this.state.network &&
this.setState({ isOceanNetwork, network })
} }
public render() { public render() {

View File

@ -5,7 +5,6 @@ export const User = React.createContext({
isBurner: false, isBurner: false,
isWeb3Capable: false, isWeb3Capable: false,
isLoading: false, isLoading: false,
isOceanNetwork: false,
account: '', account: '',
web3: {}, web3: {},
ocean: {}, ocean: {},
@ -29,5 +28,6 @@ export const User = React.createContext({
export const Market = React.createContext({ export const Market = React.createContext({
totalAssets: 0, totalAssets: 0,
categories: [''], categories: [''],
network: '' network: '',
networkMatch: false
}) })

View File

@ -5,5 +5,6 @@
"wrongNetworkPacific": "Not connected to Pacific network. Please connect in MetaMask with Custom RPC <code>https://pacific.oceanprotocol.com</code>", "wrongNetworkPacific": "Not connected to Pacific network. Please connect in MetaMask with Custom RPC <code>https://pacific.oceanprotocol.com</code>",
"wrongNetworkNile": "Not connected to Nile network. Please connect in MetaMask with Custom RPC <code>https://nile.dev-ocean.com</code>", "wrongNetworkNile": "Not connected to Nile network. Please connect in MetaMask with Custom RPC <code>https://nile.dev-ocean.com</code>",
"wrongNetworkDuero": "Not connected to Duero network. Please connect in MetaMask with Custom RPC <code>https://duero.dev-ocean.com</code>", "wrongNetworkDuero": "Not connected to Duero network. Please connect in MetaMask with Custom RPC <code>https://duero.dev-ocean.com</code>",
"wrongNetworkSpree": "Not connected to Spree network. Please connect in MetaMask with Custom RPC <code>http://localhost:8545</code>",
"seedphrase": "You can use this seed phrase to import this burner wallet account into other wallets, e.g. MetaMask." "seedphrase": "You can use this seed phrase to import this burner wallet account into other wallets, e.g. MetaMask."
} }

View File

@ -99,9 +99,7 @@ export default class Faucet extends PureComponent<{}, FaucetState> {
<Button <Button
primary primary
onClick={() => this.getTokens(this.context.requestFromFaucet)} onClick={() => this.getTokens(this.context.requestFromFaucet)}
disabled={ disabled={!this.context.isLogged}
!this.context.isLogged || !this.context.isOceanNetwork
}
> >
Request Ether Request Ether
</Button> </Button>

View File

@ -10,9 +10,7 @@ export default class History extends Component {
return ( return (
<Route title="History"> <Route title="History">
<Content> <Content>
{(!this.context.isLogged || {!this.context.isLogged && <Web3message />}
!this.context.isOceanNetwork) && <Web3message />}
<AssetsUser list /> <AssetsUser list />
</Content> </Content>
</Route> </Route>