fee_rate_oracle 0.1.0

Saturn Fee Rate Oracle
Documentation
use arch_program::{
    account::AccountInfo,
    program::{invoke_signed, next_account_info},
    program_error::ProgramError,
    pubkey::Pubkey,
    rent,
    system_instruction::create_account,
};

#[cfg(not(feature = "no-entrypoint"))]
use arch_program::entrypoint;

pub const FEERATE_ORACLE_ACCOUNTS: usize = 1;

pub use saturn_mempool_oracle_sdk::*;

#[cfg(not(feature = "no-entrypoint"))]
entrypoint!(update_data);

pub fn update_data(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> Result<(), ProgramError> {
    let account_iter = &mut accounts.iter();
    let signer_account = next_account_info(account_iter)?;

    if *signer_account.key != OWNER_PUBKEY {
        return Err(ProgramError::IncorrectProgramId);
    }

    assert!(signer_account.is_signer, "signer account must be a signer");

    let oracle_account = next_account_info(account_iter)?;

    assert_eq!(instruction_data.len(), 8);

    if oracle_account.lamports() == 0 {
        let (pda, bump_seed) = mempool_pda_address(program_id, 0);

        if *oracle_account.key != pda {
            return Err(ProgramError::Custom(1));
        }

        let idx_seed = 0_u32.to_le_bytes();
        let combined_seeds: [&[u8]; 2] = [&idx_seed, &[bump_seed]];
        let cpi_signer_seeds: &[&[&[u8]]] = &[&combined_seeds];

        let system_program_id = next_account_info(account_iter)?;

        let instruction = create_account(
            signer_account.key,
            oracle_account.key,
            rent::minimum_rent(instruction_data.len()),
            instruction_data.len() as u64,
            &program_id,
        );

        invoke_signed(
            &instruction,
            &[
                signer_account.clone(),
                oracle_account.clone(),
                system_program_id.clone(),
            ],
            cpi_signer_seeds,
        )?;
    }

    oracle_account
        .data
        .try_borrow_mut()
        .unwrap()
        .copy_from_slice(instruction_data);

    Ok(())
}

pub fn id() -> Pubkey {
    Pubkey([
        9, 158, 213, 129, 221, 103, 238, 78, 89, 237, 205, 219, 236, 104, 238, 95, 98, 246, 234, 7,
        117, 137, 234, 155, 194, 18, 237, 74, 30, 253, 66, 162,
    ])
}

pub const ID: Pubkey = Pubkey([
    9, 158, 213, 129, 221, 103, 238, 78, 89, 237, 205, 219, 236, 104, 238, 95, 98, 246, 234, 7,
    117, 137, 234, 155, 194, 18, 237, 74, 30, 253, 66, 162,
]);

pub const OWNER_PUBKEY: Pubkey = Pubkey([
    157, 90, 116, 11, 120, 205, 121, 221, 134, 130, 113, 18, 9, 209, 179, 246, 37, 95, 224, 226,
    236, 170, 222, 25, 251, 236, 215, 228, 124, 135, 135, 107,
]);