Skip to content

Commit

Permalink
Merge pull request #131 from vu3th/dev
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
johnson86tw authored May 25, 2023
2 parents eacc577 + 12a4b6c commit 77148f1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions demo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ethers } from 'ethers'
const app = createApp(App)

app.use(VueDapp, {
connectTimeout: 5000,
autoConnect: true,
dumb: false,
networks: {
Expand Down
4 changes: 3 additions & 1 deletion src/components/Board.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export default defineComponent({
const isAutoConnecting = ref(false)
const isAutoConnect = inject('autoConnect')
const connectTimeout = inject('connectTimeout') as number | undefined
onMounted(async () => {
if (isAutoConnect) {
try {
Expand All @@ -70,7 +72,7 @@ export default defineComponent({
const onClickWallet = async (connector: Connector) => {
try {
close()
await connectWith(connector)
await connectWith(connector, connectTimeout)
} catch (err: any) {
props.connectErrorHandler && props.connectErrorHandler(err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/composables/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ export function useWallet(options: useWalletOptions = { useEthers: true }) {
}
}

async function connectWith(connector: Connector) {
async function connectWith(connector: Connector, timeout?: number) {
wallet.error = ''
wallet.status = 'connecting'

// 1. connect wallet
try {
if (!connector) throw new ConnectorNotFoundError()

const { provider } = await connector.connect()
const { provider } = await connector.connect(timeout)

wallet.connector = markRaw(connector)
wallet.provider = markRaw(provider)
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export abstract class Connector<
this.options = options
}

abstract connect(): Promise<Required<ConnectorData>>
abstract connect(timeout?: number): Promise<Required<ConnectorData>>
abstract getProvider(): Promise<Provider>
abstract disconnect(): Promise<void>
abstract onDisconnect(handler: (...args: any[]) => any): void
Expand Down
35 changes: 30 additions & 5 deletions src/connectors/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class MetaMaskConnector extends Connector<
return false
}

async connect() {
async connect(timeout?: number) {
let provider = await this.getProvider()

/**
Expand All @@ -96,10 +96,35 @@ export class MetaMaskConnector extends Connector<

this.#provider = provider

const accounts = await this.#provider.request({
method: 'eth_requestAccounts',
params: [{ eth_accounts: {} }],
})
let accounts

try {
if (timeout) {
accounts = await Promise.race([
this.#provider.request({
method: 'eth_requestAccounts',
params: [{ eth_accounts: {} }],
}),
new Promise<void>((_, reject) =>
setTimeout(() => {
reject(new Error('timeout'))
}, timeout),
),
])
} else {
accounts = await this.#provider.request({
method: 'eth_requestAccounts',
params: [{ eth_accounts: {} }],
})
}
} catch (error: any) {
throw new Error(
`Failed to request MetaMask${
error.message ? ': ' + error.message : ''
}`,
)
}

const account = accounts[0]

return {
Expand Down
4 changes: 4 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type PluginOptions = {
[key: number]: AddEthereumChainParameter
}
dumb: boolean
connectTimeout?: number
}

export const VueDapp: Plugin = {
Expand All @@ -22,6 +23,9 @@ export const VueDapp: Plugin = {
availableNetworks.value = { ...NETWORK_DETAILS, ...options.networks }
}

options?.connectTimeout &&
app.provide('connectTimeout', options?.connectTimeout)

app.provide('autoConnect', options?.autoConnect || false)
if (options?.autoConnect && options?.persistDisconnect === false) {
const { persistDisconnect } = useWallet()
Expand Down

0 comments on commit 77148f1

Please sign in to comment.