"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDeploymentByAddr = exports.getContractAddrById = exports.getContractById = exports.getContractsByChain = exports.getErc20 = exports.getContract = void 0;
var ethers_1 = require("ethers");
var constants_1 = require("./constants");
var contracts_json_1 = __importDefault(require("../contracts.json"));
var typechain_1 = require("../typechain");
var vaults_1 = require("../vaults");
var strategies_1 = require("../strategies");
function getContract(address, abi, signerOrProvider) {
    return new ethers_1.Contract(address, abi, signerOrProvider);
}
exports.getContract = getContract;
var getErc20 = function (address, signerOrProvider) { return typechain_1.ERC20__factory.connect(address, signerOrProvider); };
exports.getErc20 = getErc20;
var getContractsByChain = function (chain) { var _a, _b; return (_b = (_a = contracts_json_1.default[constants_1.CHAIN_IDS[chain]]) === null || _a === void 0 ? void 0 : _a.find(function (x) { return x.name === chain; })) === null || _b === void 0 ? void 0 : _b.contracts; };
exports.getContractsByChain = getContractsByChain;
function getContractById(chain, id) {
    var _a;
    // Dangerous if more than one contract uses the same id.
    return ((_a = (0, exports.getContractsByChain)(chain)) === null || _a === void 0 ? void 0 : _a[id]) || null;
}
exports.getContractById = getContractById;
function getContractAddrById(chain, id) {
    var _a;
    return (_a = getContractById(chain, id)) === null || _a === void 0 ? void 0 : _a.address;
}
exports.getContractAddrById = getContractAddrById;
function getDeploymentByAddr(chain, address) {
    var _a;
    // Looks for any deployed contract with a given address.
    // If multiple are found, attempts to filter by known ID.
    if (!chain)
        return;
    var contracts = (0, exports.getContractsByChain)(chain);
    if (!contracts)
        return;
    var ids = (_a = Object.keys(contracts)) === null || _a === void 0 ? void 0 : _a.filter(function (k) { var _a; return ((_a = contracts[k].address) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === (address === null || address === void 0 ? void 0 : address.toLowerCase()); });
    if (!(ids === null || ids === void 0 ? void 0 : ids.length))
        return;
    if (ids.length === 1) {
        var id_1 = ids[0];
        return __assign({ id: id_1 }, contracts[id_1]);
    }
    // If more than one ID exists for this address,
    // try to match an ID to a known vault/strat/module ID
    // to avoid proxy/implementation/old contract.
    var id = ids.find(function (k) { return __spreadArray(__spreadArray([], __read(vaults_1.allVaultIds), false), __read(strategies_1.allStrategyIds), false).includes(k); });
    if (id)
        return __assign({ id: id }, contracts[id]);
    throw new Error("found multiple deployed contracts matching this address ".concat(address));
}
exports.getDeploymentByAddr = getDeploymentByAddr;
//# sourceMappingURL=contracts.js.map