react/src/providers/OceanProvider
dependabot[bot] 9d2c0f3c63
Bump prettier from 2.2.1 to 2.3.0 (#307)
* Bump prettier from 2.2.1 to 2.3.0

Bumps [prettier](https://github.com/prettier/prettier) from 2.2.1 to 2.3.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.2.1...2.3.0)

Signed-off-by: dependabot[bot] <support@github.com>

* prettier updates

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2021-05-25 09:48:08 +02:00
..
OceanProvider.tsx Bump prettier from 2.2.1 to 2.3.0 (#307) 2021-05-25 09:48:08 +02:00
ProviderStatus.ts format +new oceanlib 2020-07-14 14:34:08 +03:00
README.md metadataStore → metadataCache 2020-10-13 16:50:36 +02:00
getDefaultProviders.ts remove default Web3 providers 2020-07-20 12:22:26 +02:00
index.ts format +new oceanlib 2020-07-14 14:34:08 +03:00

README.md

OceanProvider

The OceanProvider maintains a connection to the Ocean Protocol network in multiple steps:

  1. On mount, setup Web3Modal.
  2. Once connection with Web3Modal is started, Web3 becomes available.
  3. Once Web3 becomes available, connection to Ocean Protocol components are initiated, all available under the ocean object.

With the included useOcean helper hook you can access all context values from any component.

Usage

Wrap your whole app with the OceanProvider:

import React, { ReactNode } from 'react'
import { OceanProvider } from '@oceanprotocol/react'
import { ConfigHelper } from '@oceanprotocol/lib'

const web3ModalOpts = {
  network: 'mainnet', // optional
  cacheProvider: true, // optional
  providerOptions // required
}

const oceanDefaultConfig = new ConfigHelper().getConfig(
  'mainnet',
  'YOUR_INFURA_PROJECT_ID'
)

const config = {
  ...oceanDefaultConfig,
  metadataCacheUri: 'https://your-metadata-cache.com',
  providerUri: 'https://your-provider.com'
}

export default function MyApp({
  children
}: {
  children: ReactNode
}): ReactNode {
  return (
    <OceanProvider initialConfig={config} web3ModalOpts={web3ModalOpts}>
      <h1>My App</h1>
      {children}
    </OceanProvider>
  )
}

The OceanProvider uses Web3Modal to make its initial wallet connection. If you do not pass web3ModalOpts as a prop, only the default injected provider will be available. Adding more providers requires you to add them as dependencies to your project and pass them as providerOptions. See all the available Provider Options.

You can then access the provider context values with the useOcean hook:

import { useOcean } from '@oceanprotocol/react'

function MyComponent() {
  const { ocean, accountId } = useOcean()

  return (
    <ul>
      <li>Ocean available: {`${Boolean(ocean)}`}</li>
      <li>Account: {accountId}</li>
    </ul>
  )
}