Skip to content

Commit ffe9735

Browse files
committed
check switchboard program id
1 parent 20f4107 commit ffe9735

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

token-lending/cli/src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ type Error = Box<dyn std::error::Error>;
8080
type CommandResult = Result<(), Error>;
8181

8282
const PYTH_PROGRAM_ID: &str = "gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s";
83-
// const SWITCHBOARD_PROGRAM_ID: &str = "DtmE9D2CSB4L5D6A15mraeEjrGMm6auWVzgaD8hK2tZM";
83+
const SWITCHBOARD_PROGRAM_ID: &str = "DtmE9D2CSB4L5D6A15mraeEjrGMm6auWVzgaD8hK2tZM";
84+
const SWITCHBOARD_PROGRAM_ID_DEV: &str = "7azgmy1pFXHikv36q1zZASvFq5vFa39TT9NweVugKKTU";
8485

8586
fn main() {
8687
solana_logger::setup_with_default("solana=info");
@@ -165,6 +166,16 @@ fn main() {
165166
.default_value(PYTH_PROGRAM_ID)
166167
.help("Oracle (Pyth) program ID for quoting market prices"),
167168
)
169+
.arg(
170+
Arg::with_name("switchboard_oracle_program_id")
171+
.long("switchboard-oracle")
172+
.validator(is_pubkey)
173+
.value_name("PUBKEY")
174+
.takes_value(true)
175+
.required(true)
176+
.default_value(SWITCHBOARD_PROGRAM_ID_DEV)
177+
.help("Oracle (switchboard) program ID for quoting market prices"),
178+
)
168179
.arg(
169180
Arg::with_name("quote_currency")
170181
.long("quote")
@@ -557,11 +568,14 @@ fn main() {
557568
let lending_market_owner = pubkey_of(arg_matches, "lending_market_owner").unwrap();
558569
let quote_currency = quote_currency_of(arg_matches, "quote_currency").unwrap();
559570
let oracle_program_id = pubkey_of(arg_matches, "oracle_program_id").unwrap();
571+
let switchboard_oracle_program_id = pubkey_of(arg_matches, "switchboard_oracle_program_id").unwrap();
572+
560573
command_create_lending_market(
561574
&config,
562575
lending_market_owner,
563576
quote_currency,
564577
oracle_program_id,
578+
switchboard_oracle_program_id
565579
)
566580
}
567581
("add-reserve", Some(arg_matches)) => {
@@ -679,6 +693,7 @@ fn command_create_lending_market(
679693
lending_market_owner: Pubkey,
680694
quote_currency: [u8; 32],
681695
oracle_program_id: Pubkey,
696+
switchboard_oracle_program_id: Pubkey,
682697
) -> CommandResult {
683698
let lending_market_keypair = Keypair::new();
684699
println!(
@@ -707,6 +722,7 @@ fn command_create_lending_market(
707722
quote_currency,
708723
lending_market_keypair.pubkey(),
709724
oracle_program_id,
725+
switchboard_oracle_program_id,
710726
),
711727
],
712728
Some(&config.fee_payer.pubkey()),

token-lending/program/src/instruction.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub enum LendingInstruction {
2525
/// 1. `[]` Rent sysvar.
2626
/// 2. `[]` Token program id.
2727
/// 3. `[]` Oracle program id.
28+
/// 4. `[]` Switchboard Oracle program id.
2829
InitLendingMarket {
2930
/// Owner authority which can add new reserves
3031
owner: Pubkey,
@@ -694,6 +695,7 @@ pub fn init_lending_market(
694695
quote_currency: [u8; 32],
695696
lending_market_pubkey: Pubkey,
696697
oracle_program_id: Pubkey,
698+
switchboard_oracle_program_id: Pubkey,
697699
) -> Instruction {
698700
Instruction {
699701
program_id,
@@ -702,6 +704,7 @@ pub fn init_lending_market(
702704
AccountMeta::new_readonly(sysvar::rent::id(), false),
703705
AccountMeta::new_readonly(spl_token::id(), false),
704706
AccountMeta::new_readonly(oracle_program_id, false),
707+
AccountMeta::new_readonly(switchboard_oracle_program_id, false),
705708
],
706709
data: LendingInstruction::InitLendingMarket {
707710
owner,

token-lending/program/src/processor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use solana_program::{
2727
};
2828
use spl_token::solana_program::instruction::AccountMeta;
2929
use spl_token::state::{Account, Mint};
30-
use std::convert::TryInto;
30+
use std::{convert::TryInto};
3131
use std::result::Result;
3232
use switchboard_program::{
3333
get_aggregator, get_aggregator_result, AggregatorState, RoundResult, SwitchboardAccountType,
@@ -139,6 +139,7 @@ fn process_init_lending_market(
139139
let rent = &Rent::from_account_info(next_account_info(account_info_iter)?)?;
140140
let token_program_id = next_account_info(account_info_iter)?;
141141
let oracle_program_id = next_account_info(account_info_iter)?;
142+
let switchboard_oracle_program_id = next_account_info(account_info_iter)?;
142143

143144
assert_rent_exempt(rent, lending_market_info)?;
144145
let mut lending_market = assert_uninitialized::<LendingMarket>(lending_market_info)?;
@@ -153,6 +154,7 @@ fn process_init_lending_market(
153154
quote_currency,
154155
token_program_id: *token_program_id.key,
155156
oracle_program_id: *oracle_program_id.key,
157+
switchboard_oracle_program_id: *switchboard_oracle_program_id.key,
156158
});
157159
LendingMarket::pack(lending_market, &mut lending_market_info.data.borrow_mut())?;
158160

@@ -300,6 +302,10 @@ fn process_init_reserve(
300302
return Err(LendingError::InvalidOracleConfig.into());
301303
}
302304

305+
if &lending_market.switchboard_oracle_program_id != switchboard_feed_info.owner {
306+
msg!("Pyth price account provided is not owned by the lending market oracle program");
307+
return Err(LendingError::InvalidOracleConfig.into());
308+
}
303309
let market_price = get_price(switchboard_feed_info, pyth_price_info, clock)?;
304310

305311
let authority_signer_seeds = &[

token-lending/program/src/state/lending_market.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub struct LendingMarket {
2323
pub token_program_id: Pubkey,
2424
/// Oracle (Pyth) program id
2525
pub oracle_program_id: Pubkey,
26+
/// Oracle (Switchboard) program id
27+
pub switchboard_oracle_program_id: Pubkey,
2628
}
2729

2830
impl LendingMarket {
@@ -41,6 +43,7 @@ impl LendingMarket {
4143
self.quote_currency = params.quote_currency;
4244
self.token_program_id = params.token_program_id;
4345
self.oracle_program_id = params.oracle_program_id;
46+
self.switchboard_oracle_program_id = params.switchboard_oracle_program_id;
4447
}
4548
}
4649

@@ -57,6 +60,8 @@ pub struct InitLendingMarketParams {
5760
pub token_program_id: Pubkey,
5861
/// Oracle (Pyth) program id
5962
pub oracle_program_id: Pubkey,
63+
/// Oracle (Switchboard) program id
64+
pub switchboard_oracle_program_id: Pubkey,
6065
}
6166

6267
impl Sealed for LendingMarket {}
@@ -66,7 +71,7 @@ impl IsInitialized for LendingMarket {
6671
}
6772
}
6873

69-
const LENDING_MARKET_LEN: usize = 258; // 1 + 1 + 32 + 32 + 32 + 32 + 128
74+
const LENDING_MARKET_LEN: usize = 290; // 1 + 1 + 32 + 32 + 32 + 32 + 32 + 128
7075
impl Pack for LendingMarket {
7176
const LEN: usize = LENDING_MARKET_LEN;
7277

@@ -80,6 +85,7 @@ impl Pack for LendingMarket {
8085
quote_currency,
8186
token_program_id,
8287
oracle_program_id,
88+
switchboard_oracle_program_id,
8389
_padding,
8490
) = mut_array_refs![
8591
output,
@@ -89,6 +95,7 @@ impl Pack for LendingMarket {
8995
32,
9096
PUBKEY_BYTES,
9197
PUBKEY_BYTES,
98+
PUBKEY_BYTES,
9299
128
93100
];
94101

@@ -98,6 +105,7 @@ impl Pack for LendingMarket {
98105
quote_currency.copy_from_slice(self.quote_currency.as_ref());
99106
token_program_id.copy_from_slice(self.token_program_id.as_ref());
100107
oracle_program_id.copy_from_slice(self.oracle_program_id.as_ref());
108+
switchboard_oracle_program_id.copy_from_slice(self.switchboard_oracle_program_id.as_ref());
101109
}
102110

103111
/// Unpacks a byte buffer into a [LendingMarketInfo](struct.LendingMarketInfo.html)
@@ -111,6 +119,7 @@ impl Pack for LendingMarket {
111119
quote_currency,
112120
token_program_id,
113121
oracle_program_id,
122+
switchboard_oracle_program_id,
114123
_padding,
115124
) = array_refs![
116125
input,
@@ -120,6 +129,7 @@ impl Pack for LendingMarket {
120129
32,
121130
PUBKEY_BYTES,
122131
PUBKEY_BYTES,
132+
PUBKEY_BYTES,
123133
128
124134
];
125135

@@ -136,6 +146,7 @@ impl Pack for LendingMarket {
136146
quote_currency: *quote_currency,
137147
token_program_id: Pubkey::new_from_array(*token_program_id),
138148
oracle_program_id: Pubkey::new_from_array(*oracle_program_id),
149+
switchboard_oracle_program_id: Pubkey::new_from_array(*switchboard_oracle_program_id),
139150
})
140151
}
141152
}

token-lending/program/tests/helpers/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub fn add_lending_market(test: &mut ProgramTest) -> TestLendingMarket {
110110
quote_currency: QUOTE_CURRENCY,
111111
token_program_id: spl_token::id(),
112112
oracle_program_id,
113+
switchboard_oracle_program_id: oracle_program_id,
113114
}),
114115
&spl_token_lending::id(),
115116
);
@@ -120,6 +121,7 @@ pub fn add_lending_market(test: &mut ProgramTest) -> TestLendingMarket {
120121
authority: lending_market_authority,
121122
quote_currency: QUOTE_CURRENCY,
122123
oracle_program_id,
124+
switchboard_oracle_program_id: oracle_program_id,
123125
}
124126
}
125127

@@ -458,6 +460,7 @@ pub struct TestLendingMarket {
458460
pub authority: Pubkey,
459461
pub quote_currency: [u8; 32],
460462
pub oracle_program_id: Pubkey,
463+
pub switchboard_oracle_program_id: Pubkey,
461464
}
462465

463466
pub struct BorrowArgs<'a> {
@@ -506,6 +509,7 @@ impl TestLendingMarket {
506509
QUOTE_CURRENCY,
507510
lending_market_pubkey,
508511
oracle_program_id,
512+
oracle_program_id,
509513
),
510514
],
511515
Some(&payer.pubkey()),
@@ -521,6 +525,7 @@ impl TestLendingMarket {
521525
authority: lending_market_authority,
522526
quote_currency: QUOTE_CURRENCY,
523527
oracle_program_id,
528+
switchboard_oracle_program_id: oracle_program_id,
524529
}
525530
}
526531

@@ -783,6 +788,7 @@ impl TestReserve {
783788
payer: &Keypair,
784789
user_accounts_owner: &Keypair,
785790
) -> Result<Self, TransactionError> {
791+
print!("asdfasd1");
786792
let reserve_keypair = Keypair::new();
787793
let reserve_pubkey = reserve_keypair.pubkey();
788794
let collateral_mint_keypair = Keypair::new();

token-lending/program/tests/init_lending_market.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ async fn test_already_initialized() {
5050
existing_market.quote_currency,
5151
existing_market.pubkey,
5252
existing_market.oracle_program_id,
53+
existing_market.switchboard_oracle_program_id,
5354
)],
5455
Some(&payer.pubkey()),
5556
);

token-lending/program/tests/set_lending_market_owner.rs

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

2828
// limit to track compute unit increase
29-
test.set_bpf_compute_max_units(2_000);
29+
test.set_bpf_compute_max_units(3_000);
3030

3131
let lending_market = add_lending_market(&mut test);
3232
let (mut banks_client, payer, recent_blockhash) = test.start().await;

0 commit comments

Comments
 (0)