1
1
//! Program state processor
2
2
3
3
use crate :: {
4
- self as spl_token_lending,
4
+ self as spl_token_lending, emit_log_event ,
5
5
error:: LendingError ,
6
6
instruction:: LendingInstruction ,
7
- logs:: { emit_log_event, PythOraclePriceUpdate , SwitchboardV1OraclePriceUpdate } ,
7
+ logs:: {
8
+ LogEventType , PythError , PythOraclePriceUpdate , SwitchboardError ,
9
+ SwitchboardV1OraclePriceUpdate ,
10
+ } ,
8
11
math:: { Decimal , Rate , TryAdd , TryDiv , TryMul , TrySub , WAD } ,
9
12
pyth,
10
13
state:: {
@@ -2153,15 +2156,24 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
2153
2156
. map_err ( |_| ProgramError :: InvalidAccountData ) ?;
2154
2157
2155
2158
if pyth_price. ptype != pyth:: PriceType :: Price {
2156
- msg ! ( "Oracle price type is invalid {}" , pyth_price. ptype as u8 ) ;
2159
+ emit_log_event ! ( & PythError {
2160
+ event_type: LogEventType :: PythOraclePriceUpdateType ,
2161
+ oracle_pubkey: * pyth_price_info. key,
2162
+ error_message: format!( "Oracle price type is invalid: {}" , pyth_price. ptype as u8 ) ,
2163
+ } ) ;
2164
+
2157
2165
return Err ( LendingError :: InvalidOracleConfig . into ( ) ) ;
2158
2166
}
2159
2167
2160
2168
if pyth_price. agg . status != pyth:: PriceStatus :: Trading {
2161
- msg ! (
2162
- "Oracle price status is invalid: {}" ,
2163
- pyth_price. agg. status as u8
2164
- ) ;
2169
+ emit_log_event ! ( & PythError {
2170
+ event_type: LogEventType :: PythOraclePriceUpdateType ,
2171
+ oracle_pubkey: * pyth_price_info. key,
2172
+ error_message: format!(
2173
+ "Oracle price status is invalid: {}" ,
2174
+ pyth_price. agg. status as u8
2175
+ ) ,
2176
+ } ) ;
2165
2177
return Err ( LendingError :: InvalidOracleConfig . into ( ) ) ;
2166
2178
}
2167
2179
@@ -2170,12 +2182,20 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
2170
2182
. checked_sub ( pyth_price. valid_slot )
2171
2183
. ok_or ( LendingError :: MathOverflow ) ?;
2172
2184
if slots_elapsed >= STALE_AFTER_SLOTS_ELAPSED {
2173
- msg ! ( "Pyth oracle price is stale" ) ;
2185
+ emit_log_event ! ( & PythError {
2186
+ event_type: LogEventType :: PythOraclePriceUpdateType ,
2187
+ oracle_pubkey: * pyth_price_info. key,
2188
+ error_message: format!( "Pyth oracle price is stale: {} slots old." , slots_elapsed) ,
2189
+ } ) ;
2174
2190
return Err ( LendingError :: InvalidOracleConfig . into ( ) ) ;
2175
2191
}
2176
2192
2177
2193
let price: u64 = pyth_price. agg . price . try_into ( ) . map_err ( |_| {
2178
- msg ! ( "Oracle price cannot be negative" ) ;
2194
+ emit_log_event ! ( & PythError {
2195
+ event_type: LogEventType :: PythOraclePriceUpdateType ,
2196
+ oracle_pubkey: * pyth_price_info. key,
2197
+ error_message: "Oracle price cannot be negative" . to_string( ) ,
2198
+ } ) ;
2179
2199
LendingError :: InvalidOracleConfig
2180
2200
} ) ?;
2181
2201
@@ -2186,11 +2206,15 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
2186
2206
// 100/confidence_ratio = maximum size of confidence range as a percent of price
2187
2207
// confidence_ratio of 10 filters out pyth prices with conf > 10% of price
2188
2208
if conf. checked_mul ( confidence_ratio) . unwrap ( ) > price {
2189
- msg ! (
2190
- "Oracle price confidence is too wide. price: {}, conf: {}" ,
2191
- price,
2192
- conf,
2193
- ) ;
2209
+ emit_log_event ! ( & PythError {
2210
+ event_type: LogEventType :: PythOraclePriceUpdateType ,
2211
+ oracle_pubkey: * pyth_price_info. key,
2212
+ error_message: format!(
2213
+ "Oracle price confidence is too wide. price: {}, conf: {}" ,
2214
+ price, conf
2215
+ ) ,
2216
+ } ) ;
2217
+
2194
2218
return Err ( LendingError :: InvalidOracleConfig . into ( ) ) ;
2195
2219
}
2196
2220
@@ -2215,10 +2239,11 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
2215
2239
. ok_or ( LendingError :: MathOverflow ) ?;
2216
2240
Decimal :: from ( price) . try_div ( decimals) ?
2217
2241
} ;
2218
- emit_log_event ( & PythOraclePriceUpdate {
2242
+ emit_log_event ! ( & PythOraclePriceUpdate {
2243
+ event_type: LogEventType :: PythOraclePriceUpdateType ,
2219
2244
oracle_pubkey: * pyth_price_info. key,
2220
2245
price: market_price,
2221
- conf : conf,
2246
+ confidence : conf,
2222
2247
published_slot: pyth_price. valid_slot,
2223
2248
} ) ;
2224
2249
@@ -2238,7 +2263,11 @@ fn get_switchboard_price(
2238
2263
let account_buf = switchboard_feed_info. try_borrow_data ( ) ?;
2239
2264
// first byte type discriminator
2240
2265
if account_buf[ 0 ] != SwitchboardAccountType :: TYPE_AGGREGATOR as u8 {
2241
- msg ! ( "switchboard address not of type aggregator" ) ;
2266
+ emit_log_event ! ( & SwitchboardError {
2267
+ event_type: LogEventType :: SwitchboardErrorType ,
2268
+ oracle_pubkey: * switchboard_feed_info. key,
2269
+ error_message: "Switchboard feed is not of type aggregator" . to_string( ) ,
2270
+ } ) ;
2242
2271
return Err ( LendingError :: InvalidAccountInput . into ( ) ) ;
2243
2272
}
2244
2273
@@ -2254,7 +2283,11 @@ fn get_switchboard_price(
2254
2283
. checked_sub ( open_slot)
2255
2284
. ok_or ( LendingError :: MathOverflow ) ?;
2256
2285
if slots_elapsed >= STALE_AFTER_SLOTS_ELAPSED {
2257
- msg ! ( "Switchboard oracle price is stale" ) ;
2286
+ emit_log_event ! ( & SwitchboardError {
2287
+ event_type: LogEventType :: SwitchboardErrorType ,
2288
+ oracle_pubkey: * switchboard_feed_info. key,
2289
+ error_message: format!( "Oracle price is stale by {} slots" , slots_elapsed) ,
2290
+ } ) ;
2258
2291
return Err ( LendingError :: InvalidOracleConfig . into ( ) ) ;
2259
2292
}
2260
2293
@@ -2266,7 +2299,8 @@ fn get_switchboard_price(
2266
2299
let price = ( ( price_quotient as f64 ) * price_float) as u128 ;
2267
2300
2268
2301
let market_price = Decimal :: from ( price) . try_div ( price_quotient) ?;
2269
- emit_log_event ( & SwitchboardV1OraclePriceUpdate {
2302
+ emit_log_event ! ( & SwitchboardV1OraclePriceUpdate {
2303
+ event_type: LogEventType :: SwitchboardV1OraclePriceUpdateType ,
2270
2304
oracle_pubkey: * switchboard_feed_info. key,
2271
2305
price: market_price,
2272
2306
published_slot: open_slot,
0 commit comments