/************************************************************** * THIS FILE IS GENERATED AND SHOULD NOT BE MANUALLY MODIFIED * **************************************************************/ /** * Sparse strike exposure index for payout-liability accounting. * * The tree keys finite interval boundaries by absolute tick, matching the tick * pair packed into the durable order ID. Raw strikes are recovered only at the * pricing/settlement boundary, where callers pass the owning market's `tick_size` * (`raw_strike = tick * tick_size`); the tree stores no grid geometry. * * This height-balanced (AVL) tree stores finite interval boundaries touched by * positions. Boundary ticks are caller-chosen, so the balancing rule must not read * anything the caller supplies: rotations are driven by measured subtree height, * which bounds depth at `O(log n)` for _every_ tick set rather than in expectation * over a random one. Depth is the cost model that matters — each node is a * dynamic-field child, and `apply_at` and `prefix_pay` touch one per level against * a per-transaction cached-object ceiling. * * It tracks each order's quantity, which is also its settled payout: a winning * order pays its full quantity. Live cash backing is the max-point payout plus a * buffer over the disjoint-book gap; the tree's max-point term is the floor anchor * of that enforced reserve. * * Shape carries no value for a consistent index: `combine` is associative over the * in-order sequence, so any arrangement of the same boundaries yields identical * summaries, settlement prefixes, and linear-walk totals. That holds only while * every prefix is non-negative, which a consistent book guarantees. Under a * caller/index desync the settlement walk's underflow abort depends on which * prefixes a given shape happens to visit, so it is not a desync detector — the * per-boundary underflow in `apply_net` is the authority. * * The module also owns the valuation snapshot for the resumable flush: while a * flush generation is active, each node lazily captures its boundary quantities * immediately before its first mutation under that generation (an untouched node * is its own snapshot), and `walk_frozen` prices the tree exactly as it stood at * the snapshot instant through the same walk the live read uses. * * Delayed execution adds pins: a waiting order's boundary ticks, passed in as * `pins` (tick -> count of waiting orders; a key is present only while its count * is positive). Enqueue creates the nodes up front (`ens_node`), no deletion path * removes a pinned node, and a resolve fill inserts over existing nodes only * (`ins_old`), so the keeper never creates a node. */ import { MoveStruct } from '../utils/index.js'; import { bcs } from '@mysten/sui/bcs'; import { U64 } from '../../bcs/integers.js'; import * as table from './deps/sui/table.js'; const $moduleName = '@local-pkg/deepbook_predict::strike_payout_tree'; export const StrikePayoutTree = new MoveStruct({ name: `${$moduleName}::StrikePayoutTree`, fields: { root: bcs.option(U64), nodes: table.Table, node_count: U64, /** * Aggregate order quantity over the open-lower prefix, which is also its aggregate * settled payout. */ base: U64, /** * Flush generation whose snapshot this tree holds. Node shadows are valid only * against this value; monotonically increasing, 0 = never snapshotted. */ snapshot_seq: U64, /** * True from `snap_on` until `snap_done`/`snap_off`. While set, mutations capture * shadows and emptied nodes with shadow quantities are retained for the frozen * walk instead of removed. */ snapshot_active: bcs.bool(), /** `base` as of the snapshot instant, captured eagerly at activation. */ snapshot_base: U64, }, }); export const PayoutSummary = new MoveStruct({ name: `${$moduleName}::PayoutSummary`, fields: { start: U64, end: U64, /** * Never exceeds `start`, by construction in `bound_sum` and `combine`. That bound * is what makes `combine` associative at u64 scale — and therefore what makes the * tree's shape irrelevant to every value it reports. A summary term that could * outgrow `start` would break shape-independence with no test to catch it, and * would also abort `strike_exposure`'s plain `total - max` subtraction. */ max_payout_prefix_gain: U64, }, }); export const PayoutNode = new MoveStruct({ name: `${$moduleName}::PayoutNode`, fields: { /** * Longest root-to-leaf path in this subtree, counting this node. A leaf is 1; an * absent child is 0. Maintained by `resummarize` alongside `summary`, and read * only by `rebalance` — never by a caller, and never derived from a tick. */ height: U64, left: bcs.option(U64), right: bcs.option(U64), /** * This node's own boundary terms, stored so the subtree `summary` can be * recomputed without deriving locals by subtracting child summaries. */ local_start: U64, local_end: U64, /** * Boundary terms at generation `snapshot_seq`'s instant, captured before the first * mutation under it. Active generation + zero shadows = created post-snapshot; * older generation = untouched, live terms ARE the shadow. */ snapshot_local_start: U64, snapshot_local_end: U64, snapshot_seq: U64, summary: PayoutSummary, }, });