"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContractFactory = getContractFactory;
const tslib_1 = require("tslib");
require("@nomiclabs/hardhat-ethers");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const hardhat_1 = require("hardhat");
const ethers_1 = require("ethers");
const contracts_node_1 = require("@uma/contracts-node");
function isFactoryOptions(signerOrFactoryOptions) {
    return (typeof signerOrFactoryOptions === "object" &&
        ("signer" in signerOrFactoryOptions || "libraries" in signerOrFactoryOptions));
}
function getAcrossContractsArtifact(contractName) {
    const contractsPackagePath = path_1.default.dirname(require.resolve("@across-protocol/contracts/package.json"));
    const abiDir = path_1.default.join(contractsPackagePath, "dist", "evm", "artifacts");
    const artifactPath = path_1.default.join(abiDir, `${contractName}.sol`, `${contractName}.json`);
    const artifact = JSON.parse(fs_1.default.readFileSync(artifactPath, "utf-8"));
    return { abi: artifact.abi, bytecode: artifact.bytecode ?? "0x" };
}
async function getContractFactory(name, signerOrFactoryOptions) {
    const signer = isFactoryOptions(signerOrFactoryOptions) ? signerOrFactoryOptions.signer : signerOrFactoryOptions;
    try {
        return await hardhat_1.ethers.getContractFactory(name, signerOrFactoryOptions);
    }
    catch {
    }
    try {
        const artifact = getAcrossContractsArtifact(name);
        return 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 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 ${name}!`);
}
//# sourceMappingURL=getContractFactory.js.map