Skip to content

Commit f466deb

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

25 files changed

+13
-101
lines changed

program/rust/src/processor.rs

+5-22
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
};
@@ -109,30 +107,15 @@ pub fn process_instruction(
109107
}
110108
}
111109

112-
fn reserve_new_price_feed_index<'a>(
113-
funding_account: &AccountInfo<'a>,
114-
permissions_account: &AccountInfo<'a>,
115-
system_program: &AccountInfo<'a>,
116-
) -> Result<u32, ProgramError> {
110+
fn reserve_new_price_feed_index(permissions_account: &AccountInfo) -> Result<u32, ProgramError> {
117111
if permissions_account.data_len() < PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX {
118112
let new_size = PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX;
119113
let rent = Rent::get()?;
120114
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-
}
115+
pyth_assert(
116+
permissions_account.lamports() >= new_minimum_balance,
117+
ProgramError::AccountNotRentExempt,
118+
)?;
136119

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

program/rust/src/processor/add_price.rs

+5-13
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],
@@ -51,12 +50,10 @@ pub fn add_price(
5150
ProgramError::InvalidArgument,
5251
)?;
5352

54-
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-
}?;
53+
let (funding_account, product_account, price_account, permissions_account) = match accounts {
54+
[x, y, z, p] => Ok((x, y, z, p)),
55+
_ => Err(OracleError::InvalidNumberOfAccounts),
56+
}?;
6057

6158
check_valid_funding_account(funding_account)?;
6259
check_permissioned_funding_account(
@@ -74,10 +71,6 @@ pub fn add_price(
7471
&cmd_args.header,
7572
)?;
7673
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-
)?;
8174

8275
let mut product_data =
8376
load_checked::<ProductAccount>(product_account, cmd_args.header.version)?;
@@ -89,8 +82,7 @@ pub fn add_price(
8982
price_data.product_account = *product_account.key;
9083
price_data.next_price_account = product_data.first_price_account;
9184
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)?;
85+
price_data.feed_index = reserve_new_price_feed_index(permissions_account)?;
9486
product_data.first_price_account = *price_account.key;
9587

9688
Ok(())

program/rust/src/processor/add_product.rs

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ pub fn add_product(
6565
hdr,
6666
)?;
6767

