Skip to content

Commit 21cc8ef

Browse files
authored
Get rent (#290)
1 parent 32b0013 commit 21cc8ef

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

program/rust/src/deserialize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use bytemuck::{
66
Pod,
77
};
88
use solana_program::pubkey::Pubkey;
9-
use solana_program::rent::Rent;
109

1110
use crate::c_oracle_header::{
1211
AccountHeader,
@@ -19,6 +18,7 @@ use crate::utils::{
1918
assign_owner,
2019
check_valid_fresh_account,
2120
clear_account,
21+
get_rent,
2222
pyth_assert,
2323
send_lamports,
2424
};
@@ -127,7 +127,7 @@ pub fn create_pda_if_needed<'a, T: PythAccount>(
127127
seeds: &[&[u8]],
128128
version: u32,
129129
) -> Result<(), ProgramError> {
130-
let target_rent = Rent::default().minimum_balance(T::MINIMUM_SIZE);
130+
let target_rent = get_rent()?.minimum_balance(T::MINIMUM_SIZE);
131131
if account.lamports() < target_rent {
132132
send_lamports(
133133
funding_account,

program/rust/src/rust_oracle.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::utils::{
4545
check_valid_funding_account,
4646
check_valid_signable_account,
4747
check_valid_writable_account,
48+
get_rent,
4849
is_component_update,
4950
pyth_assert,
5051
read_pc_str_t,
@@ -107,7 +108,7 @@ pub fn resize_price_account(
107108
match account_len {
108109
PriceAccount::MINIMUM_SIZE => {
109110
// Ensure account is still rent exempt after resizing
110-
let rent: Rent = Default::default();
111+
let rent: Rent = get_rent()?;
111112
let lamports_needed: u64 = rent
112113
.minimum_balance(size_of::<PriceAccountWrapper>())
113114
.saturating_sub(price_account_info.lamports());

program/rust/src/utils.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,22 @@ pub fn check_valid_funding_account(account: &AccountInfo) -> Result<(), ProgramE
5656
)
5757
}
5858

59-
pub fn valid_signable_account(program_id: &Pubkey, account: &AccountInfo) -> bool {
60-
account.is_signer
59+
pub fn valid_signable_account(
60+
program_id: &Pubkey,
61+
account: &AccountInfo,
62+
) -> Result<bool, ProgramError> {
63+
Ok(account.is_signer
6164
&& account.is_writable
6265
&& account.owner == program_id
63-
&& Rent::default().is_exempt(account.lamports(), account.data_len())
66+
&& get_rent()?.is_exempt(account.lamports(), account.data_len()))
6467
}
6568

6669
pub fn check_valid_signable_account(
6770
program_id: &Pubkey,
6871
account: &AccountInfo,
6972
) -> Result<(), ProgramError> {
7073
pyth_assert(
71-
valid_signable_account(program_id, account),
74+
valid_signable_account(program_id, account)?,
7275
OracleError::InvalidSignableAccount.into(),
7376
)
7477
}
@@ -119,18 +122,21 @@ pub fn read_pc_str_t(source: &[u8]) -> Result<&[u8], ProgramError> {
119122
}
120123
}
121124

122-
fn valid_writable_account(program_id: &Pubkey, account: &AccountInfo) -> bool {
123-
account.is_writable
125+
fn valid_writable_account(
126+
program_id: &Pubkey,
127+
account: &AccountInfo,
128+
) -> Result<bool, ProgramError> {
129+
Ok(account.is_writable
124130
&& account.owner == program_id
125-
&& Rent::default().is_exempt(account.lamports(), account.data_len())
131+
&& get_rent()?.is_exempt(account.lamports(), account.data_len()))
126132
}
127133

128134
pub fn check_valid_writable_account(
129135
program_id: &Pubkey,
130136
account: &AccountInfo,
131137
) -> Result<(), ProgramError> {
132138
pyth_assert(
133-
valid_writable_account(program_id, account),
139+
valid_writable_account(program_id, account)?,
134140
OracleError::InvalidWritableAccount.into(),
135141
)
136142
}
@@ -244,3 +250,14 @@ pub fn assign_owner<'a>(
244250
)?;
245251
Ok(())
246252
}
253+
254+
#[cfg(not(test))]
255+
pub fn get_rent() -> Result<Rent, ProgramError> {
256+
use solana_program::sysvar::Sysvar;
257+
Rent::get()
258+
}
259+
260+
#[cfg(test)]
261+
pub fn get_rent() -> Result<Rent, ProgramError> {
262+
Ok(Rent::default())
263+
}

0 commit comments

Comments
 (0)