1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00

add notifications

This commit is contained in:
Csaba Solya 2018-07-20 13:20:40 +02:00
parent 6921f94bfe
commit a3822b4680
4 changed files with 71 additions and 30 deletions

View File

@ -1,27 +0,0 @@
const extension = require('extensionizer')
// Confirmed tx
// Transaction ${tx.nonce} confirmed! View on Etherscan
// Failed tx
// Transaction ${tx.nonce} failed. (Maybe append tx.error.message)
// Dropped tx
// A Transaction ${tx.nonce} was dropped, because another transaction with that number was successfully processed.
function showConfirmedNotification (txMeta) {
extension.notifications.create({
"type": "basic",
"title": "Confirmed transaction",
"iconUrl": extension.extension.getURL('../../../../images/icon-64.png'),
"message": JSON.stringify(txMeta)
});
}
/**
@module
*/
module.exports = {
showConfirmedNotification
}

View File

@ -5,7 +5,6 @@ const ethUtil = require('ethereumjs-util')
const log = require('loglevel')
const txStateHistoryHelper = require('./lib/tx-state-history-helper')
const createId = require('../../lib/random-id')
const transactionNotificationManager = require('./lib/transaction-notification-manager')
const { getFinalStates } = require('./lib/util')
/**
TransactionStateManager is responsible for the state of a transaction and
@ -333,8 +332,6 @@ class TransactionStateManager extends EventEmitter {
*/
setTxStatusConfirmed (txId) {
this._setTxStatus(txId, 'confirmed')
const txMeta = this.getTx(txId)
transactionNotificationManager.showConfirmedNotification(txMeta)
}
/**

View File

@ -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)
}
})
// computed balances (accounting for pending transactions)
this.balancesController = new BalancesController({
accountTracker: this.accountTracker,

View File

@ -1,4 +1,5 @@
const extension = require('extensionizer')
const explorerLink = require('etherscan-link').createExplorerLink
class ExtensionPlatform {
@ -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)
}
}
_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