Skip to content

Commit 0f557dd

Browse files
committed
Simplify stuff
1 parent d346d0b commit 0f557dd

21 files changed

+358
-74
lines changed

Cargo.lock

Lines changed: 9 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: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
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 {
8-
PythOraclePriceUpdateType,
9-
SwitchboardV1OraclePriceUpdateType,
6+
extern crate serde;
7+
extern crate serde_json;
8+
9+
#[derive(Debug, Serialize)]
10+
pub enum LogEventType {
11+
ObligationStateUpdate,
12+
ProgramVersion,
13+
PythError,
14+
PythOraclePriceUpdate,
15+
ReserveStateUpdate,
16+
SwitchboardError,
17+
SwitchboardV1OraclePriceUpdate,
1018
}
1119

1220
impl fmt::Display for LogEventType {
@@ -15,49 +23,92 @@ impl fmt::Display for LogEventType {
1523
}
1624
}
1725

18-
pub fn emit_log_event(e: &dyn LogEvent) {
19-
msg!("Solend Log Event");
20-
msg!(&e.to_string());
26+
fn pubkey_serialize<S>(x: &Pubkey, s: S) -> Result<S::Ok, S::Error>
27+
where
28+
S: serde::ser::Serializer,
29+
{
30+
s.serialize_str(&x.to_string())
2131
}
2232

23-
pub trait LogEvent {
24-
fn to_string(&self) -> String;
33+
#[macro_export]
34+
macro_rules! emit_log_event {
35+
($e:expr) => {
36+
msg!("solend-event-log:");
37+
msg!(&serde_json::to_string($e).unwrap());
38+
};
2539
}
2640

41+
#[derive(Serialize)]
2742
pub struct PythOraclePriceUpdate {
43+
pub event_type: LogEventType,
44+
#[serde(serialize_with = "pubkey_serialize")]
2845
pub oracle_pubkey: Pubkey,
2946
pub price: Decimal,
30-
pub conf: u64,
47+
pub confidence: u64,
3148
pub published_slot: u64,
3249
}
3350

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-
}
51+
#[derive(Serialize)]
52+
pub struct PythError {
53+
pub event_type: LogEventType,
54+
#[serde(serialize_with = "pubkey_serialize")]
55+
pub oracle_pubkey: Pubkey,
56+
pub error_message: String,
4557
}
4658

59+
#[derive(Serialize)]
4760
pub struct SwitchboardV1OraclePriceUpdate {
61+
pub event_type: LogEventType,
62+
#[serde(serialize_with = "pubkey_serialize")]
4863
pub oracle_pubkey: Pubkey,
4964
pub price: Decimal,
5065
pub published_slot: u64,
5166
}
5267

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-
}
68+
#[derive(Serialize)]
69+
pub struct SwitchboardError {
70+
pub event_type: LogEventType,
71+
#[serde(serialize_with = "pubkey_serialize")]
72+
pub oracle_pubkey: Pubkey,
73+
pub error_message: String,
74+
}
75+
76+
#[derive(Serialize)]
77+
pub struct ProgramVersion {
78+
pub event_type: LogEventType,
79+
pub version: u8,
80+
}
81+
82+
#[derive(Serialize)]
83+
pub struct ReserveStateUpdate {
84+
pub event_type: LogEventType,
85+
pub available_amount: u64,
86+
pub borrowed_amount_wads: Decimal,
87+
pub cumulative_borrow_rate_wads: Decimal,
88+
pub collateral_mint_total_supply: u64,
89+
pub collateral_exchange_rate: String,
90+
}
91+
92+
// ObligationStateUpdate intentionally does not contain the obligation ID
93+
// to save on compute since it is contained in the transaction itself.
94+
#[derive(Serialize)]
95+
pub struct ObligationStateUpdate {
96+
pub event_type: LogEventType,
97+
pub allowed_borrow_value: Decimal,
98+
pub unhealthy_borrow_value: Decimal,
99+
pub deposits: Vec<DepositLog>,
100+
pub borrows: Vec<BorrowLog>,
101+
}
102+
103+
#[derive(Serialize)]
104+
pub struct DepositLog {
105+
pub reserve_id_index: u8,
106+
pub deposited_amount: u64,
107+
}
108+
109+
#[derive(Serialize)]
110+
pub struct BorrowLog {
111+
pub reserve_id_index: u8,
112+
pub borrowed_amount_wads: Decimal,
113+
pub cumulative_borrow_rate_wads: Decimal,
63114
}

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 {

0 commit comments

Comments
 (0)