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

Fix/pss 539 metamsk lock issue #108

Merged
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
5 changes: 4 additions & 1 deletion src/store/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ const getters = {

const mutations = {
[types.RESET] (state) {
// we shouldn't reset networks, setted from env
const networkSettingsKeys = ['soraNetwork', 'defaultEthNetwork']
const s = initialState()
Object.keys(s).forEach(key => {

Object.keys(s).filter(key => !networkSettingsKeys.includes(key)).forEach(key => {
state[key] = s[key]
})
},
Expand Down
25 changes: 21 additions & 4 deletions src/utils/web3-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,22 @@ async function onConnectWallet (url = 'https://cloudflare-eth.com'): Promise<str
rpc: { 1: url }
})
await provider.enable()
const web3Instance = new Web3(provider)
const accounts = await web3Instance.eth.getAccounts()
return accounts.length ? accounts[0] : ''

const account = await getAccount()

return account
}

async function getAccount (): Promise<string> {
try {
const web3Instance = await getInstance()
const accounts = await web3Instance.eth.getAccounts()

return accounts.length ? accounts[0] : ''
} catch (error) {
console.error(error)
return ''
}
}

async function getInstance (): Promise<Web3> {
Expand All @@ -168,12 +181,15 @@ async function watchEthereum (cb: {
onDisconnect: Function;
}) {
const ethereum = (window as any).ethereum

if (ethereum) {
ethereum.on('accountsChanged', cb.onAccountChange)
ethereum.on('chainChanged', cb.onNetworkChange)
}

await getInstance()

if (provider) {
provider.on('accountsChanged', cb.onAccountChange)
provider.on('disconnect', cb.onDisconnect)
}
}
Expand Down Expand Up @@ -256,6 +272,7 @@ async function executeContractMethod ({

export default {
onConnect,
getAccount,
storeEthUserAddress,
getEthUserAddress,
storeEthNetwork,
Expand Down
56 changes: 46 additions & 10 deletions src/views/Bridge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ export default class Bridge extends Mixins(
onAccountChange: (addressList: string[]) => {
if (addressList.length) {
this.switchEthAccount({ address: addressList[0] })
this.updateRegisteredAssetsExternalBalance()
} else {
this.disconnectEthWallet()
}
Expand All @@ -403,6 +404,7 @@ export default class Bridge extends Mixins(
this.setEthNetwork(networkId)
this.getEthNetworkFee()
this.getRegisteredAssets()
this.updateExternalBalances()
},
onDisconnect: (code: number, reason: string) => {
this.disconnectEthWallet()
Expand All @@ -413,9 +415,12 @@ export default class Bridge extends Mixins(
}

destroyed (): void {
if (this.blockHeadersSubscriber) {
this.blockHeadersSubscriber.unsubscribe()
}
this.unsubscribeEthBlockHeaders()
}

updateExternalBalances (): void {
this.getEthBalance()
this.updateRegisteredAssetsExternalBalance()
}

connectInternalWallet (): void {
Expand All @@ -424,19 +429,34 @@ export default class Bridge extends Mixins(

async subscribeToEthBlockHeaders (): Promise<void> {
try {
await this.unsubscribeEthBlockHeaders()

const web3 = await web3Util.getInstance()

this.blockHeadersSubscriber = web3.eth.subscribe('newBlockHeaders', (error) => {
if (!error) {
this.getEthBalance()
this.updateRegisteredAssetsExternalBalance()
this.updateExternalBalances()
}
})
} catch (error) {
console.error(error)
}
}

unsubscribeEthBlockHeaders (): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.blockHeadersSubscriber) return resolve()

this.blockHeadersSubscriber.unsubscribe((error) => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}

async connectExternalWallet (): Promise<void> {
// For now it's only Metamask
if (this.isMetamaskConnecting) {
Expand Down Expand Up @@ -535,7 +555,19 @@ export default class Bridge extends Mixins(
// TODO: Check balance (ETH)
}

handleConfirmTransaction (): void {
// TODO: remove this check, when MetaMask issue will be resolved
// https://github.com/MetaMask/metamask-extension/issues/10368
async checkAccountIsConnected (): Promise<boolean> {
const account = await web3Util.getAccount()

return !!account
}

async handleConfirmTransaction (): Promise<void> {
const accountIsConnected = await this.checkAccountIsConnected()

if (!accountIsConnected) return

this.showConfirmTransactionDialog = true
}

Expand All @@ -559,10 +591,14 @@ export default class Bridge extends Mixins(
}
}

confirmTransaction (isTransactionConfirmed: boolean) {
if (isTransactionConfirmed) {
router.push({ name: PageNames.BridgeTransaction })
}
async confirmTransaction (isTransactionConfirmed: boolean): Promise<void> {
if (!isTransactionConfirmed) return

const accountIsConnected = await this.checkAccountIsConnected()

if (!accountIsConnected) return

router.push({ name: PageNames.BridgeTransaction })
}
}
</script>
Expand Down