68-
6968
let mut mapping_data = load_checked::<MappingAccount>(tail_mapping_account, hdr.version)?;
7069
// The mapping account must have free space to add the product account
7170
pyth_assert(

program/rust/src/processor/del_product.rs

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub fn del_product(
6868
cmd_args,
6969
)?;
7070

71-
7271
{
7372
let mut mapping_data = load_checked::<MappingAccount>(mapping_account, cmd_args.version)?;
7473
let product_data = load_checked::<ProductAccount>(product_account, cmd_args.version)?;

program/rust/src/processor/init_price.rs

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ pub fn init_price(
5555
&cmd_args.header,
5656
)?;
5757

58-
5958
let mut price_data = load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;
6059
pyth_assert(
6160
price_data.price_type == cmd_args.price_type,

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/processor/set_min_pub.rs

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub fn set_min_pub(
5151
&cmd.header,
5252
)?;
5353

54-
5554
let mut price_account_data = load_checked::<PriceAccount>(price_account, cmd.header.version)?;
5655
price_account_data.min_pub_ = cmd.minimum_publishers;
5756

program/rust/src/processor/upd_permissions.rs

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub fn upd_permissions(
6161
OracleError::InvalidSystemAccount.into(),
6262
)?;
6363

64-
6564
// Create PermissionAccount if it doesn't exist
6665
PermissionAccount::initialize_pda(
6766
permissions_account,

program/rust/src/processor/upd_product.rs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub fn upd_product(
4747
hdr,
4848
)?;
4949

50-
5150
{
5251
// Validate that product_account contains the appropriate account header
5352
let mut _product_data = load_checked::<ProductAccount>(product_account, hdr.version)?;

program/rust/src/tests/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ mod test_upd_price_with_validator;
2828
mod test_upd_product;
2929
mod test_utils;
3030

31-
3231
mod test_twap;
3332
mod test_upd_price_v2;

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_add_product.rs

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use {
3838
std::mem::size_of,
3939
};
4040

41-
4241
#[test]
4342
fn test_add_product() {
4443
let mut instruction_data = [0u8; PC_PROD_ACC_SIZE as usize];
@@ -201,7 +200,6 @@ fn test_add_product() {
201200
assert_eq!(mapping_data.number_of_products, PC_MAP_TABLE_SIZE);
202201
}
203202

204-
205203
// Create an add_product instruction that sets the product metadata to strings
206204
pub fn populate_instruction(instruction_data: &mut [u8], strings: &[&str]) -> usize {
207205
{

program/rust/src/tests/test_del_price.rs

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ async fn test_del_price() {
3636
.unwrap();
3737
assert!(product1_data.first_price_account == Pubkey::default());
3838

39-
4039
// price2_1 is the 2nd item in the linked list since price2_2 got added after t.
4140
assert!(sim.del_price(&product2, &price2_1).await.is_err());
4241
// Can delete the accounts in the opposite order though

program/rust/src/tests/test_del_product.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use {
1111
},
1212
};
1313

14-
1514
#[tokio::test]
1615
async fn test_del_product() {
1716
let mut sim = PythSimulator::new().await;
@@ -56,7 +55,6 @@ async fn test_del_product() {
5655
));
5756
assert!(sim.get_account(product5.pubkey()).await.is_some());
5857

59-
6058
assert!(sim.del_product(&mapping_keypair, &product4).await.is_ok());
6159
let mapping_data = sim
6260
.get_account_data_as::<MappingAccount>(mapping_keypair.pubkey())

program/rust/src/tests/test_ema.rs

-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use {
1818
test_generator::test_resources,
1919
};
2020

21-
2221
#[test_resources("program/rust/test_data/ema/*.csv")]
2322
fn test_ema(input_path_raw: &str) {
2423
let (inputs, expected_outputs) = read_test_data(input_path_raw);
@@ -110,7 +109,6 @@ fn run_ema_test(inputs: &[InputRecord], expected_outputs: &[OutputRecord]) {
110109
}
111110
}
112111

113-
114112
// TODO: put these functions somewhere more accessible
115113
pub fn upd_aggregate(
116114
price_account: &mut PriceAccount,
@@ -130,7 +128,6 @@ pub fn upd_twap(price_account: &mut PriceAccount, nslots: i64) {
130128
unsafe { c_upd_twap((price_account as *mut PriceAccount) as *mut u8, nslots) }
131129
}
132130

133-
134131
#[derive(Serialize, Deserialize, Debug)]
135132
struct InputRecord {
136133
price: i64,

program/rust/src/tests/test_full_publisher_set.rs

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ async fn test_full_publisher_set() -> Result<(), Box<dyn std::error::Error>> {
3636
.await;
3737
let price = price_accounts["LTC"];
3838

39-
4039
let n_pubs = pub_keypairs.len();
4140

4241
// Divide publishers into two even parts (assuming the max PC_NUM_COMP size is even)

program/rust/src/tests/test_message.rs

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ fn test_twap_message_roundtrip(input: TwapMessage) -> bool {
4444
}
4545
}
4646

47-
4847
fn prop_publisher_caps_message_roundtrip(input: PublisherStakeCapsMessage) -> bool {
4948
let reconstructed = from_slice::<BigEndian, Message>(&input.clone().to_bytes()).unwrap();
5049

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_upd_aggregate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ fn test_upd_aggregate() {
6767
corp_act_status_: 0,
6868
};
6969

70-
7170
let mut instruction_data = [0u8; size_of::<UpdPriceArgs>()];
7271
populate_instruction(&mut instruction_data, 42, 2, 1);
7372

program/rust/src/tests/test_upd_permissions.rs

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ async fn test_upd_permissions() {
8888
master_authority = Pubkey::new_unique();
8989
security_authority = Pubkey::new_unique();
9090

91-
9291
// Should fail because payer is not the authority
9392
assert_eq!(
9493
sim.upd_permissions(

program/rust/src/tests/test_upd_price_no_fail_on_error.rs

-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
5252

5353
update_clock_slot(&mut clock_account, 1);
5454

55-
5655
// Check that the normal upd_price fails
5756
populate_instruction(&mut instruction_data, 42, 9, 1, true);
5857

@@ -69,7 +68,6 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
6968
Err(OracleError::PermissionViolation.into())
7069
);
7170

72-
7371
populate_instruction(&mut instruction_data, 42, 9, 1, false);
7472
// We haven't permissioned the publish account for the price account
7573
// yet, so any update should fail silently and have no effect. The
@@ -85,7 +83,6 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
8583
)
8684
.is_ok());
8785

88-
8986
{
9087
let mut price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
9188
assert_eq!(price_data.comp_[0].latest_.price_, 0);
@@ -169,7 +166,6 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
169166
}
170167
}
171168

172-
173169
// Create an upd_price_no_fail_on_error or upd_price instruction with the provided parameters
174170
fn populate_instruction(
175171
instruction_data: &mut [u8],

0 commit comments

Comments
 (0)