mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
Merge pull request #73 from oceanprotocol/feature/config
config based on network
This commit is contained in:
commit
3d357646af
@ -9,6 +9,7 @@ import { ConsumeDdo } from './ConsumeDdo'
|
|||||||
|
|
||||||
import WalletConnectProvider from '@walletconnect/web3-provider'
|
import WalletConnectProvider from '@walletconnect/web3-provider'
|
||||||
import Torus from '@toruslabs/torus-embed'
|
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
|
// factory Address needs to be updated each time you deploy the contract on local network
|
||||||
const config = {
|
const config = {
|
||||||
@ -52,8 +53,9 @@ function App() {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<OceanProvider config={configRinkeby} web3ModalOpts={web3ModalOpts}>
|
<OceanProvider initialConfig={configRinkeby} web3ModalOpts={web3ModalOpts}>
|
||||||
<div className="container">
|
<div className="container">
|
||||||
|
<NetworkMonitor />
|
||||||
<div>
|
<div>
|
||||||
<Wallet />
|
<Wallet />
|
||||||
</div>
|
</div>
|
||||||
|
25
example/src/NetworkMonitor.tsx
Normal file
25
example/src/NetworkMonitor.tsx
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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 <></>
|
||||||
|
}
|
@ -59,7 +59,7 @@ function useMetadata(did?: DID | string): UseMetadata {
|
|||||||
setTitle(metadata.main.name)
|
setTitle(metadata.main.name)
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [])
|
}, [ocean])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ddo,
|
ddo,
|
||||||
|
@ -28,7 +28,7 @@ interface OceanProviderValue {
|
|||||||
balance: Balance
|
balance: Balance
|
||||||
chainId: number | undefined
|
chainId: number | undefined
|
||||||
status: ProviderStatus
|
status: ProviderStatus
|
||||||
connect: (opts?: Partial<ICoreOptions>) => Promise<void>
|
connect: (config: Config) => Promise<void>
|
||||||
logout: () => Promise<void>
|
logout: () => Promise<void>
|
||||||
refreshBalance: () => Promise<void>
|
refreshBalance: () => Promise<void>
|
||||||
}
|
}
|
||||||
@ -36,11 +36,11 @@ interface OceanProviderValue {
|
|||||||
const OceanContext = createContext(null)
|
const OceanContext = createContext(null)
|
||||||
|
|
||||||
function OceanProvider({
|
function OceanProvider({
|
||||||
config,
|
initialConfig,
|
||||||
web3ModalOpts,
|
web3ModalOpts,
|
||||||
children
|
children
|
||||||
}: {
|
}: {
|
||||||
config: Config
|
initialConfig: Config
|
||||||
web3ModalOpts?: Partial<ICoreOptions>
|
web3ModalOpts?: Partial<ICoreOptions>
|
||||||
children: any
|
children: any
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
@ -51,6 +51,7 @@ function OceanProvider({
|
|||||||
const [chainId, setChainId] = useState<number | undefined>()
|
const [chainId, setChainId] = useState<number | undefined>()
|
||||||
const [account, setAccount] = useState<Account | undefined>()
|
const [account, setAccount] = useState<Account | undefined>()
|
||||||
const [accountId, setAccountId] = useState<string | undefined>()
|
const [accountId, setAccountId] = useState<string | undefined>()
|
||||||
|
const [config, setConfig] = useState<Config>(initialConfig)
|
||||||
const [balance, setBalance] = useState<Balance | undefined>({
|
const [balance, setBalance] = useState<Balance | undefined>({
|
||||||
eth: undefined,
|
eth: undefined,
|
||||||
ocean: undefined
|
ocean: undefined
|
||||||
@ -85,10 +86,12 @@ function OceanProvider({
|
|||||||
web3Modal.cachedProvider && connect()
|
web3Modal.cachedProvider && connect()
|
||||||
}, [web3Modal])
|
}, [web3Modal])
|
||||||
|
|
||||||
async function connect() {
|
async function connect(newConfig?: Config) {
|
||||||
try {
|
try {
|
||||||
Logger.log('Connecting ...')
|
Logger.log('Connecting ...')
|
||||||
|
|
||||||
|
newConfig && setConfig(newConfig)
|
||||||
|
|
||||||
const provider = await web3Modal.connect()
|
const provider = await web3Modal.connect()
|
||||||
setWeb3Provider(provider)
|
setWeb3Provider(provider)
|
||||||
|
|
||||||
@ -100,8 +103,8 @@ function OceanProvider({
|
|||||||
setChainId(chainId)
|
setChainId(chainId)
|
||||||
Logger.log('chain id ', chainId)
|
Logger.log('chain id ', chainId)
|
||||||
|
|
||||||
config.web3Provider = web3
|
newConfig.web3Provider = web3
|
||||||
const ocean = await Ocean.getInstance(config)
|
const ocean = await Ocean.getInstance(newConfig)
|
||||||
setOcean(ocean)
|
setOcean(ocean)
|
||||||
Logger.log('Ocean instance created.', ocean)
|
Logger.log('Ocean instance created.', ocean)
|
||||||
|
|
||||||
@ -131,29 +134,11 @@ function OceanProvider({
|
|||||||
web3Modal.clearCachedProvider()
|
web3Modal.clearCachedProvider()
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Listen for provider, account & network changes
|
|
||||||
// and react to it
|
|
||||||
//
|
|
||||||
|
|
||||||
// const handleConnect = async (provider: any) => {
|
|
||||||
// Logger.debug("Handling 'connect' event with payload", provider)
|
|
||||||
// }
|
|
||||||
|
|
||||||
const handleAccountsChanged = async (accounts: string[]) => {
|
const handleAccountsChanged = async (accounts: string[]) => {
|
||||||
Logger.debug("Handling 'accountsChanged' event with payload", accounts)
|
Logger.debug("Handling 'accountsChanged' event with payload", accounts)
|
||||||
connect()
|
connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleNetworkChanged = async (networkId: string | number) => {
|
|
||||||
Logger.debug(
|
|
||||||
"Handling 'chainChanged' event with payload",
|
|
||||||
networkId,
|
|
||||||
status
|
|
||||||
)
|
|
||||||
connect()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: #68 Refetch balance periodically, or figure out some event to subscribe to
|
// TODO: #68 Refetch balance periodically, or figure out some event to subscribe to
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -161,11 +146,11 @@ function OceanProvider({
|
|||||||
|
|
||||||
if (web3Provider !== undefined && web3Provider !== null) {
|
if (web3Provider !== undefined && web3Provider !== null) {
|
||||||
web3Provider.on('accountsChanged', handleAccountsChanged)
|
web3Provider.on('accountsChanged', handleAccountsChanged)
|
||||||
web3Provider.on('chainChanged', handleNetworkChanged)
|
// web3Provider.on('chainChanged', handleNetworkChanged)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
web3Provider.removeListener('accountsChanged', handleAccountsChanged)
|
web3Provider.removeListener('accountsChanged', handleAccountsChanged)
|
||||||
web3Provider.removeListener('chainChanged', handleNetworkChanged)
|
// web3Provider.removeListener('chainChanged', handleNetworkChanged)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [web3Modal, web3Provider])
|
}, [web3Modal, web3Provider])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user