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,
|
|
|
|
} from './selectors/transactions'
|
2017-10-22 22:14:03 +02:00
|
|
|
const {
|
|
|
|
multiplyCurrencies,
|
|
|
|
} = require('./conversion-util')
|
|
|
|
|
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-12-07 01:19:45 +01:00
|
|
|
autoAddToBetaUI,
|
2018-09-24 17:57:37 +02:00
|
|
|
getShouldUseNewUi,
|
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,
|
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
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2018-11-30 23:51:24 +01:00
|
|
|
function getMetaMaskAccounts (state) {
|
|
|
|
const currentAccounts = state.metamask.accounts
|
|
|
|
const cachedBalances = state.metamask.cachedBalances
|
|
|
|
const selectedAccounts = {}
|
|
|
|
|
|
|
|
Object.keys(currentAccounts).forEach(accountID => {
|
|
|
|
const account = currentAccounts[accountID]
|
|
|
|
if (account && account.balance === null || account.balance === undefined) {
|
|
|
|
selectedAccounts[accountID] = {
|
|
|
|
...account,
|
|
|
|
balance: cachedBalances[accountID],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selectedAccounts[accountID] = account
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return selectedAccounts
|
|
|
|
}
|
|
|
|
|
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]
|
2018-03-20 12:37:23 +01:00
|
|
|
const sendToken = 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
|
|
|
|
|
|
|
function autoAddToBetaUI (state) {
|
|
|
|
const autoAddTransactionThreshold = 12
|
|
|
|
const autoAddAccountsThreshold = 2
|
|
|
|
const autoAddTokensThreshold = 1
|
|
|
|
|
|
|
|
const numberOfTransactions = state.metamask.selectedAddressTxList.length
|
2018-11-30 23:51:24 +01:00
|
|
|
const numberOfAccounts = Object.keys(getMetaMaskAccounts(state)).length
|
2017-12-07 01:19:45 +01:00
|
|
|
const numberOfTokensAdded = state.metamask.tokens.length
|
|
|
|
|
|
|
|
const userPassesThreshold = (numberOfTransactions > autoAddTransactionThreshold) &&
|
|
|
|
(numberOfAccounts > autoAddAccountsThreshold) &&
|
|
|
|
(numberOfTokensAdded > autoAddTokensThreshold)
|
|
|
|
const userIsNotInBeta = !state.metamask.featureFlags.betaUI
|
2018-02-09 02:37:22 +01:00
|
|
|
|
2017-12-07 01:19:45 +01:00
|
|
|
return userIsNotInBeta && userPassesThreshold
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:57:37 +02:00
|
|
|
function getShouldUseNewUi (state) {
|
|
|
|
const isAlreadyUsingBetaUi = state.metamask.featureFlags.betaUI
|
|
|
|
const isMascara = state.metamask.isMascara
|
|
|
|
const isFreshInstall = Object.keys(state.metamask.identities).length === 0
|
|
|
|
return isAlreadyUsingBetaUi || isMascara || isFreshInstall
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
function preferencesSelector ({ metamask }) {
|
|
|
|
return metamask.preferences
|
|
|
|
}
|