|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use clap::{builder::PossibleValue, Parser, ValueEnum}; |
| 4 | +use dialoguer::{console::Style, theme::ColorfulTheme, Input, Select}; |
| 5 | +use surfpool_types::{BlockProductionMode, CreateNetworkRequest, CreateNetworkResponse}; |
| 6 | +use txtx_cloud::{auth::AuthConfig, workspace::get_user_workspaces, LoginCommand}; |
| 7 | +use txtx_gql::kit::{reqwest, uuid::Uuid}; |
| 8 | + |
| 9 | +use crate::cli::DEFAULT_RPC_URL; |
| 10 | + |
| 11 | +#[derive(Parser, PartialEq, Clone, Debug)] |
| 12 | +pub struct CloudStartCommand { |
| 13 | + /// The name of the workspace |
| 14 | + #[arg(long = "workspace", short = 'w')] |
| 15 | + pub workspace_name: Option<String>, |
| 16 | + |
| 17 | + /// The name of the surfnet that will be created |
| 18 | + #[arg(long = "name", short = 'n')] |
| 19 | + pub name: Option<String>, |
| 20 | + |
| 21 | + /// A description for the surfnet |
| 22 | + #[arg(long = "description", short = 'd')] |
| 23 | + pub description: Option<String>, |
| 24 | + |
| 25 | + /// The RPC url to use for the datasource |
| 26 | + #[arg(long = "rpc-url", short = 'u', default_value = DEFAULT_RPC_URL)] |
| 27 | + pub datasource_rpc_url: String, |
| 28 | + |
| 29 | + /// The block production mode for the surfnet. Options are `clock`, `transaction`, and `manual`. |
| 30 | + #[arg(long = "block-production", short = 'b')] |
| 31 | + pub block_production_mode: Option<CliBlockProductionMode>, |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug, Clone, PartialEq)] |
| 35 | +pub struct CliBlockProductionMode(BlockProductionMode); |
| 36 | +impl ValueEnum for CliBlockProductionMode { |
| 37 | + fn value_variants<'a>() -> &'a [Self] { |
| 38 | + &[ |
| 39 | + CliBlockProductionMode(BlockProductionMode::Clock), |
| 40 | + CliBlockProductionMode(BlockProductionMode::Transaction), |
| 41 | + CliBlockProductionMode(BlockProductionMode::Manual), |
| 42 | + ] |
| 43 | + } |
| 44 | + |
| 45 | + fn to_possible_value(&self) -> Option<PossibleValue> { |
| 46 | + match self.0 { |
| 47 | + BlockProductionMode::Clock => Some(PossibleValue::new("clock")), |
| 48 | + BlockProductionMode::Transaction => Some(PossibleValue::new("transaction")), |
| 49 | + BlockProductionMode::Manual => Some(PossibleValue::new("manual")), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl CliBlockProductionMode { |
| 55 | + fn choices() -> Vec<String> { |
| 56 | + vec![ |
| 57 | + "Produce blocks every 400ms".to_string(), |
| 58 | + "Only produce blocks when transactions are received".to_string(), |
| 59 | + "Full manual control (via RPC methods / cloud.txtx.run)".to_string(), |
| 60 | + ] |
| 61 | + } |
| 62 | + fn from_index(index: usize) -> Result<Self, String> { |
| 63 | + let m = match index { |
| 64 | + 0 => BlockProductionMode::Clock, |
| 65 | + 1 => BlockProductionMode::Transaction, |
| 66 | + 2 => BlockProductionMode::Manual, |
| 67 | + _ => return Err(format!("invalid block production mode index: {}", index)), |
| 68 | + }; |
| 69 | + Ok(Self(m)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl fmt::Display for CliBlockProductionMode { |
| 74 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 75 | + match self.0 { |
| 76 | + BlockProductionMode::Clock => write!(f, "clock"), |
| 77 | + BlockProductionMode::Transaction => write!(f, "transaction"), |
| 78 | + BlockProductionMode::Manual => write!(f, "manual"), |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl CloudStartCommand { |
| 84 | + pub async fn start( |
| 85 | + &self, |
| 86 | + auth_service_url: &str, |
| 87 | + auth_callback_port: &str, |
| 88 | + id_service_url: &str, |
| 89 | + svm_gql_url: &str, |
| 90 | + svm_cloud_api_url: &str, |
| 91 | + ) -> Result<(), String> { |
| 92 | + let auth_config = match AuthConfig::read_from_system_config() |
| 93 | + .map_err(|e| format!("failed to authenticate user: {e}"))? |
| 94 | + { |
| 95 | + Some(auth_config) => auth_config, |
| 96 | + None => { |
| 97 | + println!( |
| 98 | + "{} Not authenticated, you must log in to continue", |
| 99 | + yellow!("-") |
| 100 | + ); |
| 101 | + txtx_cloud::login::handle_login_command( |
| 102 | + &LoginCommand::default(), |
| 103 | + auth_service_url, |
| 104 | + auth_callback_port, |
| 105 | + id_service_url, |
| 106 | + ) |
| 107 | + .await?; |
| 108 | + AuthConfig::read_from_system_config() |
| 109 | + .map_err(|e| format!("failed to authenticate user: {e}"))? |
| 110 | + .ok_or( |
| 111 | + "failed to authenticate user: no auth data found after login".to_string(), |
| 112 | + )? |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + auth_config |
| 117 | + .refresh_session_if_needed(&id_service_url, &auth_config.pat) |
| 118 | + .await?; |
| 119 | + |
| 120 | + let theme = ColorfulTheme { |
| 121 | + values_style: Style::new().green(), |
| 122 | + hint_style: Style::new().cyan(), |
| 123 | + ..ColorfulTheme::default() |
| 124 | + }; |
| 125 | + |
| 126 | + let workspaces = get_user_workspaces(&auth_config.access_token, svm_gql_url) |
| 127 | + .await |
| 128 | + .map_err(|e| format!("failed to get available workspaces: {}", e))?; |
| 129 | + |
| 130 | + let (workspace_names, workspace_ids): (Vec<String>, Vec<Uuid>) = workspaces |
| 131 | + .iter() |
| 132 | + .map(|workspace| (workspace.name.clone(), workspace.id)) |
| 133 | + .unzip(); |
| 134 | + |
| 135 | + let selected_workspace_idx = Select::with_theme(&theme) |
| 136 | + .with_prompt("Select the workspace to create the surfnet in") |
| 137 | + .items(&workspace_names) |
| 138 | + .default(0) |
| 139 | + .interact() |
| 140 | + .map_err(|e| format!("unable to get workspace: {e}"))?; |
| 141 | + |
| 142 | + let workspace_id = workspace_ids[selected_workspace_idx]; |
| 143 | + |
| 144 | + let name = match &self.name { |
| 145 | + Some(name) => name.clone(), |
| 146 | + None => { |
| 147 | + // Ask for the name of the surfnet |
| 148 | + Input::with_theme(&theme) |
| 149 | + .with_prompt("Enter the name of the surfnet") |
| 150 | + .interact_text() |
| 151 | + .map_err(|e| format!("unable to get surfnet name: {e}"))? |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + let description = match &self.description { |
| 156 | + Some(description) => Some(description.clone()), |
| 157 | + None => { |
| 158 | + // Ask for the description of the surfnet |
| 159 | + let description: String = Input::with_theme(&theme) |
| 160 | + .with_prompt("Enter an optional description for the surfnet") |
| 161 | + .allow_empty(true) |
| 162 | + .interact_text() |
| 163 | + .map_err(|e| format!("unable to get surfnet description: {e}"))?; |
| 164 | + if description.is_empty() { |
| 165 | + None |
| 166 | + } else { |
| 167 | + Some(description) |
| 168 | + } |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + let datasource_rpc_url: String = Input::with_theme(&theme) |
| 173 | + .with_prompt("Enter the RPC URL for the datasource") |
| 174 | + .default(self.datasource_rpc_url.clone()) |
| 175 | + .interact_text() |
| 176 | + .map_err(|e| format!("unable to get datasource RPC URL: {e}"))?; |
| 177 | + |
| 178 | + let block_production_mode = match &self.block_production_mode { |
| 179 | + Some(mode) => mode.0.clone(), |
| 180 | + None => { |
| 181 | + // Ask for the block production mode |
| 182 | + |
| 183 | + let selected_mode = Select::with_theme(&theme) |
| 184 | + .with_prompt("Select the block production mode") |
| 185 | + .items(&CliBlockProductionMode::choices()) |
| 186 | + .default(0) |
| 187 | + .interact() |
| 188 | + .map_err(|e| format!("unable to get block production mode: {e}"))?; |
| 189 | + |
| 190 | + CliBlockProductionMode::from_index(selected_mode)?.0 |
| 191 | + } |
| 192 | + }; |
| 193 | + |
| 194 | + println!("{} Spining up your hosted Surfnet...", yellow!("→")); |
| 195 | + let request = CreateNetworkRequest::new( |
| 196 | + workspace_id, |
| 197 | + name, |
| 198 | + description, |
| 199 | + datasource_rpc_url, |
| 200 | + block_production_mode, |
| 201 | + ); |
| 202 | + let client = reqwest::Client::new(); |
| 203 | + let res = client |
| 204 | + .post(svm_cloud_api_url) |
| 205 | + .bearer_auth(auth_config.access_token) |
| 206 | + .json(&request) |
| 207 | + .send() |
| 208 | + .await |
| 209 | + .map_err(|e| format!("failed to send request to start cloud surfnet: {e}"))?; |
| 210 | + |
| 211 | + if !res.status().is_success() { |
| 212 | + let err = res |
| 213 | + .text() |
| 214 | + .await |
| 215 | + .unwrap_or_else(|_| "Unknown error".to_string()); |
| 216 | + return Err(format!("failed to start cloud surfnet: {}", err)); |
| 217 | + } |
| 218 | + |
| 219 | + let res = res |
| 220 | + .json::<CreateNetworkResponse>() |
| 221 | + .await |
| 222 | + .map_err(|e| format!("failed to parse response: {e}"))?; |
| 223 | + |
| 224 | + println!( |
| 225 | + "\n🌊 Surf is up for network '{}'\n- Dashboard: {}\n- Rpc url: {}\n", |
| 226 | + request.name, |
| 227 | + green!("https://cloud.txtx.run/networks"), |
| 228 | + green!(res.rpc_url) |
| 229 | + ); |
| 230 | + |
| 231 | + Ok(()) |
| 232 | + } |
| 233 | +} |
0 commit comments