"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCorrespondingCollateralCurrency = exports.toVoting = exports.createExchangeRateOracleKey = exports.createInclusionOracleKey = exports.newCollateralBTCExchangeRate = exports.newMonetaryAmount = exports.roundLastNDigits = exports.computeLazyDistribution = exports.satToMBTC = exports.roundTwoDecimals = exports.MBTC_IN_SAT = void 0;
const big_js_1 = __importDefault(require("big.js"));
const bn_js_1 = __importDefault(require("bn.js"));
const monetary_js_1 = require("@interlay/monetary-js");
const currency_1 = require("../types/currency");
const encoding_1 = require("./encoding");
// set maximum exponents
big_js_1.default.PE = 21;
big_js_1.default.NE = -12;
exports.MBTC_IN_SAT = 100000;
function roundTwoDecimals(input) {
    const number = new big_js_1.default(input);
    return number.round(2).toString();
}
exports.roundTwoDecimals = roundTwoDecimals;
function satToMBTC(sat) {
    const satAmount = new big_js_1.default(sat);
    return satAmount.div(exports.MBTC_IN_SAT).toString();
}
exports.satToMBTC = satToMBTC;
function computeLazyDistribution(stake, perToken, tally) {
    return stake.mul(perToken).sub(tally);
}
exports.computeLazyDistribution = computeLazyDistribution;
function roundLastNDigits(n, x) {
    const power = Math.pow(10, n);
    // Use BN such that we perform integer division
    const bigNumber = new bn_js_1.default(x.toString());
    return bigNumber.div(new bn_js_1.default(power)).mul(new bn_js_1.default(power)).toString();
}
exports.roundLastNDigits = roundLastNDigits;
function newMonetaryAmount(amount, currency, base = false) {
    const unit = base ? currency.base : currency.rawBase;
    return new monetary_js_1.MonetaryAmount(currency, amount, unit);
}
exports.newMonetaryAmount = newMonetaryAmount;
function newCollateralBTCExchangeRate(rate, counterCurrency, useBaseUnits = false) {
    const [baseCurrencyUnit, counterCurrencyUnit] = useBaseUnits
        ? [monetary_js_1.Bitcoin.base, counterCurrency.base]
        : [monetary_js_1.Bitcoin.rawBase, counterCurrency.rawBase];
    return new monetary_js_1.ExchangeRate(monetary_js_1.Bitcoin, counterCurrency, rate, baseCurrencyUnit, counterCurrencyUnit);
}
exports.newCollateralBTCExchangeRate = newCollateralBTCExchangeRate;
function createInclusionOracleKey(api, type) {
    return api.createType("InterbtcPrimitivesOracleKey", { FeeEstimation: type });
}
exports.createInclusionOracleKey = createInclusionOracleKey;
function createExchangeRateOracleKey(api, collateralCurrency) {
    const currencyId = (0, encoding_1.newCurrencyId)(api, (0, currency_1.tickerToCurrencyIdLiteral)(collateralCurrency.ticker));
    return api.createType("InterbtcPrimitivesOracleKey", { ExchangeRate: currencyId });
}
exports.createExchangeRateOracleKey = createExchangeRateOracleKey;
function toVoting(governanceCurrency) {
    switch (governanceCurrency) {
        case monetary_js_1.Interlay:
            return monetary_js_1.VoteInterlay;
        case monetary_js_1.Kintsugi:
            return monetary_js_1.VoteKintsugi;
        default:
            throw new Error("Provided currency is not a governance currency");
    }
}
exports.toVoting = toVoting;
function getCorrespondingCollateralCurrency(governanceCurrency) {
    switch (governanceCurrency.ticker) {
        case "KINT":
            return monetary_js_1.Kusama;
        case "INTR":
            return monetary_js_1.Polkadot;
        default:
            throw new Error("Provided currency is not a governance currency");
    }
}
exports.getCorrespondingCollateralCurrency = getCorrespondingCollateralCurrency;
//# sourceMappingURL=currency.js.map