2020-01-09 04:34:58 +01:00
|
|
|
import abi from 'human-standard-token-abi'
|
|
|
|
import { multiplyCurrencies } from '../../helpers/utils/conversion-util'
|
2020-03-06 22:34:56 +01:00
|
|
|
import {
|
|
|
|
accountsWithSendEtherInfoSelector,
|
|
|
|
getAddressBook,
|
2020-03-11 19:40:35 +01:00
|
|
|
getSelectedAccount,
|
|
|
|
getTargetAccount,
|
2020-03-06 22:34:56 +01:00
|
|
|
getSelectedAddress,
|
|
|
|
} from '../../selectors/selectors'
|
2020-01-09 04:34:58 +01:00
|
|
|
import { estimateGasPriceFromRecentBlocks, calcGasTotal } from './send.utils'
|
2018-09-13 10:47:05 +02:00
|
|
|
import {
|
2019-08-26 19:30:39 +02:00
|
|
|
getAveragePriceEstimateInHexWEI,
|
2019-04-17 21:15:13 +02:00
|
|
|
} from '../../selectors/custom-gas'
|
2018-04-07 00:29:51 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getAmountConversionRate (state) {
|
2018-04-27 21:03:00 +02:00
|
|
|
return getSelectedToken(state)
|
|
|
|
? getSelectedTokenToFiatRate(state)
|
|
|
|
: getConversionRate(state)
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getBlockGasLimit (state) {
|
2018-05-22 17:16:53 +02:00
|
|
|
return state.metamask.currentBlockGasLimit
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getConversionRate (state) {
|
2018-04-07 00:29:51 +02:00
|
|
|
return state.metamask.conversionRate
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getCurrentCurrency (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.currentCurrency
|
|
|
|
}
|
2018-04-07 00:29:51 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getNativeCurrency (state) {
|
2018-10-26 10:26:43 +02:00
|
|
|
return state.metamask.nativeCurrency
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getCurrentNetwork (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.network
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getForceGasMin (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.send.forceGasMin
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getGasLimit (state) {
|
2018-09-13 10:47:05 +02:00
|
|
|
return state.metamask.send.gasLimit || '0'
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getGasPrice (state) {
|
2019-08-26 19:30:39 +02:00
|
|
|
return state.metamask.send.gasPrice || getAveragePriceEstimateInHexWEI(state)
|
2018-05-05 17:11:53 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getGasPriceFromRecentBlocks (state) {
|
2018-05-22 10:10:06 +02:00
|
|
|
return estimateGasPriceFromRecentBlocks(state.metamask.recentBlocks)
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getGasTotal (state) {
|
2018-09-13 10:47:05 +02:00
|
|
|
return calcGasTotal(getGasLimit(state), getGasPrice(state))
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getPrimaryCurrency (state) {
|
2018-04-27 21:03:00 +02:00
|
|
|
const selectedToken = getSelectedToken(state)
|
|
|
|
return selectedToken && selectedToken.symbol
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getRecentBlocks (state) {
|
2018-05-22 10:10:06 +02:00
|
|
|
return state.metamask.recentBlocks
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSelectedIdentity (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
const selectedAddress = getSelectedAddress(state)
|
|
|
|
const identities = state.metamask.identities
|
|
|
|
|
|
|
|
return identities[selectedAddress]
|
2018-04-11 16:21:54 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSelectedToken (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
const tokens = state.metamask.tokens || []
|
|
|
|
const selectedTokenAddress = state.metamask.selectedTokenAddress
|
|
|
|
const selectedToken = tokens.filter(({ address }) => address === selectedTokenAddress)[0]
|
|
|
|
const sendToken = state.metamask.send.token
|
|
|
|
|
|
|
|
return selectedToken || sendToken || null
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSelectedTokenContract (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
const selectedToken = getSelectedToken(state)
|
2018-05-14 14:13:55 +02:00
|
|
|
|
2018-04-27 02:38:14 +02:00
|
|
|
return selectedToken
|
|
|
|
? global.eth.contract(abi).at(selectedToken.address)
|
|
|
|
: null
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSelectedTokenExchangeRate (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
const tokenExchangeRates = state.metamask.tokenExchangeRates
|
|
|
|
const selectedToken = getSelectedToken(state) || {}
|
|
|
|
const { symbol = '' } = selectedToken
|
|
|
|
const pair = `${symbol.toLowerCase()}_eth`
|
2020-02-15 21:56:48 +01:00
|
|
|
const { rate: tokenExchangeRate = 0 } = (tokenExchangeRates && tokenExchangeRates[pair]) || {}
|
2018-04-27 02:38:14 +02:00
|
|
|
|
|
|
|
return tokenExchangeRate
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSelectedTokenToFiatRate (state) {
|
2018-04-07 00:29:51 +02:00
|
|
|
const selectedTokenExchangeRate = getSelectedTokenExchangeRate(state)
|
2018-04-11 16:21:54 +02:00
|
|
|
const conversionRate = getConversionRate(state)
|
2018-04-07 00:29:51 +02:00
|
|
|
|
|
|
|
const tokenToFiatRate = multiplyCurrencies(
|
|
|
|
conversionRate,
|
|
|
|
selectedTokenExchangeRate,
|
|
|
|
{ toNumericBase: 'dec' }
|
|
|
|
)
|
|
|
|
|
|
|
|
return tokenToFiatRate
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendAmount (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.send.amount
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendHexData (state) {
|
2018-07-17 00:50:26 +02:00
|
|
|
return state.metamask.send.data
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendHexDataFeatureFlagState (state) {
|
2018-08-16 09:43:13 +02:00
|
|
|
return state.metamask.featureFlags.sendHexData
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendEditingTransactionId (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.send.editingTransactionId
|
|
|
|
}
|
2018-04-07 00:29:51 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendErrors (state) {
|
2018-04-27 21:03:00 +02:00
|
|
|
return state.send.errors
|
2018-04-27 02:38:14 +02:00
|
|
|
}
|
2018-04-07 00:29:51 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendFrom (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.send.from
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendFromBalance (state) {
|
2020-03-11 19:40:35 +01:00
|
|
|
const fromAccount = getSendFromObject(state)
|
|
|
|
return fromAccount.balance
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 19:40:35 +01:00
|
|
|
export function getSendFromObject (state) {
|
|
|
|
const fromAddress = getSendFrom(state)
|
|
|
|
return fromAddress
|
|
|
|
? getTargetAccount(state, fromAddress)
|
|
|
|
: getSelectedAccount(state)
|
2018-04-26 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendMaxModeState (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.send.maxModeOn
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendTo (state) {
|
2018-04-07 00:29:51 +02:00
|
|
|
return state.metamask.send.to
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendToNickname (state) {
|
2019-07-31 21:56:44 +02:00
|
|
|
return state.metamask.send.toNickname
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendToAccounts (state) {
|
2018-04-07 00:29:51 +02:00
|
|
|
const fromAccounts = accountsWithSendEtherInfoSelector(state)
|
|
|
|
const addressBookAccounts = getAddressBook(state)
|
2019-04-12 02:03:33 +02:00
|
|
|
return [...fromAccounts, ...addressBookAccounts]
|
2018-04-07 00:29:51 +02:00
|
|
|
}
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getTokenBalance (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.send.tokenBalance
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendEnsResolution (state) {
|
2019-07-31 21:56:44 +02:00
|
|
|
return state.metamask.send.ensResolution
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getSendEnsResolutionError (state) {
|
2019-07-31 21:56:44 +02:00
|
|
|
return state.metamask.send.ensResolutionError
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getTokenExchangeRate (state, tokenSymbol) {
|
2018-04-27 02:38:14 +02:00
|
|
|
const pair = `${tokenSymbol.toLowerCase()}_eth`
|
|
|
|
const tokenExchangeRates = state.metamask.tokenExchangeRates
|
|
|
|
const { rate: tokenExchangeRate = 0 } = tokenExchangeRates[pair] || {}
|
|
|
|
|
|
|
|
return tokenExchangeRate
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getUnapprovedTxs (state) {
|
2018-04-27 02:38:14 +02:00
|
|
|
return state.metamask.unapprovedTxs
|
2018-04-26 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getQrCodeData (state) {
|
2018-07-25 02:32:20 +02:00
|
|
|
return state.appState.qrCodeData
|
2018-07-27 02:56:58 +02:00
|
|
|
}
|