"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BTCStateProvider = void 0;
const cache_1 = require("../../../models/cache");
const FeeProviders = __importStar(require("../../fee"));
const internal_1 = require("../internal/internal");
class BTCStateProvider extends internal_1.InternalStateProvider {
    constructor(chain = 'BTC') {
        super(chain);
    }
    async getFee(params) {
        const { chain, network, target, mode } = params;
        const cacheKey = `getFee-${chain}-${network}-${target}${mode ? '-' + mode.toLowerCase() : ''}`;
        return cache_1.CacheStorage.getGlobalOrRefresh(cacheKey, async () => {
            const rpcEstimate = await this.getRPC(chain, network).getEstimateSmartFee(Number(target), mode);
            const estimates = (await Promise.allSettled([
                FeeProviders.BlockCypher.getFee(network, target),
                FeeProviders.MempoolSpace.getFee(network, target),
                FeeProviders.Bitgo.getFee(network, target),
            ])
                .then(results => results.filter(result => result.status === 'fulfilled')))
                .map(result => result.value);
            // NOTE: rpcEstimate is in BTC per kilobyte, estimates is in sats per byte
            let feerate = rpcEstimate.feerate * 1e5; // convert to sats per byte
            feerate = feerate + estimates.reduce((acc, v) => acc += v, 0);
            feerate = Math.ceil(feerate / (estimates.length + 1));
            feerate = Number((feerate / 1e5).toFixed(8)); // convert to BTC per KB
            return {
                feerate,
                blocks: target,
            };
        }, 5 * cache_1.CacheStorage.Times.Minute);
    }
}
exports.BTCStateProvider = BTCStateProvider;
//# sourceMappingURL=btc.js.map