Skip to content

feat(oracle): WIP add scores account #407

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

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b343951
feat(oracle): WIP add scores account
keyvankhademi Jun 7, 2024
1561e80
feat: use bitset to reduce scores account size
keyvankhademi Jun 10, 2024
b58625c
fix: pre-commit
keyvankhademi Jun 10, 2024
18c6787
fix: add price test
keyvankhademi Jun 13, 2024
32c577e
fix: rename score to cap
keyvankhademi Jun 13, 2024
2ec0004
feat: remove publisher sorting features
keyvankhademi Jun 14, 2024
a02a10b
feat: improve caps account calculations
keyvankhademi Jun 14, 2024
0c98527
feat: rename score to publisher_caps
keyvankhademi Jun 14, 2024
87f61ec
fix: bypass false positive clippy
keyvankhademi Jun 14, 2024
2f6f359
feat: optimize oracle command enum
keyvankhademi Jun 14, 2024
2697cb2
feat: test new way of calculating score
keyvankhademi Jun 14, 2024
d07a256
fix: correct rust doc format
keyvankhademi Jun 17, 2024
98a6a52
fix: clippy
keyvankhademi Jun 17, 2024
4260e2e
feat: add vscode settings
keyvankhademi Jun 17, 2024
6c9d3e6
fix: clippy warning
keyvankhademi Jun 17, 2024
8e00103
feat: change OracleCommand repr from i32 to u32
keyvankhademi Jun 17, 2024
aa1e0a7
feat: add initial tests for publisher caps
keyvankhademi Jun 17, 2024
2372be6
feat: add migration step
keyvankhademi Jun 17, 2024
63352ec
test: try a different implementation for program size
keyvankhademi Jun 17, 2024
aaa12bf
test: try manual swap to decrease program size
keyvankhademi Jun 17, 2024
e2aa645
fix: add price when initializing the caps account
keyvankhademi Jun 17, 2024
2169e8d
fix: rename scores account to cap account
keyvankhademi Jun 17, 2024
de0f7c3
refactor: rename calculate_scores to calculate_caps
keyvankhademi Jun 17, 2024
4396054
refactor: rename symbols to prices in PublisherCapsAccount
keyvankhademi Jun 17, 2024
0563db6
refactor: update publisher permission index to use price instead of s…
keyvankhademi Jun 17, 2024
6c321bf
refactor: update publisher permission index to use price instead of s…
keyvankhademi Jun 17, 2024
9e90897
chore: Update num-traits dependency to version 0.2
keyvankhademi Jun 17, 2024
5c1f750
feat: add initial quickcheck for caps account
keyvankhademi Jun 18, 2024
02f6257
refactor: update upd_price.rs to use send_message_to_message_buffer f…
keyvankhademi Jun 18, 2024
7800412
feat: send caps to message buffer for add publisher
keyvankhademi Jun 19, 2024
9b52a40
fix: comment migration code to test program size
keyvankhademi Jun 19, 2024
a18ec0c
feat: remove init mapping to reduce program size
keyvankhademi Jun 19, 2024
5932402
feat: keep init mapping for tests
keyvankhademi Jun 19, 2024
21c38c8
fix: self ref error
keyvankhademi Jun 19, 2024
413abc3
fix: revert mapping account changes
keyvankhademi Jun 19, 2024
23a1da8
feat: add cfg test flag for init mapping
keyvankhademi Jun 19, 2024
1a11ca4
temporary remove pipeline test
keyvankhademi Jun 19, 2024
1fa4c6a
remove upd_permissions to check size
keyvankhademi Jun 19, 2024
dd453fb
add feature test to be able to compile oracle for test
keyvankhademi Jun 19, 2024
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
Prev Previous commit
Next Next commit
refactor: update upd_price.rs to use send_message_to_message_buffer f…
…unction
  • Loading branch information
