1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-12-24 10:06:20 +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,41 +85,45 @@ function OceanProvider({
}, [web3Modal]) }, [web3Modal])
async function connect() { async function connect() {
Logger.log('Connecting ...') try {
Logger.log('Connecting ...')
const provider = await web3Modal.connect() const provider = await web3Modal.connect()
setWeb3Provider(provider) setWeb3Provider(provider)
const web3 = new Web3(provider) const web3 = new Web3(provider)
setWeb3(web3) setWeb3(web3)
Logger.log('Web3 created.', web3) Logger.log('Web3 created.', web3)
const chainId = web3 && (await web3.eth.getChainId()) const chainId = web3 && (await web3.eth.getChainId())
setChainId(chainId) setChainId(chainId)
Logger.log('chain id ', chainId) Logger.log('chain id ', chainId)
config.web3Provider = web3 config.web3Provider = web3
const ocean = await Ocean.getInstance(config) const ocean = await Ocean.getInstance(config)
setOcean(ocean) setOcean(ocean)
Logger.log('Ocean instance created.', ocean) Logger.log('Ocean instance created.', ocean)
setStatus(ProviderStatus.CONNECTED) setStatus(ProviderStatus.CONNECTED)
const account = (await ocean.accounts.list())[0] const account = (await ocean.accounts.list())[0]
setAccount(account) setAccount(account)
Logger.log('Account ', account) Logger.log('Account ', account)
const accountId = await getAccountId(web3) const accountId = await getAccountId(web3)
setAccountId(accountId) setAccountId(accountId)
Logger.log('account id', accountId) Logger.log('account id', accountId)
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)