swr#responseInterface TypeScript Examples

The following examples show how to use swr#responseInterface. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: data.ts    From hypertext with GNU General Public License v3.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useETHBalance(address?: string | null, suspense = false): responseInterface<TokenAmount, any> {
  const { chainId, library } = useWeb3React()
  const shouldFetch = typeof chainId === 'number' && typeof address === 'string' && !!library

  const result = useSWR(shouldFetch ? [chainId, address, DataType.ETHBalance] : null, getETHBalance(library), {
    suspense,
  })
  useKeepSWRDataLiveAsBlocksArrive(result.mutate)
  return result
}
Example #2
Source File: data.ts    From hypertext with GNU General Public License v3.0 6 votes vote down vote up
export function useTokenBalance(
  token?: Token,
  address?: string | null,
  suspense = false
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
): responseInterface<TokenAmount, any> {
  const contract = useContract(token?.address, IERC20.abi)

  const result = useSWR(
    typeof address === 'string' && token && contract
      ? [address, token.chainId, token.address, DataType.TokenBalance]
      : null,
    getTokenBalance(contract as Contract, token as Token),
    { suspense }
  )
  useKeepSWRDataLiveAsBlocksArrive(result.mutate)
  return result
}
Example #3
Source File: data.ts    From hypertext with GNU General Public License v3.0 6 votes vote down vote up
export function useTokenAllowance(
  token?: Token,
  owner?: string | null,
  spender?: string
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
): responseInterface<TokenAmount, any> {
  const contract = useContract(token?.address, IERC20.abi)

  const result = useSWR(
    typeof owner === 'string' && typeof spender === 'string' && token && contract
      ? [owner, spender, token.chainId, token.address, DataType.TokenAllowance]
      : null,
    getTokenAllowance(contract as Contract, token as Token)
  )
  useKeepSWRDataLiveAsBlocksArrive(result.mutate)
  return result
}
Example #4
Source File: data.ts    From hypertext with GNU General Public License v3.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useReserves(tokenA?: Token, tokenB?: Token): responseInterface<Pair | null, any> {
  const invalid = !!tokenA && !!tokenB && tokenA.equals(tokenB)
  const [token0, token1] =
    !!tokenA && !!tokenB && !invalid ? (tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]) : []
  const pairAddress = !!token0 && !!token1 ? Pair.getAddress(token0, token1) : undefined
  const contract = useContract(pairAddress, IUniswapV2Pair.abi)
  const result = useSWR(
    token0 && pairAddress && contract && token1 ? [token0.chainId, pairAddress, DataType.Reserves] : null,
    getReserves(contract as Contract, token0 as Token, token1 as Token)
  )
  useKeepSWRDataLiveAsBlocksArrive(result.mutate)
  return result
}
Example #5
Source File: data.ts    From hypertext with GNU General Public License v3.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useOnchainToken(address?: string, suspense = false): responseInterface<Token | null, any> {
  const { chainId } = useWeb3React()
  const contract = useContract(address, IERC20.abi)
  const contractBytes32 = useContract(address, ERC20_BYTES32)
  return useSWR(
    typeof chainId === 'number' && typeof address === 'string' && contract && contractBytes32
      ? [chainId, address, DataType.Token]
      : null,
    getOnchainToken(contract as Contract, contractBytes32 as Contract),
    {
      dedupingInterval: 60 * 1000,
      refreshInterval: 60 * 1000,
      suspense,
    }
  )
}
Example #6
Source File: data.ts    From hypertext with GNU General Public License v3.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useRemoteTokens(query = '', suspense = false): responseInterface<RemoteToken[], any> {
  const { chainId } = useWeb3React()
  const shouldFetch = chainId === ChainId.MAINNET && query.length > 0
  return useSWR(shouldFetch ? [query, DataType.RemoteTokens] : null, getRemoteTokens, {
    dedupingInterval: 60 * 5 * 1000,
    refreshInterval: 60 * 5 * 1000,
    suspense,
  })
}
Example #7
Source File: hooks.ts    From hypertext with GNU General Public License v3.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useKeepSWRDataLiveAsBlocksArrive(mutate: responseInterface<any, any>['mutate']): void {
  // because we don't care about the referential identity of mutate, just bind it to a ref
  const mutateRef = useRef(mutate)
  useEffect(() => {
    mutateRef.current = mutate
  })
  // then, whenever a new block arrives, trigger a mutation
  const { data } = useBlockNumber()
  useEffect(() => {
    mutateRef.current()
  }, [data])
}
Example #8
Source File: client.ts    From genql with MIT License 6 votes vote down vote up
useQuery = <R extends QueryRequest>(
    q: R,
    options = {},
): responseInterface<FieldsSelection<Query, R>, ClientError> => {
    return useSWR<any>(JSON.stringify(q), {
        fetcher: (q) => q && client.query(JSON.parse(q)),

        ...options,
    })
}
Example #9
Source File: data.ts    From hypertext with GNU General Public License v3.0 5 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useBlockNumber(): responseInterface<number, any> {
  const { library } = useWeb3React()
  const shouldFetch = !!library
  return useSWR(shouldFetch ? [DataType.BlockNumber] : null, getBlockNumber(library), {
    refreshInterval: 10 * 1000,
  })
}