1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Adds updateTransaction to background and used it to update after editing in send-v2.

This commit is contained in:
Dan 2017-12-20 13:47:16 -03:30 committed by Chi Kei Chan
parent 1f1fc2c49e
commit bf4043c59b
8 changed files with 85 additions and 101 deletions

View File

@ -184,16 +184,15 @@ module.exports = class TransactionController extends EventEmitter {
return await this.txGasUtil.analyzeGasUsage(txMeta) return await this.txGasUtil.analyzeGasUsage(txMeta)
} }
async updateTransaction (txMeta) {
this.txStateManager.updateTx(txMeta, 'confTx: user updated transaction')
}
async updateAndApproveTransaction (txMeta) { async updateAndApproveTransaction (txMeta) {
this.txStateManager.updateTx(txMeta, 'confTx: user approved transaction') this.txStateManager.updateTx(txMeta, 'confTx: user approved transaction')
await this.approveTransaction(txMeta.id) await this.approveTransaction(txMeta.id)
} }
async updateAndCancelTransaction (txMeta) {
this.txStateManager.updateTx(txMeta, 'confTx: user rejected transaction')
await this.cancelTransaction(txMeta.id)
}
async approveTransaction (txId) { async approveTransaction (txId) {
let nonceLock let nonceLock
try { try {

View File

@ -365,8 +365,8 @@ module.exports = class MetamaskController extends EventEmitter {
// txController // txController
cancelTransaction: nodeify(txController.cancelTransaction, txController), cancelTransaction: nodeify(txController.cancelTransaction, txController),
updateTransaction: nodeify(txController.updateTransaction, txController),
updateAndApproveTransaction: nodeify(txController.updateAndApproveTransaction, txController), updateAndApproveTransaction: nodeify(txController.updateAndApproveTransaction, txController),
updateAndCancelTransaction: nodeify(txController.updateAndCancelTransaction, txController),
// messageManager // messageManager
signMessage: nodeify(this.signMessage, this), signMessage: nodeify(this.signMessage, this),

View File

@ -125,8 +125,8 @@ var actions = {
sendTx: sendTx, sendTx: sendTx,
signTx: signTx, signTx: signTx,
signTokenTx: signTokenTx, signTokenTx: signTokenTx,
updateTransaction,
updateAndApproveTx, updateAndApproveTx,
updateAndCancelTx,
cancelTx: cancelTx, cancelTx: cancelTx,
completedTx: completedTx, completedTx: completedTx,
txError: txError, txError: txError,
@ -693,29 +693,28 @@ function signTokenTx (tokenAddress, toAddress, amount, txData) {
} }
} }
function updateAndApproveTx (txData) { function updateTransaction (txData) {
log.info('actions: updateAndApproveTx: ' + JSON.stringify(txData)) log.info('actions: updateTx: ' + JSON.stringify(txData))
return (dispatch) => { return (dispatch) => {
log.debug(`actions calling background.updateAndApproveTx`) log.debug(`actions calling background.updateTx`)
background.updateAndApproveTransaction(txData, (err) => { background.updateTransaction(txData, (err) => {
dispatch(actions.hideLoadingIndication()) dispatch(actions.hideLoadingIndication())
dispatch(actions.updateTransactionParams(txData.id, txData.txParams)) dispatch(actions.updateTransactionParams(txData.id, txData.txParams))
dispatch(actions.clearSend())
if (err) { if (err) {
dispatch(actions.txError(err)) dispatch(actions.txError(err))
dispatch(actions.goHome()) dispatch(actions.goHome())
return log.error(err.message) return log.error(err.message)
} }
dispatch(actions.completedTx(txData.id)) dispatch(actions.showConfTxPage({ id: txData.id }))
}) })
} }
} }
function updateAndCancelTx (txData) { function updateAndApproveTx (txData) {
log.info('actions: updateAndCancelTx: ' + JSON.stringify(txData)) log.info('actions: updateAndApproveTx: ' + JSON.stringify(txData))
return (dispatch) => { return (dispatch) => {
log.debug(`actions calling background.updateAndCancelTx`) log.debug(`actions calling background.updateAndApproveTx`)
background.updateAndCancelTransaction(txData, (err) => { background.updateAndApproveTransaction(txData, (err) => {
dispatch(actions.hideLoadingIndication()) dispatch(actions.hideLoadingIndication())
dispatch(actions.updateTransactionParams(txData.id, txData.txParams)) dispatch(actions.updateTransactionParams(txData.id, txData.txParams))
dispatch(actions.clearSend()) dispatch(actions.clearSend())
@ -773,6 +772,7 @@ function cancelTx (txData) {
return (dispatch) => { return (dispatch) => {
log.debug(`background.cancelTransaction`) log.debug(`background.cancelTransaction`)
background.cancelTransaction(txData.id, () => { background.cancelTransaction(txData.id, () => {
dispatch(actions.clearSend())
dispatch(actions.completedTx(txData.id)) dispatch(actions.completedTx(txData.id))
}) })
} }

View File

@ -55,7 +55,6 @@ function mapDispatchToProps (dispatch) {
dispatch(actions.showSendPage()) dispatch(actions.showSendPage())
}, },
cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })), cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })),
updateAndCancelTx: txMeta => dispatch(actions.updateAndCancelTx(txMeta)),
} }
} }
@ -422,13 +421,9 @@ ConfirmSendEther.prototype.onSubmit = function (event) {
ConfirmSendEther.prototype.cancel = function (event, txMeta) { ConfirmSendEther.prototype.cancel = function (event, txMeta) {
event.preventDefault() event.preventDefault()
const { send, updateAndCancelTx, cancelTransaction } = this.props const { cancelTransaction } = this.props
if (send.editingTransactionId) { cancelTransaction(txMeta)
updateAndCancelTx(txMeta)
} else {
cancelTransaction(txMeta)
}
} }
ConfirmSendEther.prototype.checkValidity = function () { ConfirmSendEther.prototype.checkValidity = function () {
@ -452,26 +447,6 @@ ConfirmSendEther.prototype.gatherTxMeta = function () {
const state = this.state const state = this.state
const txData = clone(state.txData) || clone(props.txData) const txData = clone(state.txData) || clone(props.txData)
if (props.send.editingTransactionId) {
const {
send: {
memo,
amount: value,
gasLimit: gas,
gasPrice,
},
} = props
const { txParams: { from, to } } = txData
txData.txParams = {
from: ethUtil.addHexPrefix(from),
to: ethUtil.addHexPrefix(to),
memo: memo && ethUtil.addHexPrefix(memo),
value: ethUtil.addHexPrefix(value),
gas: ethUtil.addHexPrefix(gas),
gasPrice: ethUtil.addHexPrefix(gasPrice),
}
}
// log.debug(`UI has defaulted to tx meta ${JSON.stringify(txData)}`) // log.debug(`UI has defaulted to tx meta ${JSON.stringify(txData)}`)
return txData return txData
} }

View File

@ -89,7 +89,6 @@ function mapDispatchToProps (dispatch, ownProps) {
})) }))
dispatch(actions.showSendTokenPage()) dispatch(actions.showSendTokenPage())
}, },
updateAndCancelTx: txMeta => dispatch(actions.updateAndCancelTx(txMeta)),
} }
} }
@ -416,13 +415,9 @@ ConfirmSendToken.prototype.onSubmit = function (event) {
ConfirmSendToken.prototype.cancel = function (event, txMeta) { ConfirmSendToken.prototype.cancel = function (event, txMeta) {
event.preventDefault() event.preventDefault()
const { send, updateAndCancelTx, cancelTransaction } = this.props const { send, cancelTransaction } = this.props
if (send.editingTransactionId) { cancelTransaction(txMeta)
updateAndCancelTx(txMeta)
} else {
cancelTransaction(txMeta)
}
} }
ConfirmSendToken.prototype.checkValidity = function () { ConfirmSendToken.prototype.checkValidity = function () {
@ -446,39 +441,6 @@ ConfirmSendToken.prototype.gatherTxMeta = function () {
const state = this.state const state = this.state
const txData = clone(state.txData) || clone(props.txData) const txData = clone(state.txData) || clone(props.txData)
if (props.send.editingTransactionId) {
const {
send: {
memo,
amount,
gasLimit: gas,
gasPrice,
to,
},
} = props
const { txParams: { from, to: tokenAddress } } = txData
const tokenParams = {
from: ethUtil.addHexPrefix(from),
value: '0',
gas: ethUtil.addHexPrefix(gas),
gasPrice: ethUtil.addHexPrefix(gasPrice),
}
const data = '0xa9059cbb' + Array.prototype.map.call(
ethAbi.rawEncode(['address', 'uint256'], [to, ethUtil.addHexPrefix(amount)]),
x => ('00' + x.toString(16)).slice(-2)
).join('')
txData.txParams = {
...tokenParams,
to: ethUtil.addHexPrefix(tokenAddress),
memo: memo && ethUtil.addHexPrefix(memo),
data,
}
}
// log.debug(`UI has defaulted to tx meta ${JSON.stringify(txData)}`) // log.debug(`UI has defaulted to tx meta ${JSON.stringify(txData)}`)
return txData return txData
} }

View File

@ -50,6 +50,7 @@ function mapStateToProps (state) {
data, data,
amountConversionRate: selectedToken ? tokenToFiatRate : conversionRate, amountConversionRate: selectedToken ? tokenToFiatRate : conversionRate,
tokenContract: getSelectedTokenContract(state), tokenContract: getSelectedTokenContract(state),
unapprovedTxs: state.metamask.unapprovedTxs,
} }
} }
@ -64,6 +65,7 @@ function mapDispatchToProps (dispatch) {
), ),
signTx: txParams => dispatch(actions.signTx(txParams)), signTx: txParams => dispatch(actions.signTx(txParams)),
updateAndApproveTx: txParams => dispatch(actions.updateAndApproveTx(txParams)), updateAndApproveTx: txParams => dispatch(actions.updateAndApproveTx(txParams)),
updateTx: txData => dispatch(actions.updateTransaction(txData)),
setSelectedAddress: address => dispatch(actions.setSelectedAddress(address)), setSelectedAddress: address => dispatch(actions.setSelectedAddress(address)),
addToAddressBook: address => dispatch(actions.addToAddressBook(address)), addToAddressBook: address => dispatch(actions.addToAddressBook(address)),
updateGasTotal: newTotal => dispatch(actions.updateGasTotal(newTotal)), updateGasTotal: newTotal => dispatch(actions.updateGasTotal(newTotal)),