keyvankhademi committed Jun 18, 2024
commit 02f6257cb46dcd34238ff5d35c6d00d90a5cb4d4
70 changes: 10 additions & 60 deletions program/rust/src/processor/upd_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ use {
get_status_for_conf_price_ratio,
is_component_update,
pyth_assert,
send_message_to_message_buffer,
try_convert,
MessageBufferAccounts,
},
OracleError,
},
solana_program::{
account_info::AccountInfo,
clock::Clock,
entrypoint::ProgramResult,
instruction::{
AccountMeta,
Instruction,
},
program::invoke_signed,
program_error::ProgramError,
program_memory::sol_memcmp,
pubkey::Pubkey,
Expand Down Expand Up @@ -202,60 +199,22 @@ pub fn upd_price(
{
if let Some(accumulator_accounts) = maybe_accumulator_accounts {
if price_data.message_sent_ == 0 {
// Check that the oracle PDA is correctly configured for the program we are calling.
let oracle_auth_seeds: &[&[u8]] = &[
UPD_PRICE_WRITE_SEED.as_bytes(),
&accumulator_accounts.program_id.key.to_bytes(),
];
let (expected_oracle_auth_pda, bump) =
Pubkey::find_program_address(oracle_auth_seeds, program_id);
pyth_assert(
expected_oracle_auth_pda == *accumulator_accounts.oracle_auth_pda.key,
OracleError::InvalidPda.into(),
)?;

let account_metas = vec![
AccountMeta {
pubkey: *accumulator_accounts.whitelist.key,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: *accumulator_accounts.oracle_auth_pda.key,
is_signer: true,
is_writable: false,
},
AccountMeta {
pubkey: *accumulator_accounts.message_buffer_data.key,
is_signer: false,
is_writable: true,
},
];

let message = vec![
price_data
.as_price_feed_message(price_account.key)
.to_bytes(),
price_data.as_twap_message(price_account.key).to_bytes(),
];

// Append a TWAP message if available

// anchor discriminator for "global:put_all"
let discriminator: [u8; 8] = [212, 225, 193, 91, 151, 238, 20, 93];
let create_inputs_ix = Instruction::new_with_borsh(
*accumulator_accounts.program_id.key,
&(discriminator, price_account.key.to_bytes(), message),
account_metas,
);

let auth_seeds_with_bump: &[&[u8]] = &[
UPD_PRICE_WRITE_SEED.as_bytes(),
&accumulator_accounts.program_id.key.to_bytes(),
&[bump],
];
send_message_to_message_buffer(
program_id,
price_account.key,
UPD_PRICE_WRITE_SEED,
accounts,
accumulator_accounts,
message,
)?;

invoke_signed(&create_inputs_ix, accounts, &[auth_seeds_with_bump])?;
price_data.message_sent_ = 1;
}
}
Expand Down Expand Up @@ -322,15 +281,6 @@ fn find_publisher_index(comps: &[PriceComponent], key: &Pubkey) -> Option<usize>
binary_search_result
}

#[allow(dead_code)]
// Wrapper struct for the accounts required to add data to the accumulator program.
struct MessageBufferAccounts<'a, 'b: 'a> {
program_id: &'a AccountInfo<'b>,
whitelist: &'a AccountInfo<'b>,
oracle_auth_pda: &'a AccountInfo<'b>,
message_buffer_data: &'a AccountInfo<'b>,
}

#[cfg(test)]
mod test {
use {
Expand Down
76 changes: 75 additions & 1 deletion program/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ use {
solana_program::{
account_info::AccountInfo,
bpf_loader_upgradeable,
program::invoke,
instruction::{
AccountMeta,
Instruction,
},
program::{
invoke,
invoke_signed,
},
program_error::ProgramError,
pubkey::Pubkey,
system_instruction::transfer,
Expand Down Expand Up @@ -258,3 +265,70 @@ pub fn send_lamports<'a>(
)?;
Ok(())
}

// Wrapper struct for the accounts required to add data to the accumulator program.
pub struct MessageBufferAccounts<'a, 'b: 'a> {
pub program_id: &'a AccountInfo<'b>,
pub whitelist: &'a AccountInfo<'b>,
pub oracle_auth_pda: &'a AccountInfo<'b>,
pub message_buffer_data: &'a AccountInfo<'b>,
}

pub fn send_message_to_message_buffer(
program_id: &Pubkey,
base_account_key: &Pubkey,
seed: &str,
accounts: &[AccountInfo],
accumulator_accounts: MessageBufferAccounts,
message: Vec<Vec<u8>>,
) -> Result<(), ProgramError> {
let account_metas = vec![
AccountMeta {
pubkey: *accumulator_accounts.whitelist.key,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: *accumulator_accounts.oracle_auth_pda.key,
is_signer: true,
is_writable: false,
},
AccountMeta {
pubkey: *accumulator_accounts.message_buffer_data.key,
is_signer: false,
is_writable: true,
},
];

// Check that the oracle PDA is correctly configured for the program we are calling.
let oracle_auth_seeds: &[&[u8]] = &[
seed.as_bytes(),
&accumulator_accounts.program_id.key.to_bytes(),
];
let (expected_oracle_auth_pda, bump) =
Pubkey::find_program_address(oracle_auth_seeds, program_id);
pyth_assert(
expected_oracle_auth_pda == *accumulator_accounts.oracle_auth_pda.key,
OracleError::InvalidPda.into(),
)?;

// Append a TWAP message if available

// anchor discriminator for "global:put_all"
let discriminator: [u8; 8] = [212, 225, 193, 91, 151, 238, 20, 93];
let create_inputs_ix = Instruction::new_with_borsh(
*accumulator_accounts.program_id.key,
&(discriminator, base_account_key.to_bytes(), message),
account_metas,
);

let auth_seeds_with_bump: &[&[u8]] = &[
seed.as_bytes(),
&accumulator_accounts.program_id.key.to_bytes(),
&[bump],
];

invoke_signed(&create_inputs_ix, accounts, &[auth_seeds_with_bump])?;

Ok(())
}