"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContract = exports.getPoolComptroller = exports.getPoolUnitroller = exports.getPoolAddress = exports.getBytecodeHash = exports.getSaltsHash = exports.filterPoolName = exports.filter = exports.filterOnlyObjectProperties = void 0;
const bad_words_1 = __importDefault(require("bad-words"));
const ethers_1 = require("ethers");
const Comptroller_1 = __importDefault(require("../../abis/Comptroller"));
const Unitroller_json_1 = __importDefault(require("../../artifacts/Unitroller.json"));
function filterOnlyObjectProperties(obj) {
    return Object.fromEntries(Object.entries(obj).filter(([k]) => isNaN(k)));
}
exports.filterOnlyObjectProperties = filterOnlyObjectProperties;
exports.filter = new bad_words_1.default({ placeHolder: " " });
exports.filter.addWords(...["R1", "R2", "R3", "R4", "R5", "R6", "R7"]);
const filterPoolName = (name) => {
    return exports.filter.clean(name);
};
exports.filterPoolName = filterPoolName;
const getSaltsHash = (from, poolName, blockNumber) => {
    return ethers_1.utils.solidityKeccak256(["address", "string", "uint"], [from, poolName, blockNumber]);
};
exports.getSaltsHash = getSaltsHash;
const getBytecodeHash = (feeDistributorAddress) => {
    return ethers_1.utils.keccak256(Unitroller_json_1.default.bytecode.object + new ethers_1.utils.AbiCoder().encode(["address"], [feeDistributorAddress]).slice(2));
};
exports.getBytecodeHash = getBytecodeHash;
const getPoolAddress = (from, poolName, marketsCounter, feeDistributorAddress, poolDirectoryAddress) => {
    return ethers_1.utils.getCreate2Address(poolDirectoryAddress, (0, exports.getSaltsHash)(from, poolName, marketsCounter), (0, exports.getBytecodeHash)(feeDistributorAddress));
};
exports.getPoolAddress = getPoolAddress;
const getPoolUnitroller = (poolAddress, signer) => {
    return new ethers_1.Contract(poolAddress, Unitroller_json_1.default.abi, signer);
};
exports.getPoolUnitroller = getPoolUnitroller;
const getPoolComptroller = (poolAddress, signer) => {
    return new ethers_1.Contract(poolAddress, Comptroller_1.default, signer);
};
exports.getPoolComptroller = getPoolComptroller;
const getContract = (address, abi, providerOrSigner) => {
    return new ethers_1.Contract(address, abi, providerOrSigner);
};
exports.getContract = getContract;
