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

Transaction notifications #4840

Merged
merged 5 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"activeTab",
"webRequest",
"*://*.eth/",
"*://*.test/"
"*://*.test/",
"notifications"
],
"web_accessible_resources": [
"inpage.js"
Expand Down
7 changes: 7 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ module.exports = class MetamaskController extends EventEmitter {
})
this.txController.on('newUnapprovedTx', opts.showUnapprovedTx.bind(opts))

this.txController.on(`tx:status-update`, (txId, status) => {
if (status === 'confirmed' || status === 'failed' || status === 'dropped') {
const txMeta = this.txController.txStateManager.getTx(txId)
this.platform.showTransactionNotification(txMeta)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the generic handler.

}
})

// computed balances (accounting for pending transactions)
this.balancesController = new BalancesController({
accountTracker: this.accountTracker,
Expand Down
64 changes: 64 additions & 0 deletions app/scripts/platforms/extension.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const extension = require('extensionizer')
const explorerLink = require('etherscan-link').createExplorerLink

class ExtensionPlatform {

Expand Down Expand Up @@ -31,6 +32,69 @@ class ExtensionPlatform {
cb(e)
}
}

showTransactionNotification (txMeta) {

const status = txMeta.status
if (status === 'confirmed') {
this._showConfirmedTransaction(txMeta)
} else if (status === 'failed') {
this._showFailedTransaction(txMeta)
} else if (status === 'dropped') {
this._showDroppedTransaction(txMeta)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was already on the fence about showing dropped txs, but if Firefox risks not showing any, I think I'd rather remove the dropped handler.

}
}

_showConfirmedTransaction (txMeta) {

this._subscribeToNotificationClicked()

const url = explorerLink(txMeta.hash, parseInt(txMeta.metamaskNetworkId))
const nonce = parseInt(txMeta.txParams.nonce, 16)

const title = 'Confirmed transaction'
const message = `Transaction ${nonce} confirmed! View on EtherScan`
this._showNotification(title, message, url)
}

_showFailedTransaction (txMeta) {

const nonce = parseInt(txMeta.txParams.nonce, 16)
const title = 'Failed transaction'
const message = `Transaction ${nonce} failed! ${txMeta.err.message}`
this._showNotification(title, message)
}

_showDroppedTransaction (txMeta) {

const nonce = parseInt(txMeta.txParams.nonce, 16)
const title = 'Dropped transaction'
const message = `Transaction ${nonce} was dropped, because another transaction with that number was successfully processed.`
this._showNotification(title, message)
}

_showNotification (title, message, url) {
extension.notifications.create(
url,
{
'type': 'basic',
'title': title,
'iconUrl': extension.extension.getURL('../../images/icon-64.png'),
'message': message,
})
}

_subscribeToNotificationClicked () {
if (!extension.notifications.onClicked.hasListener(this._viewOnEtherScan)) {
extension.notifications.onClicked.addListener(this._viewOnEtherScan)
}
}

_viewOnEtherScan (txId) {
if (txId.startsWith('http://')) {
global.metamaskController.platform.openWindow({ url: txId })
}
}
}

module.exports = ExtensionPlatform