Skip to content

Commit

Permalink
chore: bring back helpers for add_mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche committed Sep 6, 2024
1 parent 0e7cd8c commit 4522c1a
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions program/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ pub fn check_valid_funding_account(account: &AccountInfo) -> Result<(), ProgramE
)
}

pub fn valid_signable_account(
program_id: &Pubkey,
account: &AccountInfo,
) -> Result<bool, ProgramError> {
Ok(account.is_signer
&& account.is_writable
&& account.owner == program_id
&& get_rent()?.is_exempt(account.lamports(), account.data_len()))
}

pub fn check_valid_signable_account(
program_id: &Pubkey,
account: &AccountInfo,
) -> Result<(), ProgramError> {
pyth_assert(
valid_signable_account(program_id, account)?,
OracleError::InvalidSignableAccount.into(),
)
}

/// Check that `account` is a valid signable pyth account or
/// that `funding_account` is a signer and is permissioned by the `permission_account`
pub fn check_permissioned_funding_account(
Expand All @@ -80,6 +100,34 @@ pub fn check_permissioned_funding_account(
check_valid_writable_account(program_id, account)
}

/// Check that `account` is a valid signable pyth account or
/// that `funding_account` is a signer and is permissioned by the `permission_account`
pub fn check_valid_signable_account_or_permissioned_funding_account(
program_id: &Pubkey,
account: &AccountInfo,
funding_account: &AccountInfo,
permissions_account_option: Option<&AccountInfo>,
cmd_hdr: &CommandHeader,
) -> Result<(), ProgramError> {
if let Some(permissions_account) = permissions_account_option {
check_valid_permissions_account(program_id, permissions_account)?;
let permissions_account_data =
load_checked::<PermissionAccount>(permissions_account, cmd_hdr.version)?;
check_valid_funding_account(funding_account)?;
pyth_assert(
permissions_account_data.is_authorized(
funding_account.key,
OracleCommand::from_i32(cmd_hdr.command)
.ok_or(OracleError::UnrecognizedInstruction)?,
),
OracleError::PermissionViolation.into(),
)?;
check_valid_writable_account(program_id, account)
} else {
check_valid_signable_account(program_id, account)
}
}

/// Returns `true` if the `account` is fresh, i.e., its data can be overwritten.
/// Use this check to prevent accidentally overwriting accounts whose data is already populated.
pub fn valid_fresh_account(account: &AccountInfo) -> bool {
Expand Down

0 comments on commit 4522c1a

Please sign in to comment.