Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check switchboard program id #25

Merged
merged 4 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions token-lending/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type CommandResult = Result<(), Error>;

const PYTH_PROGRAM_ID: &str = "gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s";
// 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,15 @@ 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 +694,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 +723,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
8 changes: 7 additions & 1 deletion token-lending/js/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const LendingMarketLayout: typeof BufferLayout.Structure = BufferLayout.s
Layout.publicKey("quoteTokenMint"),
Layout.publicKey("tokenProgramId"),
Layout.publicKey("oracleProgramId"),
Layout.publicKey("switchboardOracleProgramId"),
BufferLayout.blob(128, "padding"),
]
);
Expand Down Expand Up @@ -126,7 +127,12 @@ export class LendingMarket {
isWritable: false,
},
{
pubkey: new Account().publicKey, // TODO use the oracle program id
pubkey: new Account().publicKey, // TODO use the pyth oracle program id
isSigner: false,
isWritable: false,
},
{
pubkey: new Account().publicKey, // TODO use the switchboard oracle program id
isSigner: false,
isWritable: false,
},
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
7 changes: 7 additions & 0 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
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!("Switchboard account provided is not owned by the switchboard 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 Expand Up @@ -2181,6 +2187,7 @@ fn get_switchboard_price(
const STALE_AFTER_SLOTS_ELAPSED: u64 = 100;

let account_buf = switchboard_feed_info.try_borrow_data()?;
// first byte type discriminator
if account_buf[0] != SwitchboardAccountType::TYPE_AGGREGATOR as u8 {
msg!("switchboard address not of type aggregator");
return Err(LendingError::InvalidAccountInput.into());
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),
})
}
}
5 changes: 5 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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add the key here? pyth_oracle_program_id or whatever it is
just so it matches

switchboard_oracle_program_id: oracle_program_id,
}
}

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