2626 state:: {
2727 config,
2828 config_read,
29- deprecated_config,
30- deprecated_config_read,
3129 price_feed_bucket,
3230 price_feed_read_bucket,
31+ set_contract_version,
3332 ConfigInfo ,
3433 PythDataSource ,
3534 } ,
5150 OverflowOperation ,
5251 QueryRequest ,
5352 Response ,
54- StdError ,
5553 StdResult ,
5654 WasmMsg ,
5755 WasmQuery ,
8280 } ,
8381} ;
8482
83+ const CONTRACT_VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
84+
8585/// Migration code that runs once when the contract is upgraded. On upgrade, the migrate
8686/// function in the *new* code version is run, which allows the new code to update the on-chain
8787/// state before any of its other functions are invoked.
9494/// `Ok(Response::default())`
9595#[ cfg_attr( not( feature = "library" ) , entry_point) ]
9696pub 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 ) )
120100}
121101
122102#[ cfg_attr( not( feature = "library" ) , entry_point) ]
@@ -139,6 +119,8 @@ pub fn instantiate(
139119 } ;
140120 config ( deps. storage ) . save ( & state) ?;
141121
122+ set_contract_version ( deps. storage , & String :: from ( CONTRACT_VERSION ) ) ?;
123+
142124 Ok ( Response :: default ( ) )
143125}
144126
@@ -563,9 +545,12 @@ pub fn get_valid_time_period(deps: &Deps) -> StdResult<Duration> {
563545mod test {
564546 use {
565547 super :: * ,
566- crate :: governance:: GovernanceModule :: {
567- Executor ,
568- Target ,
548+ crate :: {
549+ governance:: GovernanceModule :: {
550+ Executor ,
551+ Target ,
552+ } ,
553+ state:: get_contract_version,
569554 } ,
570555 cosmwasm_std:: {
571556 coins,
@@ -754,6 +739,75 @@ mod test {
754739 update_price_feeds ( deps. as_mut ( ) , env, info, & [ msg] )
755740 }
756741
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+
757811 #[ test]
758812 fn test_process_batch_attestation_empty_array ( ) {
759813 let ( mut deps, env) = setup_test ( ) ;
0 commit comments