1- import React , { useState , useCallback , useEffect , useContext } from 'react'
2- import ReactGA from 'react-ga'
3- import { ThemeContext } from 'styled-components'
4- import { parseEther , parseUnits } from '@ethersproject/units'
5- import { JSBI , Percent , TokenAmount , TradeType , WETH , Fraction } from '@uniswap/sdk'
6- import { ArrowDown , ChevronDown , ChevronUp , Repeat } from 'react-feather'
7- import { withRouter , RouteComponentProps } from 'react-router-dom'
81import { BigNumber } from '@ethersproject/bignumber'
92import { MaxUint256 } from '@ethersproject/constants'
103import { Contract } from '@ethersproject/contracts'
11- import { useUserAdvanced } from '../../state/application/hooks'
12- import { useTokenBalanceTreatingWETHasETH , useAllTokenBalancesTreatingWETHasETH } from '../../state/wallet/hooks'
13- import { Field , SwapAction , useSwapStateReducer } from './swap-store'
4+ import { parseEther , parseUnits } from '@ethersproject/units'
5+ import { Fraction , JSBI , Percent , TokenAmount , TradeType , WETH } from '@uniswap/sdk'
6+ import React , { useCallback , useContext , useEffect , useState } from 'react'
7+ import { ArrowDown , ChevronDown , ChevronUp , Repeat } from 'react-feather'
8+ import ReactGA from 'react-ga'
9+ import { RouteComponentProps , withRouter } from 'react-router-dom'
1410import { Text } from 'rebass'
11+ import { ThemeContext } from 'styled-components'
1512import Card , { BlueCard , GreyCard , YellowCard } from '../../components/Card'
1613import { AutoColumn , ColumnCenter } from '../../components/Column'
17- import { AutoRow , RowBetween , RowFixed } from '../Row'
1814import { ROUTER_ADDRESS } from '../../constants'
1915import { useTokenAllowance } from '../../data/Allowances'
20- import { useAddUserToken , useFetchTokenByAddress } from '../../state/user/hooks'
21- import { useAllTokens , useToken } from '../../contexts/Tokens'
22- import { useHasPendingApproval , useTransactionAdder } from '../../state/transactions/hooks'
16+ import { useV1TradeLinkIfBetter } from '../../data/V1'
2317import { useTokenContract , useWeb3React } from '../../hooks'
18+ import { useTokenByAddressAndAutomaticallyAdd } from '../../hooks/Tokens'
2419import { useTradeExactIn , useTradeExactOut } from '../../hooks/Trades'
25- import { useWalletModalToggle } from '../../state/application/hooks'
20+ import { useUserAdvanced , useWalletModalToggle } from '../../state/application/hooks'
21+ import { useHasPendingApproval , useTransactionAdder } from '../../state/transactions/hooks'
22+ import { useAllTokenBalancesTreatingWETHasETH } from '../../state/wallet/hooks'
2623import { Hover , TYPE } from '../../theme'
2724import { Link } from '../../theme/components'
2825import {
26+ basisPointsToPercent ,
2927 calculateGasMargin ,
3028 getEtherscanLink ,
3129 getRouterContract ,
32- basisPointsToPercent ,
33- QueryParams ,
34- getSigner
30+ getSigner ,
31+ QueryParams
3532} from '../../utils'
3633import Copy from '../AccountDetails/Copy'
3734import AddressInputPanel from '../AddressInputPanel'
3835import { ButtonError , ButtonLight , ButtonPrimary , ButtonSecondary } from '../Button'
3936import ConfirmationModal from '../ConfirmationModal'
4037import CurrencyInputPanel from '../CurrencyInputPanel'
4138import QuestionHelper from '../Question'
39+ import { AutoRow , RowBetween , RowFixed } from '../Row'
4240import SlippageTabs from '../SlippageTabs'
4341import TokenLogo from '../TokenLogo'
4442import {
@@ -55,7 +53,7 @@ import {
5553 TruncatedText ,
5654 Wrapper
5755} from './styleds'
58- import { useV1TradeLinkIfBetter } from '../../data/V1 '
56+ import { Field , SwapAction , useSwapStateReducer } from './swap-store '
5957
6058enum SwapType {
6159 EXACT_TOKENS_FOR_TOKENS ,
@@ -108,35 +106,10 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
108106 const [ tradeError , setTradeError ] = useState < string > ( '' ) // error for things like reserve size or route
109107
110108 const tokens = {
111- [ Field . INPUT ] : useToken ( fieldData [ Field . INPUT ] . address ) ,
112- [ Field . OUTPUT ] : useToken ( fieldData [ Field . OUTPUT ] . address )
109+ [ Field . INPUT ] : useTokenByAddressAndAutomaticallyAdd ( fieldData [ Field . INPUT ] . address ) ,
110+ [ Field . OUTPUT ] : useTokenByAddressAndAutomaticallyAdd ( fieldData [ Field . OUTPUT ] . address )
113111 }
114112
115- // ensure input + output tokens are added to localstorage
116- const fetchTokenByAddress = useFetchTokenByAddress ( )
117- const addToken = useAddUserToken ( )
118- const allTokens = useAllTokens ( )
119- const inputTokenAddress = fieldData [ Field . INPUT ] . address
120- useEffect ( ( ) => {
121- if ( inputTokenAddress && ! Object . keys ( allTokens ) . some ( tokenAddress => tokenAddress === inputTokenAddress ) ) {
122- fetchTokenByAddress ( inputTokenAddress ) . then ( token => {
123- if ( token !== null ) {
124- addToken ( token )
125- }
126- } )
127- }
128- } , [ inputTokenAddress , allTokens , fetchTokenByAddress , addToken ] )
129- const outputTokenAddress = fieldData [ Field . OUTPUT ] . address
130- useEffect ( ( ) => {
131- if ( outputTokenAddress && ! Object . keys ( allTokens ) . some ( tokenAddress => tokenAddress === outputTokenAddress ) ) {
132- fetchTokenByAddress ( outputTokenAddress ) . then ( token => {
133- if ( token !== null ) {
134- addToken ( token )
135- }
136- } )
137- }
138- } , [ outputTokenAddress , allTokens , fetchTokenByAddress , addToken ] )
139-
140113 // token contracts for approvals and direct sends
141114 const tokenContractInput : Contract = useTokenContract ( tokens [ Field . INPUT ] ?. address )
142115 const tokenContractOutput : Contract = useTokenContract ( tokens [ Field . OUTPUT ] ?. address )
@@ -157,8 +130,8 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
157130
158131 // get user- and token-specific lookup data
159132 const userBalances = {
160- [ Field . INPUT ] : useTokenBalanceTreatingWETHasETH ( account , tokens [ Field . INPUT ] ) ,
161- [ Field . OUTPUT ] : useTokenBalanceTreatingWETHasETH ( account , tokens [ Field . OUTPUT ] )
133+ [ Field . INPUT ] : allBalances ?. [ tokens [ Field . INPUT ] ?. address ] ?. raw ,
134+ [ Field . OUTPUT ] : allBalances ?. [ tokens [ Field . OUTPUT ] ?. address ] ?. raw
162135 }
163136
164137 // parse the amount that the user typed
@@ -773,7 +746,7 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
773746 if ( sending && ! sendingWithSwap ) {
774747 return (
775748 < AutoColumn >
776- < ButtonPrimary onClick = { onSend } >
749+ < ButtonPrimary onClick = { onSend } id = "exchange-page-confirm-send" >
777750 < Text color = "white" fontSize = { 20 } >
778751 Confirm send
779752 </ Text >
@@ -868,7 +841,12 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
868841 </ AutoColumn >
869842
870843 < AutoRow >
871- < ButtonError onClick = { onSwap } error = { ! ! warningHigh } style = { { margin : '10px 0 0 0' } } >
844+ < ButtonError
845+ onClick = { onSwap }
846+ error = { ! ! warningHigh }
847+ style = { { margin : '10px 0 0 0' } }
848+ id = "exchange-page-confirm-swap-or-send"
849+ >
872850 < Text fontSize = { 20 } fontWeight = { 500 } >
873851 { warningHigh ? ( sending ? 'Send Anyway' : 'Swap Anyway' ) : sending ? 'Confirm Send' : 'Confirm Swap' }
874852 </ Text >
@@ -951,7 +929,7 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
951929 }
952930
953931 return (
954- < Wrapper id = "exchangePage " >
932+ < Wrapper id = "exchange-page " >
955933 < ConfirmationModal
956934 isOpen = { showConfirm }
957935 title = { sendingWithSwap ? 'Confirm swap and send' : sending ? 'Confirm Send' : 'Confirm Swap' }
@@ -989,7 +967,7 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
989967 showSendWithSwap = { true }
990968 advanced = { advanced }
991969 label = { '' }
992- inputId = "swapInputField "
970+ id = "swap-currency-input "
993971 otherSelectedTokenAddress = { tokens [ Field . OUTPUT ] ?. address }
994972 />
995973 </ InputGroup >
@@ -1034,7 +1012,7 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
10341012 } }
10351013 onTokenSelection = { address => onTokenSelection ( Field . INPUT , address ) }
10361014 otherSelectedTokenAddress = { tokens [ Field . OUTPUT ] ?. address }
1037- inputId = "swapInputField "
1015+ id = "swap-currency-input "
10381016 />
10391017
10401018 { sendingWithSwap ? (
@@ -1078,7 +1056,7 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
10781056 onTokenSelection = { address => onTokenSelection ( Field . OUTPUT , address ) }
10791057 advanced = { advanced }
10801058 otherSelectedTokenAddress = { tokens [ Field . INPUT ] ?. address }
1081- inputId = "swapOutputField "
1059+ id = "swap-currency-output "
10821060 />
10831061 { sendingWithSwap && (
10841062 < RowBetween padding = "0 1rem 0 12px" >
@@ -1199,6 +1177,7 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
11991177 onClick = { ( ) => {
12001178 setShowConfirm ( true )
12011179 } }
1180+ id = "exchange-swap-button"
12021181 disabled = { ! isValid }
12031182 error = { ! ! warningHigh }
12041183 >
@@ -1229,7 +1208,7 @@ function ExchangePage({ sendingInput = false, history, params }: ExchangePagePro
12291208 < AdvancedDropwdown >
12301209 { ! showAdvanced && (
12311210 < Hover >
1232- < RowBetween onClick = { ( ) => setShowAdvanced ( true ) } padding = { '8px 20px' } >
1211+ < RowBetween onClick = { ( ) => setShowAdvanced ( true ) } padding = { '8px 20px' } id = "exchange-show-advanced" >
12331212 < Text fontSize = { 16 } fontWeight = { 500 } style = { { userSelect : 'none' } } >
12341213 Show Advanced
12351214 </ Text >
0 commit comments