View File

@ -115,7 +115,7 @@ function currentTxView (opts) {
log.info('rendering current tx view') log.info('rendering current tx view')
const { txData } = opts const { txData } = opts
const { txParams, msgParams } = txData const { txParams, msgParams } = txData
console.log(`22222 currentTxView txData`, txData);
if (txParams) { if (txParams) {
log.debug('txParams detected, rendering pending tx') log.debug('txParams detected, rendering pending tx')
return h(PendingTx, opts) return h(PendingTx, opts)

View File

@ -2,6 +2,7 @@ const { inherits } = require('util')
const PersistentForm = require('../lib/persistent-form') const PersistentForm = require('../lib/persistent-form')
const h = require('react-hyperscript') const h = require('react-hyperscript')
const ethAbi = require('ethereumjs-abi')
const ethUtil = require('ethereumjs-util') const ethUtil = require('ethereumjs-util')
const Identicon = require('./components/identicon') const Identicon = require('./components/identicon')
@ -552,6 +553,47 @@ SendTransactionScreen.prototype.addToAddressBookIfNew = function (newAddress) {
} }
} }
SendTransactionScreen.prototype.getEditedTx = function () {
const {
from: {address: from},
to,
amount,
gasLimit: gas,
gasPrice,
selectedToken,
editingTransactionId,
unapprovedTxs,
} = this.props
const editingTx = unapprovedTxs[editingTransactionId]
editingTx.txParams = {
from: ethUtil.addHexPrefix(from),
gas: ethUtil.addHexPrefix(gas),
gasPrice: ethUtil.addHexPrefix(gasPrice),
}
if (selectedToken) {
const data = '0xa9059cbb' + Array.prototype.map.call(
ethAbi.rawEncode(['address', 'uint256'], [to, ethUtil.addHexPrefix(amount)]),
x => ('00' + x.toString(16)).slice(-2)
).join('')
Object.assign(editingTx.txParams, {
value: ethUtil.addHexPrefix('0'),
to: ethUtil.addHexPrefix(selectedToken.address),
data,
})
} else {
Object.assign(editingTx.txParams, {
value: ethUtil.addHexPrefix(amount),
to: ethUtil.addHexPrefix(to),
})
}
return editingTx
}
SendTransactionScreen.prototype.onSubmit = function (event) { SendTransactionScreen.prototype.onSubmit = function (event) {
event.preventDefault() event.preventDefault()
const { const {
@ -562,10 +604,12 @@ SendTransactionScreen.prototype.onSubmit = function (event) {
gasPrice, gasPrice,
signTokenTx, signTokenTx,
signTx, signTx,
updateTx,
selectedToken, selectedToken,
editingTransactionId, editingTransactionId,
errors: { amount: amountError, to: toError }, errors: { amount: amountError, to: toError },
backToConfirmScreen, backToConfirmScreen,
unapprovedTxs,
} = this.props } = this.props
const noErrors = !amountError && toError === null const noErrors = !amountError && toError === null
@ -577,23 +621,25 @@ SendTransactionScreen.prototype.onSubmit = function (event) {
this.addToAddressBookIfNew(to) this.addToAddressBookIfNew(to)
if (editingTransactionId) { if (editingTransactionId) {
backToConfirmScreen(editingTransactionId) const editedTx = this.getEditedTx()
return
}
const txParams = { updateTx(editedTx)
from, } else {
value: '0',
gas,
gasPrice,
}
if (!selectedToken) { const txParams = {
txParams.value = amount from,
txParams.to = to value: '0',
} gas,
gasPrice,
}
selectedToken if (!selectedToken) {
? signTokenTx(selectedToken.address, to, amount, txParams) txParams.value = amount
: signTx(txParams) txParams.to = to
}
selectedToken
? signTokenTx(selectedToken.address, to, amount, txParams)
: signTx(txParams)
}
} }