Skip to content

Commit

Permalink
Merge pull request #1661 from torusresearch/fix/unlock-notification-fix
Browse files Browse the repository at this point in the history
unlock notification sent only after address is updated
  • Loading branch information
chaitanyapotti authored Aug 24, 2021
2 parents 01ced8a + e6cbb38 commit af35ccb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/controllers/TorusController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -390,6 +395,7 @@ export default class TorusController extends EventEmitter {
this.walletConnectController.setSelectedAddress(address)
this.gasFeeController.getGasFeeEstimatesAndStartPolling()
}
this.unlock()
}

/**
Expand Down
16 changes: 14 additions & 2 deletions src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit af35ccb

Please sign in to comment.