"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomAddress = exports.retry = exports.delay = exports.getSamplesBetween = exports.calcApr = exports.calcPeriodicCompoundInterest = exports.calcContinuousCompoundInterest = exports.percent = exports.calculateGasFees = exports.gasCost = exports.nativeToToken = exports.fixedPointAdjustment = exports.max = exports.min = exports.fromWei = exports.toBNWei = exports.bs58 = exports.keccak256 = exports.MAX_BIG_INT = exports.AddressZero = void 0;
var tslib_1 = require("tslib");
var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
var bs58_1 = tslib_1.__importDefault(require("bs58"));
exports.bs58 = bs58_1.default;
var ethers_1 = require("ethers");
var BigNumberUtils_1 = require("./BigNumberUtils");
var FormattingUtils_1 = require("./FormattingUtils");
exports.AddressZero = ethers_1.ethers.constants.AddressZero;
exports.MAX_BIG_INT = BigNumberUtils_1.BigNumber.from(Number.MAX_SAFE_INTEGER.toString());
exports.keccak256 = ethers_1.ethers.utils.keccak256;
var toBNWei = function (num, decimals) { return (0, BigNumberUtils_1.parseUnits)(num.toString(), decimals); };
exports.toBNWei = toBNWei;
var fromWei = function (num, decimals) { return (0, BigNumberUtils_1.formatUnits)(num.toString(), decimals); };
exports.fromWei = fromWei;
function min(a, b) {
    var bna = (0, BigNumberUtils_1.toBN)(a);
    var bnb = (0, BigNumberUtils_1.toBN)(b);
    return bna.lte(bnb) ? bna : bnb;
}
exports.min = min;
function max(a, b) {
    var bna = (0, BigNumberUtils_1.toBN)(a);
    var bnb = (0, BigNumberUtils_1.toBN)(b);
    return bna.gte(bnb) ? bna : bnb;
}
exports.max = max;
exports.fixedPointAdjustment = (0, exports.toBNWei)("1");
function nativeToToken(fromAmount, price, toDecimals, nativeDecimals) {
    if (price === void 0) { price = 1; }
    if (toDecimals === void 0) { toDecimals = 18; }
    if (nativeDecimals === void 0) { nativeDecimals = 18; }
    var priceWei = (0, exports.toBNWei)(price);
    var toAmount = (0, exports.toBNWei)(fromAmount).div(priceWei);
    return (0, FormattingUtils_1.ConvertDecimals)(nativeDecimals, toDecimals)(toAmount).toString();
}
exports.nativeToToken = nativeToToken;
var gasCost = function (gas, gasPrice) {
    return BigNumberUtils_1.BigNumber.from(gas).mul(gasPrice);
};
exports.gasCost = gasCost;
function calculateGasFees(gas, gasPrice, price, decimals) {
    if (price === void 0) { price = 1; }
    if (decimals === void 0) { decimals = 18; }
    var amountNative = (0, exports.gasCost)(gas, gasPrice);
    return nativeToToken(amountNative, price, decimals);
}
exports.calculateGasFees = calculateGasFees;
function percent(numerator, denominator) {
    return exports.fixedPointAdjustment.mul(numerator).div(denominator);
}
exports.percent = percent;
var calcContinuousCompoundInterest = function (startAmount, endAmount, periodsElapsed, periodsPerYear) {
    var years = new decimal_js_1.default(periodsPerYear).div(periodsElapsed);
    return new decimal_js_1.default(endAmount).div(startAmount).ln().div(years).toString();
};
exports.calcContinuousCompoundInterest = calcContinuousCompoundInterest;
var calcPeriodicCompoundInterest = function (startAmount, endAmount, periodsElapsed, periodsPerYear) {
    var n = new decimal_js_1.default(periodsPerYear);
    var A = new decimal_js_1.default(endAmount);
    var P = new decimal_js_1.default(startAmount);
    var t = new decimal_js_1.default(periodsPerYear).div(periodsElapsed);
    var one = new decimal_js_1.default(1);
    return n
        .mul(A.div(P)
        .pow(one.div(n.div(t)))
        .sub(one))
        .toFixed(18);
};
exports.calcPeriodicCompoundInterest = calcPeriodicCompoundInterest;
var calcApr = function (startAmount, endAmount, periodsElapsed, periodsPerYear) {
    return new decimal_js_1.default(endAmount).sub(startAmount).div(startAmount).mul(periodsPerYear).div(periodsElapsed).toFixed(18);
};
exports.calcApr = calcApr;
var getSamplesBetween = function (min, max, size) {
    var keepIterate = true;
    var intervals = [];
    while (keepIterate) {
        var to = Math.min(min + size - 1, max);
        intervals.push([min, to]);
        min = to + 1;
        if (min >= max)
            keepIterate = false;
    }
    return intervals;
};
exports.getSamplesBetween = getSamplesBetween;
function delay(seconds) {
    return new Promise(function (resolve) { return setTimeout(resolve, seconds * 1000); });
}
exports.delay = delay;
function retry(call, times, delayS) {
    var _this = this;
    var promiseChain = call();
    for (var i = 0; i < times; i++)
        promiseChain = promiseChain.catch(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, delay(delayS)];
                    case 1:
                        _a.sent();
                        return [4, call()];
                    case 2: return [2, _a.sent()];
                }
            });
        }); });
    return promiseChain;
}
exports.retry = retry;
function randomAddress() {
    return ethers_1.ethers.utils.getAddress(ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.randomBytes(20)));
}
exports.randomAddress = randomAddress;
//# sourceMappingURL=common.js.map