"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bitgo = exports.BitgoClass = void 0;
const util_1 = __importDefault(require("util"));
const request_1 = __importDefault(require("request"));
const logger_1 = __importDefault(require("../../logger"));
class BitgoClass {
    constructor() {
        this.feeUrls = {
            mainnet: 'https://www.bitgo.com/api/v2/btc/tx/fee',
            testnet3: 'https://www.bitgo-test.com/api/v2/tbtc/tx/fee',
            testnet4: 'https://www.bitgo-test.com/api/v2/tbtc4/tx/fee'
        };
    }
    async getFee(network, nblocks) {
        try {
            network = network === 'regtest' ? 'testnet4' : network;
            nblocks = Math.min(Math.max(nblocks, 2), 1000); // min 2, max 1000
            const res = await util_1.default.promisify(request_1.default.get).call(request_1.default, {
                uri: `${this.feeUrls[network]}?numBlocks=${nblocks}`,
                json: true
            });
            if (res.statusCode !== 200) {
                throw new Error(`Status code: ${res.statusCode}`);
            }
            const fee = res.body;
            return fee.feePerKb / 1000; // convert to sats/B
        }
        catch (err) {
            logger_1.default.warn('Error getting fee from bitgo: %o', err.message || err);
            throw err;
        }
    }
    ;
}
exports.BitgoClass = BitgoClass;
;
exports.Bitgo = new BitgoClass();
//# sourceMappingURL=bitgo.js.map