1
+ #[ cfg( feature = "injective" ) ]
2
+ use crate :: injective:: {
3
+ create_relay_pyth_prices_msg,
4
+ InjectiveMsgWrapper as MsgWrapper ,
5
+ } ;
6
+ #[ cfg( not( feature = "injective" ) ) ]
7
+ use cosmwasm_std:: Empty as MsgWrapper ;
1
8
use {
2
9
crate :: {
3
10
governance:: {
@@ -128,7 +135,12 @@ pub fn parse_and_verify_vaa(deps: DepsMut, block_time: u64, data: &Binary) -> St
128
135
}
129
136
130
137
#[ cfg_attr( not( feature = "library" ) , entry_point) ]
131
- pub fn execute ( deps : DepsMut , env : Env , info : MessageInfo , msg : ExecuteMsg ) -> StdResult < Response > {
138
+ pub fn execute (
139
+ deps : DepsMut ,
140
+ env : Env ,
141
+ info : MessageInfo ,
142
+ msg : ExecuteMsg ,
143
+ ) -> StdResult < Response < MsgWrapper > > {
132
144
match msg {
133
145
ExecuteMsg :: UpdatePriceFeeds { data } => update_price_feeds ( deps, env, info, & data) ,
134
146
ExecuteMsg :: ExecuteGovernanceInstruction { data } => {
@@ -147,7 +159,7 @@ fn update_price_feeds(
147
159
env : Env ,
148
160
info : MessageInfo ,
149
161
data : & [ Binary ] ,
150
- ) -> StdResult < Response > {
162
+ ) -> StdResult < Response < MsgWrapper > > {
151
163
let state = config_read ( deps. storage ) . load ( ) ?;
152
164
153
165
// Check that a sufficient fee was sent with the message
@@ -157,8 +169,9 @@ fn update_price_feeds(
157
169
return Err ( PythContractError :: InsufficientFee . into ( ) ) ;
158
170
}
159
171
160
- let mut total_attestations: usize = 0 ;
161
- let mut new_attestations: usize = 0 ;
172
+ let mut num_total_attestations: usize = 0 ;
173
+ let mut total_new_attestations: Vec < PriceAttestation > = vec ! [ ] ;
174
+
162
175
for datum in data {
163
176
let vaa = parse_and_verify_vaa ( deps. branch ( ) , env. block . time . seconds ( ) , datum) ?;
164
177
verify_vaa_from_data_source ( & state, & vaa) ?;
@@ -167,16 +180,36 @@ fn update_price_feeds(
167
180
let batch_attestation = BatchPriceAttestation :: deserialize ( & data[ ..] )
168
181
. map_err ( |_| PythContractError :: InvalidUpdatePayload ) ?;
169
182
170
- let ( num_updates , num_new ) =
183
+ let ( num_attestations , new_attestations ) =
171
184
process_batch_attestation ( & mut deps, & env, & batch_attestation) ?;
172
- total_attestations += num_updates;
173
- new_attestations += num_new;
185
+ num_total_attestations += num_attestations;
186
+ for new_attestation in new_attestations {
187
+ total_new_attestations. push ( new_attestation. to_owned ( ) ) ;
188
+ }
174
189
}
175
190
176
- Ok ( Response :: new ( )
177
- . add_attribute ( "action" , "update_price_feeds" )
178
- . add_attribute ( "num_attestations" , format ! ( "{total_attestations}" ) )
179
- . add_attribute ( "num_updated" , format ! ( "{new_attestations}" ) ) )
191
+ let num_total_new_attestations = total_new_attestations. len ( ) ;
192
+
193
+ let response = Response :: new ( ) ;
194
+
195
+ #[ cfg( feature = "injective" ) ]
196
+ {
197
+ let inj_message =
198
+ create_relay_pyth_prices_msg ( env. contract . address , total_new_attestations) ;
199
+ Ok ( response
200
+ . add_message ( inj_message)
201
+ . add_attribute ( "action" , "update_price_feeds" )
202
+ . add_attribute ( "num_attestations" , format ! ( "{num_total_attestations}" ) )
203
+ . add_attribute ( "num_updated" , format ! ( "{num_total_new_attestations}" ) ) )
204
+ }
205
+
206
+ #[ cfg( not( feature = "injective" ) ) ]
207
+ {
208
+ Ok ( response
209
+ . add_attribute ( "action" , "update_price_feeds" )
210
+ . add_attribute ( "num_attestations" , format ! ( "{num_total_attestations}" ) )
211
+ . add_attribute ( "num_updated" , format ! ( "{num_total_new_attestations}" ) ) )
212
+ }
180
213
}
181
214
182
215
/// Execute a governance instruction provided as the VAA `data`.
@@ -187,7 +220,7 @@ fn execute_governance_instruction(
187
220
env : Env ,
188
221
_info : MessageInfo ,
189
222
data : & Binary ,
190
- ) -> StdResult < Response > {
223
+ ) -> StdResult < Response < MsgWrapper > > {
191
224
let vaa = parse_and_verify_vaa ( deps. branch ( ) , env. block . time . seconds ( ) , data) ?;
192
225
let state = config_read ( deps. storage ) . load ( ) ?;
193
226
verify_vaa_from_governance_source ( & state, & vaa) ?;
@@ -282,7 +315,7 @@ fn transfer_governance(
282
315
next_config : & mut ConfigInfo ,
283
316
current_config : & ConfigInfo ,
284
317
parsed_claim_vaa : & ParsedVAA ,
285
- ) -> StdResult < Response > {
318
+ ) -> StdResult < Response < MsgWrapper > > {
286
319
let claim_vaa_instruction =
287
320
GovernanceInstruction :: deserialize ( parsed_claim_vaa. payload . as_slice ( ) )
288
321
. map_err ( |_| PythContractError :: InvalidGovernancePayload ) ?;
@@ -335,7 +368,7 @@ fn transfer_governance(
335
368
/// Upgrades the contract at `address` to `new_code_id` (by sending a `Migrate` message). The
336
369
/// migration will fail unless this contract is the admin of the contract being upgraded.
337
370
/// (Typically, `address` is this contract's address, and the contract is its own admin.)
338
- fn upgrade_contract ( address : & Addr , new_code_id : u64 ) -> StdResult < Response > {
371
+ fn upgrade_contract ( address : & Addr , new_code_id : u64 ) -> StdResult < Response < MsgWrapper > > {
339
372
Ok ( Response :: new ( )
340
373
. add_message ( CosmosMsg :: Wasm ( WasmMsg :: Migrate {
341
374
contract_addr : address. to_string ( ) ,
@@ -371,26 +404,23 @@ fn verify_vaa_from_governance_source(state: &ConfigInfo, vaa: &ParsedVAA) -> Std
371
404
}
372
405
373
406
/// Update the on-chain storage for any new price updates provided in `batch_attestation`.
374
- fn process_batch_attestation (
407
+ fn process_batch_attestation < ' a > (
375
408
deps : & mut DepsMut ,
376
409
env : & Env ,
377
- batch_attestation : & BatchPriceAttestation ,
378
- ) -> StdResult < ( usize , usize ) > {
379
- let mut new_attestations_cnt : usize = 0 ;
410
+ batch_attestation : & ' a BatchPriceAttestation ,
411
+ ) -> StdResult < ( usize , Vec < & ' a PriceAttestation > ) > {
412
+ let mut new_attestations = vec ! [ ] ;
380
413
381
414
// Update prices
382
415
for price_attestation in batch_attestation. price_attestations . iter ( ) {
383
416
let price_feed = create_price_feed_from_price_attestation ( price_attestation) ;
384
417
385
418
if update_price_feed_if_new ( deps, env, price_feed) ? {
386
- new_attestations_cnt += 1 ;
419
+ new_attestations . push ( price_attestation ) ;
387
420
}
388
421
}
389
422
390
- Ok ( (
391
- batch_attestation. price_attestations . len ( ) ,
392
- new_attestations_cnt,
393
- ) )
423
+ Ok ( ( batch_attestation. price_attestations . len ( ) , new_attestations) )
394
424
}
395
425
396
426
fn create_price_feed_from_price_attestation ( price_attestation : & PriceAttestation ) -> PriceFeed {
@@ -691,7 +721,7 @@ mod test {
691
721
emitter_address : & [ u8 ] ,
692
722
emitter_chain : u16 ,
693
723
funds : & [ Coin ] ,
694
- ) -> StdResult < Response > {
724
+ ) -> StdResult < Response < MsgWrapper > > {
695
725
let ( mut deps, env) = setup_test ( ) ;
696
726
config ( & mut deps. storage ) . save ( config_info) . unwrap ( ) ;
697
727
@@ -706,11 +736,11 @@ mod test {
706
736
let attestations = BatchPriceAttestation {
707
737
price_attestations : vec ! [ ] ,
708
738
} ;
709
- let ( total_attestations , new_attestations) =
739
+ let ( num_attestations , new_attestations) =
710
740
process_batch_attestation ( & mut deps. as_mut ( ) , & env, & attestations) . unwrap ( ) ;
711
741
712
- assert_eq ! ( total_attestations , 0 ) ;
713
- assert_eq ! ( new_attestations, 0 ) ;
742
+ assert_eq ! ( num_attestations , 0 ) ;
743
+ assert_eq ! ( new_attestations. len ( ) , 0 ) ;
714
744
}
715
745
716
746
#[ test]
@@ -824,7 +854,7 @@ mod test {
824
854
let attestations = BatchPriceAttestation {
825
855
price_attestations : vec ! [ price_attestation] ,
826
856
} ;
827
- let ( total_attestations , new_attestations) =
857
+ let ( num_attestations , new_attestations) =
828
858
process_batch_attestation ( & mut deps. as_mut ( ) , & env, & attestations) . unwrap ( ) ;
829
859
830
860
@@ -834,8 +864,8 @@ mod test {
834
864
let price = stored_price_feed. get_price_unchecked ( ) ;
835
865
let ema_price = stored_price_feed. get_ema_price_unchecked ( ) ;
836
866
837
- assert_eq ! ( total_attestations , 1 ) ;
838
- assert_eq ! ( new_attestations, 1 ) ;
867
+ assert_eq ! ( num_attestations , 1 ) ;
868
+ assert_eq ! ( new_attestations. len ( ) , 1 ) ;
839
869
840
870
// for price
841
871
assert_eq ! ( price. price, 99 ) ;
@@ -876,7 +906,7 @@ mod test {
876
906
let attestations = BatchPriceAttestation {
877
907
price_attestations : vec ! [ price_attestation] ,
878
908
} ;
879
- let ( total_attestations , new_attestations) =
909
+ let ( num_attestations , new_attestations) =
880
910
process_batch_attestation ( & mut deps. as_mut ( ) , & env, & attestations) . unwrap ( ) ;
881
911
882
912
@@ -886,8 +916,8 @@ mod test {
886
916
let price = stored_price_feed. get_price_unchecked ( ) ;
887
917
let ema_price = stored_price_feed. get_ema_price_unchecked ( ) ;
888
918
889
- assert_eq ! ( total_attestations , 1 ) ;
890
- assert_eq ! ( new_attestations, 1 ) ;
919
+ assert_eq ! ( num_attestations , 1 ) ;
920
+ assert_eq ! ( new_attestations. len ( ) , 1 ) ;
891
921
892
922
// for price
893
923
assert_eq ! ( price. price, 100 ) ;
@@ -1147,7 +1177,7 @@ mod test {
1147
1177
fn apply_governance_vaa (
1148
1178
initial_config : & ConfigInfo ,
1149
1179
vaa : & ParsedVAA ,
1150
- ) -> StdResult < ( Response , ConfigInfo ) > {
1180
+ ) -> StdResult < ( Response < MsgWrapper > , ConfigInfo ) > {
1151
1181
let ( mut deps, env) = setup_test ( ) ;
1152
1182
config ( & mut deps. storage ) . save ( initial_config) . unwrap ( ) ;
1153
1183
0 commit comments