mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
Web3Provider concept
This commit is contained in:
parent
44b145a2ba
commit
f168f20213
36
README.md
36
README.md
@ -20,6 +20,8 @@
|
|||||||
|
|
||||||
- [🏗 Installation](#-installation)
|
- [🏗 Installation](#-installation)
|
||||||
- [🏄 Usage](#-usage)
|
- [🏄 Usage](#-usage)
|
||||||
|
- [Providers](#providers)
|
||||||
|
- [Hooks](#hooks)
|
||||||
- [🦑 Development](#-development)
|
- [🦑 Development](#-development)
|
||||||
- [✨ Code Style](#-code-style)
|
- [✨ Code Style](#-code-style)
|
||||||
- [👩🔬 Testing](#-testing)
|
- [👩🔬 Testing](#-testing)
|
||||||
@ -38,27 +40,49 @@ npm install @oceanprotocol/react
|
|||||||
|
|
||||||
## 🏄 Usage
|
## 🏄 Usage
|
||||||
|
|
||||||
First, wrap your App with the `OceanProvider` and provide its config object:
|
First, wrap your App with the `Web3Provider` and the `OceanProvider`.
|
||||||
|
|
||||||
|
### Providers
|
||||||
|
|
||||||
```tsx
|
```tsx
|
||||||
import React from 'react'
|
import React, { ReactNode } from 'react'
|
||||||
import { OceanProvider, Config } from '@oceanprotocol/react'
|
import Web3 from 'web3'
|
||||||
|
import { Web3Provider, OceanProvider, Config } from '@oceanprotocol/react'
|
||||||
|
|
||||||
const config: Config = {
|
const config: Config = {
|
||||||
nodeUri: '',
|
nodeUri: '',
|
||||||
...
|
aquariusUri: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function MyApp({ children }: { children: React.ReactNode }): React.ReactNode {
|
export default function MyApp({
|
||||||
|
children
|
||||||
|
}: {
|
||||||
|
children: ReactNode
|
||||||
|
}): ReactNode {
|
||||||
return (
|
return (
|
||||||
<OceanProvider config={config}>
|
<Web3Provider>
|
||||||
|
{(web3: Web3) => (
|
||||||
|
<OceanProvider config={config} web3={web3}>
|
||||||
<h1>My App</h1>
|
<h1>My App</h1>
|
||||||
{children}
|
{children}
|
||||||
</OceanProvider>
|
</OceanProvider>
|
||||||
|
)}
|
||||||
|
</Web3Provider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The `OceanProvider` requires a Web3 instance to be passed as prop. To get you started, we added a basic `Web3Provider` which assumes an injected provider (like MetaMask), and will ask for permissions automatically on first mount.
|
||||||
|
|
||||||
|
We suggest you replace this provider with a more complete solution, since there are many UX considerations not handled in that basic provider, like activate only on user intent, listen for account & network changes, display connection instructions and errors, etc.
|
||||||
|
|
||||||
|
Some great solutions we liked to work with:
|
||||||
|
|
||||||
|
- [web3-react](https://github.com/NoahZinsmeister/web3-react)
|
||||||
|
- [web3modal](https://github.com/web3modal/web3modal)
|
||||||
|
|
||||||
|
### Hooks
|
||||||
|
|
||||||
Then within your component use the provided hooks to interact with Ocean's functionality. Each hook can be used independently:
|
Then within your component use the provided hooks to interact with Ocean's functionality. Each hook can be used independently:
|
||||||
|
|
||||||
```tsx
|
```tsx
|
||||||
|
@ -6,6 +6,7 @@ import React, {
|
|||||||
createContext
|
createContext
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { Ocean, Config, Account, Aquarius, Logger } from '@oceanprotocol/squid'
|
import { Ocean, Config, Account, Aquarius, Logger } from '@oceanprotocol/squid'
|
||||||
|
import Web3 from 'web3'
|
||||||
import Balance from '@oceanprotocol/squid/dist/node/models/Balance'
|
import Balance from '@oceanprotocol/squid/dist/node/models/Balance'
|
||||||
import { connectOcean } from './utils'
|
import { connectOcean } from './utils'
|
||||||
|
|
||||||
@ -29,13 +30,13 @@ const OceanContext = createContext(null)
|
|||||||
|
|
||||||
function OceanProvider({
|
function OceanProvider({
|
||||||
config,
|
config,
|
||||||
|
web3,
|
||||||
children
|
children
|
||||||
}: {
|
}: {
|
||||||
config: Config
|
config: Config
|
||||||
|
web3: Web3
|
||||||
children: ReactNode
|
children: ReactNode
|
||||||
}): ReactNode {
|
}): ReactNode {
|
||||||
// TODO: handle web3
|
|
||||||
const { web3 } = useWeb3()
|
|
||||||
const [ocean, setOcean] = useState<Ocean | undefined>()
|
const [ocean, setOcean] = useState<Ocean | undefined>()
|
||||||
const [aquarius, setAquarius] = useState<Aquarius | undefined>()
|
const [aquarius, setAquarius] = useState<Aquarius | undefined>()
|
||||||
const [account, setAccount] = useState<Account | undefined>()
|
const [account, setAccount] = useState<Account | undefined>()
|
||||||
@ -84,10 +85,10 @@ function OceanProvider({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function debug(): Promise<void> {
|
async function debug(): Promise<void> {
|
||||||
if (!ocean) return
|
if (!ocean) return
|
||||||
console.debug(
|
Logger.debug(
|
||||||
`Ocean instance initiated with:\n ${JSON.stringify(config, null, 2)}`
|
`Ocean instance initiated with:\n ${JSON.stringify(config, null, 2)}`
|
||||||
)
|
)
|
||||||
console.debug(await ocean.versions.get())
|
Logger.debug(await ocean.versions.get())
|
||||||
}
|
}
|
||||||
debug()
|
debug()
|
||||||
}, [ocean])
|
}, [ocean])
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Ocean, Config, Account } from '@oceanprotocol/squid'
|
import { Ocean, Config, Account, Logger } from '@oceanprotocol/squid'
|
||||||
import Balance from '@oceanprotocol/squid/dist/node/models/Balance'
|
import Balance from '@oceanprotocol/squid/dist/node/models/Balance'
|
||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
|
|
||||||
@ -11,12 +11,12 @@ export async function connectOcean(
|
|||||||
accountId: string
|
accountId: string
|
||||||
balance: Balance
|
balance: Balance
|
||||||
}> {
|
}> {
|
||||||
console.debug('Connecting to Ocean...')
|
Logger.debug('Connecting to Ocean...')
|
||||||
const ocean = await Ocean.getInstance({
|
const ocean = await Ocean.getInstance({
|
||||||
web3Provider: web3.currentProvider,
|
web3Provider: web3.currentProvider,
|
||||||
...config
|
...config
|
||||||
})
|
})
|
||||||
console.debug('Ocean instance ready.')
|
Logger.debug('Ocean instance ready.')
|
||||||
|
|
||||||
const oceanAccounts = await ocean.accounts.list()
|
const oceanAccounts = await ocean.accounts.list()
|
||||||
const account = oceanAccounts[0]
|
const account = oceanAccounts[0]
|
||||||
|
0
src/providers/Web3Provider/README.md
Normal file
0
src/providers/Web3Provider/README.md
Normal file
24
src/providers/Web3Provider/Web3Provider.tsx
Normal file
24
src/providers/Web3Provider/Web3Provider.tsx
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import React, { ReactNode, useContext, useState, createContext } from 'react'
|
||||||
|
import Web3 from 'web3'
|
||||||
|
|
||||||
|
interface Web3ProviderValue {
|
||||||
|
web3: Web3
|
||||||
|
}
|
||||||
|
|
||||||
|
const Web3Context = createContext(null)
|
||||||
|
|
||||||
|
function Web3Provider({ children }: { children: ReactNode }): ReactNode {
|
||||||
|
const [web3, setWeb3] = useState<Web3 | undefined>()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Web3Context.Provider value={{ web3 } as Web3ProviderValue}>
|
||||||
|
{children}
|
||||||
|
</Web3Context.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper hook to access the provider values
|
||||||
|
const useWeb3 = (): Web3ProviderValue => useContext(Web3Context)
|
||||||
|
|
||||||
|
export { Web3Provider, useWeb3 }
|
||||||
|
export default Web3Provider
|
@ -0,0 +1 @@
|
|||||||
|
export * from './Web3Provider'
|
Loading…
x
Reference in New Issue
Block a user