react/src/providers/OceanProvider/OceanProvider.tsx

189 lines
5.1 KiB
TypeScript
Raw Normal View History

2020-07-18 02:44:41 +02:00
import React, {
useContext,
useState,
useEffect,
createContext,
ReactElement
} from 'react'
2020-04-27 16:29:34 +02:00
import Web3 from 'web3'
2020-07-09 14:33:22 +02:00
import ProviderStatus from './ProviderStatus'
import { Ocean, Logger, Account, Config } from '@oceanprotocol/lib'
2020-07-14 13:20:17 +02:00
import Web3Modal, { ICoreOptions } from 'web3modal'
2020-07-16 11:25:01 +02:00
import { getDefaultProviders } from './getDefaultProviders'
2020-07-30 23:31:33 +02:00
import { getAccountId, getBalance } from '../../utils'
2020-07-20 12:22:26 +02:00
2020-07-20 13:09:00 +02:00
interface Balance {
2020-07-20 13:24:42 +02:00
eth: string | undefined
ocean: string | undefined
2020-07-20 13:09:00 +02:00
}
2020-04-25 02:28:16 +02:00
interface OceanProviderValue {
2020-07-09 14:33:22 +02:00
web3: Web3 | undefined
web3Provider: any
web3Modal: Web3Modal
2020-04-25 02:28:16 +02:00
ocean: Ocean
2020-07-09 14:33:22 +02:00
config: Config
2020-04-27 10:34:22 +02:00
account: Account
accountId: string
2020-07-20 13:09:00 +02:00
balance: Balance
2020-07-09 14:33:22 +02:00
chainId: number | undefined
status: ProviderStatus
2020-08-11 12:15:20 +02:00
connect: (config?: Config) => Promise<void>
2020-07-20 13:58:45 +02:00
logout: () => Promise<void>
2020-08-04 13:40:39 +02:00
refreshBalance: () => Promise<void>
2020-07-09 14:33:22 +02:00
}
const OceanContext = createContext({} as OceanProviderValue)
2020-04-25 02:28:16 +02:00
function OceanProvider({
2020-08-06 15:39:41 +02:00
initialConfig,
2020-07-30 23:18:23 +02:00
web3ModalOpts,
2020-04-25 02:28:16 +02:00
children
}: {
2020-08-06 15:39:41 +02:00
initialConfig: Config
2020-07-30 23:18:23 +02:00
web3ModalOpts?: Partial<ICoreOptions>
2020-05-07 13:42:29 +02:00
children: any
2020-07-18 02:44:41 +02:00
}): ReactElement {
2020-07-09 14:33:22 +02:00
const [web3, setWeb3] = useState<Web3 | undefined>()
const [web3Provider, setWeb3Provider] = useState<any | undefined>()
2020-04-25 02:28:16 +02:00
const [ocean, setOcean] = useState<Ocean | undefined>()
const [web3Modal, setWeb3Modal] = useState<Web3Modal>()
2020-07-09 14:33:22 +02:00
const [chainId, setChainId] = useState<number | undefined>()
2020-04-27 10:34:22 +02:00
const [account, setAccount] = useState<Account | undefined>()
const [accountId, setAccountId] = useState<string | undefined>()
2020-08-06 15:39:41 +02:00
const [config, setConfig] = useState<Config>(initialConfig)
2020-07-20 13:24:42 +02:00
const [balance, setBalance] = useState<Balance | undefined>({
eth: undefined,
ocean: undefined
})
2020-07-17 12:24:46 +02:00
const [status, setStatus] = useState<ProviderStatus>(
ProviderStatus.NOT_AVAILABLE
)
2020-07-09 14:33:22 +02:00
2020-07-30 23:18:23 +02:00
async function init() {
2020-07-14 13:34:08 +02:00
Logger.log('Ocean Provider init')
2020-07-16 16:03:26 +02:00
window &&
window.ethereum &&
(window.ethereum.autoRefreshOnNetworkChange = false)
2020-07-30 23:18:23 +02:00
Logger.log('Web3Modal init.')
if (web3ModalOpts === undefined) {
web3ModalOpts = await getDefaultProviders()
}
const web3ModalInstance = new Web3Modal(web3ModalOpts)
setWeb3Modal(web3ModalInstance)
Logger.log('Web3Modal instance created.', web3ModalInstance)
2020-07-09 14:33:22 +02:00
}
2020-08-06 15:39:41 +02:00
async function connect(newConfig?: Config) {
try {
2020-08-11 12:19:27 +02:00
Logger.log('Connecting ...', newConfig)
2020-07-20 13:09:00 +02:00
2020-08-06 15:39:41 +02:00
newConfig && setConfig(newConfig)
const provider = await web3Modal?.connect()
setWeb3Provider(provider)
2020-07-09 14:33:22 +02:00
const web3 = new Web3(provider)
setWeb3(web3)
Logger.log('Web3 created.', web3)
2020-07-09 14:33:22 +02:00
const chainId = web3 && (await web3.eth.getChainId())
setChainId(chainId)
Logger.log('chain id ', chainId)
2020-07-20 13:33:42 +02:00
2020-08-11 12:15:20 +02:00
config.web3Provider = web3
const ocean = await Ocean.getInstance(config)
setOcean(ocean)
Logger.log('Ocean instance created.', ocean)
2020-07-20 13:37:34 +02:00
setStatus(ProviderStatus.CONNECTED)
2020-07-20 13:09:00 +02:00
const account = (await ocean.accounts.list())[0]
setAccount(account)
Logger.log('Account ', account)
2020-07-13 11:57:27 +02:00
const accountId = await getAccountId(web3)
setAccountId(accountId)
Logger.log('account id', accountId)
2020-07-20 13:09:00 +02:00
const balance = await getBalance(account)
setBalance(balance)
Logger.log('balance', JSON.stringify(balance))
} catch (error) {
Logger.error(error)
}
2020-07-09 14:33:22 +02:00
}
2020-09-15 21:57:44 +02:00
// On mount setup Web3Modal instance
useEffect(() => {
init()
}, [])
// Connect automatically to cached provider if present
useEffect(() => {
if (!web3Modal) return
web3Modal.cachedProvider && connect()
}, [web3Modal])
2020-08-04 13:40:39 +02:00
async function refreshBalance() {
const balance = account && (await getBalance(account))
2020-08-04 13:40:39 +02:00
setBalance(balance)
}
2020-07-09 14:33:22 +02:00
async function logout() {
2020-07-31 12:05:44 +02:00
// TODO: #67 check how is the proper way to logout
web3Modal?.clearCachedProvider()
2020-07-09 14:33:22 +02:00
}
const handleAccountsChanged = async (accounts: string[]) => {
Logger.debug("Handling 'accountsChanged' event with payload", accounts)
2020-07-30 23:18:23 +02:00
connect()
2020-07-09 14:33:22 +02:00
}
2020-07-31 12:05:44 +02:00
// TODO: #68 Refetch balance periodically, or figure out some event to subscribe to
2020-04-25 02:28:16 +02:00
useEffect(() => {
// web3Modal && web3Modal.on('connect', handleConnect)
2020-07-09 14:33:22 +02:00
if (web3Provider !== undefined && web3Provider !== null) {
web3Provider.on('accountsChanged', handleAccountsChanged)
2020-08-10 12:34:54 +02:00
// web3Provider.on('chainChanged', handleNetworkChanged)
2020-07-09 14:33:22 +02:00
return () => {
web3Provider.removeListener('accountsChanged', handleAccountsChanged)
2020-08-10 12:34:54 +02:00
// web3Provider.removeListener('chainChanged', handleNetworkChanged)
2020-07-09 14:33:22 +02:00
}
2020-04-25 02:28:16 +02:00
}
2020-07-17 12:24:46 +02:00
}, [web3Modal, web3Provider])
2020-07-09 14:33:22 +02:00
2020-04-25 02:28:16 +02:00
return (
<OceanContext.Provider
2020-04-27 10:34:22 +02:00
value={
{
2020-07-09 14:33:22 +02:00
web3,
web3Provider,
web3Modal,
2020-04-27 10:34:22 +02:00
ocean,
account,
accountId,
balance,
2020-07-09 14:33:22 +02:00
chainId,
2020-04-27 14:37:26 +02:00
status,
2020-07-13 11:57:27 +02:00
config,
2020-07-09 14:33:22 +02:00
connect,
2020-08-04 13:40:39 +02:00
logout,
refreshBalance
2020-04-27 10:34:22 +02:00
} as OceanProviderValue
}
2020-04-25 02:28:16 +02:00
>
{children}
</OceanContext.Provider>
)
}
2020-04-27 14:58:37 +02:00
// Helper hook to access the provider values
const useOcean = (): OceanProviderValue => useContext(OceanContext)
2020-04-25 02:28:16 +02:00
2020-07-20 13:09:00 +02:00
export { OceanProvider, useOcean, OceanProviderValue, Balance }
2020-04-25 02:28:16 +02:00
export default OceanProvider