import { HttpsProxyAgent } from 'https-proxy-agent';
import { MAX_UINT_32 } from '../clients/constants';
/**
 * Returns a random integer value between 0 and (n-1).
 */
export function randomInt(n) {
    return Math.floor(Math.random() * n);
}
/**
 * Generate a random clientId.
 */
export function generateRandomClientId() {
    return randomInt(MAX_UINT_32 + 1);
}
/**
 * Deterministically generate a valid clientId from an arbitrary string by performing a
 * quick hashing function on the string.
 */
export 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.
 */
export 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.
 */
export 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.
 */
export 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.`;
}
export 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);
}
export function getProxyAgent(proxy) {
    const auth = proxy.auth ? `${proxy.auth.username}:${proxy.auth.password}@` : '';
    return new HttpsProxyAgent(`${proxy.protocol}://${auth}${proxy.host}:${proxy.port}`);
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbGliL3V0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxlQUFlLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQUVwRCxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFFbkQ7O0dBRUc7QUFDSCxNQUFNLFVBQVUsU0FBUyxDQUFDLENBQVM7SUFDakMsT0FBTyxJQUFJLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxNQUFNLEVBQUUsR0FBRyxDQUFDLENBQUMsQ0FBQztBQUN2QyxDQUFDO0FBRUQ7O0dBRUc7QUFDSCxNQUFNLFVBQVUsc0JBQXNCO0lBQ3BDLE9BQU8sU0FBUyxDQUFDLFdBQVcsR0FBRyxDQUFDLENBQUMsQ0FBQztBQUNwQyxDQUFDO0FBRUQ7OztHQUdHO0FBQ0gsTUFBTSxVQUFVLGtCQUFrQixDQUFDLEtBQWE7SUFDOUMsSUFBSSxJQUFJLEdBQVcsQ0FBQyxDQUFDO0lBQ3JCLElBQUksS0FBSyxDQUFDLE1BQU0sS0FBSyxDQUFDO1FBQUUsT0FBTyxJQUFJLENBQUM7SUFDcEMsS0FBSyxJQUFJLENBQUMsR0FBRyxDQUFDLEVBQUUsQ0FBQyxHQUFHLEtBQUssQ0FBQyxNQUFNLEVBQUUsQ0FBQyxFQUFFLEVBQUUsQ0FBQztRQUN0QyxJQUFJLEdBQUcsQ0FBQyxJQUFJLElBQUksQ0FBQyxDQUFDLE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