Skip to content

Commit a62dead

Browse files
committed
Simplify stuff
t# Please enter the commit message for your changes. Lines starting
1 parent 7b4be89 commit a62dead

File tree

7 files changed

+93
-53
lines changed

7 files changed

+93
-53
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token-lending/program/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ spl-token = { version = "3.2.0", features=["no-entrypoint"] }
2121
switchboard-program = "0.2.0"
2222
thiserror = "1.0"
2323
uint = "=0.9.0"
24+
serde = "1.0"
25+
serde_derive = "1.0"
26+
serde_json = "1.0"
2427

2528
[dev-dependencies]
2629
assert_matches = "1.5.0"

token-lending/program/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub mod state;
1414
// Export current sdk types for downstream users building with a different sdk version
1515
pub use solana_program;
1616

17+
#[macro_use]
18+
extern crate serde_derive;
19+
1720
solana_program::declare_id!("So1endDq2YkqhipRh3WViPa8hdiSpxWy6z3Z6tMCpAo");
1821

1922
/// Canonical null pubkey. Prints out as "nu11111111111111111111111111111111111111111"

token-lending/program/src/logs.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#![allow(missing_docs)]
22
use crate::math::Decimal;
3-
use solana_program::{msg, pubkey::Pubkey};
3+
use solana_program::pubkey::Pubkey;
44
use std::fmt;
55

6-
#[derive(Debug)]
7-
enum LogEventType {
6+
extern crate serde;
7+
extern crate serde_json;
8+
9+
#[derive(Debug, Serialize)]
10+
pub enum LogEventType {
811
PythOraclePriceUpdateType,
12+
PythErrorType,
913
SwitchboardV1OraclePriceUpdateType,
14+
SwitchboardErrorType,
1015
}
1116

1217
impl fmt::Display for LogEventType {
@@ -15,49 +20,41 @@ impl fmt::Display for LogEventType {
1520
}
1621
}
1722

18-
pub fn emit_log_event(e: &dyn LogEvent) {
19-
msg!("Solend Log Event");
20-
msg!(&e.to_string());
21-
}
22-
23-
pub trait LogEvent {
24-
fn to_string(&self) -> String;
23+
#[macro_export]
24+
macro_rules! emit_log_event {
25+
($e:expr) => {
26+
msg!("solend-event-log");
27+
msg!(&serde_json::to_string($e).unwrap());
28+
};
2529
}
2630

31+
#[derive(Serialize)]
2732
pub struct PythOraclePriceUpdate {
33+
pub event_type: LogEventType,
2834
pub oracle_pubkey: Pubkey,
2935
pub price: Decimal,
30-
pub conf: u64,
36+
pub confidence: u64,
3137
pub published_slot: u64,
3238
}
3339

34-
impl LogEvent for PythOraclePriceUpdate {
35-
fn to_string(&self) -> String {
36-
return format!(
37-
"{},{},{},{},{}",
38-
LogEventType::PythOraclePriceUpdateType.to_string(),
39-
self.oracle_pubkey.to_string(),
40-
self.price.to_string(),
41-
self.conf.to_string(),
42-
self.published_slot,
43-
);
44-
}
40+
#[derive(Serialize)]
41+
pub struct PythError {
42+
pub event_type: LogEventType,
43+
pub oracle_pubkey: Pubkey,
44+
pub error_message: String,
4545
}
4646

47+
#[derive(Serialize)]
4748
pub struct SwitchboardV1OraclePriceUpdate {
49+
pub event_type: LogEventType,
4850
pub oracle_pubkey: Pubkey,
4951
pub price: Decimal,
5052
pub published_slot: u64,
5153
}
5254

53-
impl LogEvent for SwitchboardV1OraclePriceUpdate {
54-
fn to_string(&self) -> String {
55-
return format!(
56-
"{},{},{},{}",
57-
LogEventType::SwitchboardV1OraclePriceUpdateType.to_string(),
58-
self.oracle_pubkey.to_string(),
59-
self.price.to_string(),
60-
self.published_slot,
61-
);
62-
}
55+
#[derive(Serialize)]
56+
pub struct SwitchboardError {
57+
pub event_type: LogEventType,
58+
pub oracle_pubkey: Pubkey,
59+
pub error_message: String,
6360
}

token-lending/program/src/math/decimal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ use uint::construct_uint;
2222

2323
// U192 with 192 bits consisting of 3 x 64-bit words
2424
construct_uint! {
25+
#[derive(Serialize)]
2526
pub struct U192(3);
2627
}
2728

2829
/// Large decimal values, precise to 18 digits
29-
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Eq, Ord)]
30+
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Eq, Ord, Serialize)]
3031
pub struct Decimal(pub U192);
3132

