UNPKG

@nktkas/hyperliquid

Version:

Hyperliquid API SDK for all major JS runtimes, written in TypeScript.

36 lines (35 loc) 1.44 kB
/** * Nonce manager for generating unique, monotonically increasing nonces. * @module */ /** Default upper bound on map size before stale entries are pruned. */ const DEFAULT_MAX_ENTRIES = 10_000; /** * Creates a nonce manager that issues unique, monotonically increasing nonces per key. * * Uses `Date.now()` in ms; if the previous nonce for the key is greater than or equal to * `Date.now()`, increments by 1 to maintain monotonicity. * * To bound memory under high-cardinality workloads (e.g., a server proxying many wallets), * stale entries are pruned when the internal map grows beyond `maxEntries`. An entry is * considered stale if `Date.now()` has advanced past its last issued nonce. * * @param maxEntries Upper bound on map size before stale entries are pruned. Default: `10000`. * @return A {@linkcode NonceManager}. */ export function createNonceManager(maxEntries = DEFAULT_MAX_ENTRIES) { const map = new Map(); return { getNonce (key) { const now = Date.now(); if (map.size > maxEntries) { for (const [k, last] of map){ if (now > last) map.delete(k); } } const last = map.get(key) ?? 0; const nonce = now > last ? now : last + 1; map.set(key, nonce); return nonce; } }; } /** Default global nonce manager instance. */ export const globalNonceManager = /* @__PURE__ */ createNonceManager(); //# sourceMappingURL=_nonce.js.map