2019-03-22 15:50:12 +01:00
|
|
|
import { NETWORK_TYPES } from '../helpers/constants/common'
|
2019-03-05 16:45:01 +01:00
|
|
|
import { stripHexPrefix } from 'ethereumjs-util'
|
2019-02-26 19:30:41 +01:00
|
|
|
|
2017-10-30 18:48:50 +01:00
|
|
|
const abi = require('human-standard-token-abi')
|
2018-07-31 07:03:20 +02:00
|
|
|
import {
|
|
|
|
transactionsSelector,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from './transactions'
|
2017-10-22 22:14:03 +02:00
|
|
|
const {
|
|
|
|
multiplyCurrencies,
|
2019-03-22 00:03:30 +01:00
|
|
|
} = require('../helpers/utils/conversion-util')
|
2017-10-22 22:14:03 +02:00
|
|
|
|
2017-08-10 06:40:01 +02:00
|
|
|
const selectors = {
|
|
|
|
getSelectedAddress,
|
|
|
|
getSelectedIdentity,
|
|
|
|
getSelectedAccount,
|
2017-09-06 12:17:49 +02:00
|
|
|
getSelectedToken,
|
2017-10-13 22:19:22 +02:00
|
|
|
getSelectedTokenExchangeRate,
|
2018-08-30 04:13:25 +02:00
|
|
|
getSelectedTokenAssetImage,
|
2018-08-30 18:51:57 +02:00
|
|
|
getAssetImages,
|
2017-10-18 20:08:53 +02:00
|
|
|
getTokenExchangeRate,
|
2017-08-11 04:35:01 +02:00
|
|
|
conversionRateSelector,
|
|
|
|
transactionsSelector,
|
2017-10-13 22:19:22 +02:00
|
|
|
accountsWithSendEtherInfoSelector,
|
|
|
|
getCurrentAccountWithSendEtherInfo,
|
2018-06-19 17:53:33 +02:00
|
|
|
getGasIsLoading,
|
2018-03-09 04:49:26 +01:00
|
|
|
getForceGasMin,
|
2017-10-17 17:46:36 +02:00
|
|
|
getAddressBook,
|
2017-10-17 19:22:23 +02:00
|
|
|
getSendFrom,
|
2017-10-19 04:09:26 +02:00
|
|
|
getCurrentCurrency,
|
2018-10-26 10:26:43 +02:00
|
|
|
getNativeCurrency,
|
2017-10-22 22:14:03 +02:00
|
|
|
getSendAmount,
|
|
|
|
getSelectedTokenToFiatRate,
|
2017-10-30 18:48:50 +01:00
|
|
|
getSelectedTokenContract,
|
2017-11-16 19:14:25 +01:00
|
|
|
getSendMaxModeState,
|
2018-01-10 19:25:38 +01:00
|
|
|
getCurrentViewContext,
|
2018-06-23 08:52:45 +02:00
|
|
|
getTotalUnapprovedCount,
|
2018-10-17 01:03:29 +02:00
|
|
|
preferencesSelector,
|
2018-11-30 23:51:24 +01:00
|
|
|
getMetaMaskAccounts,
|
2018-10-26 07:50:36 +02:00
|
|
|
getCurrentEthBalance,
|
2018-12-05 13:34:19 +01:00
|
|
|
getNetworkIdentifier,
|
2019-01-30 13:16:12 +01:00
|
|
|
isBalanceCached,
|
2019-02-06 01:24:28 +01:00
|
|
|
getAdvancedInlineGasShown,
|
2019-02-26 19:30:41 +01:00
|
|
|
getIsMainnet,
|
2019-03-05 16:45:01 +01:00
|
|
|
getCurrentNetworkId,
|
|
|
|
getSelectedAsset,
|
|
|
|
getCurrentKeyring,
|
|
|
|
getAccountType,
|
|
|
|
getNumberOfAccounts,
|
|
|
|
getNumberOfTokens,
|
2019-03-22 15:50:12 +01:00
|
|
|
isEthereumNetwork,
|
2019-04-29 08:18:40 +02:00
|
|
|
getMetaMetricState,
|
2019-05-09 19:27:14 +02:00
|
|
|
getRpcPrefsForCurrentProvider,
|
2017-08-10 06:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = selectors
|
|
|
|
|
2018-12-05 13:34:19 +01:00
|
|
|
function getNetworkIdentifier (state) {
|
|
|
|
const { metamask: { provider: { type, nickname, rpcTarget } } } = state
|
|
|
|
|
|
|
|
return nickname || rpcTarget || type
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:45:01 +01:00
|
|
|
function getCurrentKeyring (state) {
|
|
|
|
const identity = getSelectedIdentity(state)
|
|
|
|
|
|
|
|
if (!identity) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const simpleAddress = stripHexPrefix(identity.address).toLowerCase()
|
|
|
|
|
|
|
|
const keyring = state.metamask.keyrings.find((kr) => {
|
|
|
|
return kr.accounts.includes(simpleAddress) ||
|
|
|
|
kr.accounts.includes(identity.address)
|
|
|
|
})
|
|
|
|
|
|
|
|
return keyring
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAccountType (state) {
|
|
|
|
const currentKeyring = getCurrentKeyring(state)
|
|
|
|
const type = currentKeyring && currentKeyring.type
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'Trezor Hardware':
|
|
|
|
case 'Ledger Hardware':
|
|
|
|
return 'hardware'
|
|
|
|
case 'Simple Key Pair':
|
|
|
|
return 'imported'
|
|
|
|
default:
|
|
|
|
return 'default'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedAsset (state) {
|
2019-04-18 01:29:45 +02:00
|
|
|
const selectedToken = getSelectedToken(state)
|
|
|
|
return selectedToken && selectedToken.symbol || 'ETH'
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentNetworkId (state) {
|
|
|
|
return state.metamask.network
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:50:48 +02:00
|
|
|
function getSelectedAddress (state) {
|
2018-11-30 23:51:24 +01:00
|
|
|
const selectedAddress = state.metamask.selectedAddress || Object.keys(getMetaMaskAccounts(state))[0]
|
2017-08-10 06:40:01 +02:00
|
|
|
|
|
|
|
return selectedAddress
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:50:48 +02:00
|
|
|
function getSelectedIdentity (state) {
|
2017-08-10 06:40:01 +02:00
|
|
|
const selectedAddress = getSelectedAddress(state)
|
|
|
|
const identities = state.metamask.identities
|
|
|
|
|
|
|
|
return identities[selectedAddress]
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:45:01 +01:00
|
|
|
function getNumberOfAccounts (state) {
|
|
|
|
return Object.keys(state.metamask.accounts).length
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNumberOfTokens (state) {
|
|
|
|
const tokens = state.metamask.tokens
|
|
|
|
return tokens ? tokens.length : 0
|
|
|
|
}
|
|
|
|
|
2018-11-30 23:51:24 +01:00
|
|
|
function getMetaMaskAccounts (state) {
|
|
|
|
const currentAccounts = state.metamask.accounts
|
2019-01-30 13:16:12 +01:00
|
|
|
const cachedBalances = state.metamask.cachedBalances[state.metamask.network]
|
2018-11-30 23:51:24 +01:00
|
|
|
const selectedAccounts = {}
|
|
|
|
|
|
|
|
Object.keys(currentAccounts).forEach(accountID => {
|
|
|
|
const account = currentAccounts[accountID]
|
|
|
|
if (account && account.balance === null || account.balance === undefined) {
|
|
|
|
selectedAccounts[accountID] = {
|
|
|
|
...account,
|
2019-01-30 13:16:12 +01:00
|
|
|
balance: cachedBalances && cachedBalances[accountID],
|
2018-11-30 23:51:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selectedAccounts[accountID] = account
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return selectedAccounts
|
|
|
|
}
|
|
|
|
|
2019-01-30 13:16:12 +01:00
|
|
|
function isBalanceCached (state) {
|
|
|
|
const selectedAccountBalance = state.metamask.accounts[getSelectedAddress(state)].balance
|
|
|
|
const cachedBalance = getSelectedAccountCachedBalance(state)
|
|
|
|
|
|
|
|
return Boolean(!selectedAccountBalance && cachedBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedAccountCachedBalance (state) {
|
|
|
|
const cachedBalances = state.metamask.cachedBalances[state.metamask.network]
|
|
|
|
const selectedAddress = getSelectedAddress(state)
|
|
|
|
|
|
|
|
return cachedBalances && cachedBalances[selectedAddress]
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:50:48 +02:00
|
|
|
function getSelectedAccount (state) {
|
2018-11-30 23:51:24 +01:00
|
|
|
const accounts = getMetaMaskAccounts(state)
|
2017-08-10 06:40:01 +02:00
|
|
|
const selectedAddress = getSelectedAddress(state)
|
|
|
|
|
|
|
|
return accounts[selectedAddress]
|
2017-08-11 04:35:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 12:17:49 +02:00
|
|
|
function getSelectedToken (state) {
|
|
|
|
const tokens = state.metamask.tokens || []
|
|
|
|
const selectedTokenAddress = state.metamask.selectedTokenAddress
|
|
|
|
const selectedToken = tokens.filter(({ address }) => address === selectedTokenAddress)[0]
|
2019-04-29 08:18:40 +02:00
|
|
|
const sendToken = state.metamask.send && state.metamask.send.token
|
2017-09-06 12:17:49 +02:00
|
|
|
|
2018-03-20 12:37:23 +01:00
|
|
|
return selectedToken || sendToken || null
|
2017-09-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
function getSelectedTokenExchangeRate (state) {
|
2018-04-16 05:37:42 +02:00
|
|
|
const contractExchangeRates = state.metamask.contractExchangeRates
|
2017-10-13 22:19:22 +02:00
|
|
|
const selectedToken = getSelectedToken(state) || {}
|
2018-04-16 05:37:42 +02:00
|
|
|
const { address } = selectedToken
|
|
|
|
return contractExchangeRates[address] || 0
|
2017-10-13 22:19:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 04:13:25 +02:00
|
|
|
function getSelectedTokenAssetImage (state) {
|
2018-08-30 20:09:59 +02:00
|
|
|
const assetImages = state.metamask.assetImages || {}
|
2018-08-30 04:13:25 +02:00
|
|
|
const selectedToken = getSelectedToken(state) || {}
|
|
|
|
const { address } = selectedToken
|
|
|
|
return assetImages[address]
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:51:57 +02:00
|
|
|
function getAssetImages (state) {
|
2018-08-30 20:09:59 +02:00
|
|
|
const assetImages = state.metamask.assetImages || {}
|
2018-08-30 18:51:57 +02:00
|
|
|
return assetImages
|
|
|
|
}
|
|
|
|
|
2018-04-16 05:37:42 +02:00
|
|
|
function getTokenExchangeRate (state, address) {
|
|
|
|
const contractExchangeRates = state.metamask.contractExchangeRates
|
|
|
|
return contractExchangeRates[address] || 0
|
2017-10-18 20:08:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 21:47:44 +02:00
|
|
|
function conversionRateSelector (state) {
|
2017-08-11 04:35:01 +02:00
|
|
|
return state.metamask.conversionRate
|
|
|
|
}
|
|
|
|
|
2017-10-17 17:46:36 +02:00
|
|
|
function getAddressBook (state) {
|
|
|
|
return state.metamask.addressBook
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
function accountsWithSendEtherInfoSelector (state) {
|
2018-11-30 23:51:24 +01:00
|
|
|
const accounts = getMetaMaskAccounts(state)
|
|
|
|
const { identities } = state.metamask
|
2017-10-13 22:19:22 +02:00
|
|
|
|
|
|
|
const accountsWithSendEtherInfo = Object.entries(accounts).map(([key, account]) => {
|
|
|
|
return Object.assign({}, account, identities[key])
|
|
|
|
})
|
|
|
|
|
|
|
|
return accountsWithSendEtherInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentAccountWithSendEtherInfo (state) {
|
|
|
|
const currentAddress = getSelectedAddress(state)
|
|
|
|
const accounts = accountsWithSendEtherInfoSelector(state)
|
|
|
|
|
|
|
|
return accounts.find(({ address }) => address === currentAddress)
|
|
|
|
}
|
|
|
|
|
2018-10-26 07:50:36 +02:00
|
|
|
function getCurrentEthBalance (state) {
|
|
|
|
return getCurrentAccountWithSendEtherInfo(state).balance
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:53:33 +02:00
|
|
|
function getGasIsLoading (state) {
|
|
|
|
return state.appState.gasIsLoading
|
|
|
|
}
|
|
|
|
|
2018-03-09 04:49:26 +01:00
|
|
|
function getForceGasMin (state) {
|
|
|
|
return state.metamask.send.forceGasMin
|
|
|
|
}
|
|
|
|
|
2017-10-17 19:22:23 +02:00
|
|
|
function getSendFrom (state) {
|
|
|
|
return state.metamask.send.from
|
|
|
|
}
|
2017-10-19 04:09:26 +02:00
|
|
|
|
2017-10-22 22:14:03 +02:00
|
|
|
function getSendAmount (state) {
|
|
|
|
return state.metamask.send.amount
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:14:25 +01:00
|
|
|
function getSendMaxModeState (state) {
|
|
|
|
return state.metamask.send.maxModeOn
|
|
|
|
}
|
|
|
|
|
2017-10-19 04:09:26 +02:00
|
|
|
function getCurrentCurrency (state) {
|
|
|
|
return state.metamask.currentCurrency
|
|
|
|
}
|
2017-10-22 22:14:03 +02:00
|
|
|
|
2018-10-26 10:26:43 +02:00
|
|
|
function getNativeCurrency (state) {
|
|
|
|
return state.metamask.nativeCurrency
|
|
|
|
}
|
|
|
|
|
2017-10-22 22:14:03 +02:00
|
|
|
function getSelectedTokenToFiatRate (state) {
|
|
|
|
const selectedTokenExchangeRate = getSelectedTokenExchangeRate(state)
|
|
|
|
const conversionRate = conversionRateSelector(state)
|
|
|
|
|
|
|
|
const tokenToFiatRate = multiplyCurrencies(
|
|
|
|
conversionRate,
|
|
|
|
selectedTokenExchangeRate,
|
|
|
|
{ toNumericBase: 'dec' }
|
|
|
|
)
|
|
|
|
|
|
|
|
return tokenToFiatRate
|
|
|
|
}
|
2017-10-30 18:48:50 +01:00
|
|
|
|
|
|
|
function getSelectedTokenContract (state) {
|
|
|
|
const selectedToken = getSelectedToken(state)
|
|
|
|
return selectedToken
|
|
|
|
? global.eth.contract(abi).at(selectedToken.address)
|
|
|
|
: null
|
|
|
|
}
|
2017-12-07 01:19:45 +01:00
|
|
|
|
2018-01-10 19:25:38 +01:00
|
|
|
function getCurrentViewContext (state) {
|
|
|
|
const { currentView = {} } = state.appState
|
2018-02-09 02:37:22 +01:00
|
|
|
return currentView.context
|
2018-04-27 02:38:14 +02:00
|
|
|
}
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
function getTotalUnapprovedCount ({ metamask }) {
|
|
|
|
const {
|
|
|
|
unapprovedTxs = {},
|
|
|
|
unapprovedMsgCount,
|
|
|
|
unapprovedPersonalMsgCount,
|
|
|
|
unapprovedTypedMessagesCount,
|
|
|
|
} = metamask
|
|
|
|
|
|
|
|
return Object.keys(unapprovedTxs).length + unapprovedMsgCount + unapprovedPersonalMsgCount +
|
|
|
|
unapprovedTypedMessagesCount
|
|
|
|
}
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2019-02-26 19:30:41 +01:00
|
|
|
function getIsMainnet (state) {
|
|
|
|
const networkType = getNetworkIdentifier(state)
|
|
|
|
return networkType === NETWORK_TYPES.MAINNET
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:50:12 +01:00
|
|
|
function isEthereumNetwork (state) {
|
|
|
|
const networkType = getNetworkIdentifier(state)
|
|
|
|
const {
|
|
|
|
KOVAN,
|
|
|
|
MAINNET,
|
|
|
|
RINKEBY,
|
|
|
|
ROPSTEN,
|
2019-04-17 19:34:49 +02:00
|
|
|
GOERLI,
|
2019-03-22 15:50:12 +01:00
|
|
|
} = NETWORK_TYPES
|
2019-04-03 18:01:43 +02:00
|
|
|
|
2019-04-17 19:34:49 +02:00
|
|
|
return [ KOVAN, MAINNET, RINKEBY, ROPSTEN, GOERLI].includes(networkType)
|
2019-03-22 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 01:03:29 +02:00
|
|
|
function preferencesSelector ({ metamask }) {
|
|
|
|
return metamask.preferences
|
|
|
|
}
|
2019-02-06 01:24:28 +01:00
|
|
|
|
|
|
|
function getAdvancedInlineGasShown (state) {
|
|
|
|
return Boolean(state.metamask.featureFlags.advancedInlineGas)
|
|
|
|
}
|
2019-04-29 08:18:40 +02:00
|
|
|
|
|
|
|
function getMetaMetricState (state) {
|
|
|
|
return {
|
|
|
|
network: getCurrentNetworkId(state),
|
|
|
|
activeCurrency: getSelectedAsset(state),
|
|
|
|
accountType: getAccountType(state),
|
|
|
|
metaMetricsId: state.metamask.metaMetricsId,
|
|
|
|
numberOfTokens: getNumberOfTokens(state),
|
|
|
|
numberOfAccounts: getNumberOfAccounts(state),
|
|
|
|
participateInMetaMetrics: state.metamask.participateInMetaMetrics,
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
function getRpcPrefsForCurrentProvider (state) {
|
|
|
|
const { frequentRpcListDetail, provider } = state.metamask
|
|
|
|
const selectRpcInfo = frequentRpcListDetail.find(rpcInfo => rpcInfo.rpcUrl === provider.rpcTarget)
|
|
|
|
const { rpcPrefs = {} } = selectRpcInfo || {}
|
|
|
|
return rpcPrefs
|
|
|
|
}
|