"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContractFactory = getContractFactory;
var tslib_1 = require("tslib");
require("@nomiclabs/hardhat-ethers");
var fs_1 = tslib_1.__importDefault(require("fs"));
var path_1 = tslib_1.__importDefault(require("path"));
var hardhat_1 = require("hardhat");
var ethers_1 = require("ethers");
var contracts_node_1 = require("@uma/contracts-node");
function isFactoryOptions(signerOrFactoryOptions) {
    return (typeof signerOrFactoryOptions === "object" &&
        ("signer" in signerOrFactoryOptions || "libraries" in signerOrFactoryOptions));
}
function getAllFilesInPath(dirPath, arrayOfFiles) {
    if (arrayOfFiles === void 0) { arrayOfFiles = []; }
    var files = fs_1.default.readdirSync(dirPath);
    files.forEach(function (file) {
        if (fs_1.default.statSync(dirPath + "/" + file).isDirectory()) {
            arrayOfFiles = getAllFilesInPath(dirPath + "/" + file, arrayOfFiles);
        }
        else {
            arrayOfFiles.push(path_1.default.join(dirPath, "/", file));
        }
    });
    return arrayOfFiles;
}
function isFoundryArtifact(artifact) {
    return typeof artifact.bytecode === "object" && "object" in artifact.bytecode;
}
function getBytecodeFromArtifact(artifact) {
    if (isFoundryArtifact(artifact)) {
        return artifact.bytecode.object;
    }
    return artifact.bytecode;
}
function findArtifactFromPath(contractName, artifactsPath) {
    var allArtifactsPaths = getAllFilesInPath(artifactsPath);
    var desiredArtifactPaths = allArtifactsPaths.filter(function (a) { return a.endsWith("/".concat(contractName, ".json")); });
    if (desiredArtifactPaths.length !== 1) {
        throw new Error("Couldn't find desired artifact or found too many for ".concat(contractName));
    }
    var artifact = JSON.parse(fs_1.default.readFileSync(desiredArtifactPaths[0], "utf-8"));
    return {
        abi: artifact.abi,
        bytecode: getBytecodeFromArtifact(artifact),
    };
}
function getAcrossContractsArtifact(contractName) {
    var contractsPackagePath = path_1.default.dirname(require.resolve("@across-protocol/contracts/package.json"));
    var artifactsPath = path_1.default.join(contractsPackagePath, "out");
    return findArtifactFromPath(contractName, artifactsPath);
}
function getContractFactory(name, signerOrFactoryOptions) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var signer, _1, artifact;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    signer = isFactoryOptions(signerOrFactoryOptions) ? signerOrFactoryOptions.signer : signerOrFactoryOptions;
                    _a.label = 1;
                case 1:
                    _a.trys.push([1, 3, , 4]);
                    return [4, hardhat_1.ethers.getContractFactory(name, signerOrFactoryOptions)];
                case 2: return [2, _a.sent()];
                case 3:
                    _1 = _a.sent();
                    return [3, 4];
                case 4:
                    try {
                        artifact = getAcrossContractsArtifact(name);
                        return [2, new ethers_1.ContractFactory(artifact.abi, artifact.bytecode, signer)];
                    }
                    catch (_) {
                    }
                    try {
                        if (isFactoryOptions(signerOrFactoryOptions)) {
                            throw new Error("Cannot pass FactoryOptions to a contract imported from UMA");
                        }
                        return [2, new ethers_1.ContractFactory((0, contracts_node_1.getAbi)(name), (0, contracts_node_1.getBytecode)(name), signerOrFactoryOptions)];
                    }
                    catch (_) {
                    }
                    throw new Error("Could not find the artifact for ".concat(name, "!"));
            }
        });
    });
}
//# sourceMappingURL=getContractFactory.js.map