solmath 0.2.0

Deterministic fixed-point math and quantitative finance for Solana: Greeks, IV, American KBI, NIG, TWAP, and DeFi primitives.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Safe-by-construction pricing inputs.
//!
//! The raw pricing functions accept any `u128` and fail closed via `Result`
//! on out-of-range values. This module goes one step further: it makes the
//! *valid financial domain a type*. A [`Price`], [`Rate`], [`Vol`], or [`Time`]
//! can only be constructed inside the certified domain, and the input bundles
//! ([`EuropeanInputs`], [`ImpliedVolInputs`], [`TwapInputs`]) can only be built
//! from those.
//!
//! Once you hold a bundle, the pricing methods **cannot panic and cannot
//! silently wrap**: every internal bound the raw kernels assume is already
//! established by construction. Degenerate-but-in-range corners still fail
//! closed as `Err` — that is the errors-as-values contract, not a fault — but
//! nothing in this layer aborts your instruction. This is the recommended entry
//! point for on-chain programs: validate untrusted instruction data **once**
//! into these types at your program boundary, then thread them through your
//! logic.
//!
//! ```
//! use solmath::checked::{EuropeanInputs, ImpliedVolInputs};
//!
//! # fn demo() -> Result<(), solmath::SolMathError> {
//! // Validate raw instruction data once, at the boundary.
//! let inputs = EuropeanInputs::from_raw(
//!     100_000_000_000_000, // s = 100
//!     105_000_000_000_000, // k = 105
//!     50_000_000_000,      // r = 5%
//!     200_000_000_000,     // sigma = 20%
//!     1_000_000_000_000,   // t = 1 year
//! )?;
//! let greeks = inputs.full()?; // cannot panic or wrap; Err only on degenerate corners
//! let _ = greeks.call;
//! # Ok(())
//! # }
//! ```
//!
//! # Domain
//!
//! The bounds below are deliberately generous — far past any realistic economic
//! value — while remaining inside the region the kernels are proven/measured to
//! handle without overflow. Values larger than these should be rescaled
//! homogeneously (divide `s`, `k`, and the resulting price by a common factor).
//! The bounds are enforced by [`Price::MAX`] etc. and continuously verified by
//! the `checked_inputs_never_panic_over_domain` fuzz test.

use crate::error::SolMathError;
use crate::SCALE;

#[cfg(feature = "bs")]
use crate::bs::{black_scholes_price, bs_delta, bs_full, bs_gamma, bs_rho, bs_theta, bs_vega};
#[cfg(feature = "bs")]
use crate::constants::BsFull;

/// A non-negative price in fixed-point (`SCALE = 1e12`), bounded to the
/// certified pricing domain `[0, 100_000]` real units.
///
/// Spot, strike, barrier, and observed option prices are all `Price`s.
#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Price(u128);

/// A non-negative interest rate in fixed-point, bounded to `[0, 1000%]`.
#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Rate(u128);

/// A strictly positive volatility in fixed-point, bounded to `(0, 10000%]`.
///
/// Black-Scholes requires `sigma > 0`; the type enforces it, so the raw
/// `DomainError` path for zero volatility is unreachable from this layer.
#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Vol(u128);

/// A strictly positive time-to-expiry in years (fixed-point), bounded to
/// `(0, 100]` years. Black-Scholes requires `t > 0`.
#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Time(u128);

#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
impl Price {
    /// Upper bound: `100_000` real units (`1e17` raw). This is the price bound
    /// the implied-volatility kernel is proven safe against, and it also keeps
    /// the Black-Scholes Greek combinations well within `i128`.
    pub const MAX: u128 = 100_000 * SCALE;

    /// Construct a validated price. Rejects values above [`Price::MAX`].
    #[inline]
    pub const fn new(raw: u128) -> Result<Self, SolMathError> {
        if raw > Self::MAX {
            return Err(SolMathError::DomainError);
        }
        Ok(Self(raw))
    }

    /// The underlying fixed-point value.
    #[inline]
    pub const fn get(self) -> u128 {
        self.0
    }
}

#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
impl Rate {
    /// Upper bound: `1000%` (`10 * SCALE`).
    pub const MAX: u128 = 10 * SCALE;

    /// Construct a validated rate. Rejects values above [`Rate::MAX`].
    #[inline]
    pub const fn new(raw: u128) -> Result<Self, SolMathError> {
        if raw > Self::MAX {
            return Err(SolMathError::DomainError);
        }
        Ok(Self(raw))
    }

    /// The underlying fixed-point value.
    #[inline]
    pub const fn get(self) -> u128 {
        self.0
    }
}

#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
impl Vol {
    /// Upper bound: `10000%` (`100 * SCALE`).
    pub const MAX: u128 = 100 * SCALE;

