26
26
state:: {
27
27
config,
28
28
config_read,
29
- deprecated_config,
30
- deprecated_config_read,
31
29
price_feed_bucket,
32
30
price_feed_read_bucket,
31
+ set_contract_version,
33
32
ConfigInfo ,
34
33
PythDataSource ,
35
34
} ,
51
50
OverflowOperation ,
52
51
QueryRequest ,
53
52
Response ,
54
- StdError ,
55
53
StdResult ,
56
54
WasmMsg ,
57
55
WasmQuery ,
82
80
} ,
83
81
} ;
84
82
83
+ const CONTRACT_VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
84
+
85
85
/// Migration code that runs once when the contract is upgraded. On upgrade, the migrate
86
86
/// function in the *new* code version is run, which allows the new code to update the on-chain
87
87
/// state before any of its other functions are invoked.
94
94
/// `Ok(Response::default())`
95
95
#[ cfg_attr( not( feature = "library" ) , entry_point) ]
96
96
pub fn migrate ( deps : DepsMut , _env : Env , _msg : MigrateMsg ) -> StdResult < Response > {
97
- let depreceated_cfg_result = deprecated_config_read ( deps. storage ) . load ( ) ;
98
- match depreceated_cfg_result {
99
- Ok ( depreceated_cfg) => {
100
- let cfg = ConfigInfo {
101
- wormhole_contract : depreceated_cfg. wormhole_contract ,
102
- data_sources : depreceated_cfg. data_sources ,
103
- governance_source : depreceated_cfg. governance_source ,
104
- governance_source_index : depreceated_cfg. governance_source_index ,
105
- governance_sequence_number : depreceated_cfg. governance_sequence_number ,
106
- chain_id : depreceated_cfg. chain_id ,
107
- valid_time_period : depreceated_cfg. valid_time_period ,
108
- fee : depreceated_cfg. fee ,
109
- } ;
110
-
111
- config ( deps. storage ) . save ( & cfg) ?;
112
- deprecated_config ( deps. storage ) . remove ( ) ;
113
-
114
- Ok ( Response :: default ( ) )
115
- }
116
- Err ( _) => Err ( StdError :: GenericErr {
117
- msg : String :: from ( "Error reading config" ) ,
118
- } ) ,
119
- }
97
+ // a new contract version should be set everytime a contract is migrated
98
+ set_contract_version ( deps. storage , & String :: from ( CONTRACT_VERSION ) ) ?;
99
+ Ok ( Response :: default ( ) . add_attribute ( "Contract Version" , CONTRACT_VERSION ) )
120
100
}
121
101
122
102
#[ cfg_attr( not( feature = "library" ) , entry_point) ]
@@ -139,6 +119,8 @@ pub fn instantiate(
139
119
} ;
140
120
config ( deps. storage ) . save ( & state) ?;
141
121
122
+ set_contract_version ( deps. storage , & String :: from ( CONTRACT_VERSION ) ) ?;
123
+
142
124
Ok ( Response :: default ( ) )
143
125
}
144
126
@@ -563,9 +545,12 @@ pub fn get_valid_time_period(deps: &Deps) -> StdResult<Duration> {
563
545
mod test {
564
546
use {
565
547
super :: * ,
566
- crate :: governance:: GovernanceModule :: {
567
- Executor ,
568
- Target ,
548
+ crate :: {
549
+ governance:: GovernanceModule :: {
550
+ Executor ,
551
+ Target ,
552
+ } ,
553
+ state:: get_contract_version,
569
554
} ,
570
555
cosmwasm_std:: {
571
556
coins,
@@ -754,6 +739,75 @@ mod test {
754
739
update_price_feeds ( deps. as_mut ( ) , env, info, & [ msg] )
755
740
}
756
741
742
+ #[ test]
743
+ fn test_instantiate ( ) {
744
+ let mut deps = mock_dependencies ( ) ;
745
+
746
+ let instantiate_msg = InstantiateMsg {
747
+ // this is an example wormhole contract address in order to create a valid instantiate message
748
+ wormhole_contract : String :: from ( "inj1xx3aupmgv3ce537c0yce8zzd3sz567syuyedpg" ) ,
749
+ data_sources : Vec :: new ( ) ,
750
+ governance_source : PythDataSource {
751
+ emitter : Binary ( vec ! [ ] ) ,
752
+ chain_id : 0 ,
753
+ } ,
754
+ governance_source_index : 0 ,
755
+ governance_sequence_number : 0 ,
756
+ chain_id : 0 ,
757
+ valid_time_period_secs : 0 ,
758
+ fee : Coin :: new ( 0 , "" ) ,
759
+ } ;
760
+
761
+ let res = instantiate (
762
+ deps. as_mut ( ) ,
763
+ mock_env ( ) ,
764
+ MessageInfo {
765
+ sender : Addr :: unchecked ( "" ) ,
766
+ funds : Vec :: new ( ) ,
767
+ } ,
768
+ instantiate_msg,
769
+ ) ;
770
+ assert ! ( res. is_ok( ) ) ;
771
+
772
+ // check config
773
+ let config_result = config ( & mut deps. storage ) . load ( ) ;
774
+ assert ! ( config_result. is_ok( ) ) ;
775
+
776
+ // check contract version
777
+ let contract_version = get_contract_version ( & mut deps. storage ) ;
778
+ assert_eq ! ( contract_version, Ok ( String :: from( CONTRACT_VERSION ) ) ) ;
779
+ }
780
+
781
+ #[ test]
782
+ fn test_instantiate_invalid_wormhole_address ( ) {
783
+ let mut deps = mock_dependencies ( ) ;
784
+
785
+ let instantiate_msg = InstantiateMsg {
786
+ wormhole_contract : String :: from ( "" ) ,
787
+ data_sources : Vec :: new ( ) ,
788
+ governance_source : PythDataSource {
789
+ emitter : Binary ( vec ! [ ] ) ,
790
+ chain_id : 0 ,
791
+ } ,
792
+ governance_source_index : 0 ,
793
+ governance_sequence_number : 0 ,
794
+ chain_id : 0 ,
795
+ valid_time_period_secs : 0 ,
796
+ fee : Coin :: new ( 0 , "" ) ,
797
+ } ;
798
+
799
+ let res = instantiate (
800
+ deps. as_mut ( ) ,
801
+ mock_env ( ) ,
802
+ MessageInfo {
803
+ sender : Addr :: unchecked ( "" ) ,
804
+ funds : Vec :: new ( ) ,
805
+ } ,
806
+ instantiate_msg,
807
+ ) ;
808
+ assert ! ( res. is_err( ) ) ;
809
+ }
810
+
757
811
#[ test]
758
812
fn test_process_batch_attestation_empty_array ( ) {
759
813
let ( mut deps, env) = setup_test ( ) ;
0 commit comments