forked from SO4-Markets/so4-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprice.rs
More file actions
112 lines (95 loc) · 3.39 KB
/
Copy pathprice.rs
File metadata and controls
112 lines (95 loc) · 3.39 KB
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
/// Errors produced by price-source validation and aggregation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OracleError {
/// A price of zero was supplied where a positive value is required.
ZeroPrice,
/// A negative price was supplied; oracle prices must be strictly positive.
NegativePrice,
}
impl core::fmt::Display for OracleError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
OracleError::ZeroPrice => write!(f, "price must be positive, got zero"),
OracleError::NegativePrice => write!(f, "price must be positive, got negative value"),
}
}
}
/// A price source that always returns a single fixed value.
///
/// The value must be strictly positive (`> 0`). Attempting to construct a
/// `FixedSource` with zero or a negative value returns an [`OracleError`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixedSource {
price: i128,
}
impl FixedSource {
/// Create a `FixedSource` from a raw `i128` price.
///
/// Returns `Err(OracleError::ZeroPrice)` when `price == 0`, and
/// `Err(OracleError::NegativePrice)` when `price < 0`.
pub fn new(price: i128) -> Result<Self, OracleError> {
if price < 0 {
return Err(OracleError::NegativePrice);
}
if price == 0 {
return Err(OracleError::ZeroPrice);
}
Ok(Self { price })
}
/// Return the fixed price.
pub fn price(&self) -> i128 {
self.price
}
}
impl std::str::FromStr for FixedSource {
type Err = OracleError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let price: i128 = s.trim().parse().map_err(|_| OracleError::ZeroPrice)?;
Self::new(price)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
// ── FixedSource::new ───────────────────────────────────────────────────────
#[test]
fn fixed_source_rejects_zero() {
assert_eq!(FixedSource::new(0), Err(OracleError::ZeroPrice));
}
#[test]
fn fixed_source_rejects_negative_one() {
assert_eq!(FixedSource::new(-1), Err(OracleError::NegativePrice));
}
#[test]
fn fixed_source_rejects_large_negative() {
assert_eq!(
FixedSource::new(-1_000_000),
Err(OracleError::NegativePrice)
);
}
#[test]
fn fixed_source_accepts_positive() {
let src = FixedSource::new(1_000_000).unwrap();
assert_eq!(src.price(), 1_000_000);
}
// ── FixedSource::from_str ─────────────────────────────────────────────────
#[test]
fn fixed_source_from_str_rejects_zero_string() {
assert_eq!(FixedSource::from_str("0"), Err(OracleError::ZeroPrice));
}
#[test]
fn fixed_source_from_str_rejects_negative_string() {
assert_eq!(FixedSource::from_str("-1"), Err(OracleError::NegativePrice));
}
#[test]
fn fixed_source_from_str_accepts_valid_string() {
let src = FixedSource::from_str("500").unwrap();
assert_eq!(src.price(), 500);
}
#[test]
fn fixed_source_from_str_handles_whitespace() {
let src = FixedSource::from_str(" 42 ").unwrap();
assert_eq!(src.price(), 42);
}
}