cargo_tangle/command/service/
accept.rs

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