mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Fix selectors that use metamask.send.from
state (#8182)
The `metamask.send.from` field was assumed by various selectors to be an object, but instead it was recently set to a string. The selectors have been updated to assume it's a string, and to fetch the full account object explicitly. The selector `getSendFromObject` was repurposed for this, as that's basically what it already did. The optional address parameter was removed though, as that was only used to fetch the `from` address in cases where the `send` state was set without there being a `from` address set. That case is no longer possible, as the `from` address is always set upon the initialization of the `send` state. The `getSendFromObject` selector no longer fetches the 'name' of that address from the address book state either. This property was not used in either of the cases this selector was used.
This commit is contained in:
parent
3cd35fe0ae
commit
f0f5554846
@ -114,12 +114,6 @@ describe('Selectors', function () {
|
|||||||
assert.equal(gasIsLoading, false)
|
assert.equal(gasIsLoading, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Send From', function () {
|
|
||||||
it('#getSendFrom', function () {
|
|
||||||
const sendFrom = selectors.getSendFrom(mockState)
|
|
||||||
assert.equal(sendFrom, '0xc42edfcc21ed14dda456aa0756c153f7985d8813')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('#getForceGasMin', function () {
|
it('#getForceGasMin', function () {
|
||||||
const forceGasMin = selectors.getForceGasMin(mockState)
|
const forceGasMin = selectors.getForceGasMin(mockState)
|
||||||
assert.equal(forceGasMin, null)
|
assert.equal(forceGasMin, null)
|
||||||
@ -134,7 +128,6 @@ describe('Selectors', function () {
|
|||||||
const sendMaxModeState = selectors.getSendMaxModeState(mockState)
|
const sendMaxModeState = selectors.getSendMaxModeState(mockState)
|
||||||
assert.equal(sendMaxModeState, false)
|
assert.equal(sendMaxModeState, false)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
it('#getCurrentCurrency', function () {
|
it('#getCurrentCurrency', function () {
|
||||||
const currentCurrency = selectors.getCurrentCurrency(mockState)
|
const currentCurrency = selectors.getCurrentCurrency(mockState)
|
||||||
|
@ -288,10 +288,6 @@ export function getOriginFromUrl (url) {
|
|||||||
return origin
|
return origin
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTxById (transactions = [], targetId) {
|
|
||||||
return transactions.find(({ id }) => String(id) === targetId)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getAccountByAddress (accounts = [], targetAddress) {
|
export function getAccountByAddress (accounts = [], targetAddress) {
|
||||||
return accounts.find(({ address }) => address === targetAddress)
|
return accounts.find(({ address }) => address === targetAddress)
|
||||||
}
|
}
|
||||||
|
@ -24,10 +24,6 @@ import {
|
|||||||
getSendErrors,
|
getSendErrors,
|
||||||
} from '../send.selectors'
|
} from '../send.selectors'
|
||||||
import { getGasIsLoading } from '../../../selectors/selectors'
|
import { getGasIsLoading } from '../../../selectors/selectors'
|
||||||
import { networkTransactionsSelector } from '../../../selectors/transactions'
|
|
||||||
import {
|
|
||||||
getTxById,
|
|
||||||
} from '../../../helpers/utils/util'
|
|
||||||
import {
|
import {
|
||||||
isSendFormInError,
|
isSendFormInError,
|
||||||
} from './send-footer.selectors'
|
} from './send-footer.selectors'
|
||||||
@ -51,20 +47,13 @@ function mapStateToProps (state) {
|
|||||||
const gasEstimateType = activeButtonIndex >= 0
|
const gasEstimateType = activeButtonIndex >= 0
|
||||||
? gasButtonInfo[activeButtonIndex].gasEstimateType
|
? gasButtonInfo[activeButtonIndex].gasEstimateType
|
||||||
: 'custom'
|
: 'custom'
|
||||||
|
|
||||||
let fromAddress
|
|
||||||
const editingTransactionId = getSendEditingTransactionId(state)
|
const editingTransactionId = getSendEditingTransactionId(state)
|
||||||
if (editingTransactionId) {
|
|
||||||
const transactions = networkTransactionsSelector(state)
|
|
||||||
const tx = getTxById(transactions, editingTransactionId)
|
|
||||||
fromAddress = tx && tx.txParams && tx.txParams.from
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
amount: getSendAmount(state),
|
amount: getSendAmount(state),
|
||||||
data: getSendHexData(state),
|
data: getSendHexData(state),
|
||||||
editingTransactionId,
|
editingTransactionId,
|
||||||
from: getSendFromObject(state, fromAddress),
|
from: getSendFromObject(state),
|
||||||
gasLimit: getGasLimit(state),
|
gasLimit: getGasLimit(state),
|
||||||
gasPrice: getGasPrice(state),
|
gasPrice: getGasPrice(state),
|
||||||
gasTotal: getGasTotal(state),
|
gasTotal: getGasTotal(state),
|
||||||
|
@ -28,9 +28,6 @@ import {
|
|||||||
getSelectedAddress,
|
getSelectedAddress,
|
||||||
getAddressBook,
|
getAddressBook,
|
||||||
} from '../../selectors/selectors'
|
} from '../../selectors/selectors'
|
||||||
import {
|
|
||||||
networkTransactionsSelector,
|
|
||||||
} from '../../selectors/transactions'
|
|
||||||
import { getTokens } from './send-content/add-recipient/add-recipient.selectors'
|
import { getTokens } from './send-content/add-recipient/add-recipient.selectors'
|
||||||
import {
|
import {
|
||||||
updateSendTo,
|
updateSendTo,
|
||||||
@ -54,18 +51,10 @@ import {
|
|||||||
} from './send.utils.js'
|
} from './send.utils.js'
|
||||||
import {
|
import {
|
||||||
isValidDomainName,
|
isValidDomainName,
|
||||||
getTxById,
|
|
||||||
} from '../../helpers/utils/util'
|
} from '../../helpers/utils/util'
|
||||||
|
|
||||||
function mapStateToProps (state) {
|
function mapStateToProps (state) {
|
||||||
|
|
||||||
let fromAddress
|
|
||||||
const editingTransactionId = getSendEditingTransactionId(state)
|
const editingTransactionId = getSendEditingTransactionId(state)
|
||||||
if (editingTransactionId) {
|
|
||||||
const transactions = networkTransactionsSelector(state)
|
|
||||||
const tx = getTxById(transactions, editingTransactionId)
|
|
||||||
fromAddress = tx && tx.txParams && tx.txParams.from
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
addressBook: getAddressBook(state),
|
addressBook: getAddressBook(state),
|
||||||
@ -74,7 +63,7 @@ function mapStateToProps (state) {
|
|||||||
blockGasLimit: getBlockGasLimit(state),
|
blockGasLimit: getBlockGasLimit(state),
|
||||||
conversionRate: getConversionRate(state),
|
conversionRate: getConversionRate(state),
|
||||||
editingTransactionId,
|
editingTransactionId,
|
||||||
from: getSendFromObject(state, fromAddress),
|
from: getSendFromObject(state),
|
||||||
gasLimit: getGasLimit(state),
|
gasLimit: getGasLimit(state),
|
||||||
gasPrice: getGasPrice(state),
|
gasPrice: getGasPrice(state),
|
||||||
gasTotal: getGasTotal(state),
|
gasTotal: getGasTotal(state),
|
||||||
|
@ -3,9 +3,8 @@ import { multiplyCurrencies } from '../../helpers/utils/conversion-util'
|
|||||||
import {
|
import {
|
||||||
accountsWithSendEtherInfoSelector,
|
accountsWithSendEtherInfoSelector,
|
||||||
getAddressBook,
|
getAddressBook,
|
||||||
getCurrentAccountWithSendEtherInfo,
|
getSelectedAccount,
|
||||||
getTargetAccountWithSendEtherInfo,
|
getTargetAccount,
|
||||||
getMetaMaskAccounts,
|
|
||||||
getSelectedAddress,
|
getSelectedAddress,
|
||||||
} from '../../selectors/selectors'
|
} from '../../selectors/selectors'
|
||||||
import { estimateGasPriceFromRecentBlocks, calcGasTotal } from './send.utils'
|
import { estimateGasPriceFromRecentBlocks, calcGasTotal } from './send.utils'
|
||||||
@ -68,13 +67,6 @@ export function getRecentBlocks (state) {
|
|||||||
return state.metamask.recentBlocks
|
return state.metamask.recentBlocks
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSelectedAccount (state) {
|
|
||||||
const accounts = getMetaMaskAccounts(state)
|
|
||||||
const selectedAddress = getSelectedAddress(state)
|
|
||||||
|
|
||||||
return accounts[selectedAddress]
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getSelectedIdentity (state) {
|
export function getSelectedIdentity (state) {
|
||||||
const selectedAddress = getSelectedAddress(state)
|
const selectedAddress = getSelectedAddress(state)
|
||||||
const identities = state.metamask.identities
|
const identities = state.metamask.identities
|
||||||
@ -147,15 +139,15 @@ export function getSendFrom (state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getSendFromBalance (state) {
|
export function getSendFromBalance (state) {
|
||||||
const from = getSendFrom(state) || getSelectedAccount(state)
|
const fromAccount = getSendFromObject(state)
|
||||||
return from.balance
|
return fromAccount.balance
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSendFromObject (state, address) {
|
export function getSendFromObject (state) {
|
||||||
if (address) {
|
const fromAddress = getSendFrom(state)
|
||||||
return getTargetAccountWithSendEtherInfo(state, address)
|
return fromAddress
|
||||||
}
|
? getTargetAccount(state, fromAddress)
|
||||||
return getSendFrom(state) || getCurrentAccountWithSendEtherInfo(state)
|
: getSelectedAccount(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSendMaxModeState (state) {
|
export function getSendMaxModeState (state) {
|
||||||
|
@ -172,10 +172,7 @@ export default {
|
|||||||
'gasPrice': '0xaa',
|
'gasPrice': '0xaa',
|
||||||
'gasTotal': '0xb451dc41b578',
|
'gasTotal': '0xb451dc41b578',
|
||||||
'tokenBalance': 3434,
|
'tokenBalance': 3434,
|
||||||
'from': {
|
'from': '0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||||||
'address': '0xabcdefg',
|
|
||||||
'balance': '0x5f4e3d2c1',
|
|
||||||
},
|
|
||||||
'to': '0x987fedabc',
|
'to': '0x987fedabc',
|
||||||
'amount': '0x080',
|
'amount': '0x080',
|
||||||
'memo': '',
|
'memo': '',
|
||||||
|
@ -17,7 +17,6 @@ import {
|
|||||||
getGasTotal,
|
getGasTotal,
|
||||||
getPrimaryCurrency,
|
getPrimaryCurrency,
|
||||||
getRecentBlocks,
|
getRecentBlocks,
|
||||||
getSelectedAccount,
|
|
||||||
getSelectedIdentity,
|
getSelectedIdentity,
|
||||||
getSelectedToken,
|
getSelectedToken,
|
||||||
getSelectedTokenContract,
|
getSelectedTokenContract,
|
||||||
@ -233,21 +232,6 @@ describe('send selectors', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getSelectedAccount()', function () {
|
|
||||||
it('should return the currently selected account', function () {
|
|
||||||
assert.deepEqual(
|
|
||||||
getSelectedAccount(mockState),
|
|
||||||
{
|
|
||||||
code: '0x',
|
|
||||||
balance: '0x0',
|
|
||||||
nonce: '0x0',
|
|
||||||
address: '0xd85a4b6a394794842887b8284293d69163007bbb',
|
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
describe('getSelectedIdentity()', function () {
|
describe('getSelectedIdentity()', function () {
|
||||||
it('should return the identity object of the currently selected address', function () {
|
it('should return the identity object of the currently selected address', function () {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
@ -368,10 +352,7 @@ describe('send selectors', function () {
|
|||||||
it('should return the send.from', function () {
|
it('should return the send.from', function () {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
getSendFrom(mockState),
|
getSendFrom(mockState),
|
||||||
{
|
'0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||||||
address: '0xabcdefg',
|
|
||||||
balance: '0x5f4e3d2c1',
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -380,7 +361,7 @@ describe('send selectors', function () {
|
|||||||
it('should get the send.from balance if it exists', function () {
|
it('should get the send.from balance if it exists', function () {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
getSendFromBalance(mockState),
|
getSendFromBalance(mockState),
|
||||||
'0x5f4e3d2c1'
|
'0x37452b1315889f80'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -404,13 +385,15 @@ describe('send selectors', function () {
|
|||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
getSendFromObject(mockState),
|
getSendFromObject(mockState),
|
||||||
{
|
{
|
||||||
address: '0xabcdefg',
|
address: '0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||||||
balance: '0x5f4e3d2c1',
|
balance: '0x37452b1315889f80',
|
||||||
|
code: '0x',
|
||||||
|
nonce: '0xa',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return the current account with send ether info if send.from does not exist', function () {
|
it('should return the current account if send.from does not exist', function () {
|
||||||
const editedMockState = {
|
const editedMockState = {
|
||||||
metamask: Object.assign({}, mockState.metamask, {
|
metamask: Object.assign({}, mockState.metamask, {
|
||||||
send: {
|
send: {
|
||||||
@ -425,7 +408,6 @@ describe('send selectors', function () {
|
|||||||
balance: '0x0',
|
balance: '0x0',
|
||||||
nonce: '0x0',
|
nonce: '0x0',
|
||||||
address: '0xd85a4b6a394794842887b8284293d69163007bbb',
|
address: '0xd85a4b6a394794842887b8284293d69163007bbb',
|
||||||
name: 'Send Account 4',
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -159,6 +159,11 @@ export function getSelectedAccount (state) {
|
|||||||
return accounts[selectedAddress]
|
return accounts[selectedAddress]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getTargetAccount (state, targetAddress) {
|
||||||
|
const accounts = getMetaMaskAccounts(state)
|
||||||
|
return accounts[targetAddress]
|
||||||
|
}
|
||||||
|
|
||||||
export function getSelectedToken (state) {
|
export function getSelectedToken (state) {
|
||||||
const tokens = state.metamask.tokens || []
|
const tokens = state.metamask.tokens || []
|
||||||
const selectedTokenAddress = state.metamask.selectedTokenAddress
|
const selectedTokenAddress = state.metamask.selectedTokenAddress
|
||||||
@ -271,10 +276,6 @@ export function getForceGasMin (state) {
|
|||||||
return state.metamask.send.forceGasMin
|
return state.metamask.send.forceGasMin
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSendFrom (state) {
|
|
||||||
return state.metamask.send.from
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getSendAmount (state) {
|
export function getSendAmount (state) {
|
||||||
return state.metamask.send.amount
|
return state.metamask.send.amount
|
||||||
}
|
}
|
||||||
|
@ -109,17 +109,6 @@ const transactionSelectorReturnHelper = (selectedTokenAddress, transactions) =>
|
|||||||
.sort((a, b) => b.time - a.time)
|
.sort((a, b) => b.time - a.time)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const networkTransactionsSelector = createSelector(
|
|
||||||
selectedTokenAddressSelector,
|
|
||||||
transactionSubSelector,
|
|
||||||
currentNetworkTxListSelector,
|
|
||||||
(selectedTokenAddress, subSelectorTxList = [], networkTxList = []) => {
|
|
||||||
const txsToRender = networkTxList.concat(subSelectorTxList)
|
|
||||||
|
|
||||||
return transactionSelectorReturnHelper(selectedTokenAddress, txsToRender)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
export const transactionsSelector = createSelector(
|
export const transactionsSelector = createSelector(
|
||||||
selectedTokenAddressSelector,
|
selectedTokenAddressSelector,
|
||||||
transactionSubSelector,
|
transactionSubSelector,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user