Skip to content
This repository was archived by the owner on Apr 25, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pallets/services/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use parity_scale_codec::Codec;
use sp_runtime::{traits::MaybeDisplay, Serialize};
use sp_std::vec::Vec;
use tangle_primitives::services::{AssetIdT, Constraints, RpcServicesWithBlueprint};
use tangle_primitives::services::{
AssetIdT, Constraints, RpcServicesWithBlueprint, ServiceRequest,
};

pub type BlockNumberOf<Block> =
<<Block as sp_runtime::traits::HeaderProvider>::HeaderT as sp_runtime::traits::Header>::Number;
Expand All @@ -45,5 +47,18 @@ sp_api::decl_runtime_apis! {
Vec<RpcServicesWithBlueprint<C, AccountId, BlockNumberOf<Block>, AssetId>>,
sp_runtime::DispatchError,
>;

/// Query all pending service requests associated with a specific operator and blueprints.
///
/// ## Arguments
/// - `operator`: The operator account id.
/// ## Return
/// - `Vec<(u64, ServiceRequest<C, AccountId, BlockNumberOf<Block>, AssetId>)>`: A list of service requests with their IDs.
fn query_service_requests_with_blueprints_by_operator(
operator: AccountId,
) -> Result<
Vec<(u64, ServiceRequest<C, AccountId, BlockNumberOf<Block>, AssetId>)>,
sp_runtime::DispatchError,
>;
}
}
26 changes: 25 additions & 1 deletion pallets/services/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use sp_runtime::{
DispatchError, Serialize,
};
use std::sync::Arc;
use tangle_primitives::services::{AssetIdT, Constraints, RpcServicesWithBlueprint};
use tangle_primitives::services::{
AssetIdT, Constraints, RpcServicesWithBlueprint, ServiceRequest,
};

type BlockNumberOf<Block> =
<<Block as sp_runtime::traits::HeaderProvider>::HeaderT as sp_runtime::traits::Header>::Number;
Expand All @@ -49,6 +51,13 @@ where
operator: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Vec<RpcServicesWithBlueprint<X, AccountId, BlockNumber, AssetId>>>;

#[method(name = "services_queryServiceRequestsWithBlueprintsByOperator")]
fn query_service_requests_with_blueprints_by_operator(
&self,
operator: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Vec<(u64, ServiceRequest<X, AccountId, BlockNumber, AssetId>)>>;
}

/// A struct that implements the `ServicesApi`.
Expand Down Expand Up @@ -89,6 +98,21 @@ where
Err(e) => Err(custom_error_into_rpc_err(Error::RuntimeError(e))),
}
}

fn query_service_requests_with_blueprints_by_operator(
&self,
operator: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<(u64, ServiceRequest<X, AccountId, BlockNumberOf<Block>, AssetId>)>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

match api.query_service_requests_with_blueprints_by_operator(at, operator) {
Ok(Ok(res)) => Ok(res),
Ok(Err(e)) => Err(custom_error_into_rpc_err(Error::CustomDispatchError(e))),
Err(e) => Err(custom_error_into_rpc_err(Error::RuntimeError(e))),
}
}
}

/// Error type of this RPC api.
Expand Down
34 changes: 34 additions & 0 deletions pallets/services/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,38 @@ impl<T: Config> Pallet<T> {
}
Ok(result)
}

#[allow(clippy::type_complexity)]
pub fn service_requests_with_blueprints_by_operator(
operator: T::AccountId,
) -> Result<
Vec<(u64, ServiceRequest<T::Constraints, T::AccountId, BlockNumberFor<T>, T::AssetId>)>,
Error<T>,
> {
// First we need to get the operator's profile to know which blueprints they're registered for
let profile = Self::operator_profile(&operator)?;

// Get the operator's blueprints
let blueprint_ids = profile.blueprints;

// Iterate through all service requests to find the ones for the operator's blueprints
// and where the operator is included in the requested operators
let mut result = Vec::new();

// We need to iterate through all service requests to check if operator is included
for (request_id, request) in ServiceRequests::<T>::iter() {
// Check if the service request is for a blueprint the operator is registered for
if blueprint_ids.contains(&request.blueprint) {
// Check if the operator is one of the requested operators with approval state
if request.operators_with_approval_state.iter().any(|(op, _)| op == &operator) {
// Only include pending requests (those that haven't been approved by all operators yet)
if !request.is_approved() {
result.push((request_id, request));
}
}
}
}

Ok(result)
}
}
11 changes: 10 additions & 1 deletion runtime/mainnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use sp_runtime::{
SaturatedConversion,
};
use sp_staking::currency_to_vote::U128CurrencyToVote;
use tangle_primitives::services::RpcServicesWithBlueprint;
use tangle_primitives::services::{RpcServicesWithBlueprint, ServiceRequest};
pub use tangle_services::PalletServicesConstraints;

#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -1963,6 +1963,15 @@ impl_runtime_apis! {
> {
Services::services_with_blueprints_by_operator(operator).map_err(Into::into)
}

fn query_service_requests_with_blueprints_by_operator(
operator: AccountId,
) -> Result<
Vec<(u64, ServiceRequest<PalletServicesConstraints, AccountId, BlockNumberOf<Block>, AssetId>)>,
sp_runtime::DispatchError,
> {
Services::service_requests_with_blueprints_by_operator(operator).map_err(Into::into)
}
}

impl pallet_rewards_rpc_runtime_api::RewardsApi<Block, AccountId, AssetId, Balance> for Runtime {
Expand Down
11 changes: 10 additions & 1 deletion runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use sp_std::{prelude::*, vec::Vec};
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
use tangle_primitives::services::RpcServicesWithBlueprint;
use tangle_primitives::services::{RpcServicesWithBlueprint, ServiceRequest};

pub use frame_support::{
construct_runtime,
Expand Down Expand Up @@ -1653,6 +1653,15 @@ impl_runtime_apis! {
> {
Services::services_with_blueprints_by_operator(operator).map_err(Into::into)
}

fn query_service_requests_with_blueprints_by_operator(
operator: AccountId,
) -> Result<
Vec<(u64, ServiceRequest<PalletServicesConstraints, AccountId, BlockNumberOf<Block>, AssetId>)>,
sp_runtime::DispatchError,
> {
Services::service_requests_with_blueprints_by_operator(operator).map_err(Into::into)
}
}

impl pallet_rewards_rpc_runtime_api::RewardsApi<Block, AccountId, AssetId, Balance> for Runtime {
Expand Down