{"version":3,"file":"ticks.mjs","names":[],"sources":["../../src/predict/ticks.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport { PredictInputError } from './errors.js';\n\nconst TICK_BITS = 30n;\n\n/** +inf sentinel tick: the upper bound of an UP range. */\nexport const POS_INF_TICK = (1n << TICK_BITS) - 1n;\n\nexport type Side = 'up' | 'down';\n\n// Convert a raw binary-range strike to the `(lower_tick, higher_tick)` pair the\n// `mint` entrypoint takes directly (there is no standalone packed range key).\n// An UP order is `(strike, +inf)` -> lower_tick = strike/tick_size, higher_tick =\n// POS_INF_TICK; a DOWN order is `(-inf, strike)` -> lower_tick = 0 (neg-inf),\n// higher_tick = strike/tick_size.\nexport function binaryRangeTicks(\n\tstrikeRaw: bigint,\n\tside: Side,\n\ttickSize: bigint,\n): { lowerTick: bigint; higherTick: bigint } {\n\t// Anything not exactly 'up' used to fall through to DOWN, byte-identical to a real\n\t// down mint — no build error, no simulate error, no chain abort. `side` arrives from UI\n\t// state, JSON, or a DB column at runtime, where the literal type does not protect.\n\tif (side !== 'up' && side !== 'down') {\n\t\tthrow new PredictInputError(`side must be 'up' or 'down', got ${JSON.stringify(side)}`);\n\t}\n\tconst tick = strikeRaw / tickSize;\n\tif (tick * tickSize !== strikeRaw) {\n\t\tthrow new PredictInputError(`strike ${strikeRaw} is not a whole tick multiple of ${tickSize}`);\n\t}\n\tif (tick <= 0n || tick >= POS_INF_TICK) {\n\t\tthrow new PredictInputError(\n\t\t\t`strike tick ${tick} outside the finite tick domain (1..POS_INF_TICK-1)`,\n\t\t);\n\t}\n\tconst isUp = side === 'up';\n\treturn {\n\t\tlowerTick: isUp ? tick : 0n,\n\t\thigherTick: isUp ? POS_INF_TICK : tick,\n\t};\n}\n\n/**\n * Snap a strike in USD to a market's admission grid (`MarketSummary.admissionTickSize`), the step\n * every new mint boundary must be a whole multiple of. Use it on a form's bound inputs: `'down'`\n * for a lower bound and `'up'` for an upper bound keep a range from shrinking past what was typed,\n * and `'nearest'` (the default) suits a single strike. Exact: it rounds in the 1e9-scaled integers\n * the chain uses, not in floating point.\n */\nexport function snapStrike(\n\tprice: number,\n\tadmissionTickSize: number,\n\tmode: 'down' | 'up' | 'nearest' = 'nearest',\n): number {\n\tif (!(Number.isFinite(price) && price > 0)) {\n\t\tthrow new PredictInputError(`strike must be a positive number, got ${price}`);\n\t}\n\tif (!(Number.isFinite(admissionTickSize) && admissionTickSize > 0)) {\n\t\tthrow new PredictInputError(`admissionTickSize must be positive, got ${admissionTickSize}`);\n\t}\n\tconst step = BigInt(Math.round(admissionTickSize * 1e9));\n\tconst raw = BigInt(Math.round(price * 1e9));\n\tconst below = (raw / step) * step;\n\tconst snapped =\n\t\tmode === 'down' || raw === below\n\t\t\t? below\n\t\t\t: mode === 'up'\n\t\t\t\t? below + step\n\t\t\t\t: raw - below < below + step - raw\n\t\t\t\t\t? below\n\t\t\t\t\t: below + step;\n\tif (snapped === 0n) {\n\t\tthrow new PredictInputError(`strike ${price} snaps below the first ${admissionTickSize} step`);\n\t}\n\treturn Number(snapped) / 1e9;\n}\n"],"mappings":";;;AAIA,MAAM,YAAY;;AAGlB,MAAa,gBAAgB,MAAM,aAAa;AAShD,SAAgB,iBACf,WACA,MACA,UAC4C;AAI5C,KAAI,SAAS,QAAQ,SAAS,OAC7B,OAAM,IAAI,kBAAkB,oCAAoC,KAAK,UAAU,KAAK,GAAG;CAExF,MAAM,OAAO,YAAY;AACzB,KAAI,OAAO,aAAa,UACvB,OAAM,IAAI,kBAAkB,UAAU,UAAU,mCAAmC,WAAW;AAE/F,KAAI,QAAQ,MAAM,QAAQ,aACzB,OAAM,IAAI,kBACT,eAAe,KAAK,qDACpB;CAEF,MAAM,OAAO,SAAS;AACtB,QAAO;EACN,WAAW,OAAO,OAAO;EACzB,YAAY,OAAO,eAAe;EAClC;;;;;;;;;AAUF,SAAgB,WACf,OACA,mBACA,OAAkC,WACzB;AACT,KAAI,EAAE,OAAO,SAAS,MAAM,IAAI,QAAQ,GACvC,OAAM,IAAI,kBAAkB,yCAAyC,QAAQ;AAE9E,KAAI,EAAE,OAAO,SAAS,kBAAkB,IAAI,oBAAoB,GAC/D,OAAM,IAAI,kBAAkB,2CAA2C,oBAAoB;CAE5F,MAAM,OAAO,OAAO,KAAK,MAAM,oBAAoB,IAAI,CAAC;CACxD,MAAM,MAAM,OAAO,KAAK,MAAM,QAAQ,IAAI,CAAC;CAC3C,MAAM,QAAS,MAAM,OAAQ;CAC7B,MAAM,UACL,SAAS,UAAU,QAAQ,QACxB,QACA,SAAS,OACR,QAAQ,OACR,MAAM,QAAQ,QAAQ,OAAO,MAC5B,QACA,QAAQ;AACd,KAAI,YAAY,GACf,OAAM,IAAI,kBAAkB,UAAU,MAAM,yBAAyB,kBAAkB,OAAO;AAE/F,QAAO,OAAO,QAAQ,GAAG"}