Skip to content

Commit ea5e4b4

Browse files
committed
Add watch function to the switchChain method to handle chain switching more accurately than before
1 parent f2fc6bb commit ea5e4b4

31 files changed

+6482
-2341
lines changed

.eslintrc.cjs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@ require('@rushstack/eslint-patch/modern-module-resolution')
33

44
module.exports = {
55
root: true,
6-
'extends': [
6+
extends: [
77
'plugin:vue/vue3-essential',
88
'eslint:recommended',
99
'@vue/eslint-config-typescript',
10-
'@vue/eslint-config-prettier/skip-formatting',
10+
'@vue/eslint-config-prettier/skip-formatting'
1111
],
1212
parserOptions: {
1313
ecmaVersion: 'latest'
1414
},
1515
rules: {
1616
'vue/multi-word-component-names': 'off'
1717
},
18-
ignorePatterns: [
19-
'dist/**/*',
20-
'demo/**/*',
21-
'examples/**/*'
22-
]
18+
ignorePatterns: ['dist/**/*', 'demo/**/*', 'examples/**/*']
2319
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ dev:
33
format:
44
yarn format
55
check:
6-
yarn type-check-lib && yarn lint
6+
yarn type-check && yarn lint
77
build:
88
yarn build-lib && yarn build-demo
99
build-demo:

env.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module '*.vue' {
2-
import type { DefineComponent } from 'vue'
3-
const component: DefineComponent<{}, {}, any>
4-
export default component
5-
}
2+
import type { DefineComponent } from 'vue'
3+
const component: DefineComponent<{}, {}, any>
4+
export default component
5+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Vue 3 + TypeScript + Vite
2+
3+
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4+
5+
## Recommended IDE Setup
6+
7+
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).
8+
9+
## Type Support For `.vue` Imports in TS
10+
11+
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types.
12+
13+
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps:
14+
15+
1. Disable the built-in TypeScript Extension
16+
1. Run `Extensions: Show Built-in Extensions` from VSCode's command palette
17+
2. Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
18+
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + Vue + TS</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "vite",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vue-tsc && vite build",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"@kolirt/vue-web3-auth": "^2",
13+
"bootstrap": "^5.3.2",
14+
"vue": "^3.3.8"
15+
},
16+
"devDependencies": {
17+
"@vitejs/plugin-vue": "^4.5.0",
18+
"typescript": "^5.2.2",
19+
"vite": "^5.0.0",
20+
"vue-tsc": "^1.8.22"
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import App from './App.vue'
2+
3+
import { Chains, createWeb3Auth } from '@kolirt/vue-web3-auth'
4+
import 'bootstrap/dist/css/bootstrap.min.css'
5+
import { createApp } from 'vue'
6+
7+
const app = createApp(App)
8+
9+
app.use(
10+
createWeb3Auth({
11+
projectId: '3c5c8069ff37304cc62e07ae8cb592a8', // generate here https://cloud.walletconnect.com/ and turn on 'Supports Sign v2'
12+
logEnabled: true,
13+
chains: [
14+
Chains.bsc,
15+
Chains.mainnet,
16+
Chains.polygon,
17+
Chains.avalanche,
18+
Chains.polygonMumbai,
19+
Chains.sepolia,
20+
Chains.linea,
21+
Chains.bscTestnet
22+
]
23+
})
24+
)
25+
26+
app.mount('#app')

0 commit comments

Comments
 (0)