@@ -14,11 +14,11 @@ use std::{
14
14
keccak256,
15
15
sha256,
16
16
},
17
+ revert :: revert,
17
18
storage :: {
18
19
storage_map :: StorageMap ,
19
20
storage_vec :: * ,
20
21
},
21
- revert :: revert,
22
22
};
23
23
24
24
use pyth_interface :: {
@@ -31,19 +31,33 @@ use pyth_interface::{
31
31
update_type :: UpdateType ,
32
32
wormhole_light :: * ,
33
33
},
34
- errors :: {PythError , WormholeError },
35
- events :: {ConstructedEvent , NewGuardianSetEvent , UpdatedPriceFeedsEvent , ContractUpgradedEvent , GovernanceDataSourceSetEvent , DataSourcesSetEvent , FeeSetEvent , ValidPeriodSetEvent },
34
+ errors :: {
35
+ PythError ,
36
+ WormholeError ,
37
+ },
38
+ events :: {
39
+ ConstructedEvent ,
40
+ ContractUpgradedEvent ,
41
+ DataSourcesSetEvent ,
42
+ FeeSetEvent ,
43
+ GovernanceDataSourceSetEvent ,
44
+ NewGuardianSetEvent ,
45
+ UpdatedPriceFeedsEvent ,
46
+ ValidPeriodSetEvent ,
47
+ },
36
48
pyth_merkle_proof :: validate_proof,
37
- utils :: {difference, total_fee},
38
49
PythCore ,
39
50
PythInfo ,
40
51
PythInit ,
52
+ utils :: total_fee,
41
53
WormholeGuardians ,
42
54
};
43
55
44
56
use sway_libs :: ownership :: * ;
45
57
use standards :: src5 :: {SRC5 , State };
46
58
59
+ const GUARDIAN_SET_EXPIRATION_TIME_SECONDS : u64 = 86400 ; // 24 hours in seconds
60
+
47
61
configurable {
48
62
DEPLOYER : Identity = Identity :: Address (Address :: from (ZERO_B256 )),
49
63
}
@@ -140,7 +154,7 @@ impl PythCore for Contract {
140
154
141
155
let mut output_price_feeds : Vec <PriceFeed > = Vec :: with_capacity (target_price_feed_ids . len ());
142
156
let mut i = 0 ;
143
- while i < update_data . len () {
157
+ while i < update_data . len () {
144
158
let data = update_data . get (i ). unwrap ();
145
159
146
160
match UpdateType :: determine_type (data ) {
@@ -229,7 +243,7 @@ impl PythCore for Contract {
229
243
230
244
require (
231
245
target_price_feed_ids
232
- . len () == output_price_feeds
246
+ . len () == output_price_feeds
233
247
. len (),
234
248
PythError :: PriceFeedNotFoundWithinRange ,
235
249
);
@@ -270,13 +284,13 @@ impl PythCore for Contract {
270
284
) {
271
285
require (
272
286
price_feed_ids
273
- . len () == publish_times
287
+ . len () == publish_times
274
288
. len (),
275
289
PythError :: LengthOfPriceFeedIdsAndPublishTimesMustMatch ,
276
290
);
277
291
278
292
let mut i = 0 ;
279
- while i < price_feed_ids . len () {
293
+ while i < price_feed_ids . len () {
280
294
if latest_publish_time (price_feed_ids . get (i ). unwrap ()) < publish_times . get (i ). unwrap ()
281
295
{
282
296
update_price_feeds (update_data );
@@ -297,9 +311,9 @@ impl PythCore for Contract {
297
311
#[storage(read)]
298
312
fn ema_price_no_older_than (time_period : u64 , price_feed_id : PriceFeedId ) -> Price {
299
313
let price = ema_price_unsafe (price_feed_id );
300
-
314
+ let current_time = timestamp ();
301
315
require (
302
- difference ( timestamp (), price . publish_time) <= time_period ,
316
+ current_time - price . publish_time <= time_period ,
303
317
PythError :: OutdatedPrice ,
304
318
);
305
319
@@ -317,8 +331,9 @@ fn ema_price_unsafe(price_feed_id: PriceFeedId) -> Price {
317
331
#[storage(read)]
318
332
fn price_no_older_than (time_period : u64 , price_feed_id : PriceFeedId ) -> Price {
319
333
let price = price_unsafe (price_feed_id );
334
+ let current_time = timestamp ();
320
335
require (
321
- difference ( timestamp (), price . publish_time) <= time_period ,
336
+ current_time - price . publish_time <= time_period ,
322
337
PythError :: OutdatedPrice ,
323
338
);
324
339
@@ -337,7 +352,7 @@ fn price_unsafe(price_feed_id: PriceFeedId) -> Price {
337
352
fn update_fee (update_data : Vec <Bytes >) -> u64 {
338
353
let mut total_number_of_updates = 0 ;
339
354
let mut i = 0 ;
340
- while i < update_data . len () {
355
+ while i < update_data . len () {
341
356
let data = update_data . get (i ). unwrap ();
342
357
343
358
match UpdateType :: determine_type (data ) {
@@ -368,7 +383,7 @@ fn update_price_feeds(update_data: Vec<Bytes>) {
368
383
369
384
// let mut updated_price_feeds: Vec<PriceFeedId> = Vec::new(); // TODO: requires append for Vec
370
385
let mut i = 0 ;
371
- while i < update_data . len () {
386
+ while i < update_data . len () {
372
387
let data = update_data . get (i ). unwrap ();
373
388
374
389
match UpdateType :: determine_type (data ) {
@@ -473,7 +488,7 @@ impl PythInit for Contract {
473
488
// This function ensures that the sender is the owner. https://github.com/FuelLabs/sway-libs/blob/8045a19e3297599750abdf6300c11e9927a29d40/libs/src/ownership.sw#L59-L65
474
489
only_owner ();
475
490
476
- require (data_sources . len () > 0 , PythError :: InvalidDataSourcesLength );
491
+ require (data_sources . len () > 0 , PythError :: InvalidDataSourcesLength );
477
492
478
493
let mut i = 0 ;
479
494
while i < data_sources . len () {
@@ -483,7 +498,9 @@ impl PythInit for Contract {
483
498
484
499
i += 1 ;
485
500
}
486
- storage . latest_price_feed. write (StorageMap :: <PriceFeedId , PriceFeed > {});
501
+ storage
502
+ . latest_price_feed
503
+ . write (StorageMap :: <PriceFeedId , PriceFeed > {});
487
504
488
505
storage
489
506
. valid_time_period_seconds
@@ -493,7 +510,11 @@ impl PythInit for Contract {
493
510
let guardian_length : u8 = wormhole_guardian_set_addresses . len (). try_as_u8 (). unwrap ();
494
511
let mut new_guardian_set = StorageGuardianSet :: new (
495
512
0 ,
496
- StorageKey :: <StorageVec <b256 >>:: new (sha256 ((" guardian_set_keys" , wormhole_guardian_set_index )), 0 , ZERO_B256 ),
513
+ StorageKey :: <StorageVec <b256 >>:: new (
514
+ sha256 ((" guardian_set_keys" , wormhole_guardian_set_index )),
515
+ 0 ,
516
+ ZERO_B256 ,
517
+ ),
497
518
);
498
519
let mut i : u8 = 0 ;
499
520
while i < guardian_length {
@@ -502,17 +523,27 @@ impl PythInit for Contract {
502
523
i += 1 ;
503
524
}
504
525
505
- storage . wormhole_guardian_set_index. write (wormhole_guardian_set_index );
506
- storage . wormhole_guardian_sets. insert (wormhole_guardian_set_index , new_guardian_set );
526
+ storage
527
+ . wormhole_guardian_set_index
528
+ . write (wormhole_guardian_set_index );
529
+ storage
530
+ . wormhole_guardian_sets
531
+ . insert (wormhole_guardian_set_index , new_guardian_set );
507
532
508
533
storage . governance_data_source. write (governance_data_source );
509
- storage . wormhole_governance_data_source. write (wormhole_governance_data_source );
534
+ storage
535
+ . wormhole_governance_data_source
536
+ . write (wormhole_governance_data_source );
510
537
storage . governance_data_source_index. write (0 );
511
- storage . wormhole_consumed_governance_actions. write (StorageMap :: <b256 , bool > {});
538
+ storage
539
+ . wormhole_consumed_governance_actions
540
+ . write (StorageMap :: <b256 , bool > {});
512
541
storage . chain_id. write (chain_id );
513
542
storage . last_executed_governance_sequence. write (0 );
514
543
515
- storage . current_implementation. write (Identity :: Address (Address :: from (ZERO_B256 )));
544
+ storage
545
+ . current_implementation
546
+ . write (Identity :: Address (Address :: from (ZERO_B256 )));
516
547
517
548
// This function revokes ownership of the current owner and disallows any new owners. https://github.com/FuelLabs/sway-libs/blob/8045a19e3297599750abdf6300c11e9927a29d40/libs/src/ownership.sw#L89-L99
518
549
renounce_ownership ();
@@ -670,7 +701,7 @@ fn submit_new_guardian_set(encoded_vm: Bytes) {
670
701
let current_guardian_set = storage . wormhole_guardian_sets. get (current_guardian_set_index ). try_read ();
671
702
if current_guardian_set . is_some () {
672
703
let mut current_guardian_set = current_guardian_set . unwrap ();
673
- current_guardian_set . expiration_time = timestamp () + 86400 ;
704
+ current_guardian_set . expiration_time = timestamp () + GUARDIAN_SET_EXPIRATION_TIME_SECONDS ;
674
705
storage
675
706
. wormhole_guardian_sets
676
707
. insert (current_guardian_set_index , current_guardian_set );
@@ -691,27 +722,41 @@ fn submit_new_guardian_set(encoded_vm: Bytes) {
691
722
692
723
/// Transfer the governance data source to a new value with sanity checks to ensure the new governance data source can manage the contract.
693
724
#[storage(read, write)]
694
- fn authorize_governance_data_source_transfer (payload : AuthorizeGovernanceDataSourceTransferPayload ) {
725
+ fn authorize_governance_data_source_transfer (
726
+ payload : AuthorizeGovernanceDataSourceTransferPayload ,
727
+ ) {
695
728
let old_governance_data_source = governance_data_source ();
696
729
697
730
// Parse and verify the VAA contained in the payload to ensure it's valid and can manage the contract
698
731
let vm : WormholeVM = WormholeVM :: parse_and_verify_wormhole_vm (
699
732
current_guardian_set_index (),
700
- payload . claim_vaa,
701
- storage . wormhole_guardian_sets,
733
+ payload
734
+ . claim_vaa,
735
+ storage
736
+ . wormhole_guardian_sets,
702
737
);
703
738
704
739
let gi = GovernanceInstruction :: parse_governance_instruction (vm . payload);
705
- require (gi . target_chain_id == chain_id () || gi . target_chain_id == 0 , PythError :: InvalidGovernanceTarget );
740
+ require (
741
+ gi . target_chain_id == chain_id () || gi . target_chain_id == 0 ,
742
+ PythError :: InvalidGovernanceTarget ,
743
+ );
706
744
707
- require (match gi . action {
708
- GovernanceAction :: RequestGovernanceDataSourceTransfer => true ,
709
- _ => false ,
710
- }, PythError :: InvalidGovernanceMessage );
745
+ require (
746
+ match gi . action {
747
+ GovernanceAction :: RequestGovernanceDataSourceTransfer => true ,
748
+ _ => false ,
749
+ },
750
+ PythError :: InvalidGovernanceMessage ,
751
+ );
711
752
712
753
let claim_payload = GovernanceInstruction :: parse_request_governance_data_source_transfer_payload (gi . payload);
713
754
714
- require (governance_data_source_index () < claim_payload . governance_data_source_index, PythError :: OldGovernanceMessage );
755
+ require (
756
+ governance_data_source_index () < claim_payload
757
+ . governance_data_source_index,
758
+ PythError :: OldGovernanceMessage ,
759
+ );
715
760
716
761
set_governance_data_source_index (claim_payload . governance_data_source_index);
717
762
@@ -737,7 +782,7 @@ fn set_data_sources(payload: SetDataSourcesPayload) {
737
782
let old_data_sources = storage . valid_data_sources. load_vec ();
738
783
739
784
let mut i = 0 ;
740
- while i < old_data_sources . len () {
785
+ while i < old_data_sources . len () {
741
786
let data_source = old_data_sources . get (i ). unwrap ();
742
787
storage . is_valid_data_source. insert (data_source , false );
743
788
i += 1 ;
@@ -748,7 +793,7 @@ fn set_data_sources(payload: SetDataSourcesPayload) {
748
793
749
794
i = 0 ;
750
795
// Add new data sources from the payload and mark them as valid
751
- while i < payload . data_sources. len () {
796
+ while i < payload . data_sources. len () {
752
797
let data_source = payload . data_sources. get (i ). unwrap ();
753
798
storage . valid_data_sources. push (data_source );
754
799
storage . is_valid_data_source. insert (data_source , true );
@@ -777,7 +822,9 @@ fn set_fee(payload: SetFeePayload) {
777
822
#[storage(read, write)]
778
823
fn set_valid_period (payload : SetValidPeriodPayload ) {
779
824
let old_valid_period = storage . valid_time_period_seconds. read ();
780
- storage . valid_time_period_seconds. write (payload . new_valid_period);
825
+ storage
826
+ . valid_time_period_seconds
827
+ . write (payload . new_valid_period);
781
828
782
829
log (ValidPeriodSetEvent {
783
830
old_valid_period ,
@@ -809,7 +856,10 @@ impl PythGovernance for Contract {
809
856
// Log so that the GovernanceInstruction struct will show up in the ABI and can be used in the tests
810
857
log (gi );
811
858
812
- require (gi . target_chain_id == chain_id () || gi . target_chain_id == 0 , PythError :: InvalidGovernanceTarget );
859
+ require (
860
+ gi . target_chain_id == chain_id () || gi . target_chain_id == 0 ,
861
+ PythError :: InvalidGovernanceTarget ,
862
+ );
813
863
814
864
match gi . action {
815
865
GovernanceAction :: UpgradeContract => {
@@ -853,7 +903,6 @@ impl PythGovernance for Contract {
853
903
}
854
904
}
855
905
856
-
857
906
#[storage(read, write)]
858
907
fn verify_governance_vm (encoded_vm : Bytes ) -> WormholeVM {
859
908
let vm : WormholeVM = WormholeVM :: parse_and_verify_wormhole_vm (
0 commit comments