"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEther = exports.getDecimalsOfUnderly = exports.getDecimals = exports.isUnderlyAllowed = exports.isCTokenAllowed = exports.getTokenPairBySymbol = exports.getTokenPairByAddress = exports.getName = exports.getAddress = exports.getRateModals = exports.getTokenPairsByGroupId = exports.getNetworkBlocksPerYear = exports.getTokenPairs = exports.getNetworkConfig = exports.constants = void 0;
const arbitrum_1 = require("./arbitrum");
const basemain_1 = require("./basemain");
const basesepolia_1 = require("./basesepolia");
const ethereum_1 = require("./ethereum");
const goerli_1 = require("./goerli");
const metermain_1 = require("./metermain");
const metertest_1 = require("./metertest");
const sepolia_1 = require("./sepolia");
const zklink_1 = require("./zklink");
const berabartio_1 = require("./berabartio");
const bsc_1 = require("./bsc");
const b2main_1 = require("./b2main");
const merlin_1 = require("./merlin");
const core_1 = require("./core");
const monadtest_1 = require("./monadtest");
const beramain_1 = require("./beramain");
const hemi_1 = require("./hemi");
const arbitrumsepolia_1 = require("./arbitrumsepolia");
const bitlayer_1 = require("./bitlayer");
const goat_1 = require("./goat");
const monadmain_1 = require("./monadmain");
const NetworkConfigs = {
    b2main: b2main_1.b2main,
    bsc: bsc_1.bsc,
    ethereum: ethereum_1.ethereum,
    arbitrum: arbitrum_1.arbitrum,
    arbitrumsepolia: arbitrumsepolia_1.arbitrumsepolia,
    basemain: basemain_1.basemain,
    basesepolia: basesepolia_1.basesepolia,
    goerli: goerli_1.goerli,
    metermain: metermain_1.metermain,
    metertest: metertest_1.metertest,
    sepolia: sepolia_1.sepolia,
    zklink: zklink_1.zklink,
    berabartio: berabartio_1.berabartio,
    merlin: merlin_1.merlin,
    core: core_1.core,
    monadtest: monadtest_1.monadtest,
    beramain: beramain_1.beramain,
    hemi: hemi_1.hemi,
    bitlayer: bitlayer_1.bitlayer,
    goat: goat_1.goat,
    monadmain: monadmain_1.monadmain
};
let allTokenSymbols = {};
for (const netName in NetworkConfigs) {
    for (const pair of NetworkConfigs[netName].tokenPairs) {
        allTokenSymbols[pair.ctokenSym] = pair.ctokenSym;
        allTokenSymbols[pair.underlySym] = pair.underlySym;
    }
}
// Publicly revealed on the parent class
exports.constants = Object.assign({ CompoundLens: 'CompoundLens', GovernorBravo: 'GovernorBravo', Comptroller: 'Comptroller', PriceOracle: 'PriceOracle', WhitepaperInterestRate: 'WhitepaperInterestRate', Timelock: 'Timelock' }, allTokenSymbols);
const getNetworkConfig = (networkName) => {
    if (networkName in NetworkConfigs) {
        return NetworkConfigs[networkName];
    }
    throw new Error(`could not find config ${networkName}.json`);
};
exports.getNetworkConfig = getNetworkConfig;
const getTokenPairs = (networkName) => {
    const config = (0, exports.getNetworkConfig)(networkName);
    if (!config.tokenPairs) {
        throw new Error('tokenPairs is not configured in config file');
    }
    return config.tokenPairs;
};
exports.getTokenPairs = getTokenPairs;
const getNetworkBlocksPerYear = (networkName) => {
    const config = (0, exports.getNetworkConfig)(networkName);
    console.info('blocksPerYear', config.blocksPerYear);
    return config.blocksPerYear;
};
exports.getNetworkBlocksPerYear = getNetworkBlocksPerYear;
const getTokenPairsByGroupId = (networkName, assetGroupId) => {
    const config = (0, exports.getNetworkConfig)(networkName);
    if (!config.tokenPairs) {
        throw new Error('tokenPairs is not configured in config file');
    }
    return config.tokenPairs.filter((p) => p.assetGroupId == assetGroupId);
};
exports.getTokenPairsByGroupId = getTokenPairsByGroupId;
const getRateModals = (networkName) => {
    const config = (0, exports.getNetworkConfig)(networkName);
    if (!config.rateModels) {
        throw new Error('rateModels is not configured in config file');
    }
    return config.rateModels;
};
exports.getRateModals = getRateModals;
const getAddress = (networkName, tokenSym) => {
    const config = (0, exports.getNetworkConfig)(networkName);
    if (!config.addresses) {
        throw Error('addresses is not configured in config file');
    }
    if (!(tokenSym in config.addresses)) {
        throw Error(`${tokenSym} address not configued on ${networkName}`);
    }
    return config.addresses[tokenSym];
};
exports.getAddress = getAddress;
const getName = (networkName, tokenAddress) => {
    const config = (0, exports.getNetworkConfig)(networkName);
    const knownNames = {};
    if (!config.addresses) {
        throw new Error(`addresses not configued in config file`);
    }
    for (const n in config.addresses) {
        knownNames[config.addresses[n].toLowerCase()] = n;
    }
    if (!(tokenAddress.toLowerCase() in knownNames)) {
        throw Error(`${tokenAddress} name not configued on ${networkName}`);
    }
    return knownNames[tokenAddress.toLowerCase()];
};
exports.getName = getName;
const getTokenPairByAddress = (networkName, tokenAddress) => {
    const pairs = (0, exports.getTokenPairs)(networkName);
    const filtered = pairs.filter((p) => p.ctokenAddr.toLowerCase() == tokenAddress.toLowerCase() ||
        p.underlyAddr.toLowerCase() == tokenAddress.toLowerCase());
    return filtered.length > 0 ? filtered[0] : undefined;
};
exports.getTokenPairByAddress = getTokenPairByAddress;
const getTokenPairBySymbol = (networkName, symbol) => {
    const pairs = (0, exports.getTokenPairs)(networkName);
    const filtered = pairs.filter((p) => p.ctokenSym == symbol || p.underlySym == symbol);
    return filtered.length > 0 ? filtered[0] : undefined;
};
exports.getTokenPairBySymbol = getTokenPairBySymbol;
const isCTokenAllowed = (networkName, cToken) => {
    const pairs = (0, exports.getTokenPairs)(networkName);
    const allowed = pairs.some((p) => p.ctokenSym === cToken);
    if (!allowed) {
        throw Error(`cToken ${cToken} not allowed on ${networkName}`);
    }
    return true;
};
exports.isCTokenAllowed = isCTokenAllowed;
const isUnderlyAllowed = (networkName, underlyName) => {
    const pairs = (0, exports.getTokenPairs)(networkName);
    const allowed = pairs.some((p) => p.underlySym === underlyName);
    if (!allowed) {
        throw Error(`underly ${underlyName} not allowed on ${networkName}`);
    }
    return true;
};
exports.isUnderlyAllowed = isUnderlyAllowed;
const getDecimals = (networkName, tokenSymOrAddr) => {
    const pairs = (0, exports.getTokenPairs)(networkName);
    for (const p of pairs) {
        if (p.ctokenSym === tokenSymOrAddr) {
            return p.ctokenDecimal;
        }
        if (p.underlySym === tokenSymOrAddr) {
            return p.underlyDecimal;
        }
        if (p.underlyAddr.toLowerCase() === tokenSymOrAddr.toLowerCase()) {
            return p.underlyDecimal;
        }
        if (p.ctokenAddr.toLowerCase() === tokenSymOrAddr.toLowerCase()) {
            return p.ctokenDecimal;
        }
    }
    throw Error(`decimal not configured for ${tokenSymOrAddr} on ${networkName}`);
};
exports.getDecimals = getDecimals;
const getDecimalsOfUnderly = (networkName, tokenSymOrAddr) => {
    const pairs = (0, exports.getTokenPairs)(networkName);
    for (const p of pairs) {
        if (p.ctokenSym === tokenSymOrAddr) {
            return p.underlyDecimal;
        }
        if (p.underlySym === tokenSymOrAddr) {
            return p.underlyDecimal;
        }
        if (p.ctokenAddr.toLowerCase() === tokenSymOrAddr.toLowerCase()) {
            return p.underlyDecimal;
        }
        if (p.underlyAddr.toLowerCase() === tokenSymOrAddr.toLowerCase()) {
            return p.underlyDecimal;
        }
    }
    throw Error(`decimal not configured for ${tokenSymOrAddr} on ${networkName}`);
};
exports.getDecimalsOfUnderly = getDecimalsOfUnderly;
const isEther = (networkName, ctokenSym) => {
    const pairs = (0, exports.getTokenPairs)(networkName);
    return pairs.some((p) => p.ctokenSym === ctokenSym && p.ctokenIsEther);
};
exports.isEther = isEther;
// TODO: Show a specific error code when a transaction fails
// export const errorCodes = {
//   'comptroller': {
//     'codes': {
//       '0': {  'error': 'NO_ERROR',                      'description': 'Not a failure.', 'hint': '', },
//       '1': {  'error': 'UNAUTHORIZED',                  'description': 'The sender is not authorized to perform this action.', 'hint': '', },
//       '2': {  'error': 'COMPTROLLER_MISMATCH',          'description': 'Liquidation cannot be performed in markets with different comptrollers.', 'hint': '', },
//       '3': {  'error': 'INSUFFICIENT_SHORTFALL',        'description': 'The account does not have sufficient shortfall to perform this action.', 'hint': '', },
//       '4': {  'error': 'INSUFFICIENT_LIQUIDITY',        'description': 'The account does not have sufficient liquidity to perform this action.', 'hint': '', },
//       '5': {  'error': 'INVALID_CLOSE_FACTOR',          'description': 'The close factor is not valid.', 'hint': '', },
//       '6': {  'error': 'INVALID_COLLATERAL_FACTOR',     'description': 'The collateral factor is not valid.', 'hint': '', },
//       '7': {  'error': 'INVALID_LIQUIDATION_INCENTIVE', 'description': 'The liquidation incentive is invalid.', 'hint': '', },
//       '8': {  'error': 'MARKET_NOT_ENTERED',            'description': 'The market has not been entered by the account.', 'hint': '', },
//       '9': {  'error': 'MARKET_NOT_LISTED',             'description': 'The market is not currently listed by the comptroller.', 'hint': '', },
//       '10': { 'error': 'MARKET_ALREADY_LISTED',         'description': 'An admin tried to list the same market more than once.', 'hint': '', },
//       '11': { 'error': 'MATH_ERROR',                    'description': 'A math calculation error occurred.', 'hint': '', },
//       '12': { 'error': 'NONZERO_BORROW_BALANCE',        'description': 'The action cannot be performed since the account carries a borrow balance.', 'hint': '', },
//       '13': { 'error': 'PRICE_ERROR',                   'description': 'The comptroller could not obtain a required price of an asset.', 'hint': '', },
//       '14': { 'error': 'REJECTION',                     'description': 'The comptroller rejects the action requested by the market.', 'hint': '', },
//       '15': { 'error': 'SNAPSHOT_ERROR',                'description': 'The comptroller could not get the account borrows and exchange rate from the market.', 'hint': '', },
//       '16': { 'error': 'TOO_MANY_ASSETS',               'description': 'Attempted to enter more markets than are currently supported.', 'hint': '', },
//       '17': { 'error': 'TOO_MUCH_REPAY',                'description': 'Attempted to repay more than is allowed by the protocol.', 'hint': '', },
//     },
//     'info':  {
//       '0': {  'error': 'ACCEPT_ADMIN_PENDING_ADMIN_CHECK',            'description': '', 'hint': '', },
//       '1': {  'error': 'ACCEPT_PENDING_IMPLEMENTATION_ADDRESS_CHECK', 'description': '', 'hint': '', },
//       '2': {  'error': 'EXIT_MARKET_BALANCE_OWED',                    'description': '', 'hint': '', },
//       '3': {  'error': 'EXIT_MARKET_REJECTION',                       'description': '', 'hint': '', },
//       '4': {  'error': 'SET_CLOSE_FACTOR_OWNER_CHECK',                'description': '', 'hint': '', },
//       '5': {  'error': 'SET_CLOSE_FACTOR_VALIDATION',                 'description': '', 'hint': '', },
//       '6': {  'error': 'SET_COLLATERAL_FACTOR_OWNER_CHECK',           'description': '', 'hint': '', },
//       '7': {  'error': 'SET_COLLATERAL_FACTOR_NO_EXISTS',             'description': '', 'hint': '', },
//       '8': {  'error': 'SET_COLLATERAL_FACTOR_VALIDATION',            'description': '', 'hint': '', },
//       '9': {  'error': 'SET_COLLATERAL_FACTOR_WITHOUT_PRICE',         'description': '', 'hint': '', },
//       '10': { 'error': 'SET_IMPLEMENTATION_OWNER_CHECK',              'description': '', 'hint': '', },
//       '11': { 'error': 'SET_LIQUIDATION_INCENTIVE_OWNER_CHECK',       'description': '', 'hint': '', },
//       '12': { 'error': 'SET_LIQUIDATION_INCENTIVE_VALIDATION',        'description': '', 'hint': '', },
//       '13': { 'error': 'SET_MAX_ASSETS_OWNER_CHECK',                  'description': '', 'hint': '', },
//       '14': { 'error': 'SET_PENDING_ADMIN_OWNER_CHECK',               'description': '', 'hint': '', },
//       '15': { 'error': 'SET_PENDING_IMPLEMENTATION_OWNER_CHECK',      'description': '', 'hint': '', },
//       '16': { 'error': 'SET_PRICE_ORACLE_OWNER_CHECK',                'description': '', 'hint': '', },
//       '17': { 'error': 'SUPPORT_MARKET_EXISTS',                       'description': '', 'hint': '', },
//       '18': { 'error': 'SUPPORT_MARKET_OWNER_CHECK',                  'description': '', 'hint': '', },
//     }
//   }
// };
//# sourceMappingURL=index.js.map