Commit cf5c67ec authored by Ian Lapham's avatar Ian Lapham Committed by GitHub

fix rerender bugs (#666)

parent eaf8d5a3
...@@ -30,7 +30,7 @@ function PositionCard({ pairAddress, token0, token1, history, minimal = false, . ...@@ -30,7 +30,7 @@ function PositionCard({ pairAddress, token0, token1, history, minimal = false, .
const pair = tokenAmount0 && tokenAmount1 && new Pair(tokenAmount0, tokenAmount1) const pair = tokenAmount0 && tokenAmount1 && new Pair(tokenAmount0, tokenAmount1)
const userPoolBalance = allBalances?.[account]?.[pairAddress] const userPoolBalance = allBalances?.[account]?.[pairAddress]
const totalPoolTokens = useTotalSupply(pair) const totalPoolTokens = useTotalSupply(token0, token1)
const poolTokenPercentage = const poolTokenPercentage =
!!userPoolBalance && !!totalPoolTokens ? new Percent(userPoolBalance.raw, totalPoolTokens.raw) : undefined !!userPoolBalance && !!totalPoolTokens ? new Percent(userPoolBalance.raw, totalPoolTokens.raw) : undefined
......
...@@ -450,11 +450,6 @@ export function useAddressBalance(address: string, token: Token): TokenAmount | ...@@ -450,11 +450,6 @@ export function useAddressBalance(address: string, token: Token): TokenAmount |
const value = typeof chainId === 'number' ? state?.[chainId]?.[address]?.[token?.address]?.value : undefined const value = typeof chainId === 'number' ? state?.[chainId]?.[address]?.[token?.address]?.value : undefined
const formattedValue = value && token && new TokenAmount(token, value) const formattedValue = value && token && new TokenAmount(token, value)
/**
* @todo
* when catching for token, causes infinite rerender
* when the token is an exchange liquidity token
*/
useEffect(() => { useEffect(() => {
if (typeof chainId === 'number' && isAddress(address) && token && token.address && isAddress(token.address)) { if (typeof chainId === 'number' && isAddress(address) && token && token.address && isAddress(token.address)) {
startListening(chainId, address, token.address) startListening(chainId, address, token.address)
...@@ -462,8 +457,7 @@ export function useAddressBalance(address: string, token: Token): TokenAmount | ...@@ -462,8 +457,7 @@ export function useAddressBalance(address: string, token: Token): TokenAmount |
stopListening(chainId, address, token.address) stopListening(chainId, address, token.address)
} }
} }
// eslint-disable-next-line react-hooks/exhaustive-deps }, [chainId, address, startListening, stopListening, token])
}, [chainId, address, startListening, stopListening])
return useMemo(() => formattedValue, [formattedValue]) return useMemo(() => formattedValue, [formattedValue])
} }
......
...@@ -100,9 +100,13 @@ export function usePair(tokenA?: Token, tokenB?: Token): Pair | undefined { ...@@ -100,9 +100,13 @@ export function usePair(tokenA?: Token, tokenB?: Token): Pair | undefined {
const address = usePairAddress(tokenA, tokenB) const address = usePairAddress(tokenA, tokenB)
const tokenAmountA = useAddressBalance(address, tokenA) const tokenAmountA = useAddressBalance(address, tokenA)
const tokenAmountB = useAddressBalance(address, tokenB) const tokenAmountB = useAddressBalance(address, tokenB)
const pair = tokenAmountA && tokenAmountB && new Pair(tokenAmountA, tokenAmountB) const [pair, setPair] = useState<Pair>()
// return pair useEffect(() => {
if (!pair && tokenAmountA && tokenAmountB) {
setPair(new Pair(tokenAmountA, tokenAmountB))
}
}, [pair, tokenAmountA, tokenAmountB])
return useMemo(() => { return useMemo(() => {
return pair return pair
...@@ -146,9 +150,11 @@ export function useAllPairs() { ...@@ -146,9 +150,11 @@ export function useAllPairs() {
}, [allPairs]) }, [allPairs])
} }
export function useTotalSupply(pair: Pair) { export function useTotalSupply(tokenA?: Token, tokenB?: Token) {
const { library } = useWeb3React() const { library } = useWeb3React()
const pair = usePair(tokenA, tokenB)
const [totalPoolTokens, setTotalPoolTokens] = useState<TokenAmount>() const [totalPoolTokens, setTotalPoolTokens] = useState<TokenAmount>()
const pairContract = usePairContract(pair?.liquidityToken.address) const pairContract = usePairContract(pair?.liquidityToken.address)
...@@ -169,12 +175,7 @@ export function useTotalSupply(pair: Pair) { ...@@ -169,12 +175,7 @@ export function useTotalSupply(pair: Pair) {
} }
}) })
.catch(() => {}) .catch(() => {})
/** }, [pairContract, pair])
* @todo
* fix this
*/
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pairContract])
// on the block make sure we're updated // on the block make sure we're updated
useEffect(() => { useEffect(() => {
......
...@@ -103,13 +103,7 @@ export function useRoute(tokenA: Token, tokenB: Token) { ...@@ -103,13 +103,7 @@ export function useRoute(tokenA: Token, tokenB: Token) {
update([tokenA.address, tokenB.address], routeThroughETH, chainId) update([tokenA.address, tokenB.address], routeThroughETH, chainId)
} }
} }
/** }, [route, requiresHop, update, chainId, tokenA, tokenB, defaultPair, aToETH, bToETH])
* @todo
* same infinite render bug here when including
* any token or pair instance as dependency (bug also in exchanegs and balances)
*/
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [route, requiresHop, update, chainId])
return useMemo(() => route, [route]) return useMemo(() => route, [route])
} }
...@@ -161,7 +161,7 @@ export default function AddLiquidity({ token0, token1 }) { ...@@ -161,7 +161,7 @@ export default function AddLiquidity({ token0, token1 }) {
// exhchange data // exhchange data
const pair: Pair = usePair(tokens[Field.INPUT], tokens[Field.OUTPUT]) const pair: Pair = usePair(tokens[Field.INPUT], tokens[Field.OUTPUT])
const route: Route = pair ? new Route([pair], tokens[independentField]) : undefined const route: Route = pair ? new Route([pair], tokens[independentField]) : undefined
const totalSupply: TokenAmount = useTotalSupply(pair) const totalSupply: TokenAmount = useTotalSupply(tokens[Field.INPUT], tokens[Field.OUTPUT])
const [noLiquidity, setNoLiquidity] = useState<boolean>(false) // used to detect new exchange const [noLiquidity, setNoLiquidity] = useState<boolean>(false) // used to detect new exchange
// state for amount approvals // state for amount approvals
......
...@@ -164,7 +164,7 @@ export default function RemoveLiquidity({ token0, token1 }) { ...@@ -164,7 +164,7 @@ export default function RemoveLiquidity({ token0, token1 }) {
const pairContract: ethers.Contract = usePairContract(pair?.liquidityToken.address) const pairContract: ethers.Contract = usePairContract(pair?.liquidityToken.address)
// pool token data // pool token data
const totalPoolTokens: TokenAmount = useTotalSupply(pair) const totalPoolTokens: TokenAmount = useTotalSupply(tokens[Field.TOKEN0], tokens[Field.TOKEN1])
const allBalances: TokenAmount[] = useAllBalances() const allBalances: TokenAmount[] = useAllBalances()
const userLiquidity: TokenAmount = allBalances?.[account]?.[pair?.liquidityToken?.address] const userLiquidity: TokenAmount = allBalances?.[account]?.[pair?.liquidityToken?.address]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment