11use crate :: killswitch:: execute_cancel_stream_with_threshold;
2- use crate :: migrate_v0_2_1 :: migrate_v0_2_1 ;
2+ use crate :: migrate_v0_1_0 :: { migrate_v0_1_0 , OLD_POSITIONS } ;
33use crate :: msg:: {
44 AveragePriceResponse , ConfigResponse , ExecuteMsg , InstantiateMsg , LatestStreamedPriceResponse ,
55 MigrateMsg , PositionResponse , PositionsResponse , QueryMsg , StreamResponse , StreamsResponse ,
@@ -234,6 +234,9 @@ pub fn execute(
234234 exit_fee_percent,
235235 tos_version,
236236 ) ,
237+ ExecuteMsg :: MigratePosition { stream_id } => {
238+ execute_migrate_position ( deps, env, info, stream_id)
239+ }
237240 }
238241}
239242#[ allow( clippy:: too_many_arguments) ]
@@ -1123,6 +1126,36 @@ pub fn execute_update_config(
11231126 Ok ( Response :: default ( ) . add_attributes ( attributes) )
11241127}
11251128
1129+ pub fn execute_migrate_position (
1130+ deps : DepsMut ,
1131+ _env : Env ,
1132+ info : MessageInfo ,
1133+ stream_id : u64 ,
1134+ ) -> Result < Response , ContractError > {
1135+ // Old positions state is loaded and saved in new format
1136+ let old_position = OLD_POSITIONS . load ( deps. storage , ( stream_id, info. sender . clone ( ) ) ) ?;
1137+ let position: Position = Position {
1138+ owner : old_position. owner ,
1139+ in_balance : Uint256 :: from_uint128 ( old_position. in_balance ) ,
1140+ shares : Uint256 :: from_uint128 ( old_position. shares ) ,
1141+ index : old_position. index ,
1142+ last_updated : old_position. last_updated ,
1143+ operator : old_position. operator ,
1144+ tos_version : "" . to_string ( ) ,
1145+ pending_purchase : old_position. pending_purchase ,
1146+ purchased : Uint256 :: from_u128 ( old_position. purchased . into ( ) ) ,
1147+ spent : Uint256 :: from_u128 ( old_position. spent . into ( ) ) ,
1148+ } ;
1149+ OLD_POSITIONS . remove ( deps. storage , ( stream_id, info. sender . clone ( ) ) ) ;
1150+ POSITIONS . save ( deps. storage , ( stream_id, & info. sender . clone ( ) ) , & position) ?;
1151+
1152+ let attributes = vec ! [
1153+ attr( "action" , "migrate_position" ) ,
1154+ attr( "stream_id" , stream_id. to_string( ) ) ,
1155+ attr( "owner" , info. sender. clone( ) ) ,
1156+ ] ;
1157+ Ok ( Response :: default ( ) . add_attributes ( attributes) )
1158+ }
11261159fn check_access (
11271160 info : & MessageInfo ,
11281161 position_owner : & Addr ,
@@ -1149,21 +1182,29 @@ pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractE
11491182
11501183#[ cfg_attr( not( feature = "library" ) , entry_point) ]
11511184pub fn migrate ( deps : DepsMut , _env : Env , _msg : MigrateMsg ) -> Result < Response , ContractError > {
1152- let contract_info = get_contract_version ( deps. storage ) ?;
1153- let storage_contract_name: String = contract_info. contract ;
1154- let storage_version: Version = contract_info. version . parse ( ) . map_err ( from_semver) ?;
1155- let version: Version = CONTRACT_VERSION . parse ( ) . map_err ( from_semver) ?;
1156-
1157- if storage_contract_name != CONTRACT_NAME {
1185+ // This is a hard coded version of the contract that was deployed before the migration
1186+ const OLDER_CONTRACT_VERSION : & str = "0.1.0" ;
1187+ let storage_contract_info = get_contract_version ( deps. storage ) ?;
1188+ let storage_contract_name: String = storage_contract_info. contract ;
1189+ let storage_contract_version: Version =
1190+ storage_contract_info. version . parse ( ) . map_err ( from_semver) ?;
1191+
1192+ if !( storage_contract_name == CONTRACT_NAME ) {
11581193 return Err ( ContractError :: CannotMigrate {
1159- previous_contract : storage_contract_name ,
1194+ previous_contract : storage_contract_info . version ,
11601195 } ) ;
11611196 }
1162- if storage_version < version {
1163- set_contract_version ( deps. storage , CONTRACT_NAME , CONTRACT_VERSION ) ?;
1164- // migrate v0.2.0 -> v0.2.1
1165- migrate_v0_2_1 ( deps. storage ) ?;
1197+
1198+ if OLDER_CONTRACT_VERSION != storage_contract_version. to_string ( ) {
1199+ return Err ( ContractError :: CannotMigrate {
1200+ previous_contract : storage_contract_info. version ,
1201+ } ) ;
11661202 }
1203+ // Set the new contract version
1204+ set_contract_version ( deps. storage , CONTRACT_NAME , CONTRACT_VERSION ) ?;
1205+
1206+ // Migrate state from old contract to new contract
1207+ migrate_v0_1_0 ( deps. storage ) ?;
11671208
11681209 Ok ( Response :: default ( ) )
11691210}
0 commit comments