"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultFeeAPI = exports.GriefingCollateralType = void 0;
const big_js_1 = __importDefault(require("big.js"));
const encoding_1 = require("../utils/encoding");
const utils_1 = require("../utils");
var GriefingCollateralType;
(function (GriefingCollateralType) {
    GriefingCollateralType[GriefingCollateralType["Issue"] = 0] = "Issue";
    GriefingCollateralType[GriefingCollateralType["Replace"] = 1] = "Replace";
})(GriefingCollateralType = exports.GriefingCollateralType || (exports.GriefingCollateralType = {}));
class DefaultFeeAPI {
    constructor(api, oracleAPI, assetRegistryAPI, loansAPI) {
        this.api = api;
        this.oracleAPI = oracleAPI;
        this.assetRegistryAPI = assetRegistryAPI;
        this.loansAPI = loansAPI;
    }
    getGriefingCollateral(amount, type) {
        return __awaiter(this, void 0, void 0, function* () {
            let ratePromise;
            switch (type) {
                case GriefingCollateralType.Issue: {
                    ratePromise = this.getIssueGriefingCollateralRate();
                    break;
                }
                case GriefingCollateralType.Replace: {
                    ratePromise = this.getReplaceGriefingCollateralRate();
                    break;
                }
            }
            const nativeCurrency = yield utils_1.currencyIdToMonetaryCurrency(this.assetRegistryAPI, this.loansAPI, this.api.consts.vaultRegistry.getGriefingCollateralCurrencyId);
            const [griefingCollateralRate, griefingAmount] = yield Promise.all([
                ratePromise,
                this.oracleAPI.convertWrappedToCurrency(amount, nativeCurrency),
            ]);
            return griefingAmount.mul(griefingCollateralRate);
        });
    }
    getIssueGriefingCollateralRate() {
        return __awaiter(this, void 0, void 0, function* () {
            const griefingCollateralRate = yield this.api.query.fee.issueGriefingCollateral();
            return encoding_1.decodeFixedPointType(griefingCollateralRate);
        });
    }
    getReplaceGriefingCollateralRate() {
        return __awaiter(this, void 0, void 0, function* () {
            const griefingCollateralRate = yield this.api.query.fee.replaceGriefingCollateral();
            return encoding_1.decodeFixedPointType(griefingCollateralRate);
        });
    }
    getIssueFee() {
        return __awaiter(this, void 0, void 0, function* () {
            const issueFee = yield this.api.query.fee.issueFee();
            return encoding_1.decodeFixedPointType(issueFee);
        });
    }
    calculateAPY(feesWrapped, lockedCollateral, exchangeRate) {
        return __awaiter(this, void 0, void 0, function* () {
            if (lockedCollateral.isZero()) {
                return new big_js_1.default(0);
            }
            if (exchangeRate === undefined) {
                exchangeRate = yield this.oracleAPI.getExchangeRate(lockedCollateral.currency);
            }
            const feesWrappedAsCollateral = exchangeRate.toCounter(feesWrapped).toBig();
            // convert to percentage
            return feesWrappedAsCollateral.div(lockedCollateral.toBig()).mul(100);
        });
    }
}
exports.DefaultFeeAPI = DefaultFeeAPI;
//# sourceMappingURL=fee.js.map