Git Product home page Git Product logo

nixjs / wallet-connector Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 92 KB

This SDK uses the web3 standard to enable developers to easily connect to a wide range of crypto wallets, such as Binance Chain Wallet, MetaMask, ConnectWallet, and Coin98. More wallets will come in the near future.

Home Page: https://wallet-connector.vercel.app/

JavaScript 0.07% TypeScript 99.17% Shell 0.76%
web3 dapp wallet-connect wallet-connector react-wallet metamask metamask-wallet binance-wallet react wallet-dapp

wallet-connector's Introduction

Wallet Connector @nixjs23n6/wallet-connector

A simple, maximally extensible, dependency minimized framework for building modern Ethereum and other dApps.

@nixjs23n6/wc-core

Install

Install these dependencies:

yarn add @nixjs23n6/wc-core

Setup & Usage

import React, { FC, useMemo, useState } from 'react';
import {
    WALLET_TYPE,
    WalletProvider,
    BinanceProvider,
    MetaMaskProvider
} from '@nixjs23n6/wc-core'

export const Wallet: FC = () => { 
    const [state, setState] = useState({
        walletType: WALLET_TYPE.META_MASK,
        loading: false
    })

    const fetchProviderInstance = useCallback(async (wt: WALLET_TYPE) => {
        try {
            provider = new WalletProvider([BinanceProvider, MetaMaskProvider], {
                logger: {
                    debug: true,
                },
            })
            provider.connect(wt)
            provider.instance.connect()
        } catch (error) {
            console.log(error)
        }
    }, [])

    useEffect(() => {
        fetchProviderInstance(state.walletType)
    }, [state.walletType, fetchProviderInstance])

    useEffect(() => {
        if (provider.instance) {
            provider.instance.getChainId().then((res: any) => {
                console.log('getChainId', res)
            })

            provider.instance.getNetwork().then((res: any) => {
                console.log('getNetwork', res)
            })

            provider.instance.getGasPrice().then((res: any) => {
                console.log('getGasPrice', res)
            })

            provider.instance.getAddress().then((res: any) => {
                console.log('getAddress', res)
            })

            provider.instance.getBalance().then((res: any) => {
                console.log('getBalance', res)
            })

            provider.instance.getTokenDecimal('0xf76205FB45B1c688EEA4423ab5fE386E6c7F78C8').then((res: any) => {
                console.log('getTokenDecimal', res)
            })

            provider.instance.getTokenBalance('0xf76205FB45B1c688EEA4423ab5fE386E6c7F78C8', 18).then((res: any) => {
                console.log('getTokenBalance', res)
            })

            provider.instance.getTransaction('0x3aafdd0dc30dc1965f22d3bdc5d3e6f15a88efd6f83dbbe613ddca5a0cb81d49').then((res: any) => {
                console.log('getTransaction', res)
            })

            provider.instance.getTransactionReceipt('0x3aafdd0dc30dc1965f22d3bdc5d3e6f15a88efd6f83dbbe613ddca5a0cb81d49').then((res: any) => {
                console.log('getTransactionReceipt', res)
            })
            provider.instance.onAccountChanged &&
                provider.instance.onAccountChanged((data: any) => {
                    console.log('onAccountChanged')
                    console.log(data)
                })

            provider.instance.onDisconnect &&
                provider.instance.onDisconnect((data: any) => {
                    console.log('onDisconnect')
                    console.log(data)
                })
        }
    }, [provider, state.walletType])

    return <App/>
};

@nixjs23n6/wc-react

Install

Install these dependencies:

yarn add @nixjs23n6/wc-core @nixjs23n6/wc-react

Setup

import React, { FC, useMemo } from 'react';
import {
    WALLET_TYPE,
    MetaMaskProvider,
    WalletConnect,
    BinanceProvider,
    RPCS_DEFAULT,
    MOBILE_LINKS_DEFAULT,
} from '@nixjs23n6/wc-core'
import { WalletConnectionProvider } from '@nixjs23n6/wc-react'

export const Wallet: FC = () => {
    const wallets = useMemo(() => [MetaMaskProvider, BinanceProvider, WalletConnect], []) 

    return (
        <WalletConnectionProvider
        wallets={wallets}
        logger={{ debug: true }}
        walletConnectConfig={{
            rpc: RPCS_DEFAULT,
            qrcodeModalOptions: {
                mobileLinks: MOBILE_LINKS_DEFAULT,
            },
        }}>
            <WalletComponent/>
            { /* Your app's components go here, nested within the context providers. */ }
        </WalletConnectionProvider>
    );
};

Usage

import React, { FC, useCallback } from 'react';
import { WalletConnectionProvider, useWallet } from '@nixjs23n6/wc-react'

export const WalletComponent: FC = () => {
    const { instance, onConnect, providerConnected, instanceConnected } = useWallet()

    console.log('providerConnected', providerConnected)
    console.log('instanceConnected', instanceConnected)

    useEffect(() => {
        if (providerConnected) onConnect(WALLET_TYPE.META_MASK)
    }, [providerConnected, onConnect])
        useEffect(() => {
        if (instance) {
            instance.getChainId().then((res: any) => {
                console.log('getChainId', res)
            })
        }
    }, [instance])

    useEffect(() => {
        if (instance) {
            instance.getChainId().then((res: any) => {
                console.log('getChainId', res)
            })

            instance.getNetwork().then((res: any) => {
                console.log('getNetwork', res)
            })

            instance.getGasPrice().then((res: any) => {
                console.log('getGasPrice', res)
            })

            instance.getAddress().then((res: any) => {
                console.log('getAddress', res)
            })

            instance.getBalance().then((res: any) => {
                console.log('getBalance', res)
            })

            instance.getTokenDecimal('0xf76205FB45B1c688EEA4423ab5fE386E6c7F78C8').then((res: any) => {
                console.log('getTokenDecimal', res)
            })

            instance.getTokenBalance('0xf76205FB45B1c688EEA4423ab5fE386E6c7F78C8', 18).then((res: any) => {
                console.log('getTokenBalance', res)
            })

            instance.getTransaction('0x3aafdd0dc30dc1965f22d3bdc5d3e6f15a88efd6f83dbbe613ddca5a0cb81d49').then((res: any) => {
                console.log('getTransaction', res)
            })

            instance.getTransactionReceipt('0x3aafdd0dc30dc1965f22d3bdc5d3e6f15a88efd6f83dbbe613ddca5a0cb81d49').then((res: any) => {
                console.log('getTransactionReceipt', res)
            })
            instance.onAccountChanged &&
                instance.onAccountChanged((data: any) => {
                    console.log('onAccountChanged')
                    console.log(data)
                })

            instance.onDisconnect &&
                instance.onDisconnect((data: any) => {
                    console.log('onDisconnect')
                    console.log(data)
                })
        }
    }, [instance])

    return (<div>
        {WALLET_TYPE.META_MASK}
        { /* Your app's components go here, nested within the context providers. */ }
    </div>)
};

wallet-connector's People

Contributors

nixjs avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

jeffyzxc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.