hylo-core 0.5.0

Core protocol data types, math, and utilities.
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
//! Oracle-derived collateral rebalancing price curves.
//!
//! Computes the price at which collateral trades against USDC based on
//! the protocol's current collateral ratio (CR) and oracle price range.
//!
//! Two independent curves:
//! * **Sell side** (low CR, 1.0–1.35): protocol sells collateral for USDC
//! * **Buy side** (high CR, 1.65–2.0+): protocol buys collateral with USDC

use anchor_lang::prelude::*;
use fix::prelude::*;

use crate::error::CoreError;
use crate::interp::{FixInterp, Point};
use crate::pyth::OraclePrice;

/// Confidence interval multipliers for rebalance price curve construction.
#[derive(
  Copy, Clone, Debug, PartialEq, InitSpace, AnchorSerialize, AnchorDeserialize,
)]
pub struct RebalanceCurveConfig {
  floor_mult: UFixValue64,
  ceil_mult: UFixValue64,
}

impl RebalanceCurveConfig {
  #[must_use]
  pub fn new(
    floor_mult: UFixValue64,
    ceil_mult: UFixValue64,
  ) -> RebalanceCurveConfig {
    RebalanceCurveConfig {
      floor_mult,
      ceil_mult,
    }
  }

  /// Converts floor CI multiplier to `UFix64`.
  ///
  /// # Errors
  /// * Conversion fails
  pub fn floor_mult(&self) -> Result<UFix64<N2>> {
    self.floor_mult.try_into()
  }

  /// Converts ceil CI multiplier to `UFix64`.
  ///
  /// # Errors
  /// * Conversion fails
  pub fn ceil_mult(&self) -> Result<UFix64<N2>> {
    self.ceil_mult.try_into()
  }

  /// Checks both multipliers parse and are nonzero.
  ///
  /// # Errors
  /// * Multiplier has incorrect precision or is zero
  pub fn validate(self) -> Result<Self> {
    let valid =
      self.floor_mult()? > UFix64::zero() && self.ceil_mult()? > UFix64::zero();
    valid
      .then_some(self)
      .ok_or(CoreError::RebalanceCurveConfigValidation.into())
  }
}

// CR domain boundaries.
const CR_1_20: IFix64<N9> = IFix64::constant(1_200_000_000);
const CR_1_35: IFix64<N9> = IFix64::constant(1_350_000_000);
const CR_1_65: IFix64<N9> = IFix64::constant(1_650_000_000);
const CR_1_75: IFix64<N9> = IFix64::constant(1_750_000_000);

/// Convert unsigned CR to signed for curve lookup.
///
/// # Errors
/// * Conversion overflow
fn narrow_cr(cr: UFix64<N9>) -> Result<IFix64<N9>> {
  cr.narrow::<i64>()
    .ok_or(CoreError::RebalancePriceConversion.into())
}

/// Convert unsigned oracle price to signed for curve storage.
///
/// # Errors
/// * Conversion overflow
fn narrow_price(price: UFix64<N9>) -> Result<IFix64<N9>> {
  price
    .narrow::<i64>()
    .ok_or(CoreError::RebalancePriceConversion.into())
}

/// Scales confidence interval by multiplier.
///
/// # Errors
/// * Arithmetic overflow
fn scale_ci(ci: UFix64<N9>, mult: UFix64<N2>) -> Result<UFix64<N9>> {
  ci.mul_div_ceil(mult, UFix64::<N2>::one())
    .ok_or(CoreError::RebalancePriceConstruction.into())
}

/// Interpolated rebalance price controller.
/// Implementors define boundary behavior via [`price_inner`].
pub trait RebalancePriceController {
  /// Reference to the underlying interpolator.
  fn curve(&self) -> &FixInterp<2, N9>;

  /// Compute price for CR from underlying curve with boundary handling.
  ///
  /// # Errors
  /// * Domain, arithmetic, or route-inactive errors.
  fn price_inner(&self, cr: IFix64<N9>) -> Result<IFix64<N9>>;

