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

reload window upon network switch

This commit is contained in:
Max Berman 2020-01-09 18:40:42 +01:00
parent f853c880c5
commit 5e954f187a
8 changed files with 277 additions and 317 deletions

View File

@ -1,54 +1,39 @@
import React, { useState, useContext, useEffect } from 'react' import React, { useState, useContext, useEffect } from 'react'
import { urlq } from '../../utils/utils' import { urlq } from '../../utils/utils'
import { import { CONNECTIONS } from '../../config'
aquariusUri,
brizoUri,
brizoAddress,
nodeUri,
secretStoreUri,
verbose,
CONNECTIONS
} from '../../config'
import { User } from '../../context' import { User } from '../../context'
const networkUrlParam = urlq.get('network') || '' const networkUrlParam = urlq.get('network') || ''
const defaultNetworkConfig = {
nodeUri,
aquariusUri,
brizoUri,
brizoAddress,
secretStoreUri,
verbose
}
const getNetworkConfig = (network: string) => { const getNetworkConfig = (network: string) => {
console.log(network)
const index = Object.keys(CONNECTIONS).indexOf(network) const index = Object.keys(CONNECTIONS).indexOf(network)
// TypeScript doesn't let access CONNECTIONS[networkName] directly // TypeScript doesn't let access CONNECTIONS[networkName] directly
return index !== -1 return index !== -1
? Object.values(CONNECTIONS)[index] ? Object.values(CONNECTIONS)[index]
: CONNECTIONS.pacific // Use defaul config in case of mispelled URL params or : CONNECTIONS.pacific // Use default config in case of mispelled URL params or
} }
export const oceanConfig = export const oceanConfig =
networkUrlParam !== '' networkUrlParam !== ''
? getNetworkConfig(networkUrlParam) ? getNetworkConfig(networkUrlParam)
: defaultNetworkConfig : getNetworkConfig('pacific')
/* NETWORK SWITCHER */ /* NETWORK SWITCHER */
export function NetworkSwitcher() { export function NetworkSwitcher() {
const userContext = useContext(User) /*
const switchNetwork = (networkName: string): any => {
const idx = Object.keys(CONNECTIONS).indexOf(networkName)
userContext.switchNetwork(networkName, getNetworkConfig(networkName))
}
useEffect(() => { useEffect(() => {
if (networkUrlParam !== '') { if (networkUrlParam !== '') {
switchNetwork(networkUrlParam) switchNetwork(networkUrlParam)
} }
}, []) }, [])
*/
const userContext = useContext(User)
const switchNetwork = (networkName: string): any =>
// Force page to get refreshed
(window.location.href = `${window.location.origin}?network=${networkName}`)
//userContext.switchNetwork(networkName, getNetworkConfig(networkName))
return ( return (
<div> <div>
@ -66,7 +51,11 @@ export function NetworkSwitcher() {
: '' : ''
}} }}
> >
<span style={{ textTransform: 'capitalize' }}> <span
style={{
textTransform: 'capitalize'
}}
>
{networkName} {networkName}
</span> </span>
</li> </li>

View File

@ -5,36 +5,24 @@ import styles from './VersionTable.module.scss'
import VersionNumber from './VersionNumber' import VersionNumber from './VersionNumber'
import { useParams } from 'react-router-dom' import { useParams } from 'react-router-dom'
import { // const commonsConfig = {
serviceUri, // serviceUri,
nodeUri, // nodeUri,
aquariusUri, // aquariusUri,
brizoUri, // brizoUri,
brizoAddress, // brizoAddress,
secretStoreUri, // secretStoreUri,
faucetUri, // faucetUri
CONNECTIONS // }
} from '../../../config'
import { NetworkSwitcher, oceanConfig } from '../../molecules/NetworkSwitcher'
const commonsConfig = {
serviceUri,
nodeUri,
aquariusUri,
brizoUri,
brizoAddress,
secretStoreUri,
faucetUri
}
export const VersionTableContracts = ({ export const VersionTableContracts = ({
contracts, contracts,
network, network,
keeperVersion keeperVersion
}: { }: {
contracts: { contracts: { [contractName: string]: string }
[contractName: string]: string
}
network: string network: string
keeperVersion?: string keeperVersion?: string
}) => ( }) => (
@ -45,7 +33,10 @@ export const VersionTableContracts = ({
<strong>Keeper Contracts</strong> <strong>Keeper Contracts</strong>
</td> </td>
<td> <td>
<VersionNumber name="Keeper Contracts" version={keeperVersion} /> <VersionNumber
name="Keeper Contracts"
version={keeperVersion}
/>
</td> </td>
</tr> </tr>
{contracts && {contracts &&
@ -54,11 +45,12 @@ export const VersionTableContracts = ({
.sort((a, b) => a.localeCompare(b)) .sort((a, b) => a.localeCompare(b))
.map(key => { .map(key => {
const submarineLink = `https://submarine.${ const submarineLink = `https://submarine.${
network === 'pacific' ? 'oceanprotocol' : `${network}.dev-ocean` network === 'pacific'
? 'oceanprotocol'
: `${network}.dev-ocean`
}.com/address/${contracts[key]}` }.com/address/${contracts[key]}`
return ( return (
<tr key={key}> <tr key={key}>
<td> <td>
<code className={styles.label}>{key}</code> <code className={styles.label}>{key}</code>
@ -78,7 +70,7 @@ export const VersionTableContracts = ({
export const VersionTableCommons = () => ( export const VersionTableCommons = () => (
<table> <table>
<tbody> <tbody>
{Object.entries(commonsConfig).map(([key, value]) => ( {Object.entries(oceanConfig).map(([key, value]) => (
<tr key={key}> <tr key={key}>
<td> <td>
<code className={styles.label}>{key}</code> <code className={styles.label}>{key}</code>

View File

@ -8,12 +8,17 @@ import axios from 'axios'
import { version } from '../../../../package.json' import { version } from '../../../../package.json'
import styles from './index.module.scss' import styles from './index.module.scss'
import { nodeUri, faucetUri } from '../../../config' //import { nodeUri, faucetUri } from '../../../config'
import { oceanConfig } from '../../molecules/NetworkSwitcher'
import { User, Market } from '../../../context' import { User, Market } from '../../../context'
import VersionTable from './VersionTable' import VersionTable from './VersionTable'
import VersionStatus from './VersionStatus' import VersionStatus from './VersionStatus'
const { nodeUri, faucetUri } = oceanConfig
interface VersionNumbersProps { interface VersionNumbersProps {
minimal?: boolean minimal?: boolean
account: string account: string
@ -91,8 +96,8 @@ export default class VersionNumbers extends PureComponent<
// Workaround: Using account prop instead of getting it from // Workaround: Using account prop instead of getting it from
// context to be able to compare. Cause there is no `prevContext`. // context to be able to compare. Cause there is no `prevContext`.
if (prevProps.account !== this.props.account) { if (prevProps.account !== this.props.account) {
this.getOceanVersions() await this.getOceanVersions()
this.getFaucetVersion() await this.getFaucetVersion()
} }
} }

View File

@ -25,22 +25,6 @@ export const ipfsNodeUri =
// //
// OCEAN REMOTE CONNECTIONS // OCEAN REMOTE CONNECTIONS
// //
export const nodeUri =
process.env.REACT_APP_NODE_URI || 'https://pacific.oceanprotocol.com'
export const aquariusUri =
process.env.REACT_APP_AQUARIUS_URI ||
'https://aquarius.commons.oceanprotocol.com'
export const brizoUri =
process.env.REACT_APP_BRIZO_URI || 'https://brizo.commons.oceanprotocol.com'
export const brizoAddress =
process.env.REACT_APP_BRIZO_ADDRESS ||
'0x008c25ed3594e094db4592f4115d5fa74c4f41ea'
export const secretStoreUri =
process.env.REACT_APP_SECRET_STORE_URI ||
'https://secret-store.oceanprotocol.com'
export const faucetUri =
process.env.REACT_APP_FAUCET_URI || 'https://faucet.oceanprotocol.com'
export const CONNECTIONS = { export const CONNECTIONS = {
pacific: { pacific: {
nodeUri: 'https://pacific.oceanprotocol.com', nodeUri: 'https://pacific.oceanprotocol.com',
@ -48,8 +32,7 @@ export const CONNECTIONS = {
brizoUri: 'https://brizo.commons.oceanprotocol.com', brizoUri: 'https://brizo.commons.oceanprotocol.com',
brizoAddress: '0x008c25ed3594e094db4592f4115d5fa74c4f41ea', brizoAddress: '0x008c25ed3594e094db4592f4115d5fa74c4f41ea',
secretStoreUri: 'https://secret-store.oceanprotocol.com', secretStoreUri: 'https://secret-store.oceanprotocol.com',
faucetUri: 'https://faucet.oceanprotocol.com', faucetUri: 'https://faucet.oceanprotocol.com'
verbose: true
}, },
nile: { nile: {
nodeUri: 'https://nile.dev-ocean.com', nodeUri: 'https://nile.dev-ocean.com',
@ -57,8 +40,7 @@ export const CONNECTIONS = {
brizoUri: 'https://brizo.nile.dev-ocean.com', brizoUri: 'https://brizo.nile.dev-ocean.com',
brizoAddress: '0x4aaab179035dc57b35e2ce066919048686f82972', brizoAddress: '0x4aaab179035dc57b35e2ce066919048686f82972',
secretStoreUri: 'https://secret-store.nile.dev-ocean.com', secretStoreUri: 'https://secret-store.nile.dev-ocean.com',
faucetUri: 'https://faucet.nile.dev-ocean.com', faucetUri: 'https://faucet.nile.dev-ocean.com'
verbose: true
}, },
duero: { duero: {
nodeUri: 'https://duero.dev-ocean.com', nodeUri: 'https://duero.dev-ocean.com',
@ -66,7 +48,6 @@ export const CONNECTIONS = {
brizoUri: 'https://brizo.duero.dev-ocean.com', brizoUri: 'https://brizo.duero.dev-ocean.com',
brizoAddress: '0x9d4ed58293f71122ad6a733c1603927a150735d0', brizoAddress: '0x9d4ed58293f71122ad6a733c1603927a150735d0',
secretStoreUri: 'https://secret-store.duero.dev-ocean.com', secretStoreUri: 'https://secret-store.duero.dev-ocean.com',
faucetUri: 'https://faucet.duero.dev-ocean.com', faucetUri: 'https://faucet.duero.dev-ocean.com'
verbose: true
} }
} }

View File

@ -1,10 +1,11 @@
import Web3 from 'web3' import Web3 from 'web3'
import HDWalletProvider from '@truffle/hdwallet-provider' import HDWalletProvider from '@truffle/hdwallet-provider'
import { nodeUri } from '../config' import { oceanConfig } from '../components/molecules/NetworkSwitcher'
import { requestFromFaucet } from '../ocean' import { requestFromFaucet } from '../ocean'
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
const bip39 = require('bip39') const bip39 = require('bip39')
const { nodeUri } = oceanConfig
export class BurnerWalletProvider { export class BurnerWalletProvider {
private web3: Web3 private web3: Web3
@ -30,9 +31,9 @@ export class BurnerWalletProvider {
} else { } else {
mnemonic = bip39.generateMnemonic() mnemonic = bip39.generateMnemonic()
localStorage.setItem('seedphrase', mnemonic) localStorage.setItem('seedphrase', mnemonic)
localStorage.setItem('logType', 'BurnerWallet')
} }
localStorage.setItem('logType', 'BurnerWallet')
const provider = new HDWalletProvider(mnemonic, nodeUri, 0, 1) const provider = new HDWalletProvider(mnemonic, nodeUri, 0, 1)
this.web3 = new Web3(provider as any) this.web3 = new Web3(provider as any)
const accounts = await this.web3.eth.getAccounts() const accounts = await this.web3.eth.getAccounts()

View File

@ -12,17 +12,15 @@ import {
oceanConfig oceanConfig
} from '../components/molecules/NetworkSwitcher' } from '../components/molecules/NetworkSwitcher'
console.log({ oceanConfig })
const { nodeUri } = oceanConfig const { nodeUri } = oceanConfig
console.log(oceanConfig)
const POLL_ACCOUNTS = 1000 // every 1s const POLL_ACCOUNTS = 1000 // every 1s
const POLL_NETWORK = POLL_ACCOUNTS * 60 // every 1 min const POLL_NETWORK = POLL_ACCOUNTS * 60 // every 1 min
const DEFAULT_WEB3 = new Web3(new Web3.providers.HttpProvider(nodeUri)) // default web3 const DEFAULT_WEB3 = new Web3(new Web3.providers.HttpProvider(nodeUri)) // default web3
const networkUrlParam = urlq.get('network') || '' const networkUrlParam = urlq.get('network') || ''
console.log({ nodeUri })
interface UserProviderState { interface UserProviderState {
isLogged: boolean isLogged: boolean
isBurner: boolean isBurner: boolean
@ -74,20 +72,12 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
} }
private switchNetwork = async (network: string, config: Config) => { private switchNetwork = async (network: string, config: Config) => {
console.log({ network, config, oceanConfig }) this.setState({ network }, async () => {
const nodeUrl: any = config.nodeUri
this.setState(
{
network,
web3: new Web3(new Web3.providers.HttpProvider(nodeUrl))
},
() => {
this.loadOcean({ this.loadOcean({
web3Provider: this.state.web3, web3Provider: this.state.web3,
...config ...config
}) })
} })
)
} }
private loginMetamask = async () => { private loginMetamask = async () => {
@ -221,7 +211,6 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
private fetchAccounts = async () => { private fetchAccounts = async () => {
const { ocean, isLogged } = this.state const { ocean, isLogged } = this.state
if (isLogged) { if (isLogged) {
let accounts let accounts
@ -237,9 +226,10 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
accounts = await ocean.accounts.list() accounts = await ocean.accounts.list()
//console.log('fetch', accounts)
if (accounts.length > 0) { if (accounts.length > 0) {
const account = await accounts[0].getId() const account = await accounts[0].getId()
if (account !== this.state.account) { if (account !== this.state.account) {
this.setState({ this.setState({
account, account,

View File

@ -1,5 +1,6 @@
import { Ocean, Logger, Config } from '@oceanprotocol/squid' import { Ocean, Logger, Config } from '@oceanprotocol/squid'
import { faucetUri } from './config' import { oceanConfig } from './components/molecules/NetworkSwitcher'
const { faucetUri } = oceanConfig
export async function provideOcean(config: Config) { export async function provideOcean(config: Config) {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)

View File

@ -47,13 +47,13 @@ class Search extends PureComponent<SearchProps, SearchState> {
const { text, page, categories } = queryString.parse(search) const { text, page, categories } = queryString.parse(search)
if (text) { if (text) {
await this.setState({ this.setState({
searchTerm: decodeURIComponent(`${text}`) searchTerm: decodeURIComponent(`${text}`)
}) })
} }
if (categories) { if (categories) {
await this.setState({ this.setState({
searchCategories: decodeURIComponent(`${categories}`) searchCategories: decodeURIComponent(`${categories}`)
}) })
} }
@ -61,7 +61,7 @@ class Search extends PureComponent<SearchProps, SearchState> {
// switch to respective page if query string is present // switch to respective page if query string is present
if (page) { if (page) {
const currentPage = Number(page) const currentPage = Number(page)
await this.setState({ currentPage }) this.setState({ currentPage })
} }
this.searchAssets() this.searchAssets()
@ -143,7 +143,8 @@ class Search extends PureComponent<SearchProps, SearchState> {
{totalResults} results for{' '} {totalResults} results for{' '}
<span> <span>
{decodeURIComponent( {decodeURIComponent(
this.state.searchTerm || this.state.searchCategories this.state.searchTerm ||
this.state.searchCategories
)} )}
</span> </span>
</h2> </h2>