1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-22 17:33:23 +01:00

Take some of the tx Logic out of the UI and create a visble state for pending and unaproved transactions

This commit is contained in:
Frankie 2017-01-10 11:52:25 -08:00
parent af312bdc90
commit 0fae263a9a
10 changed files with 68 additions and 54 deletions

View File

@ -27,7 +27,6 @@ function triggerUi () {
if (!popupIsOpen) notification.show() if (!popupIsOpen) notification.show()
} }
// On first install, open a window to MetaMask website to how-it-works. // On first install, open a window to MetaMask website to how-it-works.
extension.runtime.onInstalled.addListener(function (details) { extension.runtime.onInstalled.addListener(function (details) {
if ((details.reason === 'install') && (!METAMASK_DEBUG)) { if ((details.reason === 'install') && (!METAMASK_DEBUG)) {
extension.tabs.create({url: 'https://metamask.io/#how-it-works'}) extension.tabs.create({url: 'https://metamask.io/#how-it-works'})

View File

@ -95,7 +95,6 @@ module.exports = class KeyringController extends EventEmitter {
isInitialized: (!!wallet || !!vault), isInitialized: (!!wallet || !!vault),
isUnlocked: Boolean(this.password), isUnlocked: Boolean(this.password),
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(),
transactions: this.configManager.getTxList(),
unconfMsgs: messageManager.unconfirmedMsgs(), unconfMsgs: messageManager.unconfirmedMsgs(),
messages: messageManager.getMsgList(), messages: messageManager.getMsgList(),
selectedAccount: address, selectedAccount: address,
@ -273,6 +272,7 @@ module.exports = class KeyringController extends EventEmitter {
setSelectedAccount (address) { setSelectedAccount (address) {
var addr = normalize(address) var addr = normalize(address)
this.configManager.setSelectedAccount(addr) this.configManager.setSelectedAccount(addr)
this.emit('update')
return Promise.resolve(addr) return Promise.resolve(addr)
} }

View File

@ -64,6 +64,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.ethStore.on('update', this.sendUpdate.bind(this)) this.ethStore.on('update', this.sendUpdate.bind(this))
this.keyringController.on('update', this.sendUpdate.bind(this)) this.keyringController.on('update', this.sendUpdate.bind(this))
this.txManager.on('update', this.sendUpdate.bind(this))
} }
getState () { getState () {

View File

@ -25,9 +25,8 @@ module.exports = class TransactionManager extends EventEmitter {
getState () { getState () {
var selectedAccount = this.getSelectedAccount() var selectedAccount = this.getSelectedAccount()
return { return {
transactions: this.getTxList(),
unconfTxs: this.getUnapprovedTxList(), unconfTxs: this.getUnapprovedTxList(),
selectedAccountTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAccount}), transactions: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAccount}),
} }
} }
@ -113,10 +112,26 @@ module.exports = class TransactionManager extends EventEmitter {
txDidComplete (txMeta, onTxDoneCb, cb, err) { txDidComplete (txMeta, onTxDoneCb, cb, err) {
if (err) return cb(err) if (err) return cb(err)
var {maxCost, txFee} = this.getMaxTxCostAndFee(txMeta)
txMeta.maxCost = maxCost
txMeta.txFee = txFee
this.addTx(txMeta, onTxDoneCb) this.addTx(txMeta, onTxDoneCb)
cb(null, txMeta) cb(null, txMeta)
} }
getMaxTxCostAndFee (txMeta) {
var txParams = txMeta.txParams
var gasMultiplier = txMeta.gasMultiplier
var gasCost = new BN(ethUtil.stripHexPrefix(txParams.gas || txMeta.estimatedGas), 16)
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice || '0x4a817c800'), 16)
gasPrice = gasPrice.mul(new BN(gasMultiplier * 100), 10).div(new BN(100, 10))
var txFee = gasCost.mul(gasPrice)
var txValue = new BN(ethUtil.stripHexPrefix(txParams.value || '0x0'), 16)
var maxCost = txValue.add(txFee)
return {maxCost, txFee}
}
getUnapprovedTxList () { getUnapprovedTxList () {
var txList = this.getTxList() var txList = this.getTxList()
return txList.filter((txMeta) => txMeta.status === 'unapproved') return txList.filter((txMeta) => txMeta.status === 'unapproved')
@ -227,6 +242,7 @@ module.exports = class TransactionManager extends EventEmitter {
setTxStatusConfirmed (txId) { setTxStatusConfirmed (txId) {
this._setTxStatus(txId, 'confirmed') this._setTxStatus(txId, 'confirmed')
this.emit('update')
} }
// merges txParams obj onto txData.txParams // merges txParams obj onto txData.txParams
@ -240,17 +256,20 @@ module.exports = class TransactionManager extends EventEmitter {
// checks if a signed tx is in a block and // checks if a signed tx is in a block and
// if included sets the tx status as 'confirmed' // if included sets the tx status as 'confirmed'
checkForTxInBlock () { checkForTxInBlock () {
var signedTxList = this.getFilteredTxList({status: 'signed', err: undefined}) var signedTxList = this.getFilteredTxList({status: 'signed'})
if (!signedTxList.length) return if (!signedTxList.length) return
signedTxList.forEach((tx) => { signedTxList.forEach((tx) => {
var txHash = tx.hash var txHash = tx.hash
var txId = tx.id var txId = tx.id
if (!txHash) return if (!txHash) {
tx.err = { errCode: 'No hash was provided', message: 'Tx could possibly have not been submitted or an error accrued during signing'}
return this.updateTx(tx)
}
this.txProviderUtils.query.getTransactionByHash(txHash, (err, txMeta) => { this.txProviderUtils.query.getTransactionByHash(txHash, (err, txMeta) => {
if (err || !txMeta) { if (err) {
tx.err = err || 'Tx could possibly have not been submitted' tx.err = {errorCode: err, message: 'Tx could possibly have not been submitted to the block chain',}
this.updateTx(tx) this.updateTx(tx)
return txMeta ? console.error(err) : console.debug(`txMeta is ${txMeta} for:`, tx) return console.error(err)
} }
if (txMeta.blockNumber) { if (txMeta.blockNumber) {
this.setTxStatusConfirmed(txId) this.setTxStatusConfirmed(txId)

View File

@ -26,11 +26,10 @@ function mapStateToProps (state) {
accounts: state.metamask.accounts, accounts: state.metamask.accounts,
address: state.metamask.selectedAccount, address: state.metamask.selectedAccount,
accountDetail: state.appState.accountDetail, accountDetail: state.appState.accountDetail,
transactions: state.metamask.transactions,
network: state.metamask.network, network: state.metamask.network,
unconfTxs: valuesFor(state.metamask.unconfTxs),
unconfMsgs: valuesFor(state.metamask.unconfMsgs), unconfMsgs: valuesFor(state.metamask.unconfMsgs),
shapeShiftTxList: state.metamask.shapeShiftTxList, shapeShiftTxList: state.metamask.shapeShiftTxList,
transactions: state.metamask.transactions,
} }
} }
@ -248,20 +247,11 @@ AccountDetailScreen.prototype.subview = function () {
} }
AccountDetailScreen.prototype.transactionList = function () { AccountDetailScreen.prototype.transactionList = function () {
const { transactions, unconfTxs, unconfMsgs, address, network, shapeShiftTxList } = this.props const {transactions, unconfMsgs, address, network, shapeShiftTxList } = this.props
var txsToRender = transactions.concat(unconfTxs)
// only transactions that are from the current address
.filter(tx => tx.txParams.from === address)
// only transactions that are on the current network
.filter(tx => tx.txParams.metamaskNetworkId === network)
// sort by recency // sort by recency
.sort((a, b) => b.time - a.time)
return h(TransactionList, { return h(TransactionList, {
txsToRender, transactions,
network, network,
unconfTxs,
unconfMsgs, unconfMsgs,
address, address,
shapeShiftTxList, shapeShiftTxList,

View File

@ -7,8 +7,6 @@ const EthBalance = require('./eth-balance')
const util = require('../util') const util = require('../util')
const addressSummary = util.addressSummary const addressSummary = util.addressSummary
const nameForAddress = require('../../lib/contract-namer') const nameForAddress = require('../../lib/contract-namer')
const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN
module.exports = PendingTxDetails module.exports = PendingTxDetails
@ -29,15 +27,9 @@ PTXP.render = function () {
var account = props.accounts[address] var account = props.accounts[address]
var balance = account ? account.balance : '0x0' var balance = account ? account.balance : '0x0'
var gasMultiplier = txData.gasMultiplier var txFee = txData.txFee
var gasCost = new BN(ethUtil.stripHexPrefix(txParams.gas || txData.estimatedGas), 16) var maxCost = txData.maxCost
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice || '0x4a817c800'), 16)
gasPrice = gasPrice.mul(new BN(gasMultiplier * 100), 10).div(new BN(100, 10))
var txFee = gasCost.mul(gasPrice)
var txValue = new BN(ethUtil.stripHexPrefix(txParams.value || '0x0'), 16)
var maxCost = txValue.add(txFee)
var dataLength = txParams.data ? (txParams.data.length - 2) / 2 : 0 var dataLength = txParams.data ? (txParams.data.length - 2) / 2 : 0
var imageify = props.imageifyIdenticons === undefined ? true : props.imageifyIdenticons var imageify = props.imageifyIdenticons === undefined ? true : props.imageifyIdenticons
return ( return (

View File

@ -13,15 +13,31 @@ function TransactionIcon () {
TransactionIcon.prototype.render = function () { TransactionIcon.prototype.render = function () {
const { transaction, txParams, isMsg } = this.props const { transaction, txParams, isMsg } = this.props
if (transaction.status === 'unapproved') {
return h('.unapproved-tx', {
style: {
width: '15px',
height: '15px',
background: '#00bfff',
borderRadius: '13px',
},
})
if (transaction.status === 'rejected') { } else if (transaction.status === 'rejected') {
return h('i.fa.fa-exclamation-triangle.fa-lg.warning', { return h('i.fa.fa-exclamation-triangle.fa-lg.warning', {
style: { style: {
width: '24px', width: '24px',
}, },
}) })
} else if (transaction.status === 'signed') {
return h('i.fa.fa-ellipsis-h', {
style: {
fontSize: '27px',
},
})
} }
if (isMsg) { if (isMsg) {
return h('i.fa.fa-certificate.fa-lg', { return h('i.fa.fa-certificate.fa-lg', {
style: { style: {

View File

@ -8,6 +8,7 @@ const explorerLink = require('../../lib/explorer-link')
const CopyButton = require('./copyButton') const CopyButton = require('./copyButton')
const vreme = new (require('vreme')) const vreme = new (require('vreme'))
const extension = require('../../../app/scripts/lib/extension') const extension = require('../../../app/scripts/lib/extension')
const Tooltip = require('./tooltip')
const TransactionIcon = require('./transaction-list-item-icon') const TransactionIcon = require('./transaction-list-item-icon')
const ShiftListItem = require('./shift-list-item') const ShiftListItem = require('./shift-list-item')
@ -59,11 +60,7 @@ TransactionListItem.prototype.render = function () {
}, [ }, [
h('.identicon-wrapper.flex-column.flex-center.select-none', [ h('.identicon-wrapper.flex-column.flex-center.select-none', [
transaction.status === 'unapproved' ? h('i.fa.fa-ellipsis-h', { h('.pop-hover', {
style: {
fontSize: '27px',
},
}) : h('.pop-hover', {
onClick: (event) => { onClick: (event) => {
event.stopPropagation() event.stopPropagation()
if (!isTx || isPending) return if (!isTx || isPending) return
@ -139,7 +136,14 @@ function failIfFailed (transaction) {
if (transaction.status === 'rejected') { if (transaction.status === 'rejected') {
return h('span.error', ' (Rejected)') return h('span.error', ' (Rejected)')
} }
if (transaction.status === 'failed') { if (transaction.err) {
return h('span.error', ' (Failed)')
return h(Tooltip, {
title: transaction.err.message,
position: 'bottom',
}, [
h('span.error', ' (Failed)'),
])
} }
} }

View File

@ -13,12 +13,13 @@ function TransactionList () {
} }
TransactionList.prototype.render = function () { TransactionList.prototype.render = function () {
const { txsToRender, network, unconfMsgs } = this.props const { transactions = [], network, unconfMsgs } = this.props
var shapeShiftTxList var shapeShiftTxList
if (network === '1') { if (network === '1') {
shapeShiftTxList = this.props.shapeShiftTxList shapeShiftTxList = this.props.shapeShiftTxList
} }
const transactions = !shapeShiftTxList ? txsToRender.concat(unconfMsgs) : txsToRender.concat(unconfMsgs, shapeShiftTxList) const txsToRender = !shapeShiftTxList ? transactions.concat(unconfMsgs) : txsToRender.concat(unconfMsgs, shapeShiftTxList)
.sort((a, b) => b.time - a.time) .sort((a, b) => b.time - a.time)
return ( return (
@ -55,8 +56,8 @@ TransactionList.prototype.render = function () {
}, },
}, [ }, [
transactions.length txsToRender.length
? transactions.map((transaction, i) => { ? txsToRender.map((transaction, i) => {
let key let key
switch (transaction.key) { switch (transaction.key) {
case 'shapeshift': case 'shapeshift':

View File

@ -46,7 +46,6 @@ ConfirmTxScreen.prototype.render = function () {
var txData = unconfTxList[index] || unconfTxList[0] || {} var txData = unconfTxList[index] || unconfTxList[0] || {}
var txParams = txData.txParams || {} var txParams = txData.txParams || {}
var isNotification = isPopupOrNotification() === 'notification' var isNotification = isPopupOrNotification() === 'notification'
return ( return (
h('.flex-column.flex-grow', [ h('.flex-column.flex-grow', [
@ -125,17 +124,10 @@ function currentTxView (opts) {
} }
ConfirmTxScreen.prototype.checkBalanceAgainstTx = function (txData) { ConfirmTxScreen.prototype.checkBalanceAgainstTx = function (txData) {
var state = this.props var state = this.props
var address = txData.txParams.from || state.selectedAccount
var txParams = txData.txParams || {}
var address = txParams.from || state.selectedAccount
var account = state.accounts[address] var account = state.accounts[address]
var balance = account ? account.balance : '0x0' var balance = account ? account.balance : '0x0'
var maxCost = new BN(txData.maxCost)
var gasCost = new BN(ethUtil.stripHexPrefix(txParams.gas || txData.estimatedGas), 16)
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice || '0x4a817c800'), 16)
var txFee = gasCost.mul(gasPrice)
var txValue = new BN(ethUtil.stripHexPrefix(txParams.value || '0x0'), 16)
var maxCost = txValue.add(txFee)
var balanceBn = new BN(ethUtil.stripHexPrefix(balance), 16) var balanceBn = new BN(ethUtil.stripHexPrefix(balance), 16)
return maxCost.gt(balanceBn) return maxCost.gt(balanceBn)