hylo-core 2.3.8

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
use anchor_spl::token::Mint;
use fix::prelude::*;
#[cfg(feature = "offchain")]
use fix::typenum::Integer;

use crate::error::CoreError;
use crate::error::CoreError::ExoAmountNormalization;

/// Largest `x` with `floor(x * num / den) <= cap`.
///
/// ```txt
/// x = ceil((cap + 1) * den / num) - 1
/// ```
#[cfg(feature = "offchain")]
#[must_use]
pub fn max_scaled_input<Exp: Integer, RExp: Integer>(
  cap: UFix64<Exp>,
  num: UFix64<RExp>,
  den: UFix64<RExp>,
) -> Option<UFix64<Exp>> {
  let atom = UFix64::new(1);
  if num == UFix64::zero() {
    None
  } else {
    cap
      .checked_add(&atom)
      .and_then(|first_output_over| first_output_over.mul_div_ceil(den, num))
      .map_or(Some(UFix64::new(u64::MAX)), |first_input_over| {
        first_input_over.checked_sub(&atom)
      })
  }
}

/// Bridges runtime mint decimals to typed `UFix64<N9>`.
///
/// # Errors
/// * Unsupported decimal count or conversion overflow
pub fn normalize_mint_exp(
  mint: &Mint,
  amount: u64,
) -> Result<UFix64<N9>, CoreError> {
  match mint.decimals {
    2 => UFix64::<N2>::new(amount).checked_convert(),
    3 => UFix64::<N3>::new(amount).checked_convert(),
    4 => UFix64::<N4>::new(amount).checked_convert(),
    5 => UFix64::<N5>::new(amount).checked_convert(),
    6 => UFix64::<N6>::new(amount).checked_convert(),
    7 => UFix64::<N7>::new(amount).checked_convert(),
    8 => UFix64::<N8>::new(amount).checked_convert(),
    9 => Some(UFix64::<N9>::new(amount)),
    10 => UFix64::<N10>::new(amount).checked_convert(),
    _ => None,
  }
  .ok_or(ExoAmountNormalization)
}

/// Converts typed `UFix64<N9>` back to a raw `u64` in the mint's native
/// decimals.
///
/// # Errors
/// * Unsupported decimal count
pub fn denormalize_mint_exp(
  mint: &Mint,
  amount: UFix64<N9>,
) -> Result<u64, CoreError> {
  match mint.decimals {
    2 => amount.checked_convert::<N2>().map(|o| o.bits),
    3 => amount.checked_convert::<N3>().map(|o| o.bits),
    4 => amount.checked_convert::<N4>().map(|o| o.bits),
    5 => amount.checked_convert::<N5>().map(|o| o.bits),
    6 => amount.checked_convert::<N6>().map(|o| o.bits),
    7 => amount.checked_convert::<N7>().map(|o| o.bits),
    8 => amount.checked_convert::<N8>().map(|o| o.bits),
    9 => Some(amount.bits),
    10 => amount.checked_convert::<N10>().map(|o| o.bits),
    _ => None,
  }
  .ok_or(ExoAmountNormalization)
}

/// Converts typed `UFix64<N9>` back to a raw `u64` in the mint's native
/// decimals, rounding up.
///
/// When splitting one normalized amount, ceil exactly one part and floor
/// the rest so the denormalized parts sum to the original amount.
///
/// # Errors
/// * Unsupported decimal count
pub fn denormalize_mint_exp_ceil(
  mint: &Mint,
  amount: UFix64<N9>,
) -> Result<u64, CoreError> {
  match mint.decimals {
    2 => amount.checked_convert_ceil::<N2>().map(|o| o.bits),
    3 => amount.checked_convert_ceil::<N3>().map(|o| o.bits),
    4 => amount.checked_convert_ceil::<N4>().map(|o| o.bits),
    5 => amount.checked_convert_ceil::<N5>().map(|o| o.bits),
    6 => amount.checked_convert_ceil::<N6>().map(|o| o.bits),
    7 => amount.checked_convert_ceil::<N7>().map(|o| o.bits),
    8 => amount.checked_convert_ceil::<N8>().map(|o| o.bits),
    9 => Some(amount.bits),
    10 => amount.checked_convert_ceil::<N10>().map(|o| o.bits),
    _ => None,
  }
  .ok_or(ExoAmountNormalization)
}

#[macro_export]
macro_rules! eq_tolerance {
  ($l:expr, $r:expr, $place:ty, $tol:expr) => {{
    let diff = $l.convert::<$place>().abs_diff(&$r.convert::<$place>());
    diff <= $tol
  }};
}

#[cfg(test)]
pub mod proptest {
  use fix::prelude::*;
  use proptest::prelude::*;

  use crate::exchange_math::collateral_ratio;
  use crate::pyth::PriceRange;

