Skip to content

Commit 6ff3f80

Browse files
committed
refactor: remove transfer to save contract size
1 parent 38ada8a commit 6ff3f80

7 files changed

+12
-69
lines changed

program/rust/src/processor.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ use {
1717
},
1818
solana_program::{
1919
entrypoint::ProgramResult,
20-
program::invoke,
2120
pubkey::Pubkey,
22-
system_instruction,
2321
sysvar::slot_history::AccountInfo,
2422
},
2523
};
@@ -110,29 +108,16 @@ pub fn process_instruction(
110108
}
111109

112110
fn reserve_new_price_feed_index<'a>(
113-
funding_account: &AccountInfo<'a>,
114111
permissions_account: &AccountInfo<'a>,
115-
system_program: &AccountInfo<'a>,
116112
) -> Result<u32, ProgramError> {
117113
if permissions_account.data_len() < PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX {
118114
let new_size = PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX;
119115
let rent = Rent::get()?;
120116
let new_minimum_balance = rent.minimum_balance(new_size);
121-
let lamports_diff = new_minimum_balance.saturating_sub(permissions_account.lamports());
122-
if lamports_diff > 0 {
123-
invoke(
124-
&system_instruction::transfer(
125-
funding_account.key,
126-
permissions_account.key,
127-
lamports_diff,
128-
),
129-
&[
130-
funding_account.clone(),
131-
permissions_account.clone(),
132-
system_program.clone(),
133-
],
134-
)?;
135-
}
117+
pyth_assert(
118+
permissions_account.lamports() >= new_minimum_balance,
119+
ProgramError::AccountNotRentExempt,
120+
)?;
136121

137122
permissions_account.realloc(new_size, true)?;
138123
let mut header = load_account_as_mut::<AccountHeader>(permissions_account)?;

program/rust/src/processor/add_price.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use {
3737
// account[1] product account [writable]
3838
// account[2] new price account [writable]
3939
// account[3] permissions account [writable]
40-
// account[4] system program account []
4140
pub fn add_price(
4241
program_id: &Pubkey,
4342
accounts: &[AccountInfo],
@@ -52,11 +51,10 @@ pub fn add_price(
5251
)?;
5352

5453

55-
let (funding_account, product_account, price_account, permissions_account, system_program) =
56-
match accounts {
57-
[x, y, z, p, q] => Ok((x, y, z, p, q)),
58-
_ => Err(OracleError::InvalidNumberOfAccounts),
59-
}?;
54+
let (funding_account, product_account, price_account, permissions_account) = match accounts {
55+
[x, y, z, p] => Ok((x, y, z, p)),
56+
_ => Err(OracleError::InvalidNumberOfAccounts),
57+
}?;
6058

6159
check_valid_funding_account(funding_account)?;
6260
check_permissioned_funding_account(
@@ -74,10 +72,6 @@ pub fn add_price(
7472
&cmd_args.header,
7573
)?;
7674
check_valid_writable_account(program_id, permissions_account)?;
77-
pyth_assert(
78-
solana_program::system_program::check_id(system_program.key),
79-
OracleError::InvalidSystemAccount.into(),
80-
)?;
8175

8276
let mut product_data =
8377
load_checked::<ProductAccount>(product_account, cmd_args.header.version)?;
@@ -89,8 +83,7 @@ pub fn add_price(
8983
price_data.product_account = *product_account.key;
9084
price_data.next_price_account = product_data.first_price_account;
9185
price_data.min_pub_ = PRICE_ACCOUNT_DEFAULT_MIN_PUB;
92-
price_data.feed_index =
93-
reserve_new_price_feed_index(funding_account, permissions_account, system_program)?;
86+
price_data.feed_index = reserve_new_price_feed_index(permissions_account)?;
9487
product_data.first_price_account = *price_account.key;
9588

9689
Ok(())

program/rust/src/processor/init_price_feed_index.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use {
2828
// account[0] funding account [signer writable]
2929
// account[1] price account [writable]
3030
// account[2] permissions account [writable]
31-
// account[3] system program account []
3231
pub fn init_price_feed_index(
3332
program_id: &Pubkey,
3433
accounts: &[AccountInfo],
@@ -41,8 +40,8 @@ pub fn init_price_feed_index(
4140
ProgramError::InvalidArgument,
4241
)?;
4342

44-
let (funding_account, price_account, permissions_account, system_program) = match accounts {
45-
[x, y, p, z] => Ok((x, y, p, z)),
43+
let (funding_account, price_account, permissions_account) = match accounts {
44+
[x, y, p] => Ok((x, y, p)),
4645
_ => Err(OracleError::InvalidNumberOfAccounts),
4746
}?;
4847

@@ -55,18 +54,13 @@ pub fn init_price_feed_index(
5554
cmd,
5655
)?;
5756
check_valid_writable_account(program_id, permissions_account)?;
58-
pyth_assert(
59-
solana_program::system_program::check_id(system_program.key),
60-
OracleError::InvalidSystemAccount.into(),
61-
)?;
6257

6358
let mut price_account_data = load_checked::<PriceAccount>(price_account, cmd.version)?;
6459
pyth_assert(
6560
price_account_data.feed_index == 0,
6661
OracleError::FeedIndexAlreadyInitialized.into(),
6762
)?;
68-
price_account_data.feed_index =
69-
reserve_new_price_feed_index(funding_account, permissions_account, system_program)?;
63+
price_account_data.feed_index = reserve_new_price_feed_index(permissions_account)?;
7064

7165
Ok(())
7266
}

program/rust/src/tests/pyth_simulator.rs

-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ impl PythSimulator {
338338
AccountMeta::new(product_keypair.pubkey(), true),
339339
AccountMeta::new(price_keypair.pubkey(), true),
340340
AccountMeta::new(self.get_permissions_pubkey(), false),
341-
AccountMeta::new(system_program::id(), false),
342341
],
343342
);
344343

program/rust/src/tests/test_add_price.rs

-9
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ fn test_add_price() {
6262
let mut permissions_setup = AccountSetup::new_permission(&program_id);
6363
let permissions_account = permissions_setup.as_account_info();
6464

65-
let mut system_program = AccountSetup::new_system_program();
66-
let system_program_account = system_program.as_account_info();
67-
6865
{
6966
let mut permissions_account_data =
7067
PermissionAccount::initialize(&permissions_account, PC_VERSION).unwrap();
@@ -92,7 +89,6 @@ fn test_add_price() {
9289
product_account.clone(),
9390
price_account.clone(),
9491
permissions_account.clone(),
95-
system_program_account.clone(),
9692
],
9793
instruction_data_add_price,
9894
)
@@ -116,7 +112,6 @@ fn test_add_price() {
116112
product_account.clone(),
117113
price_account_2.clone(),
118114
permissions_account.clone(),
119-
system_program_account.clone(),
120115
],
121116
instruction_data_add_price,
122117
)
@@ -142,7 +137,6 @@ fn test_add_price() {
142137
product_account.clone(),
143138
price_account.clone(),
144139
permissions_account.clone(),
145-
system_program_account.clone(),
146140
permissions_account.clone(),
147141
],
148142
instruction_data_add_price
@@ -159,7 +153,6 @@ fn test_add_price() {
159153
product_account.clone(),
160154
price_account.clone(),
161155
permissions_account.clone(),
162-
system_program_account.clone(),
163156
],
164157
instruction_data_add_price
165158
),
@@ -184,7 +177,6 @@ fn test_add_price() {
184177
product_account.clone(),
185178
price_account.clone(),
186179
permissions_account.clone(),
187-
system_program_account.clone(),
188180
],
189181
instruction_data_add_price
190182
),
@@ -210,7 +202,6 @@ fn test_add_price() {
210202
product_account.clone(),
211203
price_account.clone(),
212204
permissions_account.clone(),
213-
system_program_account.clone(),
214205
],
215206
instruction_data_add_price
216207
),

program/rust/src/tests/test_permission_migration.rs

-5
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ fn test_permission_migration() {
6767
let mut price_account = price_setup.as_account_info();
6868
PriceAccount::initialize(&price_account, PC_VERSION).unwrap();
6969

70-
let mut system_program = AccountSetup::new_system_program();
71-
let system_program_account = system_program.as_account_info();
72-
73-
7470
product_account.is_signer = false;
7571
mapping_account.is_signer = false;
7672
price_account.is_signer = false;
@@ -155,7 +151,6 @@ fn test_permission_migration() {
155151
product_account.clone(),
156152
price_account.clone(),
157153
permissions_account.clone(),
158-
system_program_account.clone(),
159154
],
160155
bytes_of::<AddPriceArgs>(&AddPriceArgs {
161156
header: AddPrice.into(),

program/rust/src/tests/test_utils.rs

-14
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,6 @@ impl AccountSetup {
114114
}
115115
}
116116

117-
pub fn new_system_program() -> Self {
118-
let key = system_program::id();
119-
let owner = system_program::id(); //?
120-
let balance = Rent::minimum_balance(&Rent::default(), 0);
121-
let data = [0u8; UPPER_BOUND_OF_ALL_ACCOUNT_SIZES];
122-
AccountSetup {
123-
key,
124-
owner,
125-
balance,
126-
size: 0,
127-
data,
128-
}
129-
}
130-
131117
pub fn as_account_info(&mut self) -> AccountInfo {
132118
AccountInfo::new(
133119
&self.key,

0 commit comments

Comments
 (0)