Skip to content

Commit

Permalink
4.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister committed Apr 14, 2019
1 parent d8dc858 commit 80c2162
Show file tree
Hide file tree
Showing 23 changed files with 705 additions and 635 deletions.
7 changes: 3 additions & 4 deletions .eslintrc
Expand Up @@ -2,8 +2,8 @@
"parser": "@typescript-eslint/parser",
"env": {
"es6": true,
"shared-node-browser": true,
"browser": true
"browser": true,
"jest": true
},
"parserOptions": {
"useJSXTextNode": true,
Expand All @@ -18,8 +18,7 @@
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/interface-name-prefix": "off"
"@typescript-eslint/no-explicit-any": "off"
},
"extends": [
"plugin:react/recommended",
Expand Down
99 changes: 58 additions & 41 deletions Connectors.md
@@ -1,17 +1,17 @@
# Connectors

Connectors are Javascript classes that define how your dApp will interact with the blockchain and user accounts. Connectors are _fully extensible_, meaning that if you don't like any of the options documented below, you can implement your own! `web3-react` will work just the same. For more information, see [Custom-Connectors.md](./Custom-Connectors.md).
Connectors are Javascript classes that define how your dApp will interact with the Ethereum blockchain and user accounts. Connectors are _fully extensible_, meaning that if you don't like any of the options documented below, you can implement your own! `web3-react` will work just the same. For more information, see [Custom-Connectors.md](./Custom-Connectors.md).

Note: Some Connectors throw specific errors that can be identified and handled appropriately by your dApp. In general, these error codes are available in the `.errorCodes` property of any Connector.

### `InjectedConnector`
## `InjectedConnector`

Manages connectivity to injected web3 providers such as [MetaMask](https://metamask.io/) (or [Trust](https://trustwallet.com/)/[Tokenary](https://tokenary.io/)/etc.).

```javascript
import { Connectors } from 'web3-react'

const metaMaskConnector = Connectors.InjectedConnector({
const MetaMask = Connectors.InjectedConnector({
supportedNetworks: [...]
})
```
Expand All @@ -23,41 +23,43 @@ Arguments:
Throws:

- `Connector.UNSUPPORTED_NETWORK`: Thrown if a `supportedNetworks` array is provided, and the user is not on one of those networks.

- `InjectedConnector.ETHEREUM_ACCESS_DENIED`: Thrown when a user denies permission for your dApp to access their account.

- `InjectedConnector.LEGACY_PROVIDER`: Thrown when no global `ethereum` object is available, only the deprecated `web3` object.

- `InjectedConnector.NO_WEB3`: Thrown when visiting from a non-web3 browser.

- `InjectedConnector.UNLOCK_REQUIRED`: Thrown when a user's account is locked.

### `NetworkOnlyConnector``
## `NetworkOnlyConnector``

Manages connectivity to a remote web3 provider such as [Infura](https://infura.io/).
Manages connectivity to a remote web3 provider such as [Infura](https://infura.io/) (or [Quiknode](https://quiknode.io/)/etc.).

```javascript
import { Connectors } from 'web3-react'

const infuraConnector = Connectors.NetworkOnlyConnector({
providerURL: ...,
supportedNetworks: [...]
const Infura = Connectors.NetworkOnlyConnector({
providerURL: ...
})
```

Arguments:

- `providerURL` - The URL of a remote node.
- `supportedNetworks: number[]` (optional): Enforces that the provider is connected to a particular network, throwing an error if not.

Throws:

- `Connector.UNSUPPORTED_NETWORK`: Thrown if a `supportedNetworks` array is provided, and the user is not on one of those networks.

### `TrezorConnector` _EXPERIMENTAL_
## `TrezorConnector`

Manages connectivity to a [Trezor](https://trezor.io/) device. Note: Currently, only the first account is exported/made accessible. If this limits your dApp's functionality, please file an issue.
Manages connectivity to a [Trezor](https://trezor.io/) device. Note: Currently, only the first account is exported/made accessible. If this limits your dApp's functionality, please [file an issue](https://github.com/NoahZinsmeister/web3-react/issues).

```javascript
import { Connectors } from 'web3-react'

const trezorConnector = Connectors.TrezorConnector({
const Trezor = Connectors.TrezorConnector({
supportedNetworkURLs: { ... },
defaultNetwork: ...,
manifestEmail: ...,
Expand All @@ -68,21 +70,25 @@ const trezorConnector = Connectors.TrezorConnector({
Arguments:

- `supportedNetworkURLs: any` - An object whose keys are network IDs, and values are remote nodes connected to that network ID.

- `defaultNetwork: number` - The network ID that the connector will use by default.

- `manifestEmail: string` - [Manifest email](https://github.com/trezor/connect/blob/develop/docs/index.md)

- `manifestAppUrl: string` - [Manifest email](https://github.com/trezor/connect/blob/develop/docs/index.md)

Methods:

- `changeNetwork(networkId: number)` - Changes to a different network in `supportedNetworkURLs`.

### `LedgerConnector` _EXPERIMENTAL_
## `LedgerConnector`

Manages connectivity to a [Ledger](https://www.ledger.com/) device. Note: Currently, only the first account is exported/made accessible. If this limits your dApp's functionality, please file an issue.
Manages connectivity to a [Ledger](https://www.ledger.com/) device. Note: Currently, only the first account is exported/made accessible. If this limits your dApp's functionality, please [file an issue](https://github.com/NoahZinsmeister/web3-react/issues).

```javascript
import { Connectors } from 'web3-react'

const ledgerConnector = Connectors.LedgerConnector({
const Ledger = Connectors.LedgerConnector({
supportedNetworkURLs: { ... },
defaultNetwork: ...
})
Expand All @@ -91,37 +97,65 @@ const ledgerConnector = Connectors.LedgerConnector({
Arguments:

- `supportedNetworkURLs: any` - An object whose keys are network IDs, and values are remote nodes connected to that network ID.

- `defaultNetwork: number` - The network ID that the connector will use by default.

Methods:

- `changeNetwork(networkId: number)` - Changes to a different network in `supportedNetworkURLs`.

### `FortmaticConnector` _EXPERIMENTAL_
## `WalletConnectConnector`

Manages connectivity to a [WalletConnect](https://walletconnect.org/) wallet.

```javascript
import { Connectors } from 'web3-react'

const WalletConnect = Connectors.WalletConnectConnector({
bridge: ...,
supportedNetworkURLs: ...,
defaultNetwork: ...
})
```

Arguments:

- `bridge: string` - The URL of the WalletConnect bridge.

- `supportedNetworkURLs: any` - An object whose keys are network IDs, and values are remote nodes that can connect to that network ID.

- `defaultNetwork: number` - The network ID that the connector will use by default.

## `FortmaticConnector`

Manages connectivity to [Fortmatic](https://fortmatic.com/).

```javascript
import { Connectors } from 'web3-react'

const fortmaticConnector = Connectors.FortmaticConnector({
const Fortmatic = Connectors.FortmaticConnector({
apiKey: ...,
logoutOnDeactivation: ...
logoutOnDeactivation: ...,
testNetwork: ...
})
```

Arguments:

- `apiKey: string` - Fortmatic API key.

- `logoutOnDeactivation: boolean` - Whether to log the user out or not when the connector is unset, `false` by default.
-
### `PortisConnector` _EXPERIMENTAL_

- `testNetwork: string` (optional) - A network to initialize the Formatic SDK with.

## `PortisConnector`

Manages connectivity to [Portis](https://www.portis.io/).

```javascript
import { Connectors } from 'web3-react'

const portisConnector = Connectors.PortisConnector({
const Portis = Connectors.PortisConnector({
dAppId: ...,
network: ...,
options: ...
Expand All @@ -131,28 +165,11 @@ const portisConnector = Connectors.PortisConnector({
Arguments:

- `dAppId: string` - Fortmatic API key.

- `network: any` - The network you wish to connect to.

- `options: any` (optional) - Portis SDK initialization object.

Methods:
- `changeNetwork(networkId: string)` - Changes to a different network.

### `WalletConnectConnector` _EXPERIMENTAL_

Manages connectivity to a [WalletConnect](https://walletconnect.org/) wallet.

```javascript
import { Connectors } from 'web3-react'

const walletConnectConnector = Connectors.WalletConnectConnector({
bridge: ...,
supportedNetworkURLs: ...,
defaultNetwork: ...
})
```

Arguments:

- `bridge: string` - The URL of the WalletConnect bridge.
- `supportedNetworkURLs: any` - An object whose keys are network IDs, and values are remote nodes that can connect to that network ID.
- `defaultNetwork: number` - The network ID that the connector will use by default.
- `changeNetwork(networkId: string)` - Changes to a different network.
2 changes: 1 addition & 1 deletion Contributing-Guidelines.md
Expand Up @@ -4,7 +4,7 @@ I welcome any and all questions/suggestions/PRs/etc.! If you're interested in he

- The project is written in Typescript, all source files are in the [`src/`](./src/) directory.

- All files are linted with [TSlint](https://palantir.github.io/tslint/), and formatted with [Prettier](https://prettier.io/). You can run `yarn ensureLinted` and `yarn ensureFormatted` to check this.
- All files are linted with [ESLint](https://eslint.org/), and formatted with [Prettier](https://prettier.io/). You can run `yarn ensureLinted` and `yarn ensureFormatted` to check that your code is conformant.

- To build/watch for changes, run `yarn {build,watch}`.

Expand Down
21 changes: 13 additions & 8 deletions Custom-Connectors.md
@@ -1,6 +1,6 @@
# Custom Connectors

Building your own connector is as simple as defining a class that implements a specific interface.
Building your own connector is as simple as defining a class that implements a specific interface!

## Extending the Abstract `Connector` Class

Expand Down Expand Up @@ -35,14 +35,20 @@ async getNetworkId(provider: Provider): Promise<number>
async getAccount(provider: Provider): Promise<string | null>
```

## Using `@0x/subproviders` / `web3-provider-engine`

It's likely that if you're writing a custom connector, you'll want to use the convenience classes exported by [`@0x/subproviders`](https://github.com/0xProject/0x-monorepo/tree/development/packages/subproviders) (which in turn relies on [web3-provider-engine](https://github.com/MetaMask/web3-provider-engine)), as most of the default connectors do. To facilitate this, all exports of `@0x/subproviders` are provided under the named `subproviders` export of this package.

```javascript
import { subproviders } from 'web3-react'
```

## Communicating From Your Connector

`Connectors` can emit one of 5 events to trigger specific functionality within `web3-react`. The way to do this is to call the appropriate wrapper function in the underlying `Connector` class:
`Connectors` can emit one of 3 events to trigger specific functionality within `web3-react`. The way to do this is to call the appropriate wrapper function in the underlying `Connector` class:

- `_web3ReactUpdateNetworkIdHandler(networkId: number, bypassCheck: boolean = false)`: update the current network ID. Passing `bypassCheck` as `true` prevents the `.getNetworkId` method of the active connector from being called.
- `_web3ReactUpdateAccountHandler(account: string, bypassCheck: boolean = false)`: update the current account. Passing `bypassCheck` as `true` prevents the `.getAccount` method of the active connector from being called.
- `_web3ReactUpdateNetworkIdAndAccountHandler(networkId: number, bypassNetworkIdCheck: boolean = false, account: string bypassAccountCheck: boolean = false)`: update the current network ID and account. Passing either `bypassCheck` as `true` prevents the `.getNetworkId` or `.getAccount` methods of the active connector from being called.
- `_web3ReactErrorHandler(error: Error)`: set an error.
- `_web3ReactUpdateHandler({ updateNetworkId?: boolean, updateAccount?: boolean, overrideNetworkIdCheck?: boolean, overrideAccountCheck?: boolean, networkId?: number, account?: string})`: update the current network ID and/or account. One/Both of `updateNetworkId`/`updateAccount` must be passed. `networkId`/`account` are optional arguments that indicate the new network id account (if not passed, these values are fetched using `.getNetworkId`/`.getAccount`). `overrideNetworkIdCheck`/`overrideAccountCheck` control whether, if `networkId`/`account` are passed, whether to override calling the `.getNetworkId`/`.getAccount` methods of the active connector.
- `_web3ReactErrorHandler(error: Error, clearConnector?: boolean)`: set an error, optionally clearing the current active connector.
- `_web3ReactResetHandler()`: unset the connector.

## Using `ErrorCodeMixin`
Expand All @@ -63,6 +69,5 @@ class MyConnector extends ErrorCodeMixin(Connector, MyConnectorErrorCodes) {
This ensures that a static `.errorCodes` property will exist on your connector, which will contain an object with keys and values equal to the passed error code strings.

```javascript
console.log(MyConnector.errorCodes)
// { MY_ERROR_CODE: 'MY_ERROR_CODE', ... }
console.log(MyConnector.errorCodes) // { MY_ERROR_CODE: 'MY_ERROR_CODE', ... }
```

0 comments on commit 80c2162

Please sign in to comment.