Skip to content

Commit a307171

Browse files
committed
Simplify stuff
1 parent 7b4be89 commit a307171

21 files changed

+424
-73
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.

test_output

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Running: cargo-build-bpf --manifest-path /root/repos/solana-program-library/token-lending/program/Cargo.toml --bpf-out-dir /root/repos/solana-program-library/target/deploy
2+
BPF SDK: /root/.local/share/solana/install/releases/1.8.16/solana-release/bin/sdk/bpf
3+
cargo-build-bpf child: rustup toolchain list -v
4+
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
5+
cargo-build-bpf child: /root/.local/share/solana/install/releases/1.8.16/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /root/repos/solana-program-library/target/deploy/spl_token_lending.so
6+
7+
To deploy this program:
8+
$ solana program deploy /root/repos/solana-program-library/target/deploy/spl_token_lending.so
9+
The program address will default to this keypair (override with --program-id):
10+
/root/repos/solana-program-library/target/deploy/spl_token_lending-keypair.json
11+
Running: /root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo test --manifest-path /root/repos/solana-program-library/token-lending/program/Cargo.toml --test borrow_obligation_liquidity --features test-bpf
12+
13+
running 5 tests
14+
test test_borrow_too_large ... ok
15+
test test_borrow_usdc_fixed_amount ... FAILED
16+
test test_borrow_sol_max_amount ... ok
17+
test test_borrow_max_reserves ... ok
18+
test test_borrow_limit ... ok
19+
20+
failures:
21+
22+
---- test_borrow_usdc_fixed_amount stdout ----
23+
thread 'test_borrow_usdc_fixed_amount' panicked at 'assertion failed: banks_client.process_transaction(transaction).await.is_ok()', token-lending/program/tests/borrow_obligation_liquidity.rs:120:5
24+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
25+
26+
27+
failures:
28+
test_borrow_usdc_fixed_amount
29+
30+
test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.93s
31+

token-lending/program/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ 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"
27+
arrform = "0.1.1"
2428

2529
[dev-dependencies]
2630
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: 106 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,115 @@ 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())
31+
}
32+
33+
fn decimal_serialize<S>(x: &Decimal, s: S) -> Result<S::Ok, S::Error>
34+
where
35+
S: serde::ser::Serializer,
36+
{
37+
s.serialize_str(&x.to_string())
2138
}
2239

23-
pub trait LogEvent {
24-
fn to_string(&self) -> String;
40+
#[macro_export]
41+
macro_rules! emit_log_event {
42+
($e:expr) => {
43+
msg!("solend-event-log:");
44+
msg!(&serde_json::to_string($e).unwrap());
45+
};
2546
}
2647

48+
#[derive(Serialize)]
2749
pub struct PythOraclePriceUpdate {
50+
pub event_type: LogEventType,
51+
#[serde(serialize_with = "pubkey_serialize")]
2852
pub oracle_pubkey: Pubkey,
53+
#[serde(serialize_with = "decimal_serialize")]
2954
pub price: Decimal,
30-
pub conf: u64,
55+
pub confidence: u64,
3156
pub published_slot: u64,
3257
}
3358

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-
}
59+
#[derive(Serialize)]
60+
pub struct PythError {
61+
pub event_type: LogEventType,
62+
#[serde(serialize_with = "pubkey_serialize")]
63+
pub oracle_pubkey: Pubkey,
64+
pub error_message: String,
4565
}
4666

67+
#[derive(Serialize)]
4768
pub struct SwitchboardV1OraclePriceUpdate {
69+
pub event_type: LogEventType,
70+
#[serde(serialize_with = "pubkey_serialize")]
4871
pub oracle_pubkey: Pubkey,
72+
#[serde(serialize_with = "decimal_serialize")]
4973
pub price: Decimal,
5074
pub published_slot: u64,
5175
}
5276

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-
}
77+
#[derive(Serialize)]
78+
pub struct SwitchboardError {
79+
pub event_type: LogEventType,
80+
#[serde(serialize_with = "pubkey_serialize")]
81+
pub oracle_pubkey: Pubkey,
82+
pub error_message: String,
83+
}
84+
85+
#[derive(Serialize)]
86+
pub struct ProgramVersion {
87+
pub event_type: LogEventType,
88+
pub version: u8,
89+
}
90+
91+
#[derive(Serialize)]
92+
pub struct ReserveStateUpdate {
93+
pub event_type: LogEventType,
94+
#[serde(serialize_with = "pubkey_serialize")]
95+
pub reserve_id: Pubkey,
96+
pub available_amount: u64,
97+
#[serde(serialize_with = "decimal_serialize")]
98+
pub borrowed_amount_wads: Decimal,
99+
#[serde(serialize_with = "decimal_serialize")]
100+
pub cumulative_borrow_rate_wads: Decimal,
101+
pub collateral_mint_total_supply: u64,
102+
pub collateral_exchange_rate: String,
103+
}
104+
105+
#[derive(Serialize)]
106+
pub struct ObligationStateUpdate {
107+
pub event_type: LogEventType,
108+
#[serde(serialize_with = "pubkey_serialize")]
109+
pub obligation_id: Pubkey,
110+
#[serde(serialize_with = "decimal_serialize")]
111+
pub allowed_borrow_value: Decimal,
112+
#[serde(serialize_with = "decimal_serialize")]
113+
pub unhealthy_borrow_value: Decimal,
114+
pub deposits: Vec<DepositLog>,
115+
pub borrows: Vec<BorrowLog>,
116+
}
117+
118+
#[derive(Serialize)]
119+
pub struct DepositLog {
120+
#[serde(serialize_with = "pubkey_serialize")]
121+
pub reserve_id: Pubkey,
122+
pub deposited_amount: u64,
123+
#[serde(serialize_with = "decimal_serialize")]
124+
pub market_value: Decimal,
125+
}
126+
127+
#[derive(Serialize)]
128+
pub struct BorrowLog {
129+
#[serde(serialize_with = "pubkey_serialize")]
130+
pub reserve_id: Pubkey,
131+
#[serde(serialize_with = "decimal_serialize")]
132+
pub cumulative_borrow_rate_wads: Decimal,
133+
#[serde(serialize_with = "decimal_serialize")]
134+
pub borrowed_amount_wads: Decimal,
135+
#[serde(serialize_with = "decimal_serialize")]
136+
pub market_value: Decimal,
63137
}

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)