    /// Construct a validated volatility. Requires `0 < raw <= Vol::MAX`.
    #[inline]
    pub const fn new(raw: u128) -> Result<Self, SolMathError> {
        if raw == 0 || raw > Self::MAX {
            return Err(SolMathError::DomainError);
        }
        Ok(Self(raw))
    }

    /// The underlying fixed-point value.
    #[inline]
    pub const fn get(self) -> u128 {
        self.0
    }
}

#[cfg(any(feature = "bs", feature = "barrier", feature = "asian"))]
impl Time {
    /// Upper bound: `100` years (`100 * SCALE`).
    pub const MAX: u128 = 100 * SCALE;

    /// Construct a validated time-to-expiry. Requires `0 < raw <= Time::MAX`.
    #[inline]
    pub const fn new(raw: u128) -> Result<Self, SolMathError> {
        if raw == 0 || raw > Self::MAX {
            return Err(SolMathError::DomainError);
        }
        Ok(Self(raw))
    }

    /// The underlying fixed-point value.
    #[inline]
    pub const fn get(self) -> u128 {
        self.0
    }
}

/// A validated European-option parameter set: spot, strike, rate, volatility,
/// and time. Every pricing method on this type is guaranteed not to panic or
/// silently wrap for any in-domain input; degenerate corners (e.g. a
/// near-zero spot against a huge strike) fail closed with `Err`.
#[cfg(feature = "bs")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EuropeanInputs {
    s: Price,
    k: Price,
    r: Rate,
    sigma: Vol,
    t: Time,
}

#[cfg(feature = "bs")]
impl EuropeanInputs {
    /// Bundle already-validated components.
    #[inline]
    pub const fn new(s: Price, k: Price, r: Rate, sigma: Vol, t: Time) -> Self {
        Self { s, k, r, sigma, t }
    }

    /// Validate raw fixed-point instruction data in one shot. Returns
    /// `Err(DomainError)` if any field is outside its certified range.
    #[inline]
    pub const fn from_raw(
        s: u128,
        k: u128,
        r: u128,
        sigma: u128,
        t: u128,
    ) -> Result<Self, SolMathError> {
        // `?` is not usable in const fn on the MSRV, so match explicitly.
        let s = match Price::new(s) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let k = match Price::new(k) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let r = match Rate::new(r) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let sigma = match Vol::new(sigma) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let t = match Time::new(t) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        Ok(Self { s, k, r, sigma, t })
    }

