0.1.15 • Published 2 years ago

@decent-org/wallet-provider v0.1.15

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Decent Wallet Provider

Easily connect a dApp to a web3 wallet. Built with the help of web3modal and etherss' providers, this package quickly gets a dApp connected and interacting with an EVM compaitble blockchain with just a few props and 2 components.

Supported Wallets

Injected Wallets

Metamask, Frame etc

WalletConnect

WalletConnect Provider allows connection to mobile and supported desktop apps

Installation

Versions

React

This package uses react hooks and the Context API. react@16 or greater is required.

React-scripts

if using react-scripts version 5.0.0 or greater, react-app-rewired will be needed, along with process, buffer, util packages.

npm install react-app-rewired process buffer util

and then add config-overrifdes.js from the sandbox to allow for these node packages to be added to webpack.

Provider

Web3Provider is uses React Context API to pass the wallet provider's state to the rest of the dApp. Simply Wrap the component tree you wish to have access to the wallet provider's state.

import { Web3Provider } from '@decent-org/wallet-provider'

ReactDOM.render(
  <Web3Provider config={config} theme={theme}>
    <App />
  </Web3Provider>
);

<Web3Provider> accepts 2 props.

namedefaulttyperequireddescription
config-DWPConfigtrueProvider configurations
theme'light'string | ModalThemefalseWeb3Modal theme settings

DWPConfig

Configuration for the wallet-provider.

namedefaulttyperequireddescription
providerKeys-ProviderKeysAt least one key is required.Node api keys for fallback provider
localChainIdundefinedstringfalseChain id for local node
localProviderURLundefinedstringfalseproviderURL for local node
fallbackChainId-stringtrueChain Id for when wallet is not connected
supportedChainIds-stringtrueSupported main/test net chain ids. Should be formatted as 1,3,4,42

Theme

This property is optional. See web3modal for more details.

Usage

The useWeb3Provider() hook allows access to the Wallet Provider and connection information within state. For typed definitions see types

export interface IWeb3ProviderContext {
  state: InitialState;
  connect: ConnectFn;
  disconnect: DisconnectFn;
}
import { useWeb3Provider } from '@decent-org/wallet-provider';

function Component() {
  const { state: { account, chaindId, network, connectionType } } = useWeb3Provider();

  console.log(account)
  // if connected 0x.... 
  // if not connected null
  console.log(chainId)
  // 0x04
  console.log(network)
  // 'rinkeby'
  console.log(connectionType)
  // 'injected provider'
  ... 
}

Connecting and Disconnect to Wallet

import { useWeb3Provider } from '@decent-org/wallet-provider';

function Component() {
  const { state: { account },  connect, disconnect } = useWeb3Provider();

  if(!account) {
    return (
      <button onClick={connect}>Connect</button>
    )
    return (
      <button onClick={disconnect}>Disconnect</button>
    )
  }
}

Interacting with the blockchain

function Component() {
  const { state: { provider, signer } } = useWeb3Provider();

  // When retreiving information use provider
    provider.on('block',  (block: number) => {
      setBlockNumber(block);
    };);

  // When broadcasting a transaction or interacting with a contract use Signer
  ontract.connect(daoData.moduleAddresses[1], signer);
}

Events

Add an event eventListener and subscribe to wallet-provider events. There is a Constant you can import to ensure correct subscription. For an example using React Toastify see the useEvents.

import { PROVIDER_EVENT } from '@decent-org/wallet-provider';

window.addEventListener(PROVIDER_EVENT, (event: CustomEventInit<WalletProviderEvent>) => {
      console.log(event.detail!.message)
      return
    })
  }
titletypedescriptionex: message
UNSUPPORTED_CHAIN_IDSwarnConnected chain id is not supportedSwitch to a supported chain: 1, 4

Utilities

Coming soon

Local Development

Some scripts have been created to help get going quickly

Node

nvm use to change to node version 16.15.1

Scripts

install:packages

removes node_modules from root and sandbox directories before installing packages.

npm run install:packages

build:link

Cleans, builds and then re-establishes a local npm link between the packages dist directory and a package within the node_modules of the sandbox.

npm run build:link

build:start

Note: There currently is not a hotloader to automatically build when files are updated. after running npm run build:start, you will need to restart your development server. It's recommended you spilt your terminal to run these commands as needed.

This script will do all of the above and restart sever. currently quickest way to update sandbox with more recent build.

npm run build:start

start

From the root directory you can start the development server directly, no need to cd into sandbox

npm run start
0.1.15

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago