Skip to content

Commit b81f895

Browse files
feat(sdk): fetch many and return metadata and proof to client (#2331)
1 parent f1f0470 commit b81f895

File tree

1 file changed

+94
-21
lines changed

1 file changed

+94
-21
lines changed

packages/rs-sdk/src/platform/fetch_many.rs

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use dapi_grpc::platform::v0::{
1919
GetDataContractsRequest, GetEpochsInfoRequest, GetEvonodesProposedEpochBlocksByIdsRequest,
2020
GetEvonodesProposedEpochBlocksByRangeRequest, GetIdentitiesBalancesRequest,
2121
GetIdentityKeysRequest, GetPathElementsRequest, GetProtocolVersionUpgradeStateRequest,
22-
GetProtocolVersionUpgradeVoteStatusRequest, GetVotePollsByEndDateRequest,
22+
GetProtocolVersionUpgradeVoteStatusRequest, GetVotePollsByEndDateRequest, Proof,
23+
ResponseMetadata,
2324
};
2425
use dashcore_rpc::dashcore::ProTxHash;
2526
use dpp::data_contract::DataContract;
@@ -145,8 +146,71 @@ where
145146
sdk: &Sdk,
146147
query: Q,
147148
) -> Result<O, Error> {
149+
Self::fetch_many_with_metadata_and_proof(sdk, query, None)
150+
.await
151+
.map(|(objects, _, _)| objects)
152+
}
153+
154+
/// Fetch multiple objects from Platform with metadata.
155+
///
156+
/// Fetch objects from Platform that satisfy the provided [Query].
157+
/// This method allows you to retrieve the metadata associated with the response.
158+
///
159+
/// ## Parameters
160+
///
161+
/// - `sdk`: An instance of [Sdk].
162+
/// - `query`: A query parameter implementing [`crate::platform::query::Query`] to specify the data to be fetched.
163+
/// - `settings`: An optional `RequestSettings` to give greater flexibility on the request.
164+
///
165+
/// ## Returns
166+
///
167+
/// Returns a `Result` containing either:
168+
///
169+
/// * A tuple `(O, ResponseMetadata)` where `O` is the collection of fetched objects, and `ResponseMetadata` contains metadata about the response.
170+
/// * [`Error`](crate::error::Error) when an error occurs.
171+
///
172+
/// ## Error Handling
173+
///
174+
/// Any errors encountered during the execution are returned as [Error] instances.
175+
async fn fetch_many_with_metadata<Q: Query<<Self as FetchMany<K, O>>::Request>>(
176+
sdk: &Sdk,
177+
query: Q,
178+
settings: Option<RequestSettings>,
179+
) -> Result<(O, ResponseMetadata), Error> {
180+
Self::fetch_many_with_metadata_and_proof(sdk, query, settings)
181+
.await
182+
.map(|(objects, metadata, _)| (objects, metadata))
183+
}
184+
185+
/// Fetch multiple objects from Platform with metadata and underlying proof.
186+
///
187+
/// Fetch objects from Platform that satisfy the provided [Query].
188+
/// This method allows you to retrieve the metadata and the underlying proof associated with the response.
189+
///
190+
/// ## Parameters
191+
///
192+
/// - `sdk`: An instance of [Sdk].
193+
/// - `query`: A query parameter implementing [`crate::platform::query::Query`] to specify the data to be fetched.
194+
/// - `settings`: An optional `RequestSettings` to give greater flexibility on the request.
195+
///
196+
/// ## Returns
197+
///
198+
/// Returns a `Result` containing either:
199+
///
200+
/// * A tuple `(O, ResponseMetadata, Proof)` where `O` is the collection of fetched objects, `ResponseMetadata` contains metadata about the response, and `Proof` is the underlying proof.
201+
/// * [`Error`](crate::error::Error) when an error occurs.
202+
///
203+
/// ## Error Handling
204+
///
205+
/// Any errors encountered during the execution are returned as [Error] instances.
206+
async fn fetch_many_with_metadata_and_proof<Q: Query<<Self as FetchMany<K, O>>::Request>>(
207+
sdk: &Sdk,
208+
query: Q,
209+
settings: Option<RequestSettings>,
210+
) -> Result<(O, ResponseMetadata, Proof), Error> {
148211
let request = &query.query(sdk.prove())?;
149-
let closure = |settings: RequestSettings| async move {
212+
213+
let fut = |settings: RequestSettings| async move {
150214
let ExecutionResponse {
151215
address,
152216
retries,
@@ -158,28 +222,37 @@ where
158222
.map_err(|e| e.inner_into())?;
159223

160224
let object_type = std::any::type_name::<Self>().to_string();
161-
tracing::trace!(request = ?request, response = ?response, ?address, retries, object_type, "fetched object from platform");
225+
tracing::trace!(
226+
request = ?request,
227+
response = ?response,
228+
?address,
229+
retries,
230+
object_type,
231+
"fetched objects from platform"
232+
);
162233

163-
sdk.parse_proof::<<Self as FetchMany<K, O>>::Request, O>(request.clone(), response)
164-
.await
165-
.map(|o| ExecutionResponse {
166-
inner: o,
167-
retries,
168-
address: address.clone(),
169-
})
170-
.map_err(|e| ExecutionError {
171-
inner: e,
172-
retries,
173-
address: Some(address),
174-
})
234+
sdk.parse_proof_with_metadata_and_proof::<<Self as FetchMany<K, O>>::Request, O>(
235+
request.clone(),
236+
response,
237+
)
238+
.await
239+
.map_err(|e| ExecutionError {
240+
inner: e,
241+
address: Some(address.clone()),
242+
retries,
243+
})
244+
.map(|(o, metadata, proof)| ExecutionResponse {
245+
inner: (o.unwrap_or_default(), metadata, proof),
246+
retries,
247+
address: address.clone(),
248+
})
175249
};
176250

177-
let settings = sdk.dapi_client_settings;
251+
let settings = sdk
252+
.dapi_client_settings
253+
.override_by(settings.unwrap_or_default());
178254

179-
retry(settings, closure)
180-
.await
181-
.into_inner()
182-
.map(|o| o.unwrap_or_default())
255+
retry(settings, fut).await.into_inner()
183256
}
184257

185258
/// Fetch multiple objects from Platform by their identifiers.
@@ -260,7 +333,7 @@ impl FetchMany<Identifier, Documents> for Document {
260333
let ExecutionResponse {
261334
address,
262335
retries,
263-
inner: response,
336+
inner: response
264337
} = request.execute(sdk, settings).await.map_err(|e| e.inner_into())?;
265338

266339
tracing::trace!(request=?document_query, response=?response, ?address, retries, "fetch multiple documents");

0 commit comments

Comments
 (0)