1.0.25 • Published 3 years ago

@hashlike-official/extend-web3-react-wrapper v1.0.25

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
3 years ago

@hashlike-official/extend-web3-react-wrapper

It offers a common interface for different wallet providers.
currently, metamask and kaikas.

You can easily extend this library to integrate other wallet what you want.
Just implement abstract classes under src/types folder.

  • abstract class WalletLibrary
  • abstract class WrappedContract

example

Look packages/example-cra

state management

  • states from web3-react
  • currently connected wallet, account, balance
  • functions that can change states
type WalletType = "MetaMask" | "Kaikas";

type WalletLibraryType = {
  connector: Connector;
  hooks: Web3ReactHooks;
  store: Web3ReactStore;
  currentType: WalletType | undefined;
  pending: number;
  balance: number;
  account: string;
  connect: (type: WalletType, chainId?: number) => Promise<void>;
  fetchBalance: (provider: WalletLibrary<any>) => Promise<void>;
};

you can get state inside react components like this:

import { useWeb3Store } from "@hashlike-official/extend-web3-react-wrapper";

export default function MyComponent() {
  const account = useWeb3Store(state => state.account);
  const connect = useWeb3Store(state => state.connect);
  // ...
}

hooks

  • useProvider : it returns connected wallet's provider like Web3Provider(@ethersproject/providers), Caver(caver-js)

    const provider = useProvider();
    // ...
    const getBlockNumber = useCallback(async () => {
      if (provider) {
        const blockNumber = await provider.getBlockNumber();
        setBlockNumber(blockNumber);
      }
    }, [provider]);
    // ...
  • useTransfer : it returns function which can submits transaction to the network.

    const transfer = useTransfer();
    const account = useWeb3Store((state) => state.account);
    // ...
    const sendButtonHandler = () => {
      if (account) {
        transfer(account, sendAddress, sendKlay);
      }
    };
    // ...
  • useInitWallet : you can initialize account and balance with specipic wallet type(metamask, kaikas) which stored in localStorage and specific chainId.(but it doesn't work with kaikas)

    function App() {
      useInitWallet(1001);
      // ...
    }
1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.19

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago