var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Contract } from "@ethersproject/contracts";
import { getNetwork } from "@ethersproject/networks";
import { getDefaultProvider } from "@ethersproject/providers";
import { TokenAmount, ChainId, Token } from "../../libs/uniswap";
import IUniswapV2Pair from "@uniswap/v2-core/build/IUniswapV2Pair.json";
import invariant from "tiny-invariant";
import { Pair } from "./uniswapPair";
import ERC20 from "./uniswapErc20.json";
let TOKEN_DECIMALS_CACHE = {
    [ChainId.MAINNET]: {},
};
/**
 * Contains methods for constructing instances of pairs and tokens from on-chain data.
 */
export class Fetcher {
    /**
     * Fetch information for a given token on the given chain, using the given ethers provider.
     * @param chainId chain of the token
     * @param address address of the token on the chain
     * @param provider provider used to fetch the token
     * @param symbol optional symbol of the token
     * @param name optional name of the token
     */
    static fetchTokenData(chainId, address, provider = getDefaultProvider(getNetwork(chainId)), symbol, name) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const parsedDecimals = typeof ((_a = TOKEN_DECIMALS_CACHE === null || TOKEN_DECIMALS_CACHE === void 0 ? void 0 : TOKEN_DECIMALS_CACHE[chainId]) === null || _a === void 0 ? void 0 : _a[address]) === "number"
                ? TOKEN_DECIMALS_CACHE[chainId][address]
                : yield new Contract(address, ERC20, provider).decimals().then((decimals) => {
                    TOKEN_DECIMALS_CACHE = Object.assign(Object.assign({}, TOKEN_DECIMALS_CACHE), { [chainId]: Object.assign(Object.assign({}, TOKEN_DECIMALS_CACHE === null || TOKEN_DECIMALS_CACHE === void 0 ? void 0 : TOKEN_DECIMALS_CACHE[chainId]), { [address]: decimals }) });
                    return decimals;
                });
            return new Token(chainId, address, parsedDecimals, symbol, name);
        });
    }
    /**
     * Fetches information about a pair and constructs a pair from the given two tokens.
     * @param tokenA first token
     * @param tokenB second token
     * @param provider the provider to use to fetch the data
     */
    static fetchPairData(tokenA, tokenB, provider) {
        return __awaiter(this, void 0, void 0, function* () {
            invariant(tokenA.chainId === tokenB.chainId, "CHAIN_ID");
            const address = Pair.getAddress(tokenA, tokenB);
            const [reserves0, reserves1] = yield new Contract(address, IUniswapV2Pair.abi, provider).getReserves();
            const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0];
            return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]));
        });
    }
}
