| 1 | //! Balance: query the active provider's remaining prepaid credit. |
| 2 | |
| 3 | use super::DebugAction as AppAction; |
| 4 | use codewhale_command_contract::facets::CommandDebugDiagnosticsContext; |
| 5 | use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler}; |
| 6 | use codewhale_command_contract::metadata::{ |
| 7 | CommandInfo as ContractInfo, RegisterCommand as ContractRegisterCommand, |
| 8 | }; |
| 9 | |
| 10 | use super::CommandResult; |
| 11 | |
| 12 | pub(in crate::commands) struct BalanceCmd; |
| 13 | |
| 14 | const CONTRACT_INFO: ContractInfo = ContractInfo { |
| 15 | name: "balance", |
| 16 | aliases: &[], |
| 17 | usage: "/balance", |
| 18 | description_key: "cmd_balance_description", |
| 19 | }; |
| 20 | |
| 21 | impl ContractRegisterCommand<CommandResult> for BalanceCmd { |
| 22 | fn info() -> &'static ContractInfo { |
| 23 | &CONTRACT_INFO |
| 24 | } |
| 25 | |
| 26 | fn handler() -> CommandHandler<CommandResult> { |
| 27 | CommandHandler::Contextual { |
| 28 | capabilities: CommandCapabilities::DEBUG_DIAGNOSTICS, |
| 29 | handler: |contexts, _| balance(contexts), |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Query provider account balance / credits. |
| 35 | pub fn balance(contexts: CommandContexts<'_>) -> CommandResult { |
| 36 | let mut parts = contexts.into_parts(); |
| 37 | let Some(diagnostics) = parts.debug_diagnostics.as_deref_mut() else { |
| 38 | return CommandResult::error("Command capability unavailable: debug_diagnostics"); |
| 39 | }; |
| 40 | balance_portable(diagnostics) |
| 41 | } |
| 42 | |
| 43 | fn balance_portable(diagnostics: &mut dyn CommandDebugDiagnosticsContext) -> CommandResult { |
| 44 | let projection = diagnostics.balance_projection(); |
| 45 | if !projection.supports_balance_api { |
| 46 | return CommandResult::message(format!( |
| 47 | "Balance check is not supported for {} yet. Check the provider dashboard for account balance details.", |
| 48 | projection.provider_display_name |
| 49 | )); |
| 50 | } |
| 51 | CommandResult::action(AppAction::FetchBalance) |
| 52 | } |
| 53 |