use rialo_events_core::derive_event_address;
use rialo_s_instruction::{AccountMeta, Instruction};
use rialo_s_program::system_program;
use rialo_s_pubkey::Pubkey;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum AggregatorInstruction {
Aggregate {
topic: String,
},
}
impl AggregatorInstruction {
pub fn aggregate(
aggregator_program_id: &Pubkey,
payer: &Pubkey,
oracle_report: &Pubkey,
topic: String,
) -> Instruction {
let aggregated_data_key = derive_event_address(&topic, aggregator_program_id).0;
Instruction::new_with_bincode(
*aggregator_program_id,
&AggregatorInstruction::Aggregate { topic },
vec![
AccountMeta::new(*payer, true),
AccountMeta::new_readonly(*oracle_report, false),
AccountMeta::new(aggregated_data_key, false),
AccountMeta::new_readonly(system_program::id(), false),
],
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_aggregate() {
let program_id = Pubkey::new_unique();
let creator = Pubkey::new_unique();
let oracle_report = Pubkey::new_unique();
let topic = "test_topic".to_string();
let expected_aggregated_data_key = derive_event_address(&topic, &program_id).0;
let instruction =
AggregatorInstruction::aggregate(&program_id, &creator, &oracle_report, topic);
assert_eq!(instruction.program_id, program_id);
let expected_accounts = vec![
AccountMeta::new(creator, true),
AccountMeta::new_readonly(oracle_report, false),
AccountMeta::new(expected_aggregated_data_key, false),
AccountMeta::new_readonly(system_program::id(), false),
];
assert_eq!(instruction.accounts, expected_accounts);
let _: AggregatorInstruction = bincode::deserialize(&instruction.data).unwrap();
}
}