import { __awaiter, __generator } from "tslib";
import * as fs from "fs/promises";
import { isError } from "../../typeguards";
/**
 * @dev To retrieve a new ABI, the following is useful:
 * CONTRACT_ADDRESS="0x1234..."
 * URL="http://api.etherscan.io/api?module=contract&action=getabi&format=raw&address=${CONTRACT_ADDRESS}"
 * curl -s "${URL}" | python -m json.tool > "./src/utils/abi/contracts/new-contract.json"
 */
/**
 * @notice Obtain the path to the local ABI JSON store.
 * @returns Fully-qualified path to the local ABI JSON store.
 */
export function getABIDir() {
    return "".concat(__dirname, "/contracts");
}
/**
 * @notice Retrieve an ABI description from the set of known contracts.
 * @param contractName Name of the contract ABI to retrieve.
 * @returns Contract ABI as an ethers ContractInterface type.
 */
export function getABI(contractName) {
    return __awaiter(this, void 0, void 0, function () {
        var _a, _b, err_1, msg;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    _c.trys.push([0, 2, , 3]);
                    _b = (_a = JSON).parse;
                    return [4 /*yield*/, fs.readFile("".concat(getABIDir(), "/").concat(contractName, ".json"), { encoding: "utf8" })];
                case 1: return [2 /*return*/, _b.apply(_a, [_c.sent()])];
                case 2:
                    err_1 = _c.sent();
                    msg = isError(err_1) ? err_1.message : err_1 === null || err_1 === void 0 ? void 0 : err_1.code;
                    throw new Error("Unable to retrieve ".concat(contractName, " ABI (").concat(msg !== null && msg !== void 0 ? msg : "unknown error", ")"));
                case 3: return [2 /*return*/];
            }
        });
    });
}
//# sourceMappingURL=index.js.map