1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-12-25 02:26:25 +01:00

error catching for connect, todo cleanup

This commit is contained in:
Matthias Kretschmann 2020-07-31 01:26:01 +02:00
parent 07e35da469
commit d68ccb9ff9
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -85,6 +85,7 @@ function OceanProvider({
}, [web3Modal]) }, [web3Modal])
async function connect() { async function connect() {
try {
Logger.log('Connecting ...') Logger.log('Connecting ...')
const provider = await web3Modal.connect() const provider = await web3Modal.connect()
@ -116,10 +117,13 @@ function OceanProvider({
const balance = await getBalance(account) const balance = await getBalance(account)
setBalance(balance) setBalance(balance)
Logger.log('balance', JSON.stringify(balance)) Logger.log('balance', JSON.stringify(balance))
} catch (error) {
Logger.error(error)
}
} }
async function logout() { async function logout() {
// ToDo check how is the proper way to logout // TODO: check how is the proper way to logout
web3Modal.clearCachedProvider() web3Modal.clearCachedProvider()
} }
@ -127,18 +131,18 @@ function OceanProvider({
// Listen for provider, account & network changes // Listen for provider, account & network changes
// and react to it // and react to it
// //
const handleConnect = async (provider: any) => {
Logger.debug("Handling 'connect' event with payload", provider) // const handleConnect = async (provider: any) => {
} // Logger.debug("Handling 'connect' event with payload", provider)
// }
const handleAccountsChanged = async (accounts: string[]) => { const handleAccountsChanged = async (accounts: string[]) => {
console.debug("Handling 'accountsChanged' event with payload", accounts) Logger.debug("Handling 'accountsChanged' event with payload", accounts)
connect() connect()
} }
// ToDo need to handle this, it's not implemented, need to update chainId and reinitialize ocean lib
const handleNetworkChanged = async (networkId: string | number) => { const handleNetworkChanged = async (networkId: string | number) => {
console.debug( Logger.debug(
"Handling 'networkChanged' event with payload", "Handling 'networkChanged' event with payload",
networkId, networkId,
status status
@ -146,8 +150,10 @@ function OceanProvider({
connect() connect()
} }
// TODO: Refetch balance periodically, or figure out some event to subscribe to
useEffect(() => { useEffect(() => {
web3Modal && web3Modal.on('connect', handleConnect) // web3Modal && web3Modal.on('connect', handleConnect)
if (web3Provider !== undefined && web3Provider !== null) { if (web3Provider !== undefined && web3Provider !== null) {
web3Provider.on('accountsChanged', handleAccountsChanged) web3Provider.on('accountsChanged', handleAccountsChanged)