3233
impl Decimal {

token-lending/program/src/processor.rs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! Program state processor
22
33
use crate::{
4-
self as spl_token_lending,
4+
self as spl_token_lending, emit_log_event,
55
error::LendingError,
66
instruction::LendingInstruction,
7-
logs::{emit_log_event, PythOraclePriceUpdate, SwitchboardV1OraclePriceUpdate},
7+
logs::{
8+
LogEventType, PythError, PythOraclePriceUpdate, SwitchboardError,
9+
SwitchboardV1OraclePriceUpdate,
10+
},
811
math::{Decimal, Rate, TryAdd, TryDiv, TryMul, TrySub, WAD},
912
pyth,
1013
state::{
@@ -2153,15 +2156,24 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
21532156
.map_err(|_| ProgramError::InvalidAccountData)?;
21542157

21552158
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+
21572165
return Err(LendingError::InvalidOracleConfig.into());
21582166
}
21592167

21602168
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+
});
21652177
return Err(LendingError::InvalidOracleConfig.into());
21662178
}
21672179

@@ -2170,12 +2182,20 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
21702182
.checked_sub(pyth_price.valid_slot)
21712183
.ok_or(LendingError::MathOverflow)?;
21722184
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+
});
21742190
return Err(LendingError::InvalidOracleConfig.into());
21752191
}
21762192

21772193
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+
});
21792199
LendingError::InvalidOracleConfig
21802200
})?;
21812201

@@ -2186,11 +2206,15 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
21862206
// 100/confidence_ratio = maximum size of confidence range as a percent of price
21872207
// confidence_ratio of 10 filters out pyth prices with conf > 10% of price
21882208
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+
21942218
return Err(LendingError::InvalidOracleConfig.into());
21952219
}
21962220

@@ -2215,10 +2239,11 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
22152239
.ok_or(LendingError::MathOverflow)?;
22162240
Decimal::from(price).try_div(decimals)?
22172241
};
2218-
emit_log_event(&PythOraclePriceUpdate {
2242+
emit_log_event!(&PythOraclePriceUpdate {
2243+
event_type: LogEventType::PythOraclePriceUpdateType,
22192244
oracle_pubkey: *pyth_price_info.key,
22202245
price: market_price,
2221-
conf: conf,
2246+
confidence: conf,
22222247
published_slot: pyth_price.valid_slot,
22232248
});
22242249

@@ -2238,7 +2263,11 @@ fn get_switchboard_price(
22382263
let account_buf = switchboard_feed_info.try_borrow_data()?;
22392264
// first byte type discriminator
22402265
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+
});
22422271
return Err(LendingError::InvalidAccountInput.into());
22432272
}
22442273

@@ -2254,7 +2283,11 @@ fn get_switchboard_price(
22542283
.checked_sub(open_slot)
22552284
.ok_or(LendingError::MathOverflow)?;
22562285
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+
});
22582291
return Err(LendingError::InvalidOracleConfig.into());
22592292
}
22602293

@@ -2266,7 +2299,8 @@ fn get_switchboard_price(
22662299
let price = ((price_quotient as f64) * price_float) as u128;
22672300

22682301
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,
22702304
oracle_pubkey: *switchboard_feed_info.key,
22712305
price: market_price,
22722306
published_slot: open_slot,

token-lending/program/tests/refresh_reserve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async fn test_success() {
2525
);
2626

2727
// limit to track compute unit increase
28-
test.set_bpf_compute_max_units(16_000);
28+
test.set_bpf_compute_max_units(24_000);
2929

3030
const SOL_RESERVE_LIQUIDITY_LAMPORTS: u64 = 100 * LAMPORTS_TO_SOL;
3131
const USDC_RESERVE_LIQUIDITY_FRACTIONAL: u64 = 100 * FRACTIONAL_TO_USDC;

0 commit comments

Comments
 (0)