Commit 0edb0fe5 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: memoize on-chain results (#3493)

* fix: memo-ize onchain results

* fix: typeof omission
parent 496408b3
import { BigNumber } from '@ethersproject/bignumber' import { BigNumber } from '@ethersproject/bignumber'
import { useSingleCallResult } from 'lib/hooks/multicall' import { useSingleCallResult } from 'lib/hooks/multicall'
import { useEffect, useState } from 'react' import { useMemo } from 'react'
import { useInterfaceMulticall } from './useContract' import { useInterfaceMulticall } from './useContract'
// gets the current timestamp from the blockchain // gets the current timestamp from the blockchain
export default function useCurrentBlockTimestamp(): BigNumber | undefined { export default function useCurrentBlockTimestamp(): BigNumber | undefined {
const [lastBlock, setLastBlock] = useState<BigNumber | undefined>()
const multicall = useInterfaceMulticall() const multicall = useInterfaceMulticall()
const block: BigNumber | undefined = useSingleCallResult(multicall, 'getCurrentBlockTimestamp')?.result?.[0] const resultStr: string | undefined = useSingleCallResult(
useEffect(() => { multicall,
// If block xor lastBlock are undefined, or if block has progressed, then update lastBlock. 'getCurrentBlockTimestamp'
// This prevents updates when the block doesn't change, because the returned BigNumber will still be referentially unique. )?.result?.[0]?.toString()
if (Boolean(block) !== Boolean(lastBlock) || (block && lastBlock && !block.eq(lastBlock))) { return useMemo(() => (typeof resultStr === 'string' ? BigNumber.from(resultStr) : undefined), [resultStr])
setLastBlock(block)
}
}, [block, lastBlock])
return lastBlock
} }
import JSBI from 'jsbi' import JSBI from 'jsbi'
import { useSingleCallResult } from 'lib/hooks/multicall' import { useSingleCallResult } from 'lib/hooks/multicall'
import { useMemo } from 'react'
import { useContract } from './useContract' import { useContract } from './useContract'
import useENSAddress from './useENSAddress' import useENSAddress from './useENSAddress'
...@@ -22,5 +23,5 @@ export default function useGasPrice(): JSBI | undefined { ...@@ -22,5 +23,5 @@ export default function useGasPrice(): JSBI | undefined {
const contract = useContract(address ?? undefined, CHAIN_DATA_ABI, false) const contract = useContract(address ?? undefined, CHAIN_DATA_ABI, false)
const resultStr = useSingleCallResult(contract, 'latestAnswer').result?.[0]?.toString() const resultStr = useSingleCallResult(contract, 'latestAnswer').result?.[0]?.toString()
return typeof resultStr === 'string' ? JSBI.BigInt(resultStr) : undefined return useMemo(() => (typeof resultStr === 'string' ? JSBI.BigInt(resultStr) : undefined), [resultStr])
} }
...@@ -9,5 +9,5 @@ export default function useIsArgentWallet(): boolean { ...@@ -9,5 +9,5 @@ export default function useIsArgentWallet(): boolean {
const argentWalletDetector = useArgentWalletDetectorContract() const argentWalletDetector = useArgentWalletDetectorContract()
const inputs = useMemo(() => [account ?? undefined], [account]) const inputs = useMemo(() => [account ?? undefined], [account])
const call = useSingleCallResult(argentWalletDetector, 'isArgentWallet', inputs, NEVER_RELOAD) const call = useSingleCallResult(argentWalletDetector, 'isArgentWallet', inputs, NEVER_RELOAD)
return call?.result?.[0] ?? false return Boolean(call?.result?.[0])
} }
import { BigNumber } from '@ethersproject/bignumber'
import { Currency, CurrencyAmount, Token } from '@uniswap/sdk-core' import { Currency, CurrencyAmount, Token } from '@uniswap/sdk-core'
import { useSingleCallResult } from 'lib/hooks/multicall' import { useSingleCallResult } from 'lib/hooks/multicall'
import { useMemo } from 'react'
import { useTokenContract } from './useContract' import { useTokenContract } from './useContract'
...@@ -9,7 +9,10 @@ import { useTokenContract } from './useContract' ...@@ -9,7 +9,10 @@ import { useTokenContract } from './useContract'
export function useTotalSupply(token?: Currency): CurrencyAmount<Token> | undefined { export function useTotalSupply(token?: Currency): CurrencyAmount<Token> | undefined {
const contract = useTokenContract(token?.isToken ? token.address : undefined, false) const contract = useTokenContract(token?.isToken ? token.address : undefined, false)
const totalSupply: BigNumber = useSingleCallResult(contract, 'totalSupply')?.result?.[0] const totalSupplyStr: string | undefined = useSingleCallResult(contract, 'totalSupply')?.result?.[0]?.toString()
return token?.isToken && totalSupply ? CurrencyAmount.fromRawAmount(token, totalSupply.toString()) : undefined return useMemo(
() => (token?.isToken && totalSupplyStr ? CurrencyAmount.fromRawAmount(token, totalSupplyStr) : undefined),
[token, totalSupplyStr]
)
} }
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