{"version":3,"file":"pricing.mjs","names":[],"sources":["../../../src/predict/reads/pricing.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport { Transaction } from '@mysten/sui/transactions';\nimport { type GeneratedConfig } from '../config/generated.js';\nimport { Pricer } from '../../contracts/deepbook_predict/pricing.js';\nimport type { PricerInputs, Svi } from '../pricing.js';\nimport { loadLivePricer, type MarketFeeds } from '../tx/trade.js';\nimport { inspectReturns, type ReadClient } from './inspect.js';\n\n// The chain's fixed-point scales for the rolled `PricingSVI` (see the generated\n// `Pricer`/`PricingSVI` struct): forward and rho/m/sigma at 1e9; the rolled `a`/`b` land\n// at 1e18 (roll_down multiplies the 1e9 raw by an extra 1e9). Converting to `number`\n// (float) is deliberate — this is a display pricer; a is a variance ~O(1e-2), so f64's\n// ~15 significant digits are ample and, per pricing.ts, more precise than the chain's\n// fixed point on the short-dated surfaces this prices.\nconst FORWARD_SCALE = 1e9;\nconst AB_SCALE = 1e18;\nconst RMS_SCALE = 1e9; // rho, m, sigma\n\nconst i64 = (v: { magnitude: string | number | bigint; is_negative: boolean }): number =>\n\t(v.is_negative ? -1 : 1) * Number(v.magnitude);\n\n/** A resolved pricer snapshot read from the chain: the decimal forward + rolled SVI the\n * client-side math consumes, plus the oracle source timestamps behind it (ms; for\n * staleness display — Pyth is 0 when no usable spot existed). The Block-Scholes entries are\n * the provider's PER-UPDATE source timestamps (`value_timestamp` for spot/forward,\n * `svi_timestamp` for SVI) — the clocks freshness and the SVI roll-down anchor on. */\nexport interface PricerSnapshot extends PricerInputs {\n\tsources: {\n\t\tpythSpotMs: number;\n\t\tblockScholesSpotMs: number;\n\t\tblockScholesForwardMs: number;\n\t\tblockScholesSviMs: number;\n\t};\n}\n\n// Decode a `Pricer` (already forward-resolved + roll-down-applied on-chain) into decimal\n// `PricerInputs`. Signed fields (`a`, `rho`, `m`) carry the magnitude/flag pair the chain\n// uses; `b`/`sigma` are non-negative.\nfunction decodePricer(pricer: ReturnType<typeof Pricer.parse>): PricerSnapshot {\n\tconst s = pricer.svi;\n\tconst svi: Svi = {\n\t\ta: ((s.a_is_negative ? -1 : 1) * Number(s.a_magnitude)) / AB_SCALE,\n\t\tb: Number(s.b) / AB_SCALE,\n\t\trho: i64(s.rho) / RMS_SCALE,\n\t\tm: i64(s.m) / RMS_SCALE,\n\t\tsigma: Number(s.sigma) / RMS_SCALE,\n\t};\n\treturn {\n\t\tforward: Number(pricer.forward) / FORWARD_SCALE,\n\t\tsvi,\n\t\tsources: {\n\t\t\tpythSpotMs: Number(pricer.pyth_spot_source_timestamp_ms),\n\t\t\tblockScholesSpotMs: Number(pricer.block_scholes_spot_source_timestamp_ms),\n\t\t\tblockScholesForwardMs: Number(pricer.block_scholes_forward_source_timestamp_ms),\n\t\t\tblockScholesSviMs: Number(pricer.block_scholes_svi_source_timestamp_ms),\n\t\t},\n\t};\n}\n\n// Read one live pricer snapshot for `marketId`: a single simulate of\n// `load_live_pricer` — the chain reads the oracle feeds, picks the forward\n// (Pyth-vs-Block-Scholes admin policy + freshness), and rolls the SVI down to now, then\n// returns the whole `Pricer` by value (copy+drop), whose BCS we decode. One round trip;\n// then a `boardPricer` prices every strike locally.\n//\n// `load_live_pricer` ABORTS (a typed PredictMoveError) when the market is expired, a feed\n// is stale, or the surface fails the pricing-safe envelope — i.e. exactly when the chain\n// itself cannot quote. Callers surface that the same way `read.price` does.\nexport async function readPricerSnapshot(\n\tclient: ReadClient,\n\tconfig: GeneratedConfig,\n\tmarketId: string,\n\tfeeds: MarketFeeds,\n): Promise<PricerSnapshot> {\n\tconst tx = new Transaction();\n\ttx.add(loadLivePricer(config, { expiryMarketId: marketId, ...feeds }));\n\tconst [cmd0] = await inspectReturns(client, tx);\n\treturn decodePricer(Pricer.parse(cmd0[0]));\n}\n"],"mappings":";;;;;;AAeA,MAAM,gBAAgB;AACtB,MAAM,WAAW;AACjB,MAAM,YAAY;AAElB,MAAM,OAAO,OACX,EAAE,cAAc,KAAK,KAAK,OAAO,EAAE,UAAU;AAmB/C,SAAS,aAAa,QAAyD;CAC9E,MAAM,IAAI,OAAO;CACjB,MAAM,MAAW;EAChB,IAAK,EAAE,gBAAgB,KAAK,KAAK,OAAO,EAAE,YAAY,GAAI;EAC1D,GAAG,OAAO,EAAE,EAAE,GAAG;EACjB,KAAK,IAAI,EAAE,IAAI,GAAG;EAClB,GAAG,IAAI,EAAE,EAAE,GAAG;EACd,OAAO,OAAO,EAAE,MAAM,GAAG;EACzB;AACD,QAAO;EACN,SAAS,OAAO,OAAO,QAAQ,GAAG;EAClC;EACA,SAAS;GACR,YAAY,OAAO,OAAO,8BAA8B;GACxD,oBAAoB,OAAO,OAAO,uCAAuC;GACzE,uBAAuB,OAAO,OAAO,0CAA0C;GAC/E,mBAAmB,OAAO,OAAO,sCAAsC;GACvE;EACD;;AAYF,eAAsB,mBACrB,QACA,QACA,UACA,OAC0B;CAC1B,MAAM,KAAK,IAAI,aAAa;AAC5B,IAAG,IAAI,eAAe,QAAQ;EAAE,gBAAgB;EAAU,GAAG;EAAO,CAAC,CAAC;CACtE,MAAM,CAAC,QAAQ,MAAM,eAAe,QAAQ,GAAG;AAC/C,QAAO,aAAa,OAAO,MAAM,KAAK,GAAG,CAAC"}