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

Allow editing of send ether.

This commit is contained in:
Dan 2017-11-08 13:14:48 -03:30 committed by Chi Kei Chan
parent 7eb083bd9f
commit 34ca7290c5
5 changed files with 130 additions and 22 deletions

View File

@ -115,6 +115,7 @@ var actions = {
TRANSACTION_ERROR: 'TRANSACTION_ERROR', TRANSACTION_ERROR: 'TRANSACTION_ERROR',
NEXT_TX: 'NEXT_TX', NEXT_TX: 'NEXT_TX',
PREVIOUS_TX: 'PREV_TX', PREVIOUS_TX: 'PREV_TX',
EDIT_TX: 'EDIT_TX',
signMsg: signMsg, signMsg: signMsg,
cancelMsg: cancelMsg, cancelMsg: cancelMsg,
signPersonalMsg, signPersonalMsg,
@ -129,10 +130,13 @@ var actions = {
completedTx: completedTx, completedTx: completedTx,
txError: txError, txError: txError,
nextTx: nextTx, nextTx: nextTx,
editTx,
previousTx: previousTx, previousTx: previousTx,
cancelAllTx: cancelAllTx, cancelAllTx: cancelAllTx,
viewPendingTx: viewPendingTx, viewPendingTx: viewPendingTx,
VIEW_PENDING_TX: 'VIEW_PENDING_TX', VIEW_PENDING_TX: 'VIEW_PENDING_TX',
updateTransactionParams,
UPDATE_TRANSACTION_PARAMS: 'UPDATE_TRANSACTION_PARAMS',
// send screen // send screen
estimateGas, estimateGas,
getGasPrice, getGasPrice,
@ -668,6 +672,8 @@ function updateAndApproveTx (txData) {
log.debug(`actions calling background.updateAndApproveTx`) log.debug(`actions calling background.updateAndApproveTx`)
background.updateAndApproveTransaction(txData, (err) => { background.updateAndApproveTransaction(txData, (err) => {
dispatch(actions.hideLoadingIndication()) dispatch(actions.hideLoadingIndication())
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())
@ -685,6 +691,14 @@ function completedTx (id) {
} }
} }
function updateTransactionParams (id, txParams) {
return {
type: actions.UPDATE_TRANSACTION_PARAMS,
id,
value: txParams,
}
}
function txError (err) { function txError (err) {
return { return {
type: actions.TRANSACTION_ERROR, type: actions.TRANSACTION_ERROR,
@ -948,6 +962,13 @@ function previousTx () {
} }
} }
function editTx (txId) {
return {
type: actions.EDIT_TX,
value: txId,
}
}
function showConfigPage (transitionForward = true) { function showConfigPage (transitionForward = true) {
return { return {
type: actions.SHOW_CONFIG_PAGE, type: actions.SHOW_CONFIG_PAGE,

View File

@ -19,6 +19,7 @@ function mapStateToProps (state) {
conversionRate, conversionRate,
identities, identities,
currentCurrency, currentCurrency,
send,
} = state.metamask } = state.metamask
const accounts = state.metamask.accounts const accounts = state.metamask.accounts
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0] const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
@ -27,12 +28,30 @@ function mapStateToProps (state) {
identities, identities,
selectedAddress, selectedAddress,
currentCurrency, currentCurrency,
send,
} }
} }
function mapDispatchToProps (dispatch) { function mapDispatchToProps (dispatch) {
return { return {
backToAccountDetail: address => dispatch(actions.backToAccountDetail(address)), clearSend: () => dispatch(actions.clearSend()),
editTransaction: txMeta => {
const { id, txParams } = txMeta
const {
gas: gasLimit,
gasPrice,
from,
to,
value: amount
} = txParams
dispatch(actions.editTx(id))
dispatch(actions.updateGasLimit(gasLimit)),
dispatch(actions.updateGasPrice(gasPrice)),
dispatch(actions.updateSendTo(to)),
dispatch(actions.updateSendAmount(amount)),
dispatch(actions.updateSendErrors({ to: null, amount: null })),
dispatch(actions.showSendPage())
},
cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })), cancelTransaction: ({ id }) => dispatch(actions.cancelTx({ id })),
} }
} }
@ -157,7 +176,7 @@ ConfirmSendEther.prototype.getData = function () {
} }
ConfirmSendEther.prototype.render = function () { ConfirmSendEther.prototype.render = function () {
const { backToAccountDetail, selectedAddress, currentCurrency } = this.props const { editTransaction, selectedAddress, currentCurrency, clearSend } = this.props
const txMeta = this.gatherTxMeta() const txMeta = this.gatherTxMeta()
const txParams = txMeta.txParams || {} const txParams = txMeta.txParams || {}
@ -199,8 +218,8 @@ ConfirmSendEther.prototype.render = function () {
h('div.confirm-screen-wrapper.flex-column.flex-grow', [ h('div.confirm-screen-wrapper.flex-column.flex-grow', [
h('h3.flex-center.confirm-screen-header', [ h('h3.flex-center.confirm-screen-header', [
h('button.confirm-screen-back-button', { h('button.confirm-screen-back-button', {
onClick: () => backToAccountDetail(selectedAddress), onClick: () => editTransaction(txMeta),
}, 'BACK'), }, 'EDIT'),
h('div.confirm-screen-title', 'Confirm Transaction'), h('div.confirm-screen-title', 'Confirm Transaction'),
h('div.confirm-screen-header-tip'), h('div.confirm-screen-header-tip'),
]), ]),
@ -371,7 +390,10 @@ ConfirmSendEther.prototype.render = function () {
}, [ }, [
// Cancel Button // Cancel Button
h('div.cancel.btn-light.confirm-screen-cancel-button', { h('div.cancel.btn-light.confirm-screen-cancel-button', {
onClick: (event) => this.cancel(event, txMeta), onClick: (event) => {
clearSend()
this.cancel(event, txMeta)
},
}, 'CANCEL'), }, 'CANCEL'),
// Accept Button // Accept Button
@ -419,7 +441,27 @@ ConfirmSendEther.prototype.getFormEl = function () {
ConfirmSendEther.prototype.gatherTxMeta = function () { ConfirmSendEther.prototype.gatherTxMeta = function () {
const props = this.props const props = this.props
const state = this.state const state = this.state
const txData = clone(state.txData) || clone(props.txData) let 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

@ -63,6 +63,7 @@ function mapDispatchToProps (dispatch) {
dispatch(actions.signTokenTx(tokenAddress, toAddress, amount, txData)) dispatch(actions.signTokenTx(tokenAddress, toAddress, amount, txData))
), ),
signTx: txParams => dispatch(actions.signTx(txParams)), signTx: txParams => dispatch(actions.signTx(txParams)),
updateAndApproveTx: txParams => dispatch(actions.updateAndApproveTx(txParams)),
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)),
@ -76,5 +77,6 @@ function mapDispatchToProps (dispatch) {
updateSendErrors: newError => dispatch(actions.updateSendErrors(newError)), updateSendErrors: newError => dispatch(actions.updateSendErrors(newError)),
goHome: () => dispatch(actions.goHome()), goHome: () => dispatch(actions.goHome()),
clearSend: () => dispatch(actions.clearSend()), clearSend: () => dispatch(actions.clearSend()),
backToConfirmScreen: editingTransactionId => dispatch(actions.showConfTxPage({ id: editingTransactionId })),
} }
} }

View File

@ -33,6 +33,7 @@ function reduceMetamask (state, action) {
amount: '0x0', amount: '0x0',
memo: '', memo: '',
errors: {}, errors: {},
editingTransactionId: null,
}, },
coinOptions: {}, coinOptions: {},
}, state.metamask) }, state.metamask)
@ -108,6 +109,14 @@ function reduceMetamask (state, action) {
} }
return newState return newState
case actions.EDIT_TX:
return extend(metamaskState, {
send: {
...metamaskState.send,
editingTransactionId: action.value,
},
})
case actions.SHOW_NEW_VAULT_SEED: case actions.SHOW_NEW_VAULT_SEED:
return extend(metamaskState, { return extend(metamaskState, {
isUnlocked: true, isUnlocked: true,
@ -262,6 +271,20 @@ function reduceMetamask (state, action) {
}, },
}) })
case actions.UPDATE_TRANSACTION_PARAMS:
const { id, value } = action
let { selectedAddressTxList } = metamaskState
selectedAddressTxList = selectedAddressTxList.map(tx => {
if (tx.id === id) {
tx.txParams = value
}
return tx
})
return extend(metamaskState, {
selectedAddressTxList,
})
case actions.PAIR_UPDATE: case actions.PAIR_UPDATE:
const { value: { marketinfo: pairMarketInfo } } = action const { value: { marketinfo: pairMarketInfo } } = action
return extend(metamaskState, { return extend(metamaskState, {

View File

@ -93,6 +93,9 @@ SendTransactionScreen.prototype.componentWillMount = function () {
updateGasTotal, updateGasTotal,
from, from,
tokenContract, tokenContract,
editingTransactionId,
gasPrice,
gasLimit,
} = this.props } = this.props
const { symbol } = selectedToken || {} const { symbol } = selectedToken || {}
@ -102,11 +105,13 @@ SendTransactionScreen.prototype.componentWillMount = function () {
const estimateGasParams = getParamsForGasEstimate(selectedAddress, symbol, data) const estimateGasParams = getParamsForGasEstimate(selectedAddress, symbol, data)
let newGasTotal
if (!editingTransactionId) {
Promise Promise
.all([ .all([
getGasPrice(), getGasPrice(),
estimateGas(estimateGasParams), estimateGas(estimateGasParams),
tokenContract && tokenContract.balanceOf(from.address), tokenContract && tokenContract.balanceOf(from.address)
]) ])
.then(([gasPrice, gas, usersToken]) => { .then(([gasPrice, gas, usersToken]) => {
@ -118,6 +123,14 @@ SendTransactionScreen.prototype.componentWillMount = function () {
updateGasTotal(newGasTotal) updateGasTotal(newGasTotal)
this.updateSendTokenBalance(usersToken) this.updateSendTokenBalance(usersToken)
}) })
} else {
newGasTotal = multiplyCurrencies(gasLimit, gasPrice, {
toNumericBase: 'hex',
multiplicandBase: 16,
multiplierBase: 16,
})
updateGasTotal(newGasTotal)
}
} }
SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) { SendTransactionScreen.prototype.componentDidUpdate = function (prevProps) {
@ -394,6 +407,7 @@ SendTransactionScreen.prototype.renderAmountRow = function () {
errors, errors,
amount, amount,
} = this.props } = this.props
return h('div.send-v2__form-row', [ return h('div.send-v2__form-row', [
h('div.send-v2__form-label', [ h('div.send-v2__form-label', [
@ -551,9 +565,12 @@ SendTransactionScreen.prototype.onSubmit = function (event) {
gasPrice, gasPrice,
signTokenTx, signTokenTx,
signTx, signTx,
updateAndApproveTx,
selectedToken, selectedToken,
clearSend, toAccounts,
editingTransactionId,
errors: { amount: amountError, to: toError }, errors: { amount: amountError, to: toError },
backToConfirmScreen,
} = this.props } = this.props
const noErrors = !amountError && toError === null const noErrors = !amountError && toError === null
@ -564,6 +581,11 @@ SendTransactionScreen.prototype.onSubmit = function (event) {
this.addToAddressBookIfNew(to) this.addToAddressBookIfNew(to)
if (editingTransactionId) {
backToConfirmScreen(editingTransactionId)
return
}
const txParams = { const txParams = {
from, from,
value: '0', value: '0',
@ -576,8 +598,6 @@ SendTransactionScreen.prototype.onSubmit = function (event) {
txParams.to = to txParams.to = to
} }
clearSend()
selectedToken selectedToken
? signTokenTx(selectedToken.address, to, amount, txParams) ? signTokenTx(selectedToken.address, to, amount, txParams)
: signTx(txParams) : signTx(txParams)