Skip to content

Commit

Permalink
fix: mitigate metamasks method isApproved hanging forever (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo authored and cazala committed Dec 7, 2018
1 parent 656ab38 commit 4a74b78
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/modules/wallet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function isApprovableWallet() {
return ethereum !== undefined && typeof ethereum.enable === 'function'
}

export async function isWalletApproved() {
export async function isWalletApproved(): Promise<boolean> {
const { ethereum } = window as EthereumWindow

if (ethereum === undefined) {
Expand All @@ -65,6 +65,24 @@ export async function isWalletApproved() {

// `isApproved` is not standard. It's supported by MetaMask and it's expected to be implemented by other wallet vendors
// but we need to check just in case.
const aprobable = ethereum._metamask || ethereum
return aprobable.isApproved ? await aprobable.isApproved() : true
const approvable = ethereum._metamask || ethereum

if (!approvable.isApproved) {
return true
}

// `isApproved` sometimes hangs and never resolves or rejects the promise.
// To mitigate this we'll use Promise.race, but we need `hasFinished` to avoid firing both functions.
// `Promise.race` will *not* cancel the slower promise
let hasFinished = false

return await Promise.race([
approvable.isApproved().then(result => {
hasFinished = true
return result
}),
new Promise(resolve => setTimeout(resolve, 150)).then(
() => (!hasFinished ? isWalletApproved() : false)
)
])
}

0 comments on commit 4a74b78

Please sign in to comment.