mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Refactor token selectors (#8671)
Unused token selectors have been removed, and the remaining token selector has been moved to the `metamask` Redux store.
This commit is contained in:
parent
606618ee28
commit
88bed7181f
@ -377,3 +377,5 @@ export const getSwitchToConnectedAlertEnabledness = (state) => getAlertEnabledne
|
|||||||
export const getUnconnectedAccountAlertEnabledness = (state) => getAlertEnabledness(state)[ALERT_TYPES.unconnectedAccount]
|
export const getUnconnectedAccountAlertEnabledness = (state) => getAlertEnabledness(state)[ALERT_TYPES.unconnectedAccount]
|
||||||
|
|
||||||
export const getSwitchToConnectedAlertShown = (state) => state.metamask.switchToConnectedAlertShown
|
export const getSwitchToConnectedAlertShown = (state) => state.metamask.switchToConnectedAlertShown
|
||||||
|
|
||||||
|
export const getTokens = (state) => state.metamask.tokens
|
||||||
|
@ -4,7 +4,8 @@ import { renderHook } from '@testing-library/react-hooks'
|
|||||||
import sinon from 'sinon'
|
import sinon from 'sinon'
|
||||||
import transactions from '../../../../test/data/transaction-data.json'
|
import transactions from '../../../../test/data/transaction-data.json'
|
||||||
import { useTransactionDisplayData } from '../useTransactionDisplayData'
|
import { useTransactionDisplayData } from '../useTransactionDisplayData'
|
||||||
import { tokenSelector, getPreferences, getShouldShowFiat, getNativeCurrency, getCurrentCurrency } from '../../selectors'
|
import { getPreferences, getShouldShowFiat, getNativeCurrency, getCurrentCurrency } from '../../selectors'
|
||||||
|
import { getTokens } from '../../ducks/metamask/metamask'
|
||||||
import * as i18nhooks from '../useI18nContext'
|
import * as i18nhooks from '../useI18nContext'
|
||||||
import { getMessage } from '../../helpers/utils/i18n-helper'
|
import { getMessage } from '../../helpers/utils/i18n-helper'
|
||||||
import messages from '../../../../app/_locales/en/messages.json'
|
import messages from '../../../../app/_locales/en/messages.json'
|
||||||
@ -81,7 +82,7 @@ describe('useTransactionDisplayData', function () {
|
|||||||
useI18nContext = sinon.stub(i18nhooks, 'useI18nContext')
|
useI18nContext = sinon.stub(i18nhooks, 'useI18nContext')
|
||||||
useI18nContext.returns((key, variables) => getMessage('en', messages, key, variables))
|
useI18nContext.returns((key, variables) => getMessage('en', messages, key, variables))
|
||||||
useSelector.callsFake((selector) => {
|
useSelector.callsFake((selector) => {
|
||||||
if (selector === tokenSelector) {
|
if (selector === getTokens) {
|
||||||
return []
|
return []
|
||||||
} else if (selector === getPreferences) {
|
} else if (selector === getPreferences) {
|
||||||
return {
|
return {
|
||||||
|
@ -26,7 +26,7 @@ import {
|
|||||||
import { useCurrencyDisplay } from './useCurrencyDisplay'
|
import { useCurrencyDisplay } from './useCurrencyDisplay'
|
||||||
import { useTokenDisplayValue } from './useTokenDisplayValue'
|
import { useTokenDisplayValue } from './useTokenDisplayValue'
|
||||||
import { useTokenData } from './useTokenData'
|
import { useTokenData } from './useTokenData'
|
||||||
import { tokenSelector } from '../selectors'
|
import { getTokens } from '../ducks/metamask/metamask'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} TransactionDisplayData
|
* @typedef {Object} TransactionDisplayData
|
||||||
@ -51,7 +51,7 @@ import { tokenSelector } from '../selectors'
|
|||||||
* @return {TransactionDisplayData}
|
* @return {TransactionDisplayData}
|
||||||
*/
|
*/
|
||||||
export function useTransactionDisplayData (transactionGroup) {
|
export function useTransactionDisplayData (transactionGroup) {
|
||||||
const knownTokens = useSelector(tokenSelector)
|
const knownTokens = useSelector(getTokens)
|
||||||
const t = useI18nContext()
|
const t = useI18nContext()
|
||||||
const { initialTransaction, primaryTransaction } = transactionGroup
|
const { initialTransaction, primaryTransaction } = transactionGroup
|
||||||
// initialTransaction contains the data we need to derive the primary purpose of this transaction group
|
// initialTransaction contains the data we need to derive the primary purpose of this transaction group
|
||||||
|
@ -3,9 +3,9 @@ import { compose } from 'redux'
|
|||||||
import { withRouter } from 'react-router-dom'
|
import { withRouter } from 'react-router-dom'
|
||||||
import {
|
import {
|
||||||
contractExchangeRateSelector,
|
contractExchangeRateSelector,
|
||||||
tokenSelector,
|
|
||||||
transactionFeeSelector,
|
transactionFeeSelector,
|
||||||
} from '../../selectors'
|
} from '../../selectors'
|
||||||
|
import { getTokens } from '../../ducks/metamask/metamask'
|
||||||
import { showModal } from '../../store/actions'
|
import { showModal } from '../../store/actions'
|
||||||
import {
|
import {
|
||||||
getTokenData,
|
getTokenData,
|
||||||
@ -45,7 +45,7 @@ const mapStateToProps = (state, ownProps) => {
|
|||||||
ethTransactionTotal,
|
ethTransactionTotal,
|
||||||
fiatTransactionTotal,
|
fiatTransactionTotal,
|
||||||
} = transactionFeeSelector(state, transaction)
|
} = transactionFeeSelector(state, transaction)
|
||||||
const tokens = tokenSelector(state)
|
const tokens = getTokens(state)
|
||||||
const currentToken = tokens && tokens.find(({ address }) => tokenAddress === address)
|
const currentToken = tokens && tokens.find(({ address }) => tokenAddress === address)
|
||||||
const { decimals, symbol: tokenSymbol } = currentToken || {}
|
const { decimals, symbol: tokenSymbol } = currentToken || {}
|
||||||
|
|
||||||
|
@ -4,9 +4,9 @@ import { withRouter } from 'react-router-dom'
|
|||||||
import ConfirmTokenTransactionBase from './confirm-token-transaction-base.component'
|
import ConfirmTokenTransactionBase from './confirm-token-transaction-base.component'
|
||||||
import {
|
import {
|
||||||
contractExchangeRateSelector,
|
contractExchangeRateSelector,
|
||||||
tokenSelector,
|
|
||||||
transactionFeeSelector,
|
transactionFeeSelector,
|
||||||
} from '../../selectors'
|
} from '../../selectors'
|
||||||
|
import { getTokens } from '../../ducks/metamask/metamask'
|
||||||
import {
|
import {
|
||||||
getTokenData,
|
getTokenData,
|
||||||
} from '../../helpers/utils/transactions.util'
|
} from '../../helpers/utils/transactions.util'
|
||||||
@ -38,7 +38,7 @@ const mapStateToProps = (state, ownProps) => {
|
|||||||
ethTransactionTotal,
|
ethTransactionTotal,
|
||||||
fiatTransactionTotal,
|
fiatTransactionTotal,
|
||||||
} = transactionFeeSelector(state, transaction)
|
} = transactionFeeSelector(state, transaction)
|
||||||
const tokens = tokenSelector(state)
|
const tokens = getTokens(state)
|
||||||
const currentToken = tokens && tokens.find(({ address }) => tokenAddress === address)
|
const currentToken = tokens && tokens.find(({ address }) => tokenAddress === address)
|
||||||
const { decimals, symbol: tokenSymbol } = currentToken || {}
|
const { decimals, symbol: tokenSymbol } = currentToken || {}
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ import {
|
|||||||
getSendToNickname,
|
getSendToNickname,
|
||||||
getTokenBalance,
|
getTokenBalance,
|
||||||
getQrCodeData,
|
getQrCodeData,
|
||||||
getTokens,
|
|
||||||
getSelectedAddress,
|
getSelectedAddress,
|
||||||
getAddressBook,
|
getAddressBook,
|
||||||
} from '../../selectors'
|
} from '../../selectors'
|
||||||
@ -43,6 +42,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
fetchBasicGasEstimates,
|
fetchBasicGasEstimates,
|
||||||
} from '../../ducks/gas/gas.duck'
|
} from '../../ducks/gas/gas.duck'
|
||||||
|
import { getTokens } from '../../ducks/metamask/metamask'
|
||||||
import {
|
import {
|
||||||
calcGasTotal,
|
calcGasTotal,
|
||||||
} from './send.utils.js'
|
} from './send.utils.js'
|
||||||
|
@ -4,5 +4,4 @@ export * from './first-time-flow'
|
|||||||
export * from './permissions'
|
export * from './permissions'
|
||||||
export * from './selectors'
|
export * from './selectors'
|
||||||
export * from './send'
|
export * from './send'
|
||||||
export * from './tokens'
|
|
||||||
export * from './transactions'
|
export * from './transactions'
|
||||||
|
@ -146,10 +146,6 @@ export function getGasButtonGroupShown (state) {
|
|||||||
return state.send.gasButtonGroupShown
|
return state.send.gasButtonGroupShown
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTokens (state) {
|
|
||||||
return state.metamask.tokens
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getTitleKey (state) {
|
export function getTitleKey (state) {
|
||||||
const isEditing = Boolean(getSendEditingTransactionId(state))
|
const isEditing = Boolean(getSendEditingTransactionId(state))
|
||||||
const isToken = Boolean(getSelectedToken(state))
|
const isToken = Boolean(getSelectedToken(state))
|
||||||
|
@ -31,7 +31,6 @@ import {
|
|||||||
gasFeeIsInError,
|
gasFeeIsInError,
|
||||||
getGasLoadingError,
|
getGasLoadingError,
|
||||||
getGasButtonGroupShown,
|
getGasButtonGroupShown,
|
||||||
getTokens,
|
|
||||||
getTitleKey,
|
getTitleKey,
|
||||||
isSendFormInError,
|
isSendFormInError,
|
||||||
} from '../send'
|
} from '../send'
|
||||||
@ -510,20 +509,6 @@ describe('send selectors', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('add-recipient selectors', function () {
|
|
||||||
describe('getTokens()', function () {
|
|
||||||
it('should return empty array if no tokens in state', function () {
|
|
||||||
const state = {
|
|
||||||
metamask: {
|
|
||||||
tokens: [],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.deepStrictEqual(getTokens(state), [])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('send-header selectors', function () {
|
describe('send-header selectors', function () {
|
||||||
|
|
||||||
const getMetamaskSendMockState = (send) => {
|
const getMetamaskSendMockState = (send) => {
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
import assert from 'assert'
|
|
||||||
import { selectedTokenSelector } from '../tokens'
|
|
||||||
|
|
||||||
const metaToken = {
|
|
||||||
'address': '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
|
|
||||||
'symbol': 'META',
|
|
||||||
'decimals': 18,
|
|
||||||
}
|
|
||||||
|
|
||||||
const state = {
|
|
||||||
metamask: {
|
|
||||||
selectedTokenAddress: '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
|
|
||||||
tokens: [
|
|
||||||
{
|
|
||||||
'address': '0x06012c8cf97bead5deae237070f9587f8e7a266d',
|
|
||||||
'symbol': 'CK',
|
|
||||||
'decimals': 0,
|
|
||||||
},
|
|
||||||
metaToken,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
describe('Selected Token Selector', function () {
|
|
||||||
it('selects token info from tokens based on selectedTokenAddress in state', function () {
|
|
||||||
const tokenInfo = selectedTokenSelector(state)
|
|
||||||
assert.equal(tokenInfo, metaToken)
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,11 +0,0 @@
|
|||||||
import { createSelector } from 'reselect'
|
|
||||||
|
|
||||||
export const selectedTokenAddressSelector = (state) => state.metamask.selectedTokenAddress
|
|
||||||
export const tokenSelector = (state) => state.metamask.tokens
|
|
||||||
export const selectedTokenSelector = createSelector(
|
|
||||||
tokenSelector,
|
|
||||||
selectedTokenAddressSelector,
|
|
||||||
(tokens = [], selectedTokenAddress = '') => {
|
|
||||||
return tokens.find(({ address }) => address === selectedTokenAddress)
|
|
||||||
}
|
|
||||||
)
|
|
Loading…
x
Reference in New Issue
Block a user