• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language Vue
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A single Web3 / Ethereum provider solution for all Wallets

Web3ModalVue

A single Web3 / Ethereum provider solution for all Wallets

Introduction

Web3Modal is an easy-to-use library to help developers add support for multiple providers in their apps with a simple customizable configuration.

By default Web3Modal Library supports injected providers like (Metamask, Dapper, Gnosis Safe, Frame, Web3 Browsers, etc) and WalletConnect, You can also easily configure the library to support Portis, Fortmatic, Squarelink, Torus, Authereum, D'CENT Wallet and Arkane.

React

web3modal

Usage

  1. Install Web3Modal NPM package
npm install --save web3modal-vue

# OR

yarn add web3modal-vue
  1. Install Provider packages
/* See Provider Options Section */
  1. Then you can add Web3Modal to your Dapp as follows
<template>
  <div id="app">
    <web3-modal-vue
        ref="web3modal"
        :theme="theme"
        :provider-options="providerOptions"
        cache-provider
    />
  </div>
</template>
<script>
import Web3ModalVue from "web3modal-vue";
import WalletConnectProvider from "@walletconnect/web3-provider";
import {web3Modal} from "./config/mixins";
import Header from "@/components/Header";

export default {
  components: {
    Header,
    Web3ModalVue
  },
  mixins: [web3Modal],
  data() {
    return {
      theme: 'light',
      providerOptions: {
        walletconnect: {
          package: WalletConnectProvider,
          options: {
            infuraId: "-"
          }
        }
      },
      number: 0,
      balance: 0,
    }
  },
  created() {
    if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
      this.theme = 'dark'
    }
  },
  mounted() {
    this.$nextTick(async () => {
      const web3modal = this.$refs.web3modal;
      this.$store.commit('setWeb3Modal', web3modal)
      if (web3modal.cachedProvider) {
        await this.$store.dispatch('connect')
        this.subscribeMewBlockHeaders()
      }

    })
  },
  methods: {
    connect() {
      this.$store.dispatch('connect')
    },
  }
}
</script>
import {getLibrary} from "@/utils/web3";
import {ethers} from "ethers";
import {parseInt} from 'lodash'

const web3ModalStore = {
    state: {
        web3Modal: null,

        library: getLibrary(),
        active: false,
        account: null,
        chainId: 0,
    },
    mutations: {
        setWeb3Modal(state, web3Modal) {
            state.web3Modal = web3Modal
        },
        setLibrary(state, library) {
            state.library = library
        },
        setActive(state, active) {
            state.active = active
        },
        setAccount(state, account) {
            state.account = account
        },
        setChainId(state, chainId) {
            state.chainId = chainId
        }
    },
    actions: {
        async connect({state, commit, dispatch}) {
            const provider = await state.web3Modal.connect();

            const library = new ethers.providers.Web3Provider(provider)

            library.pollingInterval = 12000
            commit('setLibrary', library)

            const accounts = await library.listAccounts()
            if (accounts.length > 0) {
                commit('setAccount', accounts[0])
            }
            const network = await library.getNetwork()
            commit('setChainId', network.chainId)
            commit('setActive', true)

            provider.on("connect", async (info) => {
                let chainId = parseInt(info.chainId)
                commit('setChainId', chainId)
                console.log("connect", info)
            });

            provider.on("accountsChanged", async (accounts) => {
                if (accounts.length > 0) {
                    commit('setAccount', accounts[0])
                } else {
                    await dispatch('resetApp')
                }
                console.log("accountsChanged")
            });
            provider.on("chainChanged", async (chainId) => {
                chainId = parseInt(chainId)
                commit('setChainId', chainId)
                console.log("chainChanged", chainId)
            });

        },
        async resetApp({state, commit}) {
            try {
                await state.web3Modal.clearCachedProvider();
            } catch (error) {
                console.error(error)
            }
            commit('setAccount', null)
            commit('setActive', false)
            commit('setLibrary', getLibrary())
        },
    }
}
export default web3ModalStore;

Provider Options

These are all the providers available with Web3Modal and how to configure their provider options:

Who using it

Submit my Dapp

Example

https://github.com/SmallRuralDog/web3modal-vue/tree/master/example

Demo

https://smallruraldog.github.io/web3modal-vue

License

MIT

More Repositories

1

electron-vue-music

基于 electron-vue 开发的音乐播放器,界面模仿QQ音乐,技术栈electron-vue+vue+vuex+vue-router+element- UI。欢迎star
JavaScript
2,119
star
2

vue3-music

VUE3+TS 开发的音乐播放器,界面模仿QQ音乐mac客户端,支持黑夜模式
Vue
1,543
star
3

laravel-vue-admin

开箱即用的Laravel后台扩展,前后端分离,后端控制前端组件,无需编写vue即可创建一个vue+vuex+vue-route+elment-ui+laravel的项目 ,丰富的表单 表格组件,强大的自定义组件功能。
PHP
414
star
4

auto-login-alimama

自动登录阿里妈妈,采集淘宝客推广订单数据,创建推广位,获取推广位列表
Python
309
star
5

cloud-doc

一个在线文档阅读的微信小程序
JavaScript
266
star
6

yundocs

云档3.0版本,taro开发。可打包多平台小程序,h5,IOS和Android客户端。欢迎star跟进
TypeScript
150
star
7

lyf-vue

这是一个基于vue2开发在线商城前端
Vue
125
star
8

laravel-b2c-store

laravel 即插即用的b2c商城扩展。
PHP
122
star
9

pay-school

一个付费课程的前端 基于 vue2.0+vuex+vue-router+vux
Vue
99
star
10

amis-admin

laravel + amis 后台管理系统,完整支持amis全部功能
PHP
92
star
11

cloud-doc-server

云档服务端
PHP
83
star
12

rn-news

react native开发的新闻阅读客户端
JavaScript
51
star
13

gf-websocket

GoFrame websocket 的应用
40
star
14

laravel-b2c-store-react

JavaScript
27
star
15

cloud-doc-v2

云档新版小程序端,基于mpvue开发
Vue
26
star
16

help-center

laravel-admin 帮助中心 工单中心
PHP
24
star
17

light-box

laravel-admin light-box扩展
CSS
8
star
18

atlas-restore

atlas还原工具,layaAri游戏图集还原
Python
7
star
19

laravel-vue-admin-demo-old

PHP
6
star
20

react-native-kuaibao

仿照天天快报的app客户端,基于react native开发
JavaScript
3
star
21

laravel-custom

laravel 5.5+ 自定义的扩展帮助工具
PHP
2
star
22

electron-vue3-music

Vue
2
star
23

w-admin

一个简单易用的中后台,基于typescript + umijs + antd + dva
TypeScript
1
star
24

fant

Lightweight Flutter UI Components built on Vant
Dart
1
star