taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

dev.rs (3480B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025, 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use clap::ValueEnum;
     18 use taler_common::{config::Config, types::amount::Decimal};
     19 use tracing::info;
     20 
     21 use crate::{
     22     config::HarnessCfg,
     23     cyclos_api::{api::CyclosAuth, client::Client, types::OrderBy},
     24     worker::{Tx, extract_tx_info},
     25 };
     26 
     27 #[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
     28 pub enum Account {
     29     Wire,
     30     Harness,
     31 }
     32 
     33 #[derive(clap::Subcommand, Debug)]
     34 pub enum DevCmd {
     35     Tx {
     36         account: Account,
     37     },
     38     Transfer {
     39         debtor: Account,
     40         amount: Decimal,
     41         subject: String,
     42     },
     43 }
     44 
     45 pub async fn dev(cfg: &Config, cmd: DevCmd) -> anyhow::Result<()> {
     46     let client = http_client::client()?;
     47     let cfg = HarnessCfg::parse(cfg)?;
     48     let wire = Client {
     49         client: &client,
     50         api_url: &cfg.worker.host.api_url,
     51         auth: &CyclosAuth::Basic {
     52             username: cfg.worker.host.username,
     53             password: cfg.worker.host.password,
     54         },
     55     };
     56     let client = Client {
     57         client: &client,
     58         api_url: &cfg.worker.host.api_url,
     59         auth: &CyclosAuth::Basic {
     60             username: cfg.username,
     61             password: cfg.password,
     62         },
     63     };
     64     match cmd {
     65         DevCmd::Tx { account } => {
     66             let client = match account {
     67                 Account::Wire => wire,
     68                 Account::Harness => client,
     69             };
     70             let mut page_idx = 0;
     71             loop {
     72                 let page = client
     73                     .history(
     74                         *cfg.worker.account_type_id,
     75                         OrderBy::DateAsc,
     76                         page_idx,
     77                         None,
     78                     )
     79                     .await?;
     80                 for transfer in page.page {
     81                     let tx = extract_tx_info(transfer);
     82                     match tx {
     83                         Tx::In(tx_in) => info!(target: "dev", "in  {tx_in}"),
     84                         Tx::Out(tx_out) => info!(target: "dev", "out {tx_out}"),
     85                     }
     86                 }
     87 
     88                 if !page.has_next_page {
     89                     break;
     90                 } else {
     91                     page_idx += 1;
     92                 }
     93             }
     94         }
     95         DevCmd::Transfer {
     96             debtor,
     97             amount,
     98             subject,
     99         } => {
    100             let (debtor, creditor) = match debtor {
    101                 Account::Wire => (wire, client),
    102                 Account::Harness => (client, wire),
    103             };
    104             let tx = debtor
    105                 .direct_payment(
    106                     *creditor.whoami().await?.id,
    107                     *cfg.worker.payment_type_id,
    108                     amount,
    109                     &subject,
    110                 )
    111                 .await?;
    112             println!("Sent tx {}", tx.id);
    113         }
    114     }
    115     Ok(())
    116 }