Catalog npm
Back to packageRELEASE DETAILS

0.13.2

npm

@0xgabi/use-wallet

[<img src="https://img.shields.io/npm/v/use-wallet" alt="" />](https://www.npmjs.com/package/use-wallet) [<img src="https://img.shields.io/bundlephobia/minzip/use-wallet" alt="" />](https://bundlephobia.com/result?p=use-wallet)

Published16 Feb 2022
Direct dependencies37
Transitive dependenciesNot calculated

README

Current package documentation

👛 useWallet()

useWallet() allows dapp users to connect to the provider of their choice in a way that is as straightforward as possible. It provides a common data structure for any connected account, no matter what provider has been chosen by the user. It aims to provide some features that are often reimplemented by dapp developers: connecting to a wallet, keeping track of transactions, and more (submit a issue or PR!).

Features

  • All-in-one solution to connect to Ethereum providers.
  • Completely library agnostic (use Web3.js, ethers.js, …).
  • Provides the current balance.
  • Keeps track of the recent transactions (coming soon).

Opinionated?

Oh yes:

  • React only.
  • Ethereum only (for now).
  • Supports one network at a time.
  • Embeds as many providers as possible.
  • Every prop and parameter is optional.

What useWallet() is not

  • An Ethereum wallet implementation.
  • A low-level, fully configurable connection system for Ethereum providers (see web3-react if you are after that).
  • An general library to interact with Ethereum (see ethers.js, Web3.js, etc.).

Used by

Usage

Add it to your project:

yarn add use-wallet

Use it in your React app:

// App.js

import React from 'react'
import { useWallet, UseWalletProvider } from 'use-wallet'

function App() {
  const wallet = useWallet()
  const blockNumber = wallet.getBlockNumber()

  return (
    <>
      <h1>Wallet</h1>
      {wallet.status === 'connected' ? (
        <div>
          <div>Account: {wallet.account}</div>
          <div>Balance: {wallet.balance}</div>
          <button onClick={() => wallet.reset()}>disconnect</button>
        </div>
      ) : (
        <div>
          Connect:
          <button onClick={() => wallet.connect()}>MetaMask</button>
          <button onClick={() => wallet.connect('frame')}>Frame</button>
          <button onClick={() => wallet.connect('portis')}>Portis</button>
        </div>
      )}
    </>
  )
}

// Wrap everything in <UseWalletProvider />
export default () => (
  <UseWalletProvider
    chainId={1}
    connectors={{
      // This is how connectors get configured
      portis: { dAppId: 'my-dapp-id-123-xyz' },
    }}
  >
    <App />
  </UseWalletProvider>
)

API

<UseWalletProvider />

This is the provider component. It should be placed above any component using useWallet(). Apart from children, it accepts two other props:

chainId

The Chain ID supported by the connection. Defaults to 1.

connectors

Configuration for the different connectors. If you use a connector that requires a configuration but do not provide one, an error will be thrown.

  • injected: no configuration needed.
  • frame: no configuration needed.
  • fortmatic: { apiKey }
  • portis: { dAppId }
  • provided: { provider }
  • authereum: no configuration needed.
  • squarelink: { clientId, options }
  • walletconnect: { rpc: { 1: 'https://rpc-url', 3: 'https://rpc-url' } }
  • walletlink: { url, appName, appLogoUrl }

See the web3-react documentation for more details.

autoConnect

Automatically connect to wallet on page load. Defaults to false.

useWallet()

This is the hook to be used throughout the app.

It takes an optional object as a single param, containing the following:

  • pollBalanceInterval: the interval used to poll the wallet balance. Defaults to 2000.
  • pollBlockNumberInterval: the interval used to poll the block number. Defaults to 5000.

It returns an object representing the connected account (“wallet”), containing:

  • account: the address of the account, or null when disconnected.
  • balance: the balance of the account, in wei.
  • chainId: The specified Chain ID of the network you're connected to.
  • connect(connectorId): call this function with a connector ID to “connect” to a provider (see above for the connectors provided by default).
  • connector: The "key" of the wallet you're connected to (e.g "metamask", "portis").
  • connectors: the full list of connectors.
  • error: contains an error object with the corresponding name and message if status is set to error.
  • ethereum: the connected Ethereum provider.
  • getBlockNumber(): this function returns the current block number. This is a function because the block number updates often, which could trigger as many extra renders. Making an explicit call for the block number allows users of useWallet() to avoid extra renders when the block number is not needed.
  • isConnected(): this function returns whether the wallet is connected.
  • networkName: a human-readable name corresponding to the Chain ID.
  • reset(): call this function to “disconnect” from the current provider. This will also clean the latest error value stored in use-wallet's state.
  • status: contains the current status of the wallet connection. The possible values are:
    • "disconnected": no wallet connected (default state).
    • "connecting": trying to connect to the wallet.
    • "connected": connected to the wallet (i.e. the account is available).
    • "error": a connection error happened.
  • type: whether or not the account is a contract. Can be null when you're disconnected, or either "contract" or "normal".

Examples

To run the examples, switch to the respective directories. Then, simply run yarn install to install, and yarn start to run the examples on localhost:1234.

Special thanks

useWallet() is a built on web3-react and its connectors, which are doing all the hard work internally.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 37

Native requirements
37 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
@aragon/provided-connector
Environment
{
  "local_name": "@aragon/provided-connector",
  "alias": false,
  "peer_metadata": null
}
^6.0.8runtime
@typescript-eslint/parser
Environment
{
  "local_name": "@typescript-eslint/parser",
  "alias": false,
  "peer_metadata": null
}
^4.1.0runtime
@web3-react/core
Environment
{
  "local_name": "@web3-react/core",
  "alias": false,
  "peer_metadata": null
}
6.1.9runtime
@0xgabi/frame-connector
Environment
{
  "local_name": "@web3-react/frame-connector",
  "alias": true,
  "peer_metadata": null
}
npm:@0xgabi/frame-connector@6.0.10runtime
@web3-react/injected-connector
Environment
{
  "local_name": "@web3-react/injected-connector",
  "alias": false,
  "peer_metadata": null
}
6.0.7runtime
@web3-react/torus-connector
Environment
{
  "local_name": "@web3-react/torus-connector",
  "alias": false,
  "peer_metadata": null
}
^6.2.9runtime
@web3-react/walletconnect-connector
Environment
{
  "local_name": "@web3-react/walletconnect-connector",
  "alias": false,
  "peer_metadata": null
}
6.2.4runtime
jsbi
Environment
{
  "local_name": "jsbi",
  "alias": false,
  "peer_metadata": null
}
^3.1.4runtime
prop-types
Environment
{
  "local_name": "prop-types",
  "alias": false,
  "peer_metadata": null
}
^15.7.2runtime
regenerator-runtime
Environment
{
  "local_name": "regenerator-runtime",
  "alias": false,
  "peer_metadata": null
}
^0.13.7runtime
rollup-plugin-image-files
Environment
{
  "local_name": "rollup-plugin-image-files",
  "alias": false,
  "peer_metadata": null
}
^1.4.2runtime
@types/react
Environment
{
  "local_name": "@types/react",
  "alias": false,
  "peer_metadata": null
}
^16.9.49development
@types/react-dom
Environment
{
  "local_name": "@types/react-dom",
  "alias": false,
  "peer_metadata": null
}
^16.9.8development
@typescript-eslint/eslint-plugin
Environment
{
  "local_name": "@typescript-eslint/eslint-plugin",
  "alias": false,
  "peer_metadata": null
}
^4.1.0development
@web3-react/abstract-connector
Environment
{
  "local_name": "@web3-react/abstract-connector",
  "alias": false,
  "peer_metadata": null
}
^6.0.7development
babel-eslint
Environment
{
  "local_name": "babel-eslint",
  "alias": false,
  "peer_metadata": null
}
^10.1.0development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^7.8.1development
eslint-config-prettier
Environment
{
  "local_name": "eslint-config-prettier",
  "alias": false,
  "peer_metadata": null
}
^6.9.0development
eslint-config-react-app
Environment
{
  "local_name": "eslint-config-react-app",
  "alias": false,
  "peer_metadata": null
}
^5.2.1development
eslint-config-standard
Environment
{
  "local_name": "eslint-config-standard",
  "alias": false,
  "peer_metadata": null
}
^14.1.0development
eslint-config-standard-react
Environment
{
  "local_name": "eslint-config-standard-react",
  "alias": false,
  "peer_metadata": null
}
^9.2.0development
eslint-plugin-flowtype
Environment
{
  "local_name": "eslint-plugin-flowtype",
  "alias": false,
  "peer_metadata": null
}
^5.2.0development
eslint-plugin-import
Environment
{
  "local_name": "eslint-plugin-import",
  "alias": false,
  "peer_metadata": null
}
^2.20.0development
eslint-plugin-jsx-a11y
Environment
{
  "local_name": "eslint-plugin-jsx-a11y",
  "alias": false,
  "peer_metadata": null
}
^6.3.1development
eslint-plugin-node
Environment
{
  "local_name": "eslint-plugin-node",
  "alias": false,
  "peer_metadata": null
}
^11.0.0development
eslint-plugin-prettier
Environment
{
  "local_name": "eslint-plugin-prettier",
  "alias": false,
  "peer_metadata": null
}
^3.1.2development
eslint-plugin-promise
Environment
{
  "local_name": "eslint-plugin-promise",
  "alias": false,
  "peer_metadata": null
}
^4.2.1development
eslint-plugin-react
Environment
{
  "local_name": "eslint-plugin-react",
  "alias": false,
  "peer_metadata": null
}
^7.20.6development
eslint-plugin-react-hooks
Environment
{
  "local_name": "eslint-plugin-react-hooks",
  "alias": false,
  "peer_metadata": null
}
^4.1.0development
eslint-plugin-standard
Environment
{
  "local_name": "eslint-plugin-standard",
  "alias": false,
  "peer_metadata": null
}
^4.0.1development
husky
Environment
{
  "local_name": "husky",
  "alias": false,
  "peer_metadata": null
}
^4.3.0development
prettier
Environment
{
  "local_name": "prettier",
  "alias": false,
  "peer_metadata": null
}
^2.1.1development
tsdx
Environment
{
  "local_name": "tsdx",
  "alias": false,
  "peer_metadata": null
}
^0.14.1development
tslib
Environment
{
  "local_name": "tslib",
  "alias": false,
  "peer_metadata": null
}
^2.0.1development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^4.0.2development
react
Environment
{
  "local_name": "react",
  "alias": false,
  "peer_metadata": null
}
^16.13.1peer
react-dom
Environment
{
  "local_name": "react-dom",
  "alias": false,
  "peer_metadata": null
}
^16.13.1peer

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-qDfn5IeN72CF62q0NI7u2Gbt7NQBw/a3zSaN4LI6/fw9uVIUFfhz0hwgKmsfbpu0g38wUIzkkPqxBr3Gwlq5vA==",
  "shasum": "0493752250a7f44178795d8273ebfbcf011df47f",
  "tarball": "https://registry.npmjs.org/@0xgabi/use-wallet/-/use-wallet-0.13.2.tgz",
  "fileCount": 24,
  "unpackedSize": 308001,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJiDUwhCRA9TVsSAnZWagAAPTEP/jq2umW5JQXbbiHnh0yb\nPEHBYlPQSd/c0dWRFtVTeN5VaJjEF8Czeou3ESN6XBvb9QtV1xH6oSEgaFZp\n1RiylFcCAUYBUEFJY9zk211hSFeGlfIwmqMi80nYv5p+TKuZcvQfZ7Cx4HIR\nLzyB4Zz+637YF6abMCUxF2AXo1FFECSSpVeCBbCGmVAi0XcRl1EBfiKvkVQy\niETgMmpE8zaotSemWUFztwfdhMFB0HEbHDSU5qaVhGnNX7E1QYD0xM2MVnCX\n4+/mz5uNhguuaGsC6eX8Nu3RjEhuR8KNhJksvI0Vdj3VhQ2e9+ShgPncspVt\nwxTznE1jCAL10Vs0uBfn4Ng9zhxW8cMkh505j3MAjDxKkd8CU2umAXOvPH0n\ntBSqmOdXCgtIv+Wc8kOYtJPt/kiAKX5TyJtAoY+2M/OuE3ggBt26TiHEK52T\nbYaKpvekbmxgOYYfEYYWz4jwyjp/pwemclUpKUCcs//HS3MdbSjKk3jwr3GN\nsAg9ienJm/fo1/G0bjPXHSyvKyYnE30VOgMcMdnLnE3o1J26Gy17+csy6bD6\ntSxRXnzbyyCgHEjWH3MtW5UCeqhc1K2lC97lgdl3DdyoJr19SUxl4Z1X5JZz\nAToNgWilmisyMEEY0vA0U/Wedw6k1nWudOVeRqIkIpYpNflKGNADlx0ITcjZ\n8+uQ\r\n=3SEg\r\n-----END PGP SIGNATURE-----\r\n",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIQC/M3sJnlbovZ7SOgcYdFQRG7vhJpO4cMvy7hrVkCH34wIgahCELD2YkwS2cmrHUIm4V5i0JjMIjXOfZg0AviMz8Cg="
    }
  ]
}

Metadata history

Meaningful changes
Current collected state Current

Observed since 21 Oct, 08:55 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:8a8faa7559ca3c69fa5fce8c06b2c88ddbecdb7ba724beec30a721027f7b8894

Other collected versions

0.15.010 Aug0.14.22 Aug0.14.12 Aug0.14.02 Aug0.13.416 Feb0.13.316 FebView all collected versions →