Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update metamask error handler flow #393

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/components/mixins/WalletConnectMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import ethersUtil, { Provider } from '@/utils/ethers-util'

import TranslationMixin from '@/components/mixins/TranslationMixin'

const checkExtensionKey = 'provider.messages.checkExtension'
const installExtensionKey = 'provider.messages.installExtension'

const getProviderName = provider => {
switch (provider) {
case Provider.Metamask:
Expand All @@ -22,7 +25,7 @@ const handleProviderError = (provider: Provider, error: any): string => {
case Provider.Metamask:
return handleMetamaskError(error)
default:
return 'provider.messages.checkExtension'
return checkExtensionKey
}
}

Expand All @@ -35,7 +38,7 @@ const handleMetamaskError = (error: any): string => {
case 4001:
return 'provider.messages.extensionLogin'
default:
return 'provider.messages.checkExtension'
return checkExtensionKey
}
}

Expand Down Expand Up @@ -73,11 +76,22 @@ export default class WalletConnectMixin extends Mixins(TranslationMixin) {
await this.connectExternalAccount({ provider })
} catch (error) {
const name = this.t(getProviderName(provider))
const message = this.te(error.message)
const key = this.te(error.message)
? error.message
: handleProviderError(provider, error)

this.$alert(this.t(message, { name }))
const message = this.t(key, { name })
const showCancelButton = key === installExtensionKey

this.$alert(message, {
showCancelButton,
cancelButtonText: this.t('provider.messages.reloadPage'),
callback: action => {
if (action === 'cancel') {
router.go(0)
}
}
})
} finally {
this.isExternalWalletConnecting = false
}
Expand Down Expand Up @@ -110,16 +124,14 @@ export default class WalletConnectMixin extends Mixins(TranslationMixin) {
}

async syncExternalAccountWithAppState () {
const connected = await ethersUtil.checkAccountIsConnected(this.evmAddress)

if (connected) return

await this.disconnectExternalAccount()

const account = await ethersUtil.getAccount()
try {
const connected = await ethersUtil.checkAccountIsConnected(this.evmAddress)

if (account) {
await this.changeExternalWallet({ address: account })
if (!connected && this.evmAddress) {
await this.disconnectExternalAccount()
}
} catch (error) {
await this.disconnectExternalAccount()
}
}
}
3 changes: 2 additions & 1 deletion src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,8 @@
"messages": {
"checkExtension": "{name} extension is busy, please check it",
"extensionLogin": "Please login to your {name} extension",
"installExtension": "{name} extension is not found. Please install it!"
"installExtension": "{name} extension is not found. Please install it!\n\nAlready installed extension? Please reload the page",
"reloadPage": "Reload page"
}
}
}
3 changes: 2 additions & 1 deletion src/lang/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ export default {
messages: {
checkExtension: '{name} extension is busy, please check it',
extensionLogin: 'Please login to your {name} extension',
installExtension: '{name} extension is not found. Please install it!'
installExtension: '{name} extension is not found. Please install it!\n\nAlready installed extension? Please reload the page',
reloadPage: 'Reload page'
}
}
}
17 changes: 6 additions & 11 deletions src/utils/ethers-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async function onConnect (options: ConnectOptions): Promise<string> {
}

async function onConnectMetamask (): Promise<string> {
provider = await detectEthereumProvider() as any
provider = await detectEthereumProvider({ timeout: 0 }) as any
if (!provider) {
throw new Error('provider.messages.installExtension')
}
Expand All @@ -203,15 +203,10 @@ async function onConnectWallet (url = 'https://cloudflare-eth.com'): Promise<str
}

async function getAccount (): Promise<string> {
try {
const ethersInstance = await getEthersInstance()
await ethersInstance.send('eth_requestAccounts', [])
const account = ethersInstance.getSigner()
return account.getAddress()
} catch (error) {
console.error(error)
return ''
}
const ethersInstance = await getEthersInstance()
await ethersInstance.send('eth_requestAccounts', [])
const account = ethersInstance.getSigner()
return account.getAddress()
}

// TODO: remove this check, when MetaMask issue will be resolved
Expand All @@ -227,7 +222,7 @@ async function checkAccountIsConnected (address: string): Promise<boolean> {

async function getEthersInstance (): Promise<ethersProvider> {
if (!provider) {
provider = await detectEthereumProvider() as any
provider = await detectEthereumProvider({ timeout: 0 }) as any
}
if (!provider) {
throw new Error('No ethereum provider instance!')
Expand Down
4 changes: 0 additions & 4 deletions src/views/Rewards.vue
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,6 @@ export default class Rewards extends Mixins(WalletConnectMixin, TransactionMixin
}
}

async handleWalletChange (): Promise<void> {
await this.changeExternalAccountProcess()
}

private async checkAccountRewards (showNotification = false): Promise<void> {
if (this.isSoraAccountConnected) {
await this.getRewardsProcess(showNotification)
Expand Down