Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
added networkmonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaisc committed Aug 10, 2020
1 parent 0498e3a commit 16ab666
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ConsumeDdo } from './ConsumeDdo'

import WalletConnectProvider from '@walletconnect/web3-provider'
import Torus from '@toruslabs/torus-embed'
import { NetworkMonitor } from './NetworkMonitor'

// factory Address needs to be updated each time you deploy the contract on local network
const config = {
Expand Down Expand Up @@ -52,8 +53,9 @@ function App() {
}, [])

return (
<OceanProvider config={configRinkeby} web3ModalOpts={web3ModalOpts}>
<OceanProvider initialConfig={configRinkeby} web3ModalOpts={web3ModalOpts}>
<div className="container">
<NetworkMonitor/>
<div>
<Wallet />
</div>
Expand Down
29 changes: 29 additions & 0 deletions example/src/NetworkMonitor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import { useOcean } from '@oceanprotocol/react'
import { ConfigHelper } from '@oceanprotocol/lib'
import { useEffect } from 'react'

export function NetworkMonitor() {
const { connect, web3Provider } = useOcean()

const handleNetworkChanged = () => {
// const config = getOceanConfig(chainId)
const config = new ConfigHelper().getConfig('rinkeby')
connect(config)
}
useEffect(() => {

if (!web3Provider) return

web3Provider.on('chainChanged', handleNetworkChanged)

return () => {
web3Provider.removeListener('chainChanged', handleNetworkChanged)
}
}, [web3Provider])

return (
<>
</>
)
}
2 changes: 1 addition & 1 deletion src/hooks/useMetadata/useMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function useMetadata(did?: DID | string): UseMetadata {
setTitle(metadata.main.name)
}
init()
}, [])
}, [ocean])

return {
ddo,
Expand Down
7 changes: 3 additions & 4 deletions src/providers/OceanProvider/OceanProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ const OceanContext = createContext(null)
function OceanProvider({
initialConfig,
web3ModalOpts,
handleNetworkChanged,
children
}: {
initialConfig: Config
web3ModalOpts?: Partial<ICoreOptions>
handleNetworkChanged: (networkId: string | number) => Promise<void>
children: any
}): ReactElement {
const [web3, setWeb3] = useState<Web3 | undefined>()
Expand Down Expand Up @@ -148,11 +146,11 @@ function OceanProvider({

if (web3Provider !== undefined && web3Provider !== null) {
web3Provider.on('accountsChanged', handleAccountsChanged)
web3Provider.on('chainChanged', handleNetworkChanged)
// web3Provider.on('chainChanged', handleNetworkChanged)

return () => {
web3Provider.removeListener('accountsChanged', handleAccountsChanged)
web3Provider.removeListener('chainChanged', handleNetworkChanged)
// web3Provider.removeListener('chainChanged', handleNetworkChanged)
}
}
}, [web3Modal, web3Provider])
Expand All @@ -175,6 +173,7 @@ function OceanProvider({
logout,
refreshBalance
} as OceanProviderValue

}
>
{children}
Expand Down

0 comments on commit 16ab666

Please sign in to comment.