"use strict";
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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAmountsOut = exports.getRouterContract = exports.shortenAddress = exports.isAddress = exports.UNISWAP_ROUTER = exports.UNISWAP_FACTORY = exports.AAVE_DATA = exports.WETH = exports.AAVE = exports.USDT = exports.USDC = exports.DAI = exports.MATIC = exports.BASE = exports.getAddr = exports.getUniAddr = exports.getGelatoAddrs = exports.defaultDex = exports.getDexFromRouter = exports.getSymbol = exports.isHiddenVault = exports.isInBeta = exports.isDeprecatedVault = exports.SECT_ADDRESS = exports.STABLES = exports.getAddress = void 0;
var constants_1 = require("./constants");
var ethers_1 = require("ethers");
var abis_1 = require("../abis");
exports.getAddress = ethers_1.utils.getAddress;
exports.STABLES = ['USDC', 'DAI', 'USDT'];
exports.SECT_ADDRESS = '0x0B63C61bba4a876a6eB8b5e596800F7649A9B71E';
var deprecatedVaults = {
    fantom: ['0x3938411fd77A5458721aF6B080b51008394568ef'],
    mainnet: ['0x814d8A046049b25BEd644d0534c9db050Fc33456'],
};
var betaVaults = {
    arbitrum: [
        '0x09e677692a17dA303A868D46C53aC53B1901D90E',
        '0x4c457498fde2E44582cd669f5ad1035bD23Ad376',
        '0x22914d115E5E9B9a88C4101a1f2E1090c2fA7913',
        '0xA6F224428a168a0F6eCFCB070D6a36e2291a6719',
        '0x8f878DE65E681bb701f014a4fbF77418c225f4Cb',
        '0xBA23f815E15242C366d78dA15A0355C242048F86',
        // ETH Vault
        // '0x6dC873656fCde76dFAe75146D9B2B4b6697a0594',
        // Newly deployed Synapse ETH arbitrum
        // '0x5FEcb965C834101FD26d6D7D392dA69746b87585',
    ],
    optimism: [
    // New Stargate USDC optimism
    // '0x4202b73de0e3893F6cB183a085b15AdA2Ed11B91',
    ],
};
var hiddenVaults = {
    // shown in development, hidden elsewhere
    localhost: ['0x6a800925aD01b44afd45f4Ac11e32ba9a49D5841'],
    avalanche: [
        '0x9A17EB664a80175c389Cb11F93ce4D7A007f884b',
        '0x64334fdf94AD45a7FB34038D4fD4650D9aF88AF5',
    ],
};
function isDeprecatedVault(chain, addr) {
    var _a, _b;
    if (!(addr === null || addr === void 0 ? void 0 : addr.length) || !(chain === null || chain === void 0 ? void 0 : chain.length))
        return;
    return !!((_b = (_a = deprecatedVaults[chain]) === null || _a === void 0 ? void 0 : _a.map(function (x) { return x.toLowerCase(); })) === null || _b === void 0 ? void 0 : _b.includes(addr.toLowerCase()));
}
exports.isDeprecatedVault = isDeprecatedVault;
function isInBeta(chain, addr) {
    var _a, _b;
    if (!(addr === null || addr === void 0 ? void 0 : addr.length) || !(chain === null || chain === void 0 ? void 0 : chain.length))
        return;
    return !!((_b = (_a = betaVaults[chain]) === null || _a === void 0 ? void 0 : _a.map(function (x) { return x.toLowerCase(); })) === null || _b === void 0 ? void 0 : _b.includes(addr.toLowerCase()));
}
exports.isInBeta = isInBeta;
function isHiddenVault(chain, addr) {
    var _a, _b;
    if (!(addr === null || addr === void 0 ? void 0 : addr.length) || !(chain === null || chain === void 0 ? void 0 : chain.length))
        return true;
    if (constants_1.NODE_ENV === 'development')
        return false;
    return (_b = (_a = hiddenVaults[chain]) === null || _a === void 0 ? void 0 : _a.map(function (x) { return x.toLowerCase(); })) === null || _b === void 0 ? void 0 : _b.includes(addr.toLowerCase());
}
exports.isHiddenVault = isHiddenVault;
// TODO: this should probably live in db
var addrs = {
    BASE: {
        mainnet: (0, exports.getAddress)('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
        avalanche: (0, exports.getAddress)('0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7'),
        fantom: (0, exports.getAddress)('0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83'),
        moonriver: (0, exports.getAddress)('0x98878B06940aE243284CA214f92Bb71a2b032B8A'),
        moonbeam: (0, exports.getAddress)('0xAcc15dC74880C9944775448304B263D191c6077F'),
        optimism: (0, exports.getAddress)('0x4200000000000000000000000000000000000006'),
        arbitrum: (0, exports.getAddress)('0x82aF49447D8a07e3bd95BD0d56f35241523fBab1'),
        // ropsten: getAddress('0xc778417E063141139Fce010982780140Aa0cD5Ab'),
        // bsc: getAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'),
        // polygon: getAddress('0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270'),
    },
    ETH: {
        mainnet: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
        optimism: (0, exports.getAddress)('0x4200000000000000000000000000000000000006'),
        arbitrum: (0, exports.getAddress)('0x82aF49447D8a07e3bd95BD0d56f35241523fBab1'),
    },
    MATIC: {
    // polygon: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
    },
    DAI: {
        mainnet: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
        // rinkeby: '0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735',
        // ropsten: '0xaD6D458402F60fD3Bd25163575031ACDce07538D',
        // bsc: '0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3',
        // polygon: '0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063',
    },
    USDC: {
        avalanche: '0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664',
        mainnet: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
        fantom: '0x04068da6c83afcfa0e13ba15a6696662335d5b75',
        moonriver: '0xE3F5a90F9cb311505cd691a46596599aA1A0AD7D',
        moonbeam: '0x8f552a71EFE5eeFc207Bf75485b356A0b3f01eC9',
        optimism: '0x7F5c764cBc14f9669B88837ca1490cCa17c31607',
        arbitrum: '0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8',
        // bsc: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',
        // polygon: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
    },
    USDT: {
        mainnet: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
        // bsc: '0x55d398326f99059fF775485246999027B3197955',
        // polygon: '0xc2132D05D31c914a87C6611C10748AEb04B58e8F',
    },
};
var getSymbol = function (chain, addr) {
    var symbols = Object.keys(addrs).filter(function (s) { return s !== 'BASE'; });
    for (var i = 0; i < symbols.length; i++)
        if (addrs[symbols[i]][chain] === (0, exports.getAddress)(addr))
            return symbols[i];
    throw new Error("Asset ".concat(addr, " on ").concat(chain, " not found in address.ts"));
};
exports.getSymbol = getSymbol;
var uniAddrs = {
    UNISWAP_FACTORY: {
        null: ethers_1.constants.AddressZero,
        pancake: '0xBCfCcbde45cE874adCB698cC183deBcF17952812',
        quickswap: '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32',
        uniswap: '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f',
        pangolin: '0xefa94DE7a4656D787667C749f7E1223D71E9FD88',
        spirit: '0xEF45d134b73241eDa7703fa787148D9C9F4950b0',
        spooky: '0x152eE697f2E276fA89E96742e9bB9aB1F2E61bE3',
        traderJoe: '0x9Ad6C38BE94206cA50bb0d90783181662f0Cfa10',
        huckleberry: '0x017603C8f29F7f6394737628a93c57ffBA1b7256',
        solarbeam: '0x049581aEB6Fe262727f290165C29BDAB065a1B68',
        solidly: '0x3fAaB499b519fdC5819e3D7ed0C26111904cbc28',
        stellaswap: '0x68A384D826D3678f78BB9FB1533c7E9577dACc0E',
        velo: '0x25CbdDb98b35ab1FF77413456B31EC81A6B6B746',
    },
    UNISWAP_ROUTER: {
        null: ethers_1.constants.AddressZero,
        pancake: '0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F',
        quickswap: '0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff',
        uniswap: '0x7a250d5630b4cf539739df2c5dacb4c659f2488d',
        pangolin: '0xE54Ca86531e17Ef3616d22Ca28b0D458b6C89106',
        spirit: '0x16327E3FbDaCA3bcF7E38F5Af2599D2DDc33aE52',
        traderJoe: '0x60aE616a2155Ee3d9A68541Ba4544862310933d4',
        spooky: '0xF491e7B69E4244ad4002BC14e878a34207E38c29',
        huckleberry: '0x2d4e873f9Ab279da9f1bb2c532d4F06f67755b77',
        solarbeam: '0xAA30eF758139ae4a7f798112902Bf6d65612045f',
        solidly: '0xa38cd27185a464914D3046f0AB9d43356B34829D',
        stellaswap: '0x70085a09D30D6f8C4ecF6eE10120d1847383BB57',
        velo: '0xa132DAB612dB5cB9fC9Ac426A0Cc215A3423F9c9',
    },
};
var getDexFromRouter = function (addr) {
    if (!addr)
        return undefined;
    var dexes = Object.keys(uniAddrs.UNISWAP_ROUTER);
    var swap = Object.keys(uniAddrs.UNISWAP_ROUTER).find(function (dex) { return uniAddrs.UNISWAP_ROUTER[dex].toLowerCase() === (addr === null || addr === void 0 ? void 0 : addr.toLowerCase()); });
    if (!swap)
        console.warn('Dex not found in address.ts');
    return swap;
};
exports.getDexFromRouter = getDexFromRouter;
exports.defaultDex = {
    mainnet: 'uniswap',
    avalanche: 'pangolin',
    moonbeam: 'stellaswap',
    // bsc: 'pancake',
    // polygon: 'quickswap',
};
var gelatoContractAddrs = {
    fantom: '0x6c3224f9b3feE000A444681d5D45e4532D5BA531',
    avalanche: '0x63C51b1D80B209Cf336Bec5a3E17D3523B088cdb',
    moonriver: '0xa5f9b728ecEB9A1F6FCC89dcc2eFd810bA4Dec41',
    moonbeam: '0xa5f9b728ecEB9A1F6FCC89dcc2eFd810bA4Dec41',
    optimism: false,
    mainnet: false,
    arbitrum: false,
};
var getGelatoAddrs = function (chain) {
    return gelatoContractAddrs[chain];
};
exports.getGelatoAddrs = getGelatoAddrs;
var getUniAddr = function (contract, dexName) {
    return uniAddrs[contract][dexName];
};
exports.getUniAddr = getUniAddr;
var getAddr = function (asset, chain) {
    var _a;
    if (constants_1.UniContracts.includes(asset)) {
        var swap = exports.defaultDex[chain];
        if (!swap)
            throw new Error("Swap record doesn't exist");
        return (0, exports.getUniAddr)(asset, swap);
    }
    var address = (_a = addrs[asset]) === null || _a === void 0 ? void 0 : _a[chain];
    if (!address)
        throw new Error("".concat(asset, " address not found for the ").concat(chain, " network"));
    return address;
};
exports.getAddr = getAddr;
// erc20 wrapped base assset of a chain, ex: WETH, WMATIC, WAVAX
var BASE = function (chain) { return (0, exports.getAddr)('BASE', chain); };
exports.BASE = BASE;
var MATIC = function (chain) { return (0, exports.getAddr)('MATIC', chain); };
exports.MATIC = MATIC;
var DAI = function (chain) { return (0, exports.getAddr)('DAI', chain); };
exports.DAI = DAI;
var USDC = function (chain) { return (0, exports.getAddr)('USDC', chain); };
exports.USDC = USDC;
var USDT = function (chain) { return (0, exports.getAddr)('USDT', chain); };
exports.USDT = USDT;
var AAVE = function (chain) { return (0, exports.getAddr)('AAVE', chain); };
exports.AAVE = AAVE;
var WETH = function (chain) { return (0, exports.getAddr)('ETH', chain); };
exports.WETH = WETH;
var AAVE_DATA = function (chain) { return (0, exports.getAddr)('AAVE_DATA', chain); };
exports.AAVE_DATA = AAVE_DATA;
var UNISWAP_FACTORY = function (chain) {
    return (0, exports.getAddr)('UNISWAP_FACTORY', chain);
};
exports.UNISWAP_FACTORY = UNISWAP_FACTORY;
var UNISWAP_ROUTER = function (chain) {
    return (0, exports.getAddr)('UNISWAP_ROUTER', chain);
};
exports.UNISWAP_ROUTER = UNISWAP_ROUTER;
// returns the checksummed address if the address is valid, otherwise returns false
function isAddress(value) {
    try {
        return ethers_1.utils.getAddress(value);
    }
    catch (_a) {
        return false;
    }
}
exports.isAddress = isAddress;
// shorten the checksummed version of the input address to have 0x + 4 characters at start and end
function shortenAddress(address, chars) {
    if (chars === void 0) { chars = 4; }
    var parsed = isAddress(address);
    if (!parsed) {
        throw Error("Invalid 'address' parameter '".concat(address, "'."));
    }
    return "".concat(parsed.substring(0, chars + 2), "...").concat(parsed.substring(42 - chars));
}
exports.shortenAddress = shortenAddress;
var getRouterContract = function (swap, signer) {
    switch (swap) {
        case 'pangolin':
            return new ethers_1.Contract((0, exports.getUniAddr)('UNISWAP_ROUTER', 'pangolin'), abis_1.pangolinRouterAbi, signer);
            break;
        case 'traderJoe':
            return new ethers_1.Contract((0, exports.getUniAddr)('UNISWAP_ROUTER', 'traderJoe'), abis_1.traderJoeRouterAbi, signer);
            break;
        case 'solarbeam':
            return new ethers_1.Contract((0, exports.getUniAddr)('UNISWAP_ROUTER', 'solarbeam'), abis_1.solarbeamRouterAbi, signer);
            break;
        case 'solidly':
            return new ethers_1.Contract((0, exports.getUniAddr)('UNISWAP_ROUTER', 'solidly'), abis_1.solidlyRouterAbi, signer);
        default:
            return new ethers_1.Contract((0, exports.getUniAddr)('UNISWAP_ROUTER', swap), abis_1.uniswapRouterAbi, signer);
    }
};
exports.getRouterContract = getRouterContract;
var getAmountsOut = function (amount, path, swap, signer) { return __awaiter(void 0, void 0, void 0, function () {
    var swapRouter, fee;
    return __generator(this, function (_a) {
        swapRouter = (0, exports.getRouterContract)(swap, signer);
        switch (swap) {
            case 'solarbeam': {
                fee = '25';
                return [2 /*return*/, swapRouter.getAmountsOut(amount, path, fee)];
            }
            default: {
                return [2 /*return*/, swapRouter.getAmountsOut(amount, path)];
            }
        }
        return [2 /*return*/];
    });
}); };
exports.getAmountsOut = getAmountsOut;
//# sourceMappingURL=address.js.map