forked from SO4-Markets/so4-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed.rs
More file actions
176 lines (158 loc) · 5.63 KB
/
Copy pathfixed.rs
File metadata and controls
176 lines (158 loc) · 5.63 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
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
use shared_config::TokenConfig;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FixedPriceError {
MissingFixedPrice,
InvalidFixedPrice(String),
}
impl std::fmt::Display for FixedPriceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::MissingFixedPrice => f.write_str("fixed price is not configured"),
Self::InvalidFixedPrice(price) => write!(f, "invalid fixed price: {price}"),
}
}
}
impl std::error::Error for FixedPriceError {}
impl crate::retry::Retryable for FixedPriceError {
fn is_retryable(&self) -> bool {
// Config/parse errors are permanent failures
false
}
}
pub fn fixed_price(token: &TokenConfig) -> Result<i128, FixedPriceError> {
let raw = token
.fixed_price
.as_deref()
.ok_or(FixedPriceError::MissingFixedPrice)?;
let price = raw
.parse::<i128>()
.map_err(|_| FixedPriceError::InvalidFixedPrice(raw.to_string()))?;
if price <= 0 {
return Err(FixedPriceError::InvalidFixedPrice(raw.to_string()));
}
Ok(price)
}
#[cfg(test)]
mod tests {
use super::*;
fn token_with_fixed_price(fixed_price: Option<&str>) -> TokenConfig {
TokenConfig {
symbol: "TUSDC".to_string(),
display_symbol: Some("USDC".to_string()),
stellar_address: "CADDR".to_string(),
sources: vec!["fixed".to_string()],
binance_symbol: None,
coinbase_symbol: None,
pyth_feed_id: None,
fixed_price: fixed_price.map(|s| s.to_string()),
min_sources: 1,
max_deviation_bps: 100,
stale_after_seconds: 60,
submit_threshold_bps: 10,
min: 0.0,
max: 0.0,
sources_used: vec![],
}
}
#[test]
fn parses_configured_fixed_price() {
let token = token_with_fixed_price(Some("1000000000000000000000000000000"));
assert_eq!(
fixed_price(&token).unwrap(),
1_000_000_000_000_000_000_000_000_000_000
);
}
#[test]
fn rejects_missing_fixed_price() {
let token = token_with_fixed_price(None);
assert_eq!(
fixed_price(&token).unwrap_err(),
FixedPriceError::MissingFixedPrice
);
}
#[test]
fn rejects_non_numeric_fixed_price() {
let token = token_with_fixed_price(Some("not-a-number"));
assert!(matches!(
fixed_price(&token).unwrap_err(),
FixedPriceError::InvalidFixedPrice(_)
));
}
#[test]
fn rejects_zero_fixed_price() {
let token = token_with_fixed_price(Some("0"));
assert!(matches!(
fixed_price(&token).unwrap_err(),
FixedPriceError::InvalidFixedPrice(_)
));
}
#[test]
fn rejects_negative_fixed_price() {
let token = token_with_fixed_price(Some("-1000000000000000000000000000000"));
assert!(matches!(
fixed_price(&token).unwrap_err(),
FixedPriceError::InvalidFixedPrice(_)
));
}
#[test]
fn rejects_fixed_price_of_negative_one() {
let token = token_with_fixed_price(Some("-1"));
assert!(matches!(
fixed_price(&token).unwrap_err(),
FixedPriceError::InvalidFixedPrice(_)
));
}
#[test]
fn accepts_smallest_valid_fixed_price() {
let token = token_with_fixed_price(Some("1"));
assert_eq!(fixed_price(&token).unwrap(), 1);
}
#[test]
fn rejects_abc_string_with_invalid_fixed_price_error() {
let token = token_with_fixed_price(Some("abc"));
assert_eq!(
fixed_price(&token).unwrap_err(),
FixedPriceError::InvalidFixedPrice("abc".to_string()),
);
}
// #571 — non-numeric parse failure must surface the raw input in the error
// message, not a synthetic "0" that would imply the value was a valid
// zero-price rather than an unparseable string.
#[test]
fn non_numeric_parse_error_display_names_raw_input_not_zero() {
let token = token_with_fixed_price(Some("not-a-number"));
let err = fixed_price(&token).unwrap_err();
assert!(
!matches!(err, FixedPriceError::MissingFixedPrice),
"non-numeric input must not produce MissingFixedPrice"
);
let msg = err.to_string();
assert!(
msg.contains("not-a-number"),
"error display must name the raw input; got: {msg}"
);
assert!(
!msg.contains(": 0"),
"error must not mislabel a non-numeric input as a zero-price; got: {msg}"
);
}
// #370 — fixed_price parses the configured i128 string and returns it exactly
#[test]
fn issue_370_fixed_source_returns_configured_value() {
// The oracle internally stores prices scaled to 30 decimal places.
// A USDC-pegged token fixed at 1.0 would be configured as
// "1000000000000000000000000000000" (1 followed by 30 zeros = 10^30).
let configured = "1000000000000000000000000000000";
let token = token_with_fixed_price(Some(configured));
let result = fixed_price(&token).unwrap();
assert_eq!(
result,
configured.parse::<i128>().unwrap(),
"fixed_price must return the configured i128 value unchanged"
);
// Also verify a different concrete value parses correctly.
let token2 = token_with_fixed_price(Some("42000000000000000000000000000000"));
let result2 = fixed_price(&token2).unwrap();
assert_eq!(result2, 42_000_000_000_000_000_000_000_000_000_000_i128);
}
}