  /// Represents a possible state of the protocol, collateral, and tokens.
  /// Always holds the Hylo invariant: `ns * ps = nx * px + nh * ph`.
  #[derive(Debug)]
  pub struct ProtocolState {
    pub usd_sol_price: UFix64<N9>,
    pub stablecoin_amount: UFix64<N6>,
    pub stablecoin_nav: UFix64<N9>,
    pub levercoin_amount: UFix64<N6>,
    pub levercoin_nav: UFix64<N9>,
  }

  impl ProtocolState {
    #[must_use]
    pub fn total_sol(&self) -> Option<UFix64<N9>> {
      let stablecoin_cap = self
        .stablecoin_amount
        .mul_div_floor(self.stablecoin_nav, UFix64::one())?;
      let levercoin_cap = self
        .levercoin_amount
        .mul_div_floor(self.levercoin_nav, UFix64::one())?;
      let tvl = stablecoin_cap.checked_add(&levercoin_cap)?;
      tvl
        .convert()
        .mul_div_floor(UFix64::one(), self.usd_sol_price)
    }

    #[must_use]
    pub fn collateral_ratio(&self) -> Option<UFix64<N9>> {
      collateral_ratio(
        self.total_sol()?,
        self.usd_sol_price,
        self.stablecoin_amount,
      )
      .ok()
    }

    #[must_use]
    pub fn next_target_collateral_ratio(&self) -> Option<UFix64<N9>> {
      let current = self.collateral_ratio()?;
      let one = UFix64::<N9>::one();
      let next = current.mul_div_ceil(UFix64::new(900_000_000), one)?;
      if next < one {
        None
      } else {
        Some(next)
      }
    }
  }

  prop_compose! {
    /// Fixed stablecoin NAV at $1 only.
    pub fn protocol_state(_: ())
      (usd_sol_price in usd_sol_price(),
      stablecoin_amount in token_amount(),
      levercoin_amount in token_amount(),
      levercoin_nav in levercoin_nav()) -> ProtocolState {
       ProtocolState {
         usd_sol_price,
         stablecoin_amount,
         stablecoin_nav: UFix64::one(),
         levercoin_amount,
         levercoin_nav,
       }
    }
  }

  prop_compose! {
    /// Makes it possible to have depegged stablecoin NAV in the state.
    pub fn protocol_state_depeg(_: ())
      (usd_sol_price in usd_sol_price(),
      stablecoin_amount in token_amount(),
      stablecoin_nav in stablecoin_nav(),
      levercoin_amount in token_amount(),
      levercoin_nav in levercoin_nav()) -> ProtocolState {
       ProtocolState {
         usd_sol_price,
         stablecoin_amount,
         stablecoin_nav,
         levercoin_amount,
         levercoin_nav,
       }
    }
  }

  pub fn usd_sol_price() -> BoxedStrategy<UFix64<N9>> {
    (10_000_000_000u64..2_500_000_000_000u64)
      .prop_map(UFix64::new)
      .boxed()
  }

  pub fn token_amount() -> BoxedStrategy<UFix64<N6>> {
    (1_0000u64..5_000_000_000_000u64)
      .prop_map(UFix64::new)
      .boxed()
  }

  pub fn stablecoin_nav() -> BoxedStrategy<UFix64<N9>> {
    (800_000_000u64..1_000_000_000u64)
      .prop_map(UFix64::new)
      .boxed()
  }

  pub fn levercoin_nav() -> BoxedStrategy<UFix64<N9>> {
    (100_000u64..1_000_000_000_000u64)
      .prop_map(UFix64::new)
      .boxed()
  }

  /// Realistic LST/SOL price between 1.0 and 5.0.
  pub fn lst_sol_price() -> BoxedStrategy<UFix64<N9>> {
    (1_000_000_000u64..5_000_000_000u64)
      .prop_map(UFix64::new)
      .boxed()
  }

  /// `PriceRange` centered at $1 with a tight symbolic confidence interval.
  pub fn dollar_centered_price_range() -> BoxedStrategy<PriceRange<N9>> {
    (1u64..5_000_000u64)
      .prop_filter_map("from_conf within range", |conf| {
        PriceRange::from_conf(UFix64::<N9>::one(), UFix64::new(conf)).ok()
      })
      .boxed()
  }

  /// Extreme LST/SOL price including depeg.
  pub fn lst_sol_price_extreme() -> BoxedStrategy<UFix64<N9>> {
    (1u64..u64::MAX / 2).prop_map(UFix64::new).boxed()
  }

  /// Realistic LST amount: dust to 100K tokens.
  /// Upper bound prevents overflow in conversions with high prices.
  pub fn lst_amount() -> BoxedStrategy<UFix64<N9>> {
    (1_000u64..100_000_000_000_000u64)
      .prop_map(UFix64::new)
      .boxed()
  }

  /// Extreme LST amount from 1 unit to half of max.
  pub fn lst_amount_extreme() -> BoxedStrategy<UFix64<N9>> {
    (1u64..u64::MAX / 2).prop_map(UFix64::new).boxed()
  }
}

