fix: memoize on-chain results (#3493)

* fix: memo-ize onchain results

* fix: typeof omission
This commit is contained in:
Zach Pomerantz 2022-03-11 14:56:05 -08:00 committed by GitHub
parent 496408b3db
commit 0edb0fe5e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 16 deletions

@ -1,20 +1,15 @@
import { BigNumber } from '@ethersproject/bignumber'
import { useSingleCallResult } from 'lib/hooks/multicall'
import { useEffect, useState } from 'react'
import { useMemo } from 'react'
import { useInterfaceMulticall } from './useContract'
// gets the current timestamp from the blockchain
export default function useCurrentBlockTimestamp(): BigNumber | undefined {
const [lastBlock, setLastBlock] = useState<BigNumber | undefined>()
const multicall = useInterfaceMulticall()
const block: BigNumber | undefined = useSingleCallResult(multicall, 'getCurrentBlockTimestamp')?.result?.[0]
useEffect(() => {
// If block xor lastBlock are undefined, or if block has progressed, then update lastBlock.
// This prevents updates when the block doesn't change, because the returned BigNumber will still be referentially unique.
if (Boolean(block) !== Boolean(lastBlock) || (block && lastBlock && !block.eq(lastBlock))) {
setLastBlock(block)
}
}, [block, lastBlock])
return lastBlock
const resultStr: string | undefined = useSingleCallResult(
multicall,
'getCurrentBlockTimestamp'
)?.result?.[0]?.toString()
return useMemo(() => (typeof resultStr === 'string' ? BigNumber.from(resultStr) : undefined), [resultStr])
}

@ -1,5 +1,6 @@
import JSBI from 'jsbi'
import { useSingleCallResult } from 'lib/hooks/multicall'
import { useMemo } from 'react'
import { useContract } from './useContract'
import useENSAddress from './useENSAddress'
@ -22,5 +23,5 @@ export default function useGasPrice(): JSBI | undefined {
const contract = useContract(address ?? undefined, CHAIN_DATA_ABI, false)
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 {
const argentWalletDetector = useArgentWalletDetectorContract()
const inputs = useMemo(() => [account ?? undefined], [account])
const call = useSingleCallResult(argentWalletDetector, 'isArgentWallet', inputs, NEVER_RELOAD)
return call?.result?.[0] ?? false
return Boolean(call?.result?.[0])
}

@ -1,6 +1,6 @@
import { BigNumber } from '@ethersproject/bignumber'
import { Currency, CurrencyAmount, Token } from '@uniswap/sdk-core'
import { useSingleCallResult } from 'lib/hooks/multicall'
import { useMemo } from 'react'
import { useTokenContract } from './useContract'
@ -9,7 +9,10 @@ import { useTokenContract } from './useContract'
export function useTotalSupply(token?: Currency): CurrencyAmount<Token> | undefined {
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]
)
}