Deploys a station canister for the user.
(
&self,
input: DeployStationInput,
ctx: &CallContext,
)
| 45 | |
| 46 | /// Deploys a station canister for the user. |
| 47 | pub async fn deploy_station( |
| 48 | &self, |
| 49 | input: DeployStationInput, |
| 50 | ctx: &CallContext, |
| 51 | ) -> ServiceResult<Principal> { |
| 52 | let user = self.user_service.get_user_by_identity(&ctx.caller(), ctx)?; |
| 53 | let config = canister_config().ok_or(DeployError::Failed { |
| 54 | reason: "Canister config not initialized.".to_string(), |
| 55 | })?; |
| 56 | let upgrader_wasm_module = config.upgrader_wasm_module; |
| 57 | let station_wasm_module = config.station_wasm_module; |
| 58 | let station_wasm_module_extra_chunks = config.station_wasm_module_extra_chunks; |
| 59 | |
| 60 | let can_deploy_station_response = user.can_deploy_station(); |
| 61 | match can_deploy_station_response { |
| 62 | CanDeployStation::Allowed(_) => {} |
| 63 | CanDeployStation::QuotaExceeded => { |
| 64 | return Err(UserError::DeployStationQuotaExceeded)?; |
| 65 | } |
| 66 | CanDeployStation::NotAllowed(subscription_status) => { |
| 67 | return Err(UserError::BadUserSubscriptionStatus { |
| 68 | subscription_status: subscription_status.into(), |
| 69 | })?; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Creates the station canister |
| 74 | let station_canister = create_canister(input.subnet_selection, CANISTER_CREATION_CYCLES) |
| 75 | .await |
| 76 | .map_err(|err| DeployError::Failed { reason: err })?; |
| 77 | |
| 78 | // Determine the actual number of cycles for canister creation on the subnet |
| 79 | // to which the station canister and its upgrader canister are deployed. |
| 80 | let station = CanisterIdRecord { |
| 81 | canister_id: station_canister, |
| 82 | }; |
| 83 | let station_cycles: u128 = mgmt::canister_status(station) |
| 84 | .await |
| 85 | .map_err(|(_, err)| DeployError::Failed { reason: err })? |
| 86 | .0 |
| 87 | .cycles |
| 88 | .0 |
| 89 | .try_into() |
| 90 | .unwrap(); |
| 91 | let actual_canister_creation_cycles = |
| 92 | CANISTER_CREATION_CYCLES.saturating_sub(station_cycles); |
| 93 | |
| 94 | // Top up the station so that it has the target cycles balance after deploying the upgrader canister. |
| 95 | let upgrader_initial_cycles = actual_canister_creation_cycles + INITIAL_UPGRADER_CYCLES; |
| 96 | let station_initial_cycles = upgrader_initial_cycles + INITIAL_STATION_CYCLES; |
| 97 | let extra_cycles = station_initial_cycles.saturating_sub(station_cycles); |
| 98 | if extra_cycles > 0 { |
| 99 | check_balance_before_transfer(extra_cycles) |
| 100 | .await |
| 101 | .map_err(|err| DeployError::Failed { reason: err })?; |
| 102 | mgmt::deposit_cycles(station, extra_cycles) |
| 103 | .await |
| 104 | .map_err(|(_, err)| DeployError::Failed { reason: err })?; |
no test coverage detected