From 74b39256d622717927df44f429406ef3040756f9 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 25 Apr 2020 02:28:16 +0200 Subject: [PATCH] add OceanProvider/useOcean draft --- src/providers/OceanProvider/OceanProvider.tsx | 98 +++++++++++++++++++ src/providers/OceanProvider/README.md | 0 src/providers/OceanProvider/index.ts | 1 + src/providers/index.ts | 1 + 4 files changed, 100 insertions(+) create mode 100644 src/providers/OceanProvider/OceanProvider.tsx create mode 100644 src/providers/OceanProvider/README.md create mode 100644 src/providers/OceanProvider/index.ts create mode 100644 src/providers/index.ts diff --git a/src/providers/OceanProvider/OceanProvider.tsx b/src/providers/OceanProvider/OceanProvider.tsx new file mode 100644 index 0000000..b33a771 --- /dev/null +++ b/src/providers/OceanProvider/OceanProvider.tsx @@ -0,0 +1,98 @@ +import React, { + ReactNode, + useContext, + useState, + useEffect, + createContext, + FunctionComponent +} from 'react' +import { Ocean, Config } from '@oceanprotocol/squid' +import Balance from '@oceanprotocol/squid/dist/node/models/Balance' + +enum OceanConnectionStatus { + OCEAN_CONNECTION_ERROR = -1, + NOT_CONNECTED = 0, + CONNECTED = 1 +} + +interface OceanProviderValue { + ocean: Ocean + account: string + balance: Balance + status: OceanConnectionStatus +} + +const OceanContext = createContext(null) + +function OceanProvider({ + config, + children +}: { + config: Config + children: ReactNode +}): ReactNode { + // TODO: handle web3 + const { web3 } = useWeb3() + const [ocean, setOcean] = useState() + const [account, setAccount] = useState() + const [balance, setBalance] = useState() + const [status, setStatus] = useState( + OceanConnectionStatus.NOT_CONNECTED + ) + + useEffect(() => { + async function init(): Promise { + console.debug('Connecting to Ocean...') + const oceanInstance = await Ocean.getInstance({ + web3Provider: web3.currentProvider, + ...config + }) + console.debug('Ocean instance ready.') + setOcean(oceanInstance) + setStatus(OceanConnectionStatus.CONNECTED) + + const oceanAccounts = await oceanInstance.accounts.list() + oceanAccounts && setAccount(oceanAccounts[0].getId()) + const { eth, ocn } = await oceanAccounts[0].getBalance() + setBalance(balance) + } + + try { + init() + } catch (error) { + console.error(error.message) + setStatus(OceanConnectionStatus.OCEAN_CONNECTION_ERROR) + throw error.message + } + }, [web3]) + + useEffect(() => { + async function debug(): Promise { + if (!ocean) return + console.debug( + `Ocean instance initiated with:\n ${JSON.stringify(config, null, 2)}` + ) + console.debug(await ocean.versions.get()) + } + debug() + }, [ocean]) + + return ( + + {children} + + ) +} + +const useOcean = () => useContext(OceanContext) + +export { + OceanProvider, + useOcean, + OceanProviderValue, + OceanConnectionStatus, + Config +} +export default OceanProvider diff --git a/src/providers/OceanProvider/README.md b/src/providers/OceanProvider/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/providers/OceanProvider/index.ts b/src/providers/OceanProvider/index.ts new file mode 100644 index 0000000..0cc537c --- /dev/null +++ b/src/providers/OceanProvider/index.ts @@ -0,0 +1 @@ +export * from './OceanProvider' diff --git a/src/providers/index.ts b/src/providers/index.ts new file mode 100644 index 0000000..0cc537c --- /dev/null +++ b/src/providers/index.ts @@ -0,0 +1 @@ +export * from './OceanProvider'