Skip to content

Commit 068ca91

Browse files
author
Sam
committed
chore: updated documentation
1 parent e2470e9 commit 068ca91

15 files changed

Lines changed: 105 additions & 431 deletions

docs/README.md

Lines changed: 25 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# `web3-react` Documentation
1+
# `web3-react-multichain` Documentation
22

33
- [Overview](#overview)
44
- [Install](#install)
@@ -9,20 +9,14 @@
99
- [useWeb3React](#useweb3react)
1010
- [Arguments](#arguments)
1111
- [Example](#example-1)
12-
- [createWeb3ReactRoot](#createweb3reactroot)
13-
- [Arguments](#arguments-1)
14-
- [Example](#example-2)
15-
- [getWeb3ReactContext](#getweb3reactcontext)
16-
- [Arguments](#arguments-2)
17-
- [Example](#example-3)
1812
- [UnsupportedChainIdError](#unsupportedchainiderror)
1913
- [Example](#example-4)
2014
- [Understanding Error Bubbling](#understanding-error-bubbling)
2115

2216
## Overview
23-
At a high level, `web3-react` is a state machine which ensures that certain key pieces of data (the user's current account, for example) relevant to your dApp are kept up-to-date. To this end, `web3-react` uses [Context](https://reactjs.org/docs/context.html) to efficiently store this data, and inject it wherever you need it in your application.
17+
At a high level, `web3-react-multichain` is a state machine which ensures that certain key pieces of data (the user's current account, for example) relevant to your dApp are kept up-to-date. To this end, `web3-react-multichain` uses [Context](https://reactjs.org/docs/context.html) to efficiently store this data, and inject it wherever you need it in your application.
2418

25-
The data conforms to the following interface:
19+
The data conforms to the following interface where Web3Provider is an [ethers-js Web3Provider](https://docs.ethers.io/v5/api/providers/other/#Web3Provider):
2620

2721
```typescript
2822
interface Web3ReactContextInterface<T = any> {
@@ -32,132 +26,65 @@ interface Web3ReactContextInterface<T = any> {
3226
throwErrors?: boolean
3327
) => Promise<void>
3428
setError: (error: Error) => void
35-
deactivate: () => void
29+
deactivate: () => Promise<void>
3630

31+
currentChainId: number
32+
currentProvider?: Web3Provider
33+
getProvider: (chainId: number) => Promise<Web3Provider | undefined>
3734
connector?: AbstractConnectorInterface
38-
library?: T
39-
chainId?: number
4035
account?: null | string
4136

4237
active: boolean
4338
error?: Error
4439
}
4540
```
4641

47-
The documentation that follows is for `@web3-react/core`, the package responsible for managing this context. To understand where the data itself comes from, head over to the [connectors/ folder](./connectors/).
42+
The documentation that follows is for `@web3-react-multichain/core`, the package responsible for managing this context. To understand where the data itself comes from, head over to the [connectors/ folder](./connectors/).
4843

4944
## Install
5045
- Grab a fresh copy of `react@>=16.8`...\
5146
`yarn add react`
5247

53-
- ...and then install `web3-react`\
54-
`yarn add @web3-react/core`
48+
- ...and then install `web3-react-multichain`\
49+
`yarn add @web3-react-multichain/core`
5550

56-
## `web3-react@core` API Reference
51+
## `web3-react-multichain@core` API Reference
5752

5853
### Web3ReactProvider
59-
`web3-react` relies on the existence of a `Web3ReactProvider` at the root of your application (or more accurately, at the root of the subtree which you'd like to have web3 functionality). It requires a single `getLibrary` prop which is responsible for instantiating a web3 convenience library object from a low-level provider.
60-
61-
#### Props
62-
```typescript
63-
getLibrary: (provider?: any, connector?: AbstractConnectorInterface) => any
64-
```
54+
`web3-react-multichain` relies on the existence of a `Web3ReactProvider` at the root of your application (or more accurately, at the root of the subtree which you'd like to have web3 functionality).
6555

6656
#### Example
6757
```javascript
68-
import { Web3ReactProvider } from '@web3-react/core'
69-
// import your favorite web3 convenience library here
70-
71-
function getLibrary(provider, connector) {
72-
return new Web3Provider(provider) // this will vary according to whether you use e.g. ethers or web3.js
73-
}
58+
import { Web3ReactProvider } from '@web3-react-multichain/core'
7459

7560
function App () {
7661
return (
77-
<Web3ReactProvider getLibrary={getLibrary}>
62+
<Web3ReactProvider>
7863
{/* <...> */}
7964
</Web3ReactProvider>
8065
)
8166
}
8267
```
8368

8469
### useWeb3React
85-
If you're using Hooks (😇), useWeb3React will be your best friend. Call it from within any function component to access context variables, just like that. It accepts an optional `key` argument, if you're using [multiple roots](#createweb3reactroot).
86-
87-
#### Arguments
88-
```typescript
89-
key?: string
90-
```
70+
If you're using Hooks (😇), useWeb3React will be your best friend. Call it from within any function component to access context variables, just like that.
9171

9272
#### Example
9373
```javascript
94-
import { useWeb3React } from '@web3-react/core'
74+
import { useWeb3React } from '@web3-react-multichain/core'
9575

9676
function Component () {
9777
const web3React = useWeb3React()
9878
// ...
9979
}
10080
```
10181

102-
### createWeb3ReactRoot
103-
In some cases, your dApp may want to maintain >1 active web3 connections simultaneously. This could be for any number of reasons, including:
104-
105-
- Wanting "always-on" access to a remote node, while letting users bring their own accounts as necessary
106-
- Communicating with a sidechain and mainnet in tandem
107-
- Balancing an in-browser burner wallet with other connection methods
108-
109-
In cases like these, you'll likely want to create a second (or maybe even third, but probably not fourth) root, which will function exactly like another [Web3ReactProvider](#web3reactprovider) (in fact, Web3ReactProvider uses createWeb3ReactRoot under the hood). It requires a `key` argument, used to identify the root to [useWeb3React](#useweb3react) (or [getWeb3ReactContext](#getweb3reactcontext)).
110-
111-
#### Arguments
112-
```typescript
113-
key: string
114-
```
115-
116-
#### Example
117-
```javascript
118-
import { Web3ReactProvider, createWeb3ReactRoot } from '@web3-react/core'
119-
// import your favorite web3 convenience library here
120-
121-
function getLibrary(provider) {
122-
return new Web3Provider(provider) // this will vary according to whether you use e.g. ethers or web3.js
123-
}
124-
125-
const Web3ReactProviderReloaded = createWeb3ReactRoot('anotherOne')
126-
127-
function App () {
128-
return (
129-
<Web3ReactProvider getLibrary={getLibrary}>
130-
<Web3ReactProviderReloaded getLibrary={getLibrary}>
131-
{/* <...> */}
132-
</Web3ReactProviderReloaded>
133-
</Web3ReactProvider>
134-
)
135-
}
136-
```
137-
138-
### getWeb3ReactContext
139-
If you're not using Hooks (😳), getWeb3ReactContext is your savior. It gives direct access to the context returned by [createContext](https://reactjs.org/docs/context.html#reactcreatecontext), which will unlock the use of [contextType](https://reactjs.org/docs/context.html#classcontexttype) in class components, the [Context.Consumer](https://reactjs.org/docs/context.html#contextconsumer) pattern, or whatever other render prop/HOC/etc. shenanigans your manager whose personal site still runs on PHP is making you write. It accepts an optional `key` argument to identify the root.
140-
141-
#### Arguments
142-
```typescript
143-
key?: string
144-
```
145-
146-
#### Example
147-
```javascript
148-
import { getWeb3ReactContext } from '@web3-react/core'
149-
150-
const web3ReactContext = getWeb3ReactContext()
151-
152-
// ...
153-
```
154-
15582
### UnsupportedChainIdError
15683
This is an error which can be used to inform users that they're connected to an unsupported network.
15784

15885
#### Example
15986
```javascript
160-
import { UnsupportedChainIdError } from '@web3-react/core'
87+
import { UnsupportedChainIdError } from '@web3-react-multichain/core'
16188
// ...
16289

16390
function Component () {
@@ -170,16 +97,16 @@ function Component () {
17097
## Understanding Error Bubbling
17198
Errors that occur during the initial activation of a connector (i.e. inside activate), are are handled in 1 of 4 ways:
17299

173-
1) In the case where there's been 1 or more other updates to the `web3-react` context between when activate was called and when it resolved with the data required to complete the activation, errors are silently suppressed (in development mode, a warning will be logged to the console). This should really only happen in cases where activation takes a very long time and the user does something in the intervening time, such as activating another connector, deactivating the current connector, etc.
174-
2) If `throwErrors` (the third argument to activate) is passed, errors will be thrown and should be handled in a .catch. No updates to the `web3-react` context will occur.
175-
3) If `onError` (the second argument to activate) is passed, that function is called with the error. No updates to the `web3-react` context will occur.
176-
4) Otherwise, the error will be set in the `web3-react` context (along with the connector).
100+
1) In the case where there's been 1 or more other updates to the `web3-react-multichain` context between when activate was called and when it resolved with the data required to complete the activation, errors are silently suppressed (in development mode, a warning will be logged to the console). This should really only happen in cases where activation takes a very long time and the user does something in the intervening time, such as activating another connector, deactivating the current connector, etc.
101+
2) If `throwErrors` (the third argument to activate) is passed, errors will be thrown and should be handled in a .catch. No updates to the `web3-react-multichain` context will occur.
102+
3) If `onError` (the second argument to activate) is passed, that function is called with the error. No updates to the `web3-react-multichain` context will occur.
103+
4) Otherwise, the error will be set in the `web3-react-multichain` context (along with the connector).
177104

178105
Errors that occur while a connector is set are handled in 1 of 2 ways:
179106

180-
1) If an `onError` function was passed, this function is called with the error. No updates to the `web3-react` context will occur.
181-
2) Otherwise, the error will be set in the `web3-react` context.
107+
1) If an `onError` function was passed, this function is called with the error. No updates to the `web3-react-multichain` context will occur.
108+
2) Otherwise, the error will be set in the `web3-react-multichain` context.
182109

183-
In all of these scenarios, note that calling setError will update the `web3-react` context. This can be called any time a connector is set, and it can be useful for e.g. manually triggering your app's handling of the `web3-react` error property.
110+
In all of these scenarios, note that calling setError will update the `web3-react-multichain` context. This can be called any time a connector is set, and it can be useful for e.g. manually triggering your app's handling of the `web3-react-multichain` error property.
184111

185-
Note: if an error is ever set in the `web3-react` context, and a connector triggers an update, the manager will attempt to revalidate all properties as if activate was called again, to recover from the error state.
112+
Note: if an error is ever set in the `web3-react-multichain` context, and a connector triggers an update, the manager will attempt to revalidate all properties as if activate was called again, to recover from the error state.

docs/connectors/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# `web3-react` Documentation - Connectors
1+
# `web3-react-multichain` Documentation - Connectors
22

3-
Connectors are stand-alone packages that interface with `web3-react` and manage connections to a single type of Ethereum node or wallet. To give an example: it's quite common for dApp users to rely on browser extensions such as [MetaMask](https://metamask.io/) to manage their account for them. So, `web3-react` defines a connector that's responsible for interfacing with browser extensions. The current list of first-party `web3-react` connectors can be found in the [top-level README.md](../../).
3+
Connectors are stand-alone packages that interface with `web3-react-multichain` and manage connections to a single type of Ethereum node or wallet. To give an example: it's quite common for dApp users to rely on browser extensions such as [MetaMask](https://metamask.io/) to manage their account for them. So, `web3-react-multichain` defines a connector that's responsible for interfacing with browser extensions. The current list of first-party `web3-react-multichain` connectors can be found in the [top-level README.md](../../).
44

5-
One enormous benefit to `web3-react` is that it's incredibly easy to extend existing/create new connectors! They're very simple Javascript class objects which simply need to define a few functions to be fully compatible with the rest of `web3-react`.
5+
One enormous benefit to `web3-react-multichain` is that it's incredibly easy to extend existing/create new connectors! They're very simple Javascript class objects which simply need to define a few functions to be fully compatible with the rest of `web3-react-multichain`.
66

77
For more information on specific first-party connectors, see the individual files in this folder.

docs/connectors/authereum.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

docs/connectors/fortmatic.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/connectors/frame.md

Lines changed: 0 additions & 38 deletions
This file was deleted.

docs/connectors/injected.md

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,82 @@
1-
# `web3-react` Documentation - Injected
1+
# `web3-react-multichain` Documentation - Injected
22

33
- [Install](#install)
44
- [Arguments](#arguments)
55
- [Example](#example)
66
- [Errors](#errors)
7+
- [AddDefaultChainError](#adddefaultchainerror)
78
- [NoEthereumProviderError](#noethereumprovidererror)
89
- [Example](#example-1)
910
- [UserRejectedRequestError](#userrejectedrequesterror)
1011
- [Example](#example-2)
1112

1213
## Install
13-
`yarn add @web3-react/injected-connector`
14+
`yarn add @web3-react-multichain/injected-connector`
1415

1516
## Arguments
1617
```typescript
17-
supportedChainIds?: number[]
18+
networks: NetworkWithInfo[];
19+
20+
// which is defined below
21+
export interface Network {
22+
rpcUrls: string[];
23+
chainId: number;
24+
blockExplorerUrls: string[];
25+
}
26+
27+
export interface NetworkWithInfo extends Network {
28+
chainName: string;
29+
nativeCurrency: {
30+
name: string;
31+
symbol: string; // 2-6 characters long
32+
decimals: 18;
33+
};
34+
iconUrls?: string[]; // Currently ignored.
35+
}
1836
```
1937

2038
## Example
2139
```javascript
22-
import { InjectedConnector } from '@web3-react/injected-connector'
40+
import { InjectedConnector } from '@web3-react-multichain/injected-connector'
41+
42+
const networks = [
43+
{
44+
rpcUrls: [`https://mainnet.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_API_KEY}`],
45+
chainId: 1,
46+
blockExplorerUrls: ['https://etherscan.io'],
47+
chainName: 'Mainnet',
48+
nativeCurrency: {
49+
name: 'ETH',
50+
symbol: 'ETH',
51+
decimals: 18
52+
}
53+
},
54+
{
55+
rpcUrls: [`https://rpc-mainnet.maticvigil.com/v1/${process.env.NEXT_PUBLIC_MATIC_VIGIL_API_KEY}`],
56+
chainId: 137,
57+
blockExplorerUrls: ['https://explorer-mainnet.maticvigil.com'],
58+
chainName: 'Matic',
59+
nativeCurrency: {
60+
name: 'Matic',
61+
symbol: 'MATIC',
62+
decimals: 18
63+
}
64+
}
65+
]
2366

24-
const injected = new InjectedConnector({ supportedChainIds: [1, 3, 4, 5, 42] })
67+
const injected = new InjectedConnector({ networks })
2568
```
2669

2770
## Errors
2871

72+
### AddDefaultChainError
73+
MetaMask throws an error when you call `wallet_addEthereumChain` with one of it's default chains as a parameter. `@web3-react-multichain/injected-connector` is using that rpc method under the hood for `getProvider` so you'll need to prompt users to change their network to a default chain manually.
74+
2975
### NoEthereumProviderError
3076

3177
#### Example
3278
```javascript
33-
import { NoEthereumProviderError } from '@web3-react/injected-connector'
79+
import { NoEthereumProviderError } from '@web3-react-multichain/injected-connector'
3480

3581
function Component () {
3682
const { error } = useWeb3React()
@@ -43,7 +89,7 @@ function Component () {
4389

4490
#### Example
4591
```javascript
46-
import { UserRejectedRequestError } from '@web3-react/injected-connector'
92+
import { UserRejectedRequestError } from '@web3-react-multichain/injected-connector'
4793

4894
function Component () {
4995
const { error } = useWeb3React()

0 commit comments

Comments
 (0)