    /// `(call, put)` European prices at SCALE.
    #[inline]
    pub fn price(&self) -> Result<(u128, u128), SolMathError> {
        black_scholes_price(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// Price plus all five Greeks in one call.
    #[inline]
    pub fn full(&self) -> Result<BsFull, SolMathError> {
        bs_full(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// High-precision (1e15 internal) price plus Greeks.
    #[cfg(feature = "transcendental")]
    #[inline]
    pub fn full_hp(&self) -> Result<BsFull, SolMathError> {
        crate::hp::bs_full_hp(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// `(call_delta, put_delta)`.
    #[inline]
    pub fn delta(&self) -> Result<(i128, i128), SolMathError> {
        bs_delta(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// Gamma.
    #[inline]
    pub fn gamma(&self) -> Result<i128, SolMathError> {
        bs_gamma(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// Vega.
    #[inline]
    pub fn vega(&self) -> Result<i128, SolMathError> {
        bs_vega(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// `(call_theta, put_theta)`.
    #[inline]
    pub fn theta(&self) -> Result<(i128, i128), SolMathError> {
        bs_theta(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// `(call_rho, put_rho)`.
    #[inline]
    pub fn rho(&self) -> Result<(i128, i128), SolMathError> {
        bs_rho(self.s.0, self.k.0, self.r.0, self.sigma.0, self.t.0)
    }

    /// The validated spot.
    #[inline]
    pub const fn spot(&self) -> Price {
        self.s
    }
    /// The validated strike.
    #[inline]
    pub const fn strike(&self) -> Price {
        self.k
    }
}

/// A validated implied-volatility problem: an observed market price plus the
/// contract parameters. [`ImpliedVolInputs::solve`] cannot panic; it returns
/// `Ok(sigma)` or `Err(NoConvergence)` for prices with no invertible volatility
/// (a mathematical outcome, not a failure).
#[cfg(feature = "iv")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ImpliedVolInputs {
    market_price: Price,
    s: Price,
    k: Price,
    r: Rate,
    t: Time,
}

#[cfg(feature = "iv")]
impl ImpliedVolInputs {
    /// Bundle already-validated components.
    #[inline]
    pub const fn new(market_price: Price, s: Price, k: Price, r: Rate, t: Time) -> Self {
        Self {
            market_price,
            s,
            k,
            r,
            t,
        }
    }

    /// Validate raw fixed-point instruction data in one shot.
    #[inline]
    pub const fn from_raw(
        market_price: u128,
        s: u128,
        k: u128,
        r: u128,
        t: u128,
    ) -> Result<Self, SolMathError> {
        let market_price = match Price::new(market_price) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let s = match Price::new(s) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let k = match Price::new(k) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let r = match Rate::new(r) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let t = match Time::new(t) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        Ok(Self {
            market_price,
            s,
            k,
            r,
            t,
        })
    }

    /// Solve for the implied volatility at SCALE.
    #[inline]
    pub fn solve(&self) -> Result<u128, SolMathError> {
        crate::iv::implied_vol(self.market_price.0, self.s.0, self.k.0, self.r.0, self.t.0)
    }
}

/// A validated European **barrier**-option parameter set: spot, strike, barrier
/// level, rate, volatility, and time. The barrier `h` is a [`Price`] like spot
/// and strike. Every method is guaranteed not to panic or silently wrap for any
/// in-domain input; degenerate corners fail closed with `Err`.
#[cfg(feature = "barrier")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BarrierInputs {
    s: Price,
    k: Price,
    h: Price,
    r: Rate,
    sigma: Vol,
    t: Time,
}

#[cfg(feature = "barrier")]
impl BarrierInputs {
    /// Bundle already-validated components.
    #[inline]
    pub const fn new(s: Price, k: Price, h: Price, r: Rate, sigma: Vol, t: Time) -> Self {
        Self {
            s,
            k,
            h,
            r,
            sigma,
            t,
        }
    }

    /// Validate raw fixed-point instruction data in one shot. Returns
    /// `Err(DomainError)` if any field is outside its certified range.
    #[inline]
    pub const fn from_raw(
        s: u128,
        k: u128,
        h: u128,
        r: u128,
        sigma: u128,
        t: u128,
    ) -> Result<Self, SolMathError> {
        let s = match Price::new(s) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let k = match Price::new(k) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let h = match Price::new(h) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let r = match Rate::new(r) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let sigma = match Vol::new(sigma) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let t = match Time::new(t) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        Ok(Self {
            s,
            k,
            h,
            r,
            sigma,
            t,
        })
    }

    /// Continuously-monitored barrier option price (fresh contract, barrier not
    /// yet breached). `barrier_type` selects knock-in/out and up/down.
    #[inline]
    pub fn price(
        &self,
        is_call: bool,
        barrier_type: crate::barrier::BarrierType,
    ) -> Result<crate::barrier::BarrierResult, SolMathError> {
        crate::barrier::barrier_option(
            self.s.0,
            self.k.0,
            self.h.0,
            self.r.0,
            self.sigma.0,
            self.t.0,
            is_call,
            barrier_type,
        )
    }

    /// Barrier option price given persisted breach state. Persist
    /// `barrier_was_breached` on-chain across observations.
    #[inline]
    pub fn price_with_state(
        &self,
        is_call: bool,
        barrier_type: crate::barrier::BarrierType,
        barrier_was_breached: bool,
    ) -> Result<crate::barrier::BarrierResult, SolMathError> {
        crate::barrier::barrier_option_with_state(
            self.s.0,
            self.k.0,
            self.h.0,
            self.r.0,
            self.sigma.0,
            self.t.0,
            is_call,
            barrier_type,
            barrier_was_breached,
        )
    }
}

/// A validated continuous arithmetic-Asian / partially fixed TWAP quote.
///
/// Unlike a vanilla European quote, TWAP validity is relational: the remaining
/// averaging window cannot exceed time to expiry, and the fixed average must be
/// present exactly when the fixed weight is non-zero. Construction checks those
/// relations once; [`Self::price`] then calls the raw kernel with the same
/// validated state.
#[cfg(feature = "asian")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TwapInputs {
    s: Price,
    k: Price,
    r: Rate,
    q: Rate,
    sigma: Vol,
    t: Time,
    averaging_time: u128,
    fixed_average: Price,
    fixed_weight: u128,
}

#[cfg(feature = "asian")]
impl TwapInputs {
    /// Validate raw fixed-point instruction data in one shot.
    #[allow(clippy::too_many_arguments)]
    pub const fn from_raw(
        s: u128,
        k: u128,
        r: u128,
        q: u128,
        sigma: u128,
        t: u128,
        averaging_time: u128,
        fixed_average: u128,
        fixed_weight: u128,
    ) -> Result<Self, SolMathError> {
        let s = match Price::new(s) {
            Ok(v) if v.get() > 0 => v,
            Ok(_) => return Err(SolMathError::DomainError),
            Err(e) => return Err(e),
        };
        let k = match Price::new(k) {
            Ok(v) if v.get() > 0 => v,
            Ok(_) => return Err(SolMathError::DomainError),
            Err(e) => return Err(e),
        };
        let r = match Rate::new(r) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let q = match Rate::new(q) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let sigma = match Vol::new(sigma) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let t = match Time::new(t) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        let fixed_average = match Price::new(fixed_average) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };

        if averaging_time > t.get() || fixed_weight > SCALE {
            return Err(SolMathError::DomainError);
        }
        if fixed_weight < SCALE && averaging_time == 0 {
            return Err(SolMathError::DomainError);
        }
        if fixed_weight == SCALE && averaging_time != 0 {
            return Err(SolMathError::DomainError);
        }
        if fixed_weight == 0 {
            if fixed_average.get() != 0 {
                return Err(SolMathError::DomainError);
            }
        } else if fixed_average.get() == 0 {
            return Err(SolMathError::DomainError);
        }

        Ok(Self {
            s,
            k,
            r,
            q,
            sigma,
            t,
            averaging_time,
            fixed_average,
            fixed_weight,
        })
    }

    /// Price the validated partially fixed TWAP state.
    #[inline]
    pub fn price(&self) -> Result<crate::asian::AsianOptionResult, SolMathError> {
        crate::asian::twap_option_price(
            self.s.0,
            self.k.0,
            self.r.0,
            self.q.0,
            self.sigma.0,
            self.t.0,
            self.averaging_time,
            self.fixed_average.0,
            self.fixed_weight,
        )
    }

    /// Remaining averaging-window length in years at `SCALE`.
    #[inline]
    pub const fn averaging_time(&self) -> u128 {
        self.averaging_time
    }

    /// Fraction of the final average already fixed, in `[0, SCALE]`.
    #[inline]
    pub const fn fixed_weight(&self) -> u128 {
        self.fixed_weight
    }

    /// Average of the already-fixed observations.
    #[inline]
    pub const fn fixed_average(&self) -> Price {
        self.fixed_average
    }
}

/// A validated weighted-pool swap request.
///
/// Unlike the option types, the pool's validity is *relational* — it depends on
/// the balance ratio `balance_in / (balance_in + amount_in) >= 0.01` and the
/// weight ratio `weight_in / weight_out <= 20`, not on independent per-field
/// bounds. [`PoolSwapInputs::from_raw`] checks the whole certified domain up
/// front, so holding a value proves the swap is quotable; [`Self::quote`] then
/// cannot fail for a domain reason and cannot panic. Rounding stays
/// protocol-favouring exactly as in [`crate::weighted_pool_swap`].
#[cfg(feature = "pool")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PoolSwapInputs {
    balance_in: u128,
    balance_out: u128,
    weight_in: u128,
    weight_out: u128,
    amount_in: u128,
    fee_rate: u128,
}

#[cfg(feature = "pool")]
impl PoolSwapInputs {
    /// Validate a raw swap against the certified pool domain. Mirrors the guard
    /// chain in [`crate::weighted_pool_swap`]: non-zero balances/weights, a fee
    /// in `[0, 100%]`, a weight ratio `w_in / w_out <= 20`, and a post-trade
    /// balance ratio `>= 0.01`. `amount_in == 0` is accepted (a no-op quote).
    pub fn from_raw(
        balance_in: u128,
        balance_out: u128,
        weight_in: u128,
        weight_out: u128,
        amount_in: u128,
        fee_rate: u128,
    ) -> Result<Self, SolMathError> {
        if weight_out == 0 || weight_in == 0 || balance_in == 0 || balance_out == 0 {
            return Err(SolMathError::DomainError);
        }
        if fee_rate > SCALE {
            return Err(SolMathError::DomainError);
        }
        // Weight ratio must not exceed 20 (exact-integer test, matching kernel).
        let weight_q = weight_in / weight_out;
        if weight_q > 20 || (weight_q == 20 && weight_in % weight_out != 0) {
            return Err(SolMathError::DomainError);
        }
        // Post-trade balance ratio must be >= 0.01, i.e. balance_in >=
        // ceil((balance_in + amount_in) / 100). amount_in == 0 trivially passes.
        if amount_in != 0 {
            let denominator = balance_in
                .checked_add(amount_in)
                .ok_or(SolMathError::Overflow)?;
            let min_balance = denominator / 100 + u128::from(denominator % 100 != 0);
            if balance_in < min_balance {
                return Err(SolMathError::DomainError);
            }
        }
        Ok(Self {
            balance_in,
            balance_out,
            weight_in,
            weight_out,
            amount_in,
            fee_rate,
        })
    }

    /// Quote the swap: returns `(net_output, fee)` at SCALE. Output rounds down
    /// and fee rounds up (protocol-favouring), matching the raw kernel exactly.
    #[inline]
    pub fn quote(&self) -> Result<(u128, u128), SolMathError> {
        crate::pool::weighted_pool_swap(
            self.balance_in,
            self.balance_out,
            self.weight_in,
            self.weight_out,
            self.amount_in,
            self.fee_rate,
        )
    }
}