11use {
2+ super :: reserve_new_price_feed_index,
23 crate :: {
3- accounts:: {
4- PermissionAccount ,
5- PriceAccount ,
6- } ,
4+ accounts:: PriceAccount ,
75 deserialize:: {
86 load,
97 load_checked,
@@ -12,28 +10,25 @@ use {
1210 utils:: {
1311 check_permissioned_funding_account,
1412 check_valid_funding_account,
13+ check_valid_writable_account,
1514 pyth_assert,
1615 } ,
1716 OracleError ,
1817 } ,
1918 solana_program:: {
2019 account_info:: AccountInfo ,
2120 entrypoint:: ProgramResult ,
22- program:: invoke,
2321 program_error:: ProgramError ,
2422 pubkey:: Pubkey ,
25- rent:: Rent ,
26- system_instruction,
27- sysvar:: Sysvar ,
2823 } ,
2924 std:: mem:: size_of,
3025} ;
3126
3227/// Init price feed index
33- // account[0] funding account [signer writable]
34- // account[1] price account [writable]
35- // account[2] permissions account [writable]
36- // account[3] system program account
28+ // account[0] funding account [signer writable]
29+ // account[1] price account [writable]
30+ // account[2] permissions account [writable]
31+ // account[3] system program account []
3732pub fn init_price_feed_index (
3833 program_id : & Pubkey ,
3934 accounts : & [ AccountInfo ] ,
@@ -59,42 +54,19 @@ pub fn init_price_feed_index(
5954 permissions_account,
6055 cmd,
6156 ) ?;
57+ check_valid_writable_account ( program_id, permissions_account) ?;
6258 pyth_assert (
6359 solana_program:: system_program:: check_id ( system_program. key ) ,
6460 OracleError :: InvalidSystemAccount . into ( ) ,
6561 ) ?;
6662
67- if permissions_account. data_len ( ) < PermissionAccount :: MIN_SIZE_WITH_LAST_FEED_INDEX {
68- let new_size = PermissionAccount :: MIN_SIZE_WITH_LAST_FEED_INDEX ;
69- let rent = Rent :: get ( ) ?;
70- let new_minimum_balance = rent. minimum_balance ( new_size) ;
71- let lamports_diff = new_minimum_balance. saturating_sub ( permissions_account. lamports ( ) ) ;
72- if lamports_diff > 0 {
73- invoke (
74- & system_instruction:: transfer (
75- funding_account. key ,
76- permissions_account. key ,
77- lamports_diff,
78- ) ,
79- & [
80- funding_account. clone ( ) ,
81- permissions_account. clone ( ) ,
82- system_program. clone ( ) ,
83- ] ,
84- ) ?;
85- }
86-
87- permissions_account. realloc ( new_size, true ) ?;
88- }
89- let mut last_feed_index = PermissionAccount :: load_last_feed_index_mut ( permissions_account) ?;
90- * last_feed_index += 1 ;
63+ let mut price_account_data = load_checked :: < PriceAccount > ( price_account, cmd. version ) ?;
9164 pyth_assert (
92- * last_feed_index < ( 1 << 28 ) ,
93- OracleError :: MaxLastFeedIndexReached . into ( ) ,
65+ price_account_data . feed_index == 0 ,
66+ OracleError :: FeedIndexAlreadyInitialized . into ( ) ,
9467 ) ?;
95-
96- let mut price_account_data = load_checked :: < PriceAccount > ( price_account, cmd. version ) ?;
97- price_account_data. feed_index = * last_feed_index;
68+ price_account_data. feed_index =
69+ reserve_new_price_feed_index ( funding_account, permissions_account, system_program) ?;
9870
9971 Ok ( ( ) )
10072}
0 commit comments