"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BNtoFloat = BNtoFloat;
exports.BNtoFloatFixed18 = BNtoFloatFixed18;
exports.floatToBN = floatToBN;
exports.toFixedEther = toFixedEther;
exports.parsePriceStringToBigNumberOn18DecimalsExt = parsePriceStringToBigNumberOn18DecimalsExt;
const ethers_1 = require("ethers");
/**
 * Parse the given BN value as float with decimal precision
 *
 * @param {BigNumber} value
 * @return {number}
 */
function BNtoFloat(value) {
    return parseFloat(value.toString()) / 1e18;
}
function BNtoFloatFixed18(value) {
    return (parseFloat(value.toString()) / 1e18).toFixed(18);
}
function floatToBN(value) {
    const asBigInt = BigInt(value);
    return ethers_1.BigNumber.from(asBigInt.toString());
}
function toFixedEther(value, decimals = 18) {
    return value.toFixed(decimals);
}
/**
 * Takes a string representation of a price and converts it into a BigNumber format,
 * ensuring that the fractional part is extended or truncated to exactly 18 decimals.
 *
 * @param {string} price
 * @param {number} decimals - The number of decimals to use for the fractional part.
 * @return {BigNumber}
 */
function parsePriceStringToBigNumberOn18DecimalsExt(price, decimals = 18) {
    try {
        // Look for the decimal point (.) and return the index of the decimal point in price cached as string
        const decimalPointIndex = price.indexOf('.');
        // Get the integer part of the price cached
        const integerPart = decimalPointIndex === -1 ? price : price.slice(0, decimalPointIndex);
        // Get the fractional part of the price cached
        const fractionalPart = decimalPointIndex === -1 ? '' : price.slice(decimalPointIndex + 1);
        // Align fractional part to 18 decimals always!
        const adjustedFractionalPart = (fractionalPart || '').padEnd(decimals, '0').slice(0, decimals);
        // Finally the bignumber representation of the price cached is the integer part + fractional part padded with zeros to the right to match the current precision
        return ethers_1.BigNumber.from(integerPart + adjustedFractionalPart);
    }
    catch (error) {
        throw new Error(`Error parsing price ${price} string to BigNumber:  ${error.message}`);
    }
}
//# sourceMappingURL=math.js.map