#[cfg(test)]
mod tests {
  use anchor_lang::prelude::program_option::COption;
  use anchor_lang::AccountDeserialize;
  use anchor_spl::token::spl_token::solana_program::program_pack::Pack;
  use anchor_spl::token::spl_token::state::Mint as SplMint;
  use anchor_spl::token::Mint;
  use anyhow::Result;
  use fix::aliases::si::{Micro, Nano};
  use fix::prelude::*;
  use proptest::prelude::*;

  #[cfg(feature = "offchain")]
  use super::max_scaled_input;
  use super::{
    denormalize_mint_exp, denormalize_mint_exp_ceil, normalize_mint_exp,
  };
  use crate::asset_swap_config::AssetSwapConfig;
  use crate::error::CoreError::SlippageExceeded;
  use crate::fees::controller::FeeExtract;
  use crate::slippage_config::SlippageConfig;

  fn test_mint(decimals: u8) -> Result<Mint> {
    let spl_mint = SplMint {
      mint_authority: COption::None,
      supply: 0,
      decimals,
      is_initialized: true,
      freeze_authority: COption::None,
    };
    let mut buf = [0u8; SplMint::LEN];
    SplMint::pack(spl_mint, &mut buf)?;
    Ok(Mint::try_deserialize(&mut buf.as_slice())?)
  }

  #[test]
  fn one_nano() {
    let one = Nano::<u64>::one();
    assert_eq!("1000000000x10^-9", format!("{one:?}"));
  }

  #[cfg(feature = "offchain")]
  #[test]
  fn max_scaled_input_saturates_on_unbounded_cap() {
    let kept = UFix64::<N4>::one() - UFix64::<N4>::new(1);
    let bound =
      max_scaled_input(UFix64::<N6>::new(u64::MAX), kept, UFix64::one());
    assert_eq!(bound, Some(UFix64::<N6>::new(u64::MAX)));
  }

  #[cfg(feature = "offchain")]
  #[test]
  fn max_input_saturates_on_unbounded_cap() {
    let one_bps = UFix64::<N4>::new(1);
    let bound = FeeExtract::max_input(one_bps, UFix64::<N6>::new(u64::MAX));
    assert_eq!(bound, Ok(UFix64::<N6>::new(u64::MAX)));
  }

  #[cfg(feature = "offchain")]
  #[test]
  fn max_scaled_input_exact_within_range() {
    let half = UFix64::<N4>::new(5000);
    let bound =
      max_scaled_input(UFix64::<N6>::new(100), half, UFix64::<N4>::one());
    assert_eq!(bound, Some(UFix64::<N6>::new(201)));
  }

  #[cfg(feature = "offchain")]
  #[test]
  fn max_scaled_input_none_on_zero_num() {
    let bound = max_scaled_input(
      UFix64::<N6>::new(100),
      UFix64::<N4>::zero(),
      UFix64::one(),
    );
    assert_eq!(bound, None);
  }

  #[test]
  fn precision6_unit() {
    let one = Micro::<u64>::one();
    let unit = one * one;
    assert_eq!(one, unit.convert());
  }

  #[test]
  fn precision6_overflow_guard() {
    let max = Micro::new(u128::MAX);
    assert!(max.checked_mul(&max).is_none());
  }

  #[test]
  fn neg_sub_underflows() {
    let last = Nano::new(169_120_000_u64);
    let now = Nano::new(151_444_800_u64);
    let sub = now.checked_sub(&last);
    assert!(sub.is_none());
  }

  #[test]
  fn slippage_neg() {
    let config =
      SlippageConfig::new(UFix64::<N6>::new(1_201_346), UFix64::new(20));
    let amount = UFix64::<N6>::new(1_198_942);
    let out = config.validate_token_out(amount);
    assert_eq!(out, Err(SlippageExceeded));
  }

  #[test]
  fn slippage_pos() {
    let config =
      SlippageConfig::new(UFix64::<N6>::new(99_411_501), UFix64::new(10));
    let amount = UFix64::<N6>::new(99_312_089);
    let out = config.validate_token_out(amount);
    assert!(out.is_ok());
  }

  proptest! {
    #[test]
    fn fee_split_denorm_conserves(
      amount in 1u64..1_000_000_000_000,
      decimals in 2u8..=9,
      fee_bps in 1u64..=100,
    ) {
      let mint = test_mint(decimals).expect("mint");

      // Normalize
      let norm = normalize_mint_exp(&mint, amount).expect("normalize");

      // Extract fees
      let config = AssetSwapConfig::new(UFixValue64::new(fee_bps, -4))
        .expect("config");
      let FeeExtract {
        fees_extracted,
        amount_remaining,
      } = config.apply_fee(norm).expect("apply_fee");

      // Denormalize, rounding the fee up and the remainder down
      let fee_out =
        denormalize_mint_exp_ceil(&mint, fees_extracted).expect("ceil");
      let amount_out =
        denormalize_mint_exp(&mint, amount_remaining).expect("floor");

      // Operation is lossless
      prop_assert_eq!(fee_out + amount_out, amount);
    }
  }
}