@@ -102,6 +102,7 @@ fn test_add_price() {
102102 assert_eq ! ( price_data. min_pub_, PRICE_ACCOUNT_DEFAULT_MIN_PUB ) ;
103103 assert ! ( price_data. product_account == * product_account. key) ;
104104 assert ! ( price_data. next_price_account == Pubkey :: default ( ) ) ;
105+ assert_eq ! ( price_data. feed_index, 1 ) ;
105106 assert ! ( product_data. first_price_account == * price_account. key) ;
106107 }
107108
@@ -125,9 +126,62 @@ fn test_add_price() {
125126 assert_eq ! ( price_data_2. min_pub_, PRICE_ACCOUNT_DEFAULT_MIN_PUB ) ;
126127 assert ! ( price_data_2. product_account == * product_account. key) ;
127128 assert ! ( price_data_2. next_price_account == * price_account. key) ;
129+ assert_eq ! ( price_data_2. feed_index, 2 ) ;
128130 assert ! ( product_data. first_price_account == * price_account_2. key) ;
129131 }
130132
133+ // Emulate pre-existing price accounts without a feed index.
134+ {
135+ let mut price_data = load_checked :: < PriceAccount > ( & price_account, PC_VERSION ) . unwrap ( ) ;
136+ price_data. feed_index = 0 ;
137+ let mut price_data_2 = load_checked :: < PriceAccount > ( & price_account_2, PC_VERSION ) . unwrap ( ) ;
138+ price_data_2. feed_index = 0 ;
139+ }
140+ let hdr_init_price_feed_index = CommandHeader :: from ( OracleCommand :: InitPriceFeedIndex ) ;
141+ let instruction_data_init_price_feed_index = bytes_of ( & hdr_init_price_feed_index) ;
142+ process_instruction (
143+ & program_id,
144+ & [
145+ funding_account. clone ( ) ,
146+ price_account. clone ( ) ,
147+ permissions_account. clone ( ) ,
148+ ] ,
149+ instruction_data_init_price_feed_index,
150+ )
151+ . unwrap ( ) ;
152+ {
153+ let price_data = load_checked :: < PriceAccount > ( & price_account, PC_VERSION ) . unwrap ( ) ;
154+ assert_eq ! ( price_data. feed_index, 3 ) ;
155+ }
156+ process_instruction (
157+ & program_id,
158+ & [
159+ funding_account. clone ( ) ,
160+ price_account_2. clone ( ) ,
161+ permissions_account. clone ( ) ,
162+ ] ,
163+ instruction_data_init_price_feed_index,
164+ )
165+ . unwrap ( ) ;
166+ {
167+ let price_data_2 = load_checked :: < PriceAccount > ( & price_account_2, PC_VERSION ) . unwrap ( ) ;
168+ assert_eq ! ( price_data_2. feed_index, 4 ) ;
169+ }
170+
171+ // Feed index is already set.
172+ assert_eq ! (
173+ process_instruction(
174+ & program_id,
175+ & [
176+ funding_account. clone( ) ,
177+ price_account_2. clone( ) ,
178+ permissions_account. clone( ) ,
179+ ] ,
180+ instruction_data_init_price_feed_index,
181+ ) ,
182+ Err ( OracleError :: FeedIndexAlreadyInitialized . into( ) )
183+ ) ;
184+
131185 // Wrong number of accounts
132186 assert_eq ! (
133187 process_instruction(
0 commit comments