Skip to content

Commit

Permalink
check switchboard program id
Browse files Browse the repository at this point in the history
  • Loading branch information
nope-finance committed Jul 27, 2021
1 parent 20f4107 commit ffe9735
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
18 changes: 17 additions & 1 deletion token-lending/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ type Error = Box<dyn std::error::Error>;
type CommandResult = Result<(), Error>;

const PYTH_PROGRAM_ID: &str = "gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s";
// const SWITCHBOARD_PROGRAM_ID: &str = "DtmE9D2CSB4L5D6A15mraeEjrGMm6auWVzgaD8hK2tZM";
const SWITCHBOARD_PROGRAM_ID: &str = "DtmE9D2CSB4L5D6A15mraeEjrGMm6auWVzgaD8hK2tZM";
const SWITCHBOARD_PROGRAM_ID_DEV: &str = "7azgmy1pFXHikv36q1zZASvFq5vFa39TT9NweVugKKTU";

fn main() {
solana_logger::setup_with_default("solana=info");
Expand Down Expand Up @@ -165,6 +166,16 @@ fn main() {
.default_value(PYTH_PROGRAM_ID)
.help("Oracle (Pyth) program ID for quoting market prices"),
)
.arg(
Arg::with_name("switchboard_oracle_program_id")
.long("switchboard-oracle")
.validator(is_pubkey)
.value_name("PUBKEY")
.takes_value(true)
.required(true)
.default_value(SWITCHBOARD_PROGRAM_ID_DEV)
.help("Oracle (switchboard) program ID for quoting market prices"),
)
.arg(
Arg::with_name("quote_currency")
.long("quote")
Expand Down Expand Up @@ -557,11 +568,14 @@ fn main() {
let lending_market_owner = pubkey_of(arg_matches, "lending_market_owner").unwrap();
let quote_currency = quote_currency_of(arg_matches, "quote_currency").unwrap();
let oracle_program_id = pubkey_of(arg_matches, "oracle_program_id").unwrap();
let switchboard_oracle_program_id = pubkey_of(arg_matches, "switchboard_oracle_program_id").unwrap();

command_create_lending_market(
&config,
lending_market_owner,
quote_currency,
oracle_program_id,
switchboard_oracle_program_id
)
}
("add-reserve", Some(arg_matches)) => {
Expand Down Expand Up @@ -679,6 +693,7 @@ fn command_create_lending_market(
lending_market_owner: Pubkey,
quote_currency: [u8; 32],
oracle_program_id: Pubkey,
switchboard_oracle_program_id: Pubkey,
) -> CommandResult {
let lending_market_keypair = Keypair::new();
println!(
Expand Down Expand Up @@ -707,6 +722,7 @@ fn command_create_lending_market(
quote_currency,
lending_market_keypair.pubkey(),
oracle_program_id,
switchboard_oracle_program_id,
),
],
Some(&config.fee_payer.pubkey()),
Expand Down
3 changes: 3 additions & 0 deletions token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum LendingInstruction {
/// 1. `[]` Rent sysvar.
/// 2. `[]` Token program id.
/// 3. `[]` Oracle program id.
/// 4. `[]` Switchboard Oracle program id.
InitLendingMarket {
/// Owner authority which can add new reserves
owner: Pubkey,
Expand Down Expand Up @@ -694,6 +695,7 @@ pub fn init_lending_market(
quote_currency: [u8; 32],
lending_market_pubkey: Pubkey,
oracle_program_id: Pubkey,
switchboard_oracle_program_id: Pubkey,
) -> Instruction {
Instruction {
program_id,
Expand All @@ -702,6 +704,7 @@ pub fn init_lending_market(
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(oracle_program_id, false),
AccountMeta::new_readonly(switchboard_oracle_program_id, false),
],
data: LendingInstruction::InitLendingMarket {
owner,
Expand Down
8 changes: 7 additions & 1 deletion token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use solana_program::{
};
use spl_token::solana_program::instruction::AccountMeta;
use spl_token::state::{Account, Mint};
use std::convert::TryInto;
use std::{convert::TryInto};
use std::result::Result;
use switchboard_program::{
get_aggregator, get_aggregator_result, AggregatorState, RoundResult, SwitchboardAccountType,
Expand Down Expand Up @@ -139,6 +139,7 @@ fn process_init_lending_market(
let rent = &Rent::from_account_info(next_account_info(account_info_iter)?)?;
let token_program_id = next_account_info(account_info_iter)?;
let oracle_program_id = next_account_info(account_info_iter)?;
let switchboard_oracle_program_id = next_account_info(account_info_iter)?;

assert_rent_exempt(rent, lending_market_info)?;
let mut lending_market = assert_uninitialized::<LendingMarket>(lending_market_info)?;
Expand All @@ -153,6 +154,7 @@ fn process_init_lending_market(
quote_currency,
token_program_id: *token_program_id.key,
oracle_program_id: *oracle_program_id.key,
switchboard_oracle_program_id: *switchboard_oracle_program_id.key,
});
LendingMarket::pack(lending_market, &mut lending_market_info.data.borrow_mut())?;

Expand Down Expand Up @@ -300,6 +302,10 @@ fn process_init_reserve(
return Err(LendingError::InvalidOracleConfig.into());
}

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

let authority_signer_seeds = &[
Expand Down
13 changes: 12 additions & 1 deletion token-lending/program/src/state/lending_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct LendingMarket {
pub token_program_id: Pubkey,
/// Oracle (Pyth) program id
pub oracle_program_id: Pubkey,
/// Oracle (Switchboard) program id
pub switchboard_oracle_program_id: Pubkey,
}

impl LendingMarket {
Expand All @@ -41,6 +43,7 @@ impl LendingMarket {
self.quote_currency = params.quote_currency;
self.token_program_id = params.token_program_id;
self.oracle_program_id = params.oracle_program_id;
self.switchboard_oracle_program_id = params.switchboard_oracle_program_id;
}
}

Expand All @@ -57,6 +60,8 @@ pub struct InitLendingMarketParams {
pub token_program_id: Pubkey,
/// Oracle (Pyth) program id
pub oracle_program_id: Pubkey,
/// Oracle (Switchboard) program id
pub switchboard_oracle_program_id: Pubkey,
}

impl Sealed for LendingMarket {}
Expand All @@ -66,7 +71,7 @@ impl IsInitialized for LendingMarket {
}
}

const LENDING_MARKET_LEN: usize = 258; // 1 + 1 + 32 + 32 + 32 + 32 + 128
const LENDING_MARKET_LEN: usize = 290; // 1 + 1 + 32 + 32 + 32 + 32 + 32 + 128
impl Pack for LendingMarket {
const LEN: usize = LENDING_MARKET_LEN;

Expand All @@ -80,6 +85,7 @@ impl Pack for LendingMarket {
quote_currency,
token_program_id,
oracle_program_id,
switchboard_oracle_program_id,
_padding,
) = mut_array_refs![
output,
Expand All @@ -89,6 +95,7 @@ impl Pack for LendingMarket {
32,
PUBKEY_BYTES,
PUBKEY_BYTES,
PUBKEY_BYTES,
128
];

Expand All @@ -98,6 +105,7 @@ impl Pack for LendingMarket {
quote_currency.copy_from_slice(self.quote_currency.as_ref());
token_program_id.copy_from_slice(self.token_program_id.as_ref());
oracle_program_id.copy_from_slice(self.oracle_program_id.as_ref());
switchboard_oracle_program_id.copy_from_slice(self.switchboard_oracle_program_id.as_ref());
}

/// Unpacks a byte buffer into a [LendingMarketInfo](struct.LendingMarketInfo.html)
Expand All @@ -111,6 +119,7 @@ impl Pack for LendingMarket {
quote_currency,
token_program_id,
oracle_program_id,
switchboard_oracle_program_id,
_padding,
) = array_refs![
input,
Expand All @@ -120,6 +129,7 @@ impl Pack for LendingMarket {
32,
PUBKEY_BYTES,
PUBKEY_BYTES,
PUBKEY_BYTES,
128
];

Expand All @@ -136,6 +146,7 @@ impl Pack for LendingMarket {
quote_currency: *quote_currency,
token_program_id: Pubkey::new_from_array(*token_program_id),
oracle_program_id: Pubkey::new_from_array(*oracle_program_id),
switchboard_oracle_program_id: Pubkey::new_from_array(*switchboard_oracle_program_id),
})
}
}
6 changes: 6 additions & 0 deletions token-lending/program/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn add_lending_market(test: &mut ProgramTest) -> TestLendingMarket {
quote_currency: QUOTE_CURRENCY,
token_program_id: spl_token::id(),
oracle_program_id,
switchboard_oracle_program_id: oracle_program_id,
}),
&spl_token_lending::id(),
);
Expand All @@ -120,6 +121,7 @@ pub fn add_lending_market(test: &mut ProgramTest) -> TestLendingMarket {
authority: lending_market_authority,
quote_currency: QUOTE_CURRENCY,
oracle_program_id,
switchboard_oracle_program_id: oracle_program_id,
}
}

Expand Down Expand Up @@ -458,6 +460,7 @@ pub struct TestLendingMarket {
pub authority: Pubkey,
pub quote_currency: [u8; 32],
pub oracle_program_id: Pubkey,
pub switchboard_oracle_program_id: Pubkey,
}

pub struct BorrowArgs<'a> {
Expand Down Expand Up @@ -506,6 +509,7 @@ impl TestLendingMarket {
QUOTE_CURRENCY,
lending_market_pubkey,
oracle_program_id,
oracle_program_id,
),
],
Some(&payer.pubkey()),
Expand All @@ -521,6 +525,7 @@ impl TestLendingMarket {
authority: lending_market_authority,
quote_currency: QUOTE_CURRENCY,
oracle_program_id,
switchboard_oracle_program_id: oracle_program_id,
}
}

Expand Down Expand Up @@ -783,6 +788,7 @@ impl TestReserve {
payer: &Keypair,
user_accounts_owner: &Keypair,
) -> Result<Self, TransactionError> {
print!("asdfasd1");
let reserve_keypair = Keypair::new();
let reserve_pubkey = reserve_keypair.pubkey();
let collateral_mint_keypair = Keypair::new();
Expand Down
1 change: 1 addition & 0 deletions token-lending/program/tests/init_lending_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async fn test_already_initialized() {
existing_market.quote_currency,
existing_market.pubkey,
existing_market.oracle_program_id,
existing_market.switchboard_oracle_program_id,
)],
Some(&payer.pubkey()),
);
Expand Down
2 changes: 1 addition & 1 deletion token-lending/program/tests/set_lending_market_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn test_success() {
);

// limit to track compute unit increase
test.set_bpf_compute_max_units(2_000);
test.set_bpf_compute_max_units(3_000);

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

0 comments on commit ffe9735

Please sign in to comment.