1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-21 00:43:21 +01:00
metamask-extension/ui/app/pages/swaps/build-quote/build-quote.js

435 lines
14 KiB
JavaScript
Raw Normal View History

2020-10-06 20:28:38 +02:00
import React, { useContext, useEffect, useState, useCallback } from 'react'
import PropTypes from 'prop-types'
import { useDispatch, useSelector } from 'react-redux'
import classnames from 'classnames'
import { uniqBy } from 'lodash'
import { useHistory } from 'react-router-dom'
import { MetaMetricsContext } from '../../../contexts/metametrics.new'
import { useTokensToSearch } from '../../../hooks/useTokensToSearch'
import { useEqualityCheck } from '../../../hooks/useEqualityCheck'
import { useSwapsEthToken } from '../../../hooks/useSwapsEthToken'
2020-10-06 20:28:38 +02:00
import { I18nContext } from '../../../contexts/i18n'
import DropdownInputPair from '../dropdown-input-pair'
import DropdownSearchList from '../dropdown-search-list'
import SlippageButtons from '../slippage-buttons'
import { getTokens } from '../../../ducks/metamask/metamask'
import InfoTooltip from '../../../components/ui/info-tooltip'
2020-10-06 20:28:38 +02:00
import {
fetchQuotesAndSetQuoteState,
setSwapsFromToken,
setSwapToToken,
getFromToken,
getToToken,
getBalanceError,
getTopAssets,
getFetchParams,
} from '../../../ducks/swaps/swaps'
2020-11-03 00:41:28 +01:00
import {
getValueFromWeiHex,
hexToDecimal,
} from '../../../helpers/utils/conversions.util'
2020-10-06 20:28:38 +02:00
import { calcTokenAmount } from '../../../helpers/utils/token-util'
import { usePrevious } from '../../../hooks/usePrevious'
import { useTokenTracker } from '../../../hooks/useTokenTracker'
import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount'
import { useEthFiatAmount } from '../../../hooks/useEthFiatAmount'
import { ETH_SWAPS_TOKEN_OBJECT } from '../../../helpers/constants/swaps'
import { resetSwapsPostFetchState, removeToken } from '../../../store/actions'
2020-10-06 20:28:38 +02:00
import { fetchTokenPrice, fetchTokenBalance } from '../swaps.util'
import SwapsFooter from '../swaps-footer'
2020-11-03 00:41:28 +01:00
const fuseSearchKeys = [
{ name: 'name', weight: 0.499 },
{ name: 'symbol', weight: 0.499 },
{ name: 'address', weight: 0.002 },
]
2020-10-06 20:28:38 +02:00
const MAX_ALLOWED_SLIPPAGE = 15
2020-11-03 00:41:28 +01:00
export default function BuildQuote({
2020-10-06 20:28:38 +02:00
inputValue,
onInputChange,
ethBalance,
setMaxSlippage,
maxSlippage,
selectedAccountAddress,
}) {
const t = useContext(I18nContext)
const dispatch = useDispatch()
const history = useHistory()
const metaMetricsEvent = useContext(MetaMetricsContext)
2020-11-03 00:41:28 +01:00
const [fetchedTokenExchangeRate, setFetchedTokenExchangeRate] = useState(
undefined,
)
2020-10-06 20:28:38 +02:00
const balanceError = useSelector(getBalanceError)
const fetchParams = useSelector(getFetchParams)
2020-11-03 00:41:28 +01:00
const { sourceTokenInfo = {}, destinationTokenInfo = {} } =
fetchParams?.metaData || {}
2020-10-06 20:28:38 +02:00
const tokens = useSelector(getTokens)
const topAssets = useSelector(getTopAssets)
const fromToken = useSelector(getFromToken)
const toToken = useSelector(getToToken) || destinationTokenInfo
const swapsEthToken = useSwapsEthToken()
2020-11-03 00:41:28 +01:00
const fetchParamsFromToken =
sourceTokenInfo?.symbol === 'ETH' ? swapsEthToken : sourceTokenInfo
2020-10-06 20:28:38 +02:00
const { loading, tokensWithBalances } = useTokenTracker(tokens)
// If the fromToken was set in a call to `onFromSelect` (see below), and that from token has a balance
// but is not in tokensWithBalances or tokens, then we want to add it to the usersTokens array so that
// the balance of the token can appear in the from token selection dropdown
2020-11-03 00:41:28 +01:00
const fromTokenArray =
fromToken?.symbol !== 'ETH' && fromToken?.balance ? [fromToken] : []
const usersTokens = uniqBy(
[...tokensWithBalances, ...tokens, ...fromTokenArray],
'address',
)
2020-10-06 20:28:38 +02:00
const memoizedUsersTokens = useEqualityCheck(usersTokens)
const selectedFromToken = useTokensToSearch({
2020-11-03 00:41:28 +01:00
providedTokens:
fromToken || fetchParamsFromToken
? [fromToken || fetchParamsFromToken]
: [],
2020-10-06 20:28:38 +02:00
usersTokens: memoizedUsersTokens,
onlyEth: (fromToken || fetchParamsFromToken)?.symbol === 'ETH',
singleToken: true,
})[0]
const tokensToSearch = useTokensToSearch({
usersTokens: memoizedUsersTokens,
topTokens: topAssets,
})
2020-11-03 00:41:28 +01:00
const selectedToToken =
tokensToSearch.find(({ address }) => address === toToken?.address) ||
toToken
2020-10-06 20:28:38 +02:00
const {
address: fromTokenAddress,
symbol: fromTokenSymbol,
string: fromTokenString,
decimals: fromTokenDecimals,
balance: rawFromTokenBalance,
2020-10-06 20:28:38 +02:00
} = selectedFromToken || {}
2020-11-03 00:41:28 +01:00
const fromTokenBalance =
rawFromTokenBalance &&
calcTokenAmount(rawFromTokenBalance, fromTokenDecimals).toString(10)
const prevFromTokenBalance = usePrevious(fromTokenBalance)
2020-10-06 20:28:38 +02:00
const swapFromTokenFiatValue = useTokenFiatAmount(
fromTokenAddress,
inputValue || 0,
fromTokenSymbol,
{
showFiat: true,
},
true,
)
2020-11-03 00:41:28 +01:00
const swapFromEthFiatValue = useEthFiatAmount(
inputValue || 0,
{ showFiat: true },
true,
)
const swapFromFiatValue =
fromTokenSymbol === 'ETH' ? swapFromEthFiatValue : swapFromTokenFiatValue
2020-10-06 20:28:38 +02:00
const onFromSelect = (token) => {
2020-11-03 00:41:28 +01:00
if (
token?.address &&
!swapFromFiatValue &&
fetchedTokenExchangeRate !== null
) {
fetchTokenPrice(token.address).then((rate) => {
if (rate !== null && rate !== undefined) {
setFetchedTokenExchangeRate(rate)
}
})
2020-10-06 20:28:38 +02:00
} else {
setFetchedTokenExchangeRate(null)
}
2020-11-03 00:41:28 +01:00
if (
token?.address &&
!memoizedUsersTokens.find(
(usersToken) => usersToken.address === token.address,
)
) {
fetchTokenBalance(token.address, selectedAccountAddress).then(
(fetchedBalance) => {
2020-10-06 20:28:38 +02:00
if (fetchedBalance?.balance) {
const balanceAsDecString = fetchedBalance.balance.toString(10)
2020-11-03 00:41:28 +01:00
const userTokenBalance = calcTokenAmount(
balanceAsDecString,
token.decimals,
)
dispatch(
setSwapsFromToken({
...token,
string: userTokenBalance.toString(10),
balance: balanceAsDecString,
}),
)
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
},
)
2020-10-06 20:28:38 +02:00
}
dispatch(setSwapsFromToken(token))
2020-11-03 00:41:28 +01:00
onInputChange(
token?.address ? inputValue : '',
token.string,
token.decimals,
)
2020-10-06 20:28:38 +02:00
}
const { destinationTokenAddedForSwap } = fetchParams || {}
const { address: toAddress } = toToken || {}
2020-11-03 00:41:28 +01:00
const onToSelect = useCallback(
(token) => {
if (destinationTokenAddedForSwap && token.address !== toAddress) {
dispatch(removeToken(toAddress))
}
dispatch(setSwapToToken(token))
},
[dispatch, destinationTokenAddedForSwap, toAddress],
)
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const hideDropdownItemIf = useCallback(
(item) => item.address === fromTokenAddress,
[fromTokenAddress],
)
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const tokensWithBalancesFromToken = tokensWithBalances.find(
(token) => token.address === fromToken?.address,
)
const previousTokensWithBalancesFromToken = usePrevious(
tokensWithBalancesFromToken,
)
useEffect(() => {
2020-11-03 00:41:28 +01:00
const notEth =
tokensWithBalancesFromToken?.address !== ETH_SWAPS_TOKEN_OBJECT.address
const addressesAreTheSame =
tokensWithBalancesFromToken?.address ===
previousTokensWithBalancesFromToken?.address
const balanceHasChanged =
tokensWithBalancesFromToken?.balance !==
previousTokensWithBalancesFromToken?.balance
if (notEth && addressesAreTheSame && balanceHasChanged) {
2020-11-03 00:41:28 +01:00
dispatch(
setSwapsFromToken({
...fromToken,
balance: tokensWithBalancesFromToken?.balance,
string: tokensWithBalancesFromToken?.string,
}),
)
}
2020-11-03 00:41:28 +01:00
}, [
dispatch,
tokensWithBalancesFromToken,
previousTokensWithBalancesFromToken,
fromToken,
])
// If the eth balance changes while on build quote, we update the selected from token
2020-10-06 20:28:38 +02:00
useEffect(() => {
2020-11-03 00:41:28 +01:00
if (
fromToken?.address === ETH_SWAPS_TOKEN_OBJECT.address &&
fromToken?.balance !== hexToDecimal(ethBalance)
) {
dispatch(
setSwapsFromToken({
...fromToken,
balance: hexToDecimal(ethBalance),
string: getValueFromWeiHex({
value: ethBalance,
numberOfDecimals: 4,
toDenomination: 'ETH',
}),
}),
)
2020-10-06 20:28:38 +02:00
}
}, [dispatch, fromToken, ethBalance])
useEffect(() => {
if (prevFromTokenBalance !== fromTokenBalance) {
onInputChange(inputValue, fromTokenBalance)
2020-10-06 20:28:38 +02:00
}
}, [onInputChange, prevFromTokenBalance, inputValue, fromTokenBalance])
2020-10-06 20:28:38 +02:00
useEffect(() => {
dispatch(resetSwapsPostFetchState())
}, [dispatch])
return (
<div className="build-quote">
<div className="build-quote__content">
<div className="build-quote__dropdown-input-pair-header">
<div className="build-quote__input-label">{t('swapSwapFrom')}</div>
{fromTokenSymbol !== 'ETH' && (
<div
className="build-quote__max-button"
2020-11-03 00:41:28 +01:00
onClick={() =>
onInputChange(fromTokenBalance || '0', fromTokenBalance)
}
>
{t('max')}
</div>
)}
2020-10-06 20:28:38 +02:00
</div>
<DropdownInputPair
onSelect={onFromSelect}
itemsToSearch={tokensToSearch}
onInputChange={(value) => {
onInputChange(value, fromTokenBalance)
2020-10-06 20:28:38 +02:00
}}
inputValue={inputValue}
leftValue={inputValue && swapFromFiatValue}
selectedItem={selectedFromToken}
maxListItems={30}
2020-11-03 00:41:28 +01:00
loading={
loading &&
(!tokensToSearch?.length ||
!topAssets ||
!Object.keys(topAssets).length)
}
selectPlaceHolderText={t('swapSelect')}
2020-10-06 20:28:38 +02:00
hideItemIf={(item) => item.address === selectedToToken?.address}
listContainerClassName="build-quote__open-dropdown"
autoFocus
2020-10-06 20:28:38 +02:00
/>
<div
className={classnames('build-quote__balance-message', {
'build-quote__balance-message--error': balanceError,
})}
>
2020-11-03 00:41:28 +01:00
{!balanceError &&
fromTokenSymbol &&
t('swapYourTokenBalance', [
fromTokenString || '0',
fromTokenSymbol,
])}
2020-10-06 20:28:38 +02:00
{balanceError && fromTokenSymbol && (
<div className="build-quite__insufficient-funds">
2020-11-03 00:41:28 +01:00
<div className="build-quite__insufficient-funds-first">
{t('swapsNotEnoughForTx', [fromTokenSymbol])}
</div>
<div className="build-quite__insufficient-funds-second">
{t('swapYourTokenBalance', [
fromTokenString || '0',
fromTokenSymbol,
])}
</div>
2020-10-06 20:28:38 +02:00
</div>
)}
</div>
2020-11-03 00:41:28 +01:00
<div className="build-quote__swap-arrows-row">
<button
2020-10-06 20:28:38 +02:00
className="build-quote__swap-arrows"
onClick={() => {
onToSelect(selectedFromToken)
onFromSelect(selectedToToken)
}}
2020-11-03 00:41:28 +01:00
>
<img
src="/images/icons/swap2.svg"
alt={t('swapSwapSwitch')}
width="12"
height="16"
/>
</button>
2020-10-06 20:28:38 +02:00
</div>
<div className="build-quote__dropdown-swap-to-header">
<div className="build-quote__input-label">{t('swapSwapTo')}</div>
</div>
<div className="dropdown-input-pair dropdown-input-pair__to">
<DropdownSearchList
startingItem={selectedToToken}
itemsToSearch={tokensToSearch}
searchPlaceholderText={t('swapSearchForAToken')}
fuseSearchKeys={fuseSearchKeys}
selectPlaceHolderText={t('swapSelectAToken')}
maxListItems={30}
onSelect={onToSelect}
2020-11-03 00:41:28 +01:00
loading={
loading &&
(!tokensToSearch?.length ||
!topAssets ||
!Object.keys(topAssets).length)
}
externallySelectedItem={selectedToToken}
hideItemIf={hideDropdownItemIf}
listContainerClassName="build-quote__open-to-dropdown"
hideRightLabels
defaultToAll
/>
</div>
{selectedToToken?.address &&
selectedToToken?.address !== ETH_SWAPS_TOKEN_OBJECT.address && (
<div className="build-quote__token-message">
{t('verifyThisTokenOn', [
<a
className="build-quote__token-etherscan-link"
key="build-quote-etherscan-link"
href={`https://etherscan.io/token/${selectedToToken.address}`}
target="_blank"
rel="noopener noreferrer"
>
{t('etherscan')}
</a>,
])}
<InfoTooltip
position="top"
contentText={t('swapVerifyTokenExplanation')}
containerClassName="build-quote__token-tooltip-container"
/>
</div>
)}
2020-10-06 20:28:38 +02:00
<div className="build-quote__slippage-buttons-container">
<SlippageButtons
onSelect={(newSlippage) => {
setMaxSlippage(newSlippage)
}}
maxAllowedSlippage={MAX_ALLOWED_SLIPPAGE}
2020-10-06 20:28:38 +02:00
/>
</div>
</div>
<SwapsFooter
onSubmit={() => {
2020-11-03 00:41:28 +01:00
dispatch(
fetchQuotesAndSetQuoteState(
history,
inputValue,
maxSlippage,
metaMetricsEvent,
),
)
2020-10-06 20:28:38 +02:00
}}
submitText={t('swapGetQuotes')}
2020-11-03 00:41:28 +01:00
disabled={
!Number(inputValue) ||
!selectedToToken?.address ||
Number(maxSlippage) === 0 ||
Number(maxSlippage) > MAX_ALLOWED_SLIPPAGE
2020-11-03 00:41:28 +01:00
}
2020-10-06 20:28:38 +02:00
hideCancel
showTermsOfService
2020-10-06 20:28:38 +02:00
/>
</div>
)
}
BuildQuote.propTypes = {
maxSlippage: PropTypes.number,
inputValue: PropTypes.string,
onInputChange: PropTypes.func,
ethBalance: PropTypes.string,
setMaxSlippage: PropTypes.func,
selectedAccountAddress: PropTypes.string,
}