  /// Collateral price at the given CR.
  ///
  /// # Errors
  /// * CR conversion
  /// * Domain
  /// * Route inactive
  fn price(&self, ucr: UFix64<N9>) -> Result<UFix64<N9>> {
    let cr = narrow_cr(ucr)?;
    self
      .price_inner(cr)?
      .narrow()
      .ok_or(CoreError::RebalancePriceConversion.into())
  }

  /// Validate curve invariants after construction.
  ///
  /// # Errors
  /// * Invariant violation (e.g. non-positive prices, floor >= ceil).
  fn validate(self) -> Result<Self>
  where
    Self: Sized;
}

/// Sell side rebalance pricing curve.
/// Active when CR is low (below 1.35).
#[derive(Debug, Clone)]
pub struct SellPriceCurve {
  curve: FixInterp<2, N9>,
}

impl SellPriceCurve {
  /// Construct sell side price curve.
  ///
  /// # Errors
  /// * Arithmetic underflow/overflow
  /// * Conversion overflow
  pub fn new(
    OraclePrice { spot, conf }: OraclePrice,
    config: &RebalanceCurveConfig,
  ) -> Result<SellPriceCurve> {
    let (floor, ceil) = spot
      .checked_sub(&scale_ci(conf, config.floor_mult()?)?)
      .zip(spot.checked_add(&scale_ci(conf, config.ceil_mult()?)?))
      .ok_or(CoreError::RebalancePriceConstruction)?;
    let curve = FixInterp::from_points([
      Point {
        x: CR_1_20,
        y: narrow_price(floor)?,
      },
      Point {
        x: CR_1_35,
        y: narrow_price(ceil)?,
      },
    ])?;
    SellPriceCurve { curve }.validate()
  }
}

impl RebalancePriceController for SellPriceCurve {
  fn curve(&self) -> &FixInterp<2, N9> {
    &self.curve
  }

  fn price_inner(&self, cr: IFix64<N9>) -> Result<IFix64<N9>> {
    let interp = self.curve();
    if cr < interp.x_min() {
      Ok(interp.y_min())
    } else if cr > interp.x_max() {
      Err(CoreError::RebalanceSellInactive.into())
    } else {
      interp.interpolate(cr)
    }
  }

  fn validate(self) -> Result<SellPriceCurve> {
    let interp = self.curve();
    (interp.y_min() > IFix64::zero() && interp.y_min() < interp.y_max())
      .then_some(self)
      .ok_or(CoreError::RebalancePriceConstruction.into())
  }
}

/// Buy-side rebalance pricing curve.
/// Active when CR is high (above 1.65).
#[derive(Debug, Clone)]
pub struct BuyPriceCurve {
  curve: FixInterp<2, N9>,
}

impl BuyPriceCurve {
  /// Construct buy side price curve.
  ///
  /// # Errors
  /// * Arithmetic underflow/overflow
  /// * Precision conversion
  pub fn new(
    OraclePrice { spot, conf }: OraclePrice,
    config: &RebalanceCurveConfig,
  ) -> Result<BuyPriceCurve> {
    let (floor, ceil) = spot
      .checked_sub(&scale_ci(conf, config.floor_mult()?)?)
      .zip(spot.checked_add(&scale_ci(conf, config.ceil_mult()?)?))
      .ok_or(CoreError::RebalancePriceConstruction)?;
    let curve = FixInterp::from_points([
      Point {
        x: CR_1_65,
        y: narrow_price(floor)?,
      },
      Point {
        x: CR_1_75,
        y: narrow_price(ceil)?,
      },
    ])?;
    BuyPriceCurve { curve }.validate()
  }
}

impl RebalancePriceController for BuyPriceCurve {
  fn curve(&self) -> &FixInterp<2, N9> {
    &self.curve
  }

