2017-08-11 04:35:01 +02:00
|
|
|
const valuesFor = require('./util').valuesFor
|
2017-10-30 18:48:50 +01:00
|
|
|
const abi = require('human-standard-token-abi')
|
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,
|
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,
|
2017-10-12 18:42:14 +02:00
|
|
|
getGasPrice,
|
|
|
|
getGasLimit,
|
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,
|
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,
|
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]
|
|
|
|
|
|
|
|
return selectedToken || null
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
function getSelectedTokenExchangeRate (state) {
|
|
|
|
const tokenExchangeRates = state.metamask.tokenExchangeRates
|
|
|
|
const selectedToken = getSelectedToken(state) || {}
|
|
|
|
const { symbol = '' } = selectedToken
|
2017-10-12 18:42:14 +02:00
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
const pair = `${symbol.toLowerCase()}_eth`
|
|
|
|
const { rate: tokenExchangeRate = 0 } = tokenExchangeRates[pair] || {}
|
|
|
|
|
|
|
|
return tokenExchangeRate
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:08:53 +02:00
|
|
|
function getTokenExchangeRate (state, tokenSymbol) {
|
|
|
|
const pair = `${tokenSymbol.toLowerCase()}_eth`
|
|
|
|
const tokenExchangeRates = state.metamask.tokenExchangeRates
|
|
|
|
const { rate: tokenExchangeRate = 0 } = tokenExchangeRates[pair] || {}
|
|
|
|
|
|
|
|
return tokenExchangeRate
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:50:48 +02:00
|
|
|
function transactionsSelector (state) {
|
2017-09-07 12:14:53 +02:00
|
|
|
const { network, selectedTokenAddress } = state.metamask
|
2017-08-11 04:35:01 +02:00
|
|
|
const unapprovedMsgs = valuesFor(state.metamask.unapprovedMsgs)
|
|
|
|
const shapeShiftTxList = (network === '1') ? state.metamask.shapeShiftTxList : undefined
|
|
|
|
const transactions = state.metamask.selectedAddressTxList || []
|
|
|
|
const txsToRender = !shapeShiftTxList ? transactions.concat(unapprovedMsgs) : transactions.concat(unapprovedMsgs, shapeShiftTxList)
|
|
|
|
|
2017-10-19 21:47:44 +02:00
|
|
|
// console.log({txsToRender, selectedTokenAddress})
|
2017-09-07 12:14:53 +02:00
|
|
|
return selectedTokenAddress
|
|
|
|
? txsToRender
|
2017-09-07 20:46:05 +02:00
|
|
|
.filter(({ txParams: { to } }) => to === selectedTokenAddress)
|
2017-09-07 12:14:53 +02:00
|
|
|
.sort((a, b) => b.time - a.time)
|
|
|
|
: txsToRender
|
|
|
|
.sort((a, b) => b.time - a.time)
|
2017-08-29 16:50:48 +02:00
|
|
|
}
|
2017-10-12 18:42:14 +02:00
|
|
|
|
|
|
|
function getGasPrice (state) {
|
|
|
|
return state.metamask.send.gasPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGasLimit (state) {
|
|
|
|
return state.metamask.send.gasLimit
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return userIsNotInBeta && userPassesThreshold
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentViewContext (state) {
|
|
|
|
const { currentView = {} } = state.appState
|
|
|
|
return currentView.context
|
2017-12-07 01:19:45 +01:00
|
|
|
}
|