Skip to content

Commit 3da84ac

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

File tree

7 files changed

+35
-43
lines changed

7 files changed

+35
-43
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: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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,
912
SwitchboardV1OraclePriceUpdateType,
1013
}
@@ -15,49 +18,27 @@ impl fmt::Display for LogEventType {
1518
}
1619
}
1720

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;
21+
#[macro_export]
22+
macro_rules! emit_log_event {
23+
($e:expr) => {
24+
msg!("solend-event-log");
25+
msg!(&serde_json::to_string($e).unwrap());
26+
};
2527
}
2628

29+
#[derive(Serialize)]
2730
pub struct PythOraclePriceUpdate {
31+
pub event_type: LogEventType,
2832
pub oracle_pubkey: Pubkey,
2933
pub price: Decimal,
30-
pub conf: u64,
34+
pub confidence: u64,
3135
pub published_slot: u64,
3236
}
3337

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-
}
45-
}
46-
38+
#[derive(Serialize)]
4739
pub struct SwitchboardV1OraclePriceUpdate {
40+
pub event_type: LogEventType,
4841
pub oracle_pubkey: Pubkey,
4942
pub price: Decimal,
5043
pub published_slot: u64,
5144
}
52-
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-
}
63-
}

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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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::{LogEventType, PythOraclePriceUpdate, SwitchboardV1OraclePriceUpdate},
88
math::{Decimal, Rate, TryAdd, TryDiv, TryMul, TrySub, WAD},
99
pyth,
1010
state::{
@@ -2215,10 +2215,11 @@ fn get_pyth_price(pyth_price_info: &AccountInfo, clock: &Clock) -> Result<Decima
22152215
.ok_or(LendingError::MathOverflow)?;
22162216
Decimal::from(price).try_div(decimals)?
22172217
};
2218-
emit_log_event(&PythOraclePriceUpdate {
2218+
emit_log_event!(&PythOraclePriceUpdate {
2219+
event_type: LogEventType::PythOraclePriceUpdateType,
22192220
oracle_pubkey: *pyth_price_info.key,
22202221
price: market_price,
2221-
conf: conf,
2222+
confidence: conf,
22222223
published_slot: pyth_price.valid_slot,
22232224
});
22242225

@@ -2266,7 +2267,8 @@ fn get_switchboard_price(
22662267
let price = ((price_quotient as f64) * price_float) as u128;
22672268

22682269
let market_price = Decimal::from(price).try_div(price_quotient)?;
2269-
emit_log_event(&SwitchboardV1OraclePriceUpdate {
2270+
emit_log_event!(&SwitchboardV1OraclePriceUpdate {
2271+
event_type: LogEventType::SwitchboardV1OraclePriceUpdateType,
22702272
oracle_pubkey: *switchboard_feed_info.key,
22712273
price: market_price,
22722274
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)