Skip to content

Commit 7b4be89

Browse files
committed
Log price update
1 parent 8ae6f4b commit 7b4be89

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

token-lending/program/src/logs.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
#![allow(missing_docs)]
2-
use solana_program::{
3-
msg,
4-
pubkey::Pubkey,
5-
};
6-
use std::{fmt};
7-
use crate::{
8-
pyth,
9-
math::{Decimal},
10-
};
11-
2+
use crate::math::Decimal;
3+
use solana_program::{msg, pubkey::Pubkey};
4+
use std::fmt;
125

136
#[derive(Debug)]
147
enum LogEventType {
158
PythOraclePriceUpdateType,
169
SwitchboardV1OraclePriceUpdateType,
1710
}
1811

19-
impl fmt::Display for LogEventType{
12+
impl fmt::Display for LogEventType {
2013
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2114
write!(f, "{:?}", self)
2215
}
@@ -27,43 +20,40 @@ pub fn emit_log_event(e: &dyn LogEvent) {
2720
msg!(&e.to_string());
2821
}
2922

30-
3123
pub trait LogEvent {
3224
fn to_string(&self) -> String;
3325
}
3426

3527
pub struct PythOraclePriceUpdate {
36-
oracle_pubkey: Pubkey,
37-
price: i64,
38-
conf: u64,
39-
status: pyth::PriceStatus,
40-
published_slot: u64,
28+
pub oracle_pubkey: Pubkey,
29+
pub price: Decimal,
30+
pub conf: u64,
31+
pub published_slot: u64,
4132
}
4233

4334
impl LogEvent for PythOraclePriceUpdate {
4435
fn to_string(&self) -> String {
4536
return format!(
46-
"{},{},{},{},{},{}",
37+
"{},{},{},{},{}",
4738
LogEventType::PythOraclePriceUpdateType.to_string(),
4839
self.oracle_pubkey.to_string(),
4940
self.price.to_string(),
5041
self.conf.to_string(),
51-
self.status.to_string(),
5242
self.published_slot,
5343
);
5444
}
5545
}
5646

5747
pub struct SwitchboardV1OraclePriceUpdate {
58-
oracle_pubkey: Pubkey,
59-
price: Decimal,
60-
published_slot: u64,
48+
pub oracle_pubkey: Pubkey,
49+
pub price: Decimal,
50+
pub published_slot: u64,
6151
}
6252

6353
impl LogEvent for SwitchboardV1OraclePriceUpdate {
6454
fn to_string(&self) -> String {
6555
return format!(
66-
"{},{},{},{}",
56+
"{},{},{},{}",
6757
LogEventType::SwitchboardV1OraclePriceUpdateType.to_string(),
6858
self.oracle_pubkey.to_string(),
6959
self.price.to_string(),

token-lending/program/src/processor.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
self as spl_token_lending,
55
error::LendingError,
66
instruction::LendingInstruction,
7-
logs::{emit_log_event},
7+
logs::{emit_log_event, PythOraclePriceUpdate, SwitchboardV1OraclePriceUpdate},
88
math::{Decimal, Rate, TryAdd, TryDiv, TryMul, TrySub, WAD},
99
pyth,
1010
state::{
@@ -2215,6 +2215,12 @@ 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 {
2219+
oracle_pubkey: *pyth_price_info.key,
2220+
price: market_price,
2221+
conf: conf,
2222+
published_slot: pyth_price.valid_slot,
2223+
});
22182224

22192225
Ok(market_price)
22202226
}
@@ -2242,10 +2248,10 @@ fn get_switchboard_price(
22422248
// return Err(LendingError::InvalidAccountInput.into());
22432249
// }
22442250
let round_result: RoundResult = get_aggregator_result(&aggregator)?;
2245-
2251+
let open_slot = round_result.round_open_slot.unwrap();
22462252
let slots_elapsed = clock
22472253
.slot
2248-
.checked_sub(round_result.round_open_slot.unwrap())
2254+
.checked_sub(open_slot)
22492255
.ok_or(LendingError::MathOverflow)?;
22502256
if slots_elapsed >= STALE_AFTER_SLOTS_ELAPSED {
22512257
msg!("Switchboard oracle price is stale");
@@ -2259,7 +2265,13 @@ fn get_switchboard_price(
22592265
let price_quotient = 10u64.pow(9);
22602266
let price = ((price_quotient as f64) * price_float) as u128;
22612267

2262-
Decimal::from(price).try_div(price_quotient)
2268+
let market_price = Decimal::from(price).try_div(price_quotient)?;
2269+
emit_log_event(&SwitchboardV1OraclePriceUpdate {
2270+
oracle_pubkey: *switchboard_feed_info.key,
2271+
price: market_price,
2272+
published_slot: open_slot,
2273+
});
2274+
Ok(market_price)
22632275
}
22642276

22652277
/// Issue a spl_token `InitializeAccount` instruction.

token-lending/program/src/pyth.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use bytemuck::{
44
cast_slice, cast_slice_mut, from_bytes, from_bytes_mut, try_cast_slice, try_cast_slice_mut,
55
Pod, PodCastError, Zeroable,
66
};
7-
use std::{
8-
mem::size_of,
9-
fmt,
10-
};
7+
use std::mem::size_of;
118

129
pub const MAGIC: u32 = 0xa1b2c3d4;
1310
pub const VERSION_2: u32 = 2;
@@ -32,7 +29,7 @@ pub enum AccountType {
3229
Price,
3330
}
3431

35-
#[derive(PartialEq, Copy, Clone, Debug)]
32+
#[derive(PartialEq, Copy, Clone)]
3633
#[repr(C)]
3734
pub enum PriceStatus {
3835
Unknown,
@@ -41,13 +38,6 @@ pub enum PriceStatus {
4138
Auction,
4239
}
4340

44-
impl fmt::Display for PriceStatus{
45-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46-
write!(f, "{:?}", self)
47-
}
48-
}
49-
50-
5141
#[derive(PartialEq, Copy, Clone)]
5242
#[repr(C)]
5343
pub enum CorpAction {

0 commit comments

Comments
 (0)