Simplify SWR code

This commit is contained in:
Willian Mitsuda 2022-04-10 01:51:52 -03:00
parent e9907f5925
commit c2e9ad7bd8
1 changed files with 25 additions and 26 deletions

View File

@ -4,6 +4,7 @@ import { Contract } from "@ethersproject/contracts";
import { BigNumber } from "@ethersproject/bignumber"; import { BigNumber } from "@ethersproject/bignumber";
import AggregatorV3Interface from "@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json"; import AggregatorV3Interface from "@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json";
import FeedRegistryInterface from "@chainlink/contracts/abi/v0.8/FeedRegistryInterface.json"; import FeedRegistryInterface from "@chainlink/contracts/abi/v0.8/FeedRegistryInterface.json";
import { Fetcher } from "swr";
import useSWRImmutable from "swr/immutable"; import useSWRImmutable from "swr/immutable";
import { ChecksummedAddress } from "./types"; import { ChecksummedAddress } from "./types";
@ -13,10 +14,13 @@ const FEED_REGISTRY_MAINNET: ChecksummedAddress =
// The USD "token" address for Chainlink feed registry's purposes // The USD "token" address for Chainlink feed registry's purposes
const USD = "0x0000000000000000000000000000000000000348"; const USD = "0x0000000000000000000000000000000000000348";
type FeedRegistryFetcherKey = [ChecksummedAddress, BlockTag];
type FeedRegistryFetcherData = [BigNumber | undefined, number | undefined];
const feedRegistryFetcherKey = ( const feedRegistryFetcherKey = (
tokenAddress: ChecksummedAddress, tokenAddress: ChecksummedAddress,
blockTag: BlockTag | undefined blockTag: BlockTag | undefined
): [ChecksummedAddress, BlockTag] | null => { ): FeedRegistryFetcherKey | null => {
if (blockTag === undefined) { if (blockTag === undefined) {
return null; return null;
} }
@ -24,18 +28,17 @@ const feedRegistryFetcherKey = (
}; };
const feedRegistryFetcher = const feedRegistryFetcher =
(provider: JsonRpcProvider | undefined) => (
async ( provider: JsonRpcProvider | undefined
tokenAddress: ChecksummedAddress, ): Fetcher<FeedRegistryFetcherData, FeedRegistryFetcherKey> =>
blockTag: BlockTag async (tokenAddress, blockTag) => {
): Promise<[BigNumber | undefined, number | undefined]> => {
// It work works on ethereum mainnet and kovan, see: // It work works on ethereum mainnet and kovan, see:
// https://docs.chain.link/docs/feed-registry/ // https://docs.chain.link/docs/feed-registry/
if (!provider || provider.network.chainId !== 1) { if (provider!.network.chainId !== 1) {
return [undefined, undefined]; throw new Error("FeedRegistry is supported only on mainnet");
} }
try { // Let SWR handle error
const feedRegistry = new Contract( const feedRegistry = new Contract(
FEED_REGISTRY_MAINNET, FEED_REGISTRY_MAINNET,
FeedRegistryInterface, FeedRegistryInterface,
@ -49,10 +52,6 @@ const feedRegistryFetcher =
blockTag, blockTag,
}); });
return [quote, decimals]; return [quote, decimals];
} catch (err) {
console.error(err);
return [undefined, undefined];
}
}; };
export const useTokenUSDOracle = ( export const useTokenUSDOracle = (