  fn price_inner(&self, cr: IFix64<N9>) -> Result<IFix64<N9>> {
    let interp = self.curve();
    if cr < interp.x_min() {
      Err(CoreError::RebalanceBuyInactive.into())
    } else if cr > interp.x_max() {
      Ok(interp.y_max())
    } else {
      interp.interpolate(cr)
    }
  }

  fn validate(self) -> Result<BuyPriceCurve> {
    let interp = self.curve();
    (interp.y_min() > IFix64::zero() && interp.y_min() < interp.y_max())
      .then_some(self)
      .ok_or(CoreError::RebalancePriceConstruction.into())
  }
}

#[cfg(test)]
mod tests {
  use more_asserts::*;
  use proptest::prelude::*;

  use super::*;
  use crate::error::CoreError;
  use crate::pyth::OraclePrice;

  const ORACLE: OraclePrice = OraclePrice {
    spot: UFix64::constant(146_401_109_370),
    conf: UFix64::constant(94_635_820),
  };

  const SELL_CONFIG: RebalanceCurveConfig = RebalanceCurveConfig {
    floor_mult: UFixValue64 { bits: 200, exp: -2 },
    ceil_mult: UFixValue64 { bits: 100, exp: -2 },
  };

  const BUY_CONFIG: RebalanceCurveConfig = RebalanceCurveConfig {
    floor_mult: UFixValue64 { bits: 100, exp: -2 },
    ceil_mult: UFixValue64 { bits: 100, exp: -2 },
  };

  const UCR_1_00: UFix64<N9> = UFix64::constant(1_000_000_000);
  const UCR_1_15: UFix64<N9> = UFix64::constant(1_150_000_000);
  const UCR_1_20: UFix64<N9> = UFix64::constant(1_200_000_000);
  const UCR_1_35: UFix64<N9> = UFix64::constant(1_350_000_000);
  const UCR_1_40: UFix64<N9> = UFix64::constant(1_400_000_000);
  const UCR_1_60: UFix64<N9> = UFix64::constant(1_600_000_000);
  const UCR_1_65: UFix64<N9> = UFix64::constant(1_650_000_000);
  const UCR_1_75: UFix64<N9> = UFix64::constant(1_750_000_000);
  const UCR_1_80: UFix64<N9> = UFix64::constant(1_800_000_000);
  const UCR_2_50: UFix64<N9> = UFix64::constant(2_500_000_000);

  #[test]
  fn sell_constructs() -> anyhow::Result<()> {
    SellPriceCurve::new(ORACLE, &SELL_CONFIG)?;
    Ok(())
  }

  #[test]
  fn buy_constructs() -> anyhow::Result<()> {
    BuyPriceCurve::new(ORACLE, &BUY_CONFIG)?;
    Ok(())
  }

  #[test]
  fn sell_rejects_negative_ci() {
    let huge_ci = OraclePrice {
      conf: ORACLE.spot,
      ..ORACLE
    };
    let res = SellPriceCurve::new(huge_ci, &SELL_CONFIG);
    assert_eq!(
      res.err(),
      Some(CoreError::RebalancePriceConstruction.into())
    );
  }

  #[test]
  fn buy_rejects_negative_ci() {
    let huge_ci = OraclePrice {
      conf: ORACLE.spot,
      ..ORACLE
    };
    let res = BuyPriceCurve::new(huge_ci, &BUY_CONFIG);
    assert_eq!(
      res.err(),
      Some(CoreError::RebalancePriceConstruction.into())
    );
  }

  #[test]
  fn sell_flat_below_domain() -> anyhow::Result<()> {
    let curve = SellPriceCurve::new(ORACLE, &SELL_CONFIG)?;
    assert_eq!(curve.price(UCR_1_00)?, curve.price(UCR_1_15)?);
    Ok(())
  }

  #[test]
  fn sell_inactive_above_domain() -> anyhow::Result<()> {
    let curve = SellPriceCurve::new(ORACLE, &SELL_CONFIG)?;
    assert_eq!(
      curve.price(UCR_1_40).err(),
      Some(CoreError::RebalanceSellInactive.into())
    );
    Ok(())
  }

  #[test]
  fn sell_endpoints() -> anyhow::Result<()> {
    let curve = SellPriceCurve::new(ORACLE, &SELL_CONFIG)?;
    let at_floor = curve.price(UCR_1_20)?;
    let at_ceil = curve.price(UCR_1_35)?;
    assert_lt!(at_floor, at_ceil);
    assert_eq!(at_floor, curve.price(UCR_1_00)?);
    Ok(())
  }

  #[test]
  fn buy_inactive_below_domain() -> anyhow::Result<()> {
    let curve = BuyPriceCurve::new(ORACLE, &BUY_CONFIG)?;
    assert_eq!(
      curve.price(UCR_1_60).err(),
      Some(CoreError::RebalanceBuyInactive.into())
    );
    Ok(())
  }

  #[test]
  fn buy_flat_above_domain() -> anyhow::Result<()> {
    let curve = BuyPriceCurve::new(ORACLE, &BUY_CONFIG)?;
    assert_eq!(curve.price(UCR_1_80)?, curve.price(UCR_2_50)?);
    Ok(())
  }

  #[test]
  fn buy_endpoints() -> anyhow::Result<()> {
    let curve = BuyPriceCurve::new(ORACLE, &BUY_CONFIG)?;
    let at_floor = curve.price(UCR_1_65)?;
    let at_ceil = curve.price(UCR_1_75)?;
    assert_lt!(at_floor, at_ceil);
    assert_eq!(at_ceil, curve.price(UCR_2_50)?);
    Ok(())
  }

  fn sell_cr() -> BoxedStrategy<UFix64<N9>> {
    (1_000_000_000u64..1_350_000_000)
      .prop_map(UFix64::new)
      .boxed()
  }

  fn buy_cr() -> BoxedStrategy<UFix64<N9>> {
    (1_650_000_000u64..4_000_000_000)
      .prop_map(UFix64::new)
      .boxed()
  }

  fn oracle_spot() -> BoxedStrategy<UFix64<N9>> {
    (10_000_000_000u64..1_000_000_000_000)
      .prop_map(UFix64::new)
      .boxed()
  }

  fn oracle_ci() -> BoxedStrategy<UFix64<N9>> {
    (10_000u64..500_000_000).prop_map(UFix64::new).boxed()
  }

  proptest! {
    #[test]
    fn sell_price_valid(
      cr in sell_cr(),
      spot in oracle_spot(),
      conf in oracle_ci(),
    ) {
      let oracle = OraclePrice { spot, conf };
      if let Ok(curve) = SellPriceCurve::new(oracle, &SELL_CONFIG) {
        let price = curve
          .price(cr)
          .map_err(|e| TestCaseError::fail(format!("{e}")))?;
        if let (Some(floor), Some(ceil)) = (
          curve.curve().y_min().narrow(),
          curve.curve().y_max().narrow(),
        ) {
          prop_assert!(price >= floor && price <= ceil);
        } else {
          Err(TestCaseError::fail("floor/ceil narrow"))?;
        }
      }
    }

    #[test]
    fn buy_price_valid(
      cr in buy_cr(),
      spot in oracle_spot(),
      conf in oracle_ci(),
    ) {
      let oracle = OraclePrice { spot, conf };
      if let Ok(curve) = BuyPriceCurve::new(oracle, &BUY_CONFIG) {
        let price = curve
          .price(cr)
          .map_err(|e| TestCaseError::fail(format!("{e}")))?;
        if let (Some(floor), Some(ceil)) = (
          curve.curve().y_min().narrow(),
          curve.curve().y_max().narrow(),
        ) {
          prop_assert!(price >= floor && price <= ceil);
        } else {
          Err(TestCaseError::fail("floor/ceil narrow"))?;
        }
      }
    }
  }
}