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-08-11 04:35:01 +02:00
|
|
|
|
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,
|
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,
|
2017-08-10 06:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = selectors
|
|
|
|
|
2017-08-29 16:50:48 +02:00
|
|
|
function getSelectedAddress (state) {
|
2017-10-03 02:28:15 +02:00
|
|
|
const selectedAddress = state.metamask.selectedAddress || Object.keys(state.metamask.accounts)[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]
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:50:48 +02:00
|
|
|
function getSelectedAccount (state) {
|
2017-08-10 06:40:01 +02:00
|
|
|
const accounts = state.metamask.accounts
|
|
|
|
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) {
|
|
|
|
const {
|
|
|
|
accounts,
|
|
|
|
identities,
|
|
|
|
} = state.metamask
|
|
|
|
|
|
|
|
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-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
|
|
|
|
const numberOfAccounts = Object.keys(state.metamask.accounts).length
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|