diff --git a/src/controllers/TorusController.js b/src/controllers/TorusController.js index a49c0b00c..f31f28e17 100644 --- a/src/controllers/TorusController.js +++ b/src/controllers/TorusController.js @@ -368,13 +368,18 @@ export default class TorusController extends EventEmitter { async initTorusKeyring(keyArray, addresses) { await Promise.all([this.keyringController.deserialize(keyArray), this.accountTracker.syncWithAddresses(addresses)]) - this.notifyAllConnections({ - method: NOTIFICATION_NAMES.unlockStateChanged, - params: { - isUnlocked: true, - accounts: [this.prefsController.store.getState().selectedAddress], - }, - }) + } + + unlock() { + if (this.prefsController.store.getState().selectedAddress) { + this.notifyAllConnections({ + method: NOTIFICATION_NAMES.unlockStateChanged, + params: { + isUnlocked: true, + accounts: [this.prefsController.store.getState().selectedAddress], + }, + }) + } } async addAccount(key, address) { @@ -390,6 +395,7 @@ export default class TorusController extends EventEmitter { this.walletConnectController.setSelectedAddress(address) this.gasFeeController.getGasFeeEstimatesAndStartPolling() } + this.unlock() } /** diff --git a/src/store/store.js b/src/store/store.js index fd5c09559..f0b9986b9 100644 --- a/src/store/store.js +++ b/src/store/store.js @@ -77,6 +77,15 @@ const getBalance = async (state, key) => }, 500) }) +const fetchGasFeeEstimates = async (state) => { + try { + return torus.torusController.gasFeeController.fetchGasFeeEstimates() + } catch (error) { + log.warn(error, 'failed fetching gas estimates') + return state.gasFees + } +} + const VuexStore = new Vuex.Store({ plugins: vuexPersist ? [vuexPersist.plugin] : [], state: defaultState, @@ -91,6 +100,7 @@ const VuexStore = new Vuex.Store({ const windowId = isTx ? payload.id : payload const channelName = `torus_channel_${windowId}` const finalUrl = `${baseRoute}confirm?instanceId=${windowId}&integrity=true&id=${windowId}` + const popupPayload = { id: windowId, origin: getIFrameOriginObject(), @@ -102,7 +112,6 @@ const VuexStore = new Vuex.Store({ whiteLabel: state.whiteLabel, selectedAddress: state.selectedAddress, networkDetails: state.networkDetails, - gasFees: state.gasFees, } if (isTx) { const txParameters = payload @@ -116,14 +125,17 @@ const VuexStore = new Vuex.Store({ popupPayload.type = type } let weiBalance = 0 + let latestGasFee = {} try { - weiBalance = await getBalance(state, state.selectedAddress) + // polling might delay fetching fee or might have outdated fee, so getting latest fee. + ;[weiBalance, latestGasFee] = await Promise.all([getBalance(state, state.selectedAddress), fetchGasFeeEstimates(state)]) } catch (error) { log.error(error, 'Unable to fetch balance within 5 secs') handleDeny(windowId, popupPayload.type) return } popupPayload.balance = fromWei(weiBalance.toString()) + popupPayload.gasFees = latestGasFee if (request.isWalletConnectRequest && isMain) { const originObj = { href: '', hostname: '' } try {