|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { Chain } from '@kolirt/vue-web3-auth' |
| 3 | +import { |
| 4 | + $off, |
| 5 | + $on, |
| 6 | + Events, |
| 7 | + account, |
| 8 | + accountDetails, |
| 9 | + chain, |
| 10 | + getAvailableChains, |
| 11 | + connect as masterConnect, |
| 12 | + disconnect as masterDisconnect, |
| 13 | + switchChain as masterSwitchChain, |
| 14 | + selectChain |
| 15 | +} from '@kolirt/vue-web3-auth' |
| 16 | +import { computed, reactive } from 'vue' |
| 17 | +
|
| 18 | +const loading = reactive({ |
| 19 | + connecting: false, |
| 20 | + connectingTo: {} as Record<number, boolean>, |
| 21 | + switchingTo: {} as Record<number, boolean>, |
| 22 | + logouting: false |
| 23 | +}) |
| 24 | +
|
| 25 | +async function connect(newChain?: Chain) { |
| 26 | + const handler = (state: boolean) => { |
| 27 | + if (!state) { |
| 28 | + if (newChain) { |
| 29 | + loading.connectingTo[newChain.id] = false |
| 30 | + } else { |
| 31 | + loading.connecting = false |
| 32 | + } |
| 33 | +
|
| 34 | + $off(Events.ModalStateChanged, handler) |
| 35 | + } |
| 36 | + } |
| 37 | +
|
| 38 | + $on(Events.ModalStateChanged, handler) |
| 39 | +
|
| 40 | + if (newChain) { |
| 41 | + loading.connectingTo[newChain.id] = true |
| 42 | + } else { |
| 43 | + loading.connecting = true |
| 44 | + } |
| 45 | +
|
| 46 | + await masterConnect(newChain) |
| 47 | +} |
| 48 | +
|
| 49 | +async function disconnect() { |
| 50 | + loading.logouting = true |
| 51 | +
|
| 52 | + const handler = () => { |
| 53 | + loading.logouting = false |
| 54 | + $off(Events.Disconnected, handler) |
| 55 | + } |
| 56 | +
|
| 57 | + $on(Events.Disconnected, handler) |
| 58 | +
|
| 59 | + await masterDisconnect().catch(() => { |
| 60 | + loading.logouting = false |
| 61 | + $off(Events.Disconnected, handler) |
| 62 | + }) |
| 63 | +} |
| 64 | +
|
| 65 | +async function switchChain(newChain: Chain) { |
| 66 | + if (!loading.switchingTo[newChain.id]) { |
| 67 | + loading.switchingTo[newChain.id] = true |
| 68 | + await masterSwitchChain(newChain) |
| 69 | + .then(() => { |
| 70 | + console.log('switchChainSuccess', chain.value) |
| 71 | + }) |
| 72 | + .catch((err) => { |
| 73 | + console.log('switchChainErr', err) |
| 74 | + }) |
| 75 | + .finally(() => { |
| 76 | + loading.switchingTo[newChain.id] = false |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | +
|
| 81 | +async function reconnect(newChain: Chain) { |
| 82 | + if (chain.value.id !== newChain.id) { |
| 83 | + await masterDisconnect() |
| 84 | + await masterConnect(newChain) |
| 85 | + } |
| 86 | +} |
| 87 | +
|
| 88 | +const chains = getAvailableChains() |
| 89 | +const availableChains = computed(() => { |
| 90 | + return chains.filter((item) => item.id !== chain.value.id) |
| 91 | +}) |
| 92 | +</script> |
| 93 | + |
| 94 | +<template> |
| 95 | + <div class="container py-5"> |
| 96 | + <div class="bg-body-secondary rounded p-5"> |
| 97 | + <div class="d-grid d-sm-flex mb-3 gap-2"> |
| 98 | + <img |
| 99 | + src="https://img.shields.io/static/v1?label=Made%20with&message=VueJS&color=limegreen&style=for-the-badge&logo=vue.js" |
| 100 | + alt="vuejs" |
| 101 | + /> |
| 102 | + <img src="https://img.shields.io/badge/Made%20for-Dapps-orange?style=for-the-badge&logo=ethereum" alt="dapp" /> |
| 103 | + </div> |
| 104 | + |
| 105 | + <iframe |
| 106 | + src="https://ghbtns.com/github-btn.html?user=kolirt&repo=vue-web3-auth&type=star&count=true&size=large" |
| 107 | + frameborder="0" |
| 108 | + scrolling="0" |
| 109 | + width="170" |
| 110 | + height="30" |
| 111 | + title="GitHub" |
| 112 | + class="mb-3" |
| 113 | + > |
| 114 | + </iframe> |
| 115 | + |
| 116 | + <div class="d-grid d-sm-flex mb-3 gap-2"> |
| 117 | + <a href="https://github.com/kolirt/vue-web3-auth" target="_blank">Github</a> |
| 118 | + <a href="https://www.npmjs.com/package/@kolirt/vue-web3-auth" target="_blank">Npmjs</a> |
| 119 | + <a href="https://github.com/kolirt/vue-web3-auth/tree/master/README.md" target="_blank">Docs</a> |
| 120 | + <a href="https://github.com/kolirt/vue-web3-auth/tree/master/examples" target="_blank">Example</a> |
| 121 | + </div> |
| 122 | + |
| 123 | + <h1>Web3 authentication for Vue3 apps</h1> |
| 124 | + <p class="lead">Simple WalletConnect Web3Modal v2 integration package for Vue3 dApps</p> |
| 125 | + |
| 126 | + <template v-if="account.connected"> |
| 127 | + <ul> |
| 128 | + <li class="mb-2"> |
| 129 | + <span>Wallet info:</span> |
| 130 | + <ul> |
| 131 | + <li>Connected: {{ account.connected }}</li> |
| 132 | + <li>Address: {{ account.address }}</li> |
| 133 | + <li>Short address: {{ account.shortAddress }}</li> |
| 134 | + </ul> |
| 135 | + </li> |
| 136 | + <li> |
| 137 | + <span>Chain info:</span> |
| 138 | + <ul> |
| 139 | + <li>ID: {{ chain.id }}</li> |
| 140 | + <li>Network: {{ chain.network }}</li> |
| 141 | + <li>Name: {{ chain.name }}</li> |
| 142 | + <li>Native currency: {{ chain.nativeCurrency.symbol }}</li> |
| 143 | + <li> |
| 144 | + Explorer: |
| 145 | + <a :href="chain.blockExplorers?.default.url" target="_blank"> |
| 146 | + {{ chain.blockExplorers?.default.name }} |
| 147 | + </a> |
| 148 | + </li> |
| 149 | + </ul> |
| 150 | + </li> |
| 151 | + </ul> |
| 152 | + |
| 153 | + <hr /> |
| 154 | + |
| 155 | + <div class="d-grid d-sm-flex mb-3 gap-2"> |
| 156 | + <button @click="accountDetails" class="btn btn-primary">Account details</button> |
| 157 | + </div> |
| 158 | + |
| 159 | + <div class="d-grid d-sm-flex mb-3 gap-2"> |
| 160 | + <button @click="selectChain" class="btn btn-primary">Select chain via the wc modal</button> |
| 161 | + </div> |
| 162 | + |
| 163 | + <div class="d-grid d-sm-flex mb-3 gap-2"> |
| 164 | + <button |
| 165 | + v-for="item in availableChains" |
| 166 | + @click="switchChain(item)" |
| 167 | + :key="item.id" |
| 168 | + class="btn btn-outline-primary" |
| 169 | + > |
| 170 | + {{ loading.switchingTo[item.id] ? `Switching chain to ${item.name}...` : `Switch chain to ${item.name}` }} |
| 171 | + </button> |
| 172 | + </div> |
| 173 | + |
| 174 | + <div class="d-grid d-sm-flex mb-3 gap-2"> |
| 175 | + <button |
| 176 | + v-for="item in availableChains" |
| 177 | + @click="reconnect(item)" |
| 178 | + :key="item.id" |
| 179 | + class="btn btn-outline-primary" |
| 180 | + > |
| 181 | + Reconnect to {{ item.name }} |
| 182 | + </button> |
| 183 | + </div> |
| 184 | + |
| 185 | + <hr /> |
| 186 | + |
| 187 | + <button @click="disconnect" class="btn btn-danger"> |
| 188 | + {{ loading.logouting ? 'Logouting...' : 'Logout' }} |
| 189 | + </button> |
| 190 | + </template> |
| 191 | + |
| 192 | + <div v-else class="d-grid d-sm-flex mb-3 gap-2"> |
| 193 | + <button @click="connect()" class="btn btn-primary"> |
| 194 | + {{ loading.connecting ? 'Connecting...' : 'Connect wallet' }} |
| 195 | + </button> |
| 196 | + |
| 197 | + <button v-for="item in chains" @click="connect(item)" :key="item.id" class="btn btn-outline-primary"> |
| 198 | + {{ loading.connectingTo[item.id] ? `Connecting to ${item.name}...` : `Connect to ${item.name}` }} |
| 199 | + </button> |
| 200 | + </div> |
| 201 | + </div> |
| 202 | + </div> |
| 203 | +</template> |
| 204 | + |
| 205 | +<style scoped></style> |
0 commit comments