mirror of
https://github.com/oceanprotocol/commons.git
synced 2023-03-15 18:03:00 +01:00
WIP: decouple handling oceanConfig with burner wallet & metamask
This commit is contained in:
parent
9125186590
commit
4b9418e94a
@ -1,8 +1,29 @@
|
|||||||
import React, { useState, useContext, useEffect, useRef } from 'react'
|
import React, { useState, useContext, useEffect, useRef } from 'react'
|
||||||
|
import Web3 from 'web3'
|
||||||
import { urlq, getObjByKey } from '../../utils/utils'
|
import { urlq, getObjByKey } from '../../utils/utils'
|
||||||
import { CONNECTIONS } from '../../config'
|
import { CONNECTIONS } from '../../config'
|
||||||
|
import { MetamaskProvider } from '../../context/MetamaskProvider'
|
||||||
import { User } from '../../context'
|
import { User } from '../../context'
|
||||||
import styles from './NetworkSwitcher.module.scss'
|
import styles from './NetworkSwitcher.module.scss'
|
||||||
|
import { Ocean, Account, Config } from '@oceanprotocol/squid'
|
||||||
|
|
||||||
|
const isMetaMask = localStorage.getItem('logType') === 'Metamask'
|
||||||
|
|
||||||
|
export const getCurrentNetConfigFromMetamask: any = async () => {
|
||||||
|
const NETWORKS = {
|
||||||
|
2199: 'duero',
|
||||||
|
846353: 'pacific',
|
||||||
|
8995: 'nile'
|
||||||
|
}
|
||||||
|
const metamaskProvider = new MetamaskProvider()
|
||||||
|
const web3 = metamaskProvider.getProvider()
|
||||||
|
const netId = await web3.eth.net.getId()
|
||||||
|
const netName = (NETWORKS as any)[netId]
|
||||||
|
const netConfig = (CONNECTIONS as any)[netName]
|
||||||
|
return { netId, web3, netName, netConfig }
|
||||||
|
}
|
||||||
|
|
||||||
|
//getCurrentNetConfigFromMetamask().then((config: any) => console.log(config))
|
||||||
|
|
||||||
const defaultNetwork = process.env.REACT_APP_OCEAN_NETWORK || 'pacific'
|
const defaultNetwork = process.env.REACT_APP_OCEAN_NETWORK || 'pacific'
|
||||||
const netUrlParam: string = urlq.get('network') || defaultNetwork
|
const netUrlParam: string = urlq.get('network') || defaultNetwork
|
||||||
@ -16,6 +37,7 @@ export function NetworkSwitcher() {
|
|||||||
const [isToggled, setIsToggled] = useState(false)
|
const [isToggled, setIsToggled] = useState(false)
|
||||||
|
|
||||||
const handleToggle = (e: any) => {
|
const handleToggle = (e: any) => {
|
||||||
|
if (isMetaMask) return
|
||||||
// avoid click event firing twice
|
// avoid click event firing twice
|
||||||
if (e.which === 1) {
|
if (e.which === 1) {
|
||||||
const isClickedInside = node.current.contains(e.target)
|
const isClickedInside = node.current.contains(e.target)
|
||||||
|
@ -15,6 +15,8 @@ import VersionStatus from './VersionStatus'
|
|||||||
|
|
||||||
import { NetworkSwitcher, oceanConfig } from '../../molecules/NetworkSwitcher'
|
import { NetworkSwitcher, oceanConfig } from '../../molecules/NetworkSwitcher'
|
||||||
|
|
||||||
|
console.log(oceanConfig)
|
||||||
|
|
||||||
const { nodeUri, faucetUri } = oceanConfig
|
const { nodeUri, faucetUri } = oceanConfig
|
||||||
|
|
||||||
interface VersionNumbersProps {
|
interface VersionNumbersProps {
|
||||||
|
@ -20,7 +20,7 @@ export default class Web3message extends PureComponent<{ extended?: boolean }> {
|
|||||||
private Message = () => {
|
private Message = () => {
|
||||||
const { networkMatch, network } = this.context
|
const { networkMatch, network } = this.context
|
||||||
|
|
||||||
console.log(networkMatch)
|
console.log({ networkMatch })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<User.Consumer>
|
<User.Consumer>
|
||||||
|
@ -6,9 +6,14 @@ import { provideOcean, requestFromFaucet, FaucetResponse } from '../ocean'
|
|||||||
import MarketProvider from './MarketProvider'
|
import MarketProvider from './MarketProvider'
|
||||||
import { MetamaskProvider } from './MetamaskProvider'
|
import { MetamaskProvider } from './MetamaskProvider'
|
||||||
import { BurnerWalletProvider } from './BurnerWalletProvider'
|
import { BurnerWalletProvider } from './BurnerWalletProvider'
|
||||||
import { urlq } from '../utils/utils'
|
import { urlq, getObjByKey } from '../utils/utils'
|
||||||
import { oceanConfig } from '../components/molecules/NetworkSwitcher'
|
import {
|
||||||
|
oceanConfig,
|
||||||
|
getCurrentNetConfigFromMetamask
|
||||||
|
} from '../components/molecules/NetworkSwitcher'
|
||||||
|
import { CONNECTIONS } from '../config'
|
||||||
|
|
||||||
|
//const nodeUri = oceanConfig.then((c: any) => c.nodeUri)
|
||||||
const { nodeUri } = oceanConfig
|
const { nodeUri } = oceanConfig
|
||||||
|
|
||||||
const POLL_ACCOUNTS = 1000 // every 1s
|
const POLL_ACCOUNTS = 1000 // every 1s
|
||||||
@ -16,8 +21,6 @@ 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
|
||||||
@ -69,6 +72,7 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private switchNetwork = async (network: string, config: Config) => {
|
private switchNetwork = async (network: string, config: Config) => {
|
||||||
|
console.log('TEST')
|
||||||
this.setState({ network }, async () => {
|
this.setState({ network }, async () => {
|
||||||
this.loadOcean({
|
this.loadOcean({
|
||||||
web3Provider: this.state.web3,
|
web3Provider: this.state.web3,
|
||||||
@ -174,30 +178,28 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
|
|||||||
|
|
||||||
private bootstrap = async () => {
|
private bootstrap = async () => {
|
||||||
const logType = localStorage.getItem('logType')
|
const logType = localStorage.getItem('logType')
|
||||||
const metamaskProvider = new MetamaskProvider()
|
|
||||||
|
|
||||||
switch (logType) {
|
switch (logType) {
|
||||||
case 'Metamask':
|
case 'Metamask':
|
||||||
if (
|
await getCurrentNetConfigFromMetamask()
|
||||||
(await metamaskProvider.isAvailable()) &&
|
.then((obj: any) => {
|
||||||
(await metamaskProvider.isLogged())
|
console.log(obj)
|
||||||
) {
|
const { netConfig, web3, netName } = obj
|
||||||
const web3 = metamaskProvider.getProvider()
|
this.setState(
|
||||||
this.setState(
|
{
|
||||||
{
|
isLogged: true,
|
||||||
isLogged: true,
|
web3,
|
||||||
web3
|
network: netName
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
this.loadOcean({
|
this.loadOcean({
|
||||||
web3Provider: this.state.web3,
|
web3Provider: web3,
|
||||||
...oceanConfig
|
...netConfig
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else {
|
})
|
||||||
this.loadDefaultWeb3()
|
.catch(() => this.loadDefaultWeb3())
|
||||||
}
|
|
||||||
break
|
break
|
||||||
case 'BurnerWallet':
|
case 'BurnerWallet':
|
||||||
this.loginBurnerWallet()
|
this.loginBurnerWallet()
|
||||||
@ -261,11 +263,14 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
|
|||||||
let network = 'Unknown'
|
let network = 'Unknown'
|
||||||
if (ocean.keeper) {
|
if (ocean.keeper) {
|
||||||
network = await ocean.keeper.getNetworkName()
|
network = await ocean.keeper.getNetworkName()
|
||||||
|
const networkId = await ocean.keeper.getNetworkId()
|
||||||
|
const netId = await this.state.web3.eth.net.getId()
|
||||||
|
|
||||||
|
console.log('Network Name:', network, networkId, netId)
|
||||||
}
|
}
|
||||||
if (network !== this.state.network) {
|
if (network !== this.state.network) {
|
||||||
this.setState({ network })
|
this.setState({ network }, () => {})
|
||||||
}
|
}
|
||||||
console.log(await ocean.keeper.getNetworkName())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public render() {
|
public render() {
|
||||||
|
Loading…
Reference in New Issue
Block a user