cargo_tangle/command/service/
accept.rs

1use blueprint_chain_setup::tangle::transactions::get_security_commitment;
2use blueprint_clients::tangle::client::OnlineClient;
3use blueprint_crypto::sp_core::SpSr25519;
4use blueprint_crypto::tangle_pair_signer::TanglePairSigner;
5use blueprint_keystore::backends::Backend;
6use blueprint_keystore::{Keystore, KeystoreConfig};
7use color_eyre::Result;
8use dialoguer::console::style;
9use tangle_subxt::tangle_testnet_runtime::api::runtime_types::tangle_primitives::services::types::Asset;
10
11use crate::wait_for_in_block_success;
12
13/// Accepts a service request.
14///
15/// # Arguments
16///
17/// * `ws_rpc_url` - WebSocket RPC URL for the Tangle Network
18/// * `_min_exposure_percent` - Minimum exposure percentage (currently unused)
19/// * `_max_exposure_percent` - Maximum exposure percentage (currently unused)
20/// * `restaking_percent` - Percentage of stake to restake
21/// * `keystore_uri` - URI for the keystore
22/// * `request_id` - ID of the request to accept
23///
24/// # Errors
25///
26/// Returns an error if:
27/// * Failed to connect to the Tangle Network
28/// * Failed to sign or submit the transaction
29/// * Transaction failed
30///
31/// # Panics
32///
33/// Panics if:
34/// * Failed to create keystore
35/// * Failed to get keys from keystore
36pub async fn accept_request(
37    ws_rpc_url: String,
38    _min_exposure_percent: u8,
39    _max_exposure_percent: u8,
40    restaking_percent: u8,
41    keystore_uri: String,
42    // keystore_password: Option<String>, // TODO: Add keystore password support
43    request_id: u64,
44) -> Result<()> {
45    let client = OnlineClient::from_url(ws_rpc_url.clone()).await?;
46
47    let config = KeystoreConfig::new().fs_root(keystore_uri.clone());
48    let keystore = Keystore::new(config).expect("Failed to create keystore");
49    let public = keystore.first_local::<SpSr25519>().unwrap();
50    let pair = keystore.get_secret::<SpSr25519>(&public).unwrap();
51    let signer = TanglePairSigner::new(pair.0);
52
53    let native_security_commitments =
54        vec![get_security_commitment(Asset::Custom(0), restaking_percent)];
55
56    println!(
57        "{}",
58        style(format!("Preparing to accept request ID: {}", request_id)).cyan()
59    );
60    let call = tangle_subxt::tangle_testnet_runtime::api::tx()
61        .services()
62        .approve(request_id, native_security_commitments);
63
64    println!(
65        "{}",
66        style(format!(
67            "Submitting Service Approval for request ID: {}",
68            request_id
69        ))
70        .cyan()
71    );
72    let res = client
73        .tx()
74        .sign_and_submit_then_watch_default(&call, &signer)
75        .await?;
76    wait_for_in_block_success(res).await;
77
78    println!(
79        "{}",
80        style(format!(
81            "Service Approval for request ID: {} submitted successfully",
82            request_id
83        ))
84        .green()
85    );
86    Ok(())
87}