"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomInt = randomInt;
exports.generateRandomClientId = generateRandomClientId;
exports.clientIdFromString = clientIdFromString;
exports.sleep = sleep;
exports.getGovAddNewMarketTitle = getGovAddNewMarketTitle;
exports.getGovAddNewMarketSummary = getGovAddNewMarketSummary;
exports.calculateClockOffsetFromFetchDateHeader = calculateClockOffsetFromFetchDateHeader;
exports.getProxyAgent = getProxyAgent;
const https_proxy_agent_1 = require("https-proxy-agent");
const constants_1 = require("../clients/constants");
/**
 * Returns a random integer value between 0 and (n-1).
 */
function randomInt(n) {
    return Math.floor(Math.random() * n);
}
/**
 * Generate a random clientId.
 */
function generateRandomClientId() {
    return randomInt(constants_1.MAX_UINT_32 + 1);
}
/**
 * Deterministically generate a valid clientId from an arbitrary string by performing a
 * quick hashing function on the string.
 */
function clientIdFromString(input) {
    let hash = 0;
    if (input.length === 0)
        return hash;
    for (let i = 0; i < input.length; i++) {
        hash = (hash << 5) - hash + input.charCodeAt(i); // eslint-disable-line no-bitwise
        hash |= 0; // eslint-disable-line no-bitwise
    }
    // Bitwise operators covert the value to a 32-bit integer.
    // We must coerce this into a 32-bit unsigned integer.
    return hash + 2 ** 31;
}
/**
 * Pauses the execution of the program for a specified time.
 * @param ms - The number of milliseconds to pause the program.
 * @returns A promise that resolves after the specified number of milliseconds.
 */
async function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
 * Returns a title to use for a gov proposal that adds a new market.
 *
 * @param ticker ticker symbol for the new market.
 * @returns title for the gov proposal.
 */
function getGovAddNewMarketTitle(ticker) {
    return `Add ${ticker} perpetual market`;
}
/**
 * Returns a summary to use for a gov proposal that adds a new market.
 *
 * @param ticker ticker symbol for the new market.
 * @param delayBlocks number of blocks to wait before activating the market.
 * @returns summary for the gov proposal.
 */
function getGovAddNewMarketSummary(ticker, delayBlocks) {
    return `Add the x/prices, x/perpetuals and x/clob parameters needed for a ${ticker} perpetual market. Create the market in INITIALIZING status and transition it to ACTIVE status after ${delayBlocks} blocks.`;
}
function calculateClockOffsetFromFetchDateHeader(clientRequestStartTime, serverReportedTime, clientRequestEndTime) {
    // use midpoint, so assume that time for request reach server === time for response to reach us
    const estimatedLocalTimeAtServerArrival = (clientRequestStartTime + clientRequestEndTime) / 2;
    // we need an offset such that estimatedLocalTimeAtServerArrival + offset = serverReportedTime
    const offset = serverReportedTime - estimatedLocalTimeAtServerArrival;
    return Math.floor(offset);
}
function getProxyAgent(proxy) {
    const auth = proxy.auth ? `${proxy.auth.username}:${proxy.auth.password}@` : '';
    return new https_proxy_agent_1.HttpsProxyAgent(`${proxy.protocol}://${auth}${proxy.host}:${proxy.port}`);
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbGliL3V0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBUUEsOEJBRUM7QUFLRCx3REFFQztBQU1ELGdEQVdDO0FBT0Qsc0JBRUM7QUFRRCwwREFFQztBQVNELDhEQUVDO0FBRUQsMEZBWUM7QUFFRCxzQ0FHQztBQWxGRCx5REFBb0Q7QUFFcEQsb0RBQW1EO0FBRW5EOztHQUVHO0FBQ0gsU0FBZ0IsU0FBUyxDQUFDLENBQVM7SUFDakMsT0FBTyxJQUFJLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxNQUFNLEVBQUUsR0FBRyxDQUFDLENBQUMsQ0FBQztBQUN2QyxDQUFDO0FBRUQ7O0dBRUc7QUFDSCxTQUFnQixzQkFBc0I7SUFDcEMsT0FBTyxTQUFTLENBQUMsdUJBQVcsR0FBRyxDQUFDLENBQUMsQ0FBQztBQUNwQyxDQUFDO0FBRUQ7OztHQUdHO0FBQ0gsU0FBZ0Isa0JBQWtCLENBQUMsS0FBYTtJQUM5QyxJQUFJLElBQUksR0FBVyxDQUFDLENBQUM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