Skip to content

Commit

Permalink
Allow QueryParameter::UnlockableByAddress for more routes (#1397)
Browse files Browse the repository at this point in the history
* Added `QueryParameter::UnlockableByAddress` to allowed query paramaters for `Client::{alias_output_ids(), basic_output_ids(), nft_output_ids()}`

* Update sdk/CHANGELOG.md

Co-authored-by: Thibault Martinez <[email protected]>

* Update changelog

* Reduce requests during syncing

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Oct 5, 2023
1 parent b3f77d5 commit 16bc460
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 233 deletions.
4 changes: 4 additions & 0 deletions bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 1.1.1 - 2023-MM-DD

### Added

- `UnlockableByAddress` to `AliasQueryParameter, NftQueryParameter, QueryParameter`;

### Fixed

- Added `SeedSecretManager` to `SecretManagerType`;
Expand Down
3 changes: 3 additions & 0 deletions bindings/nodejs/lib/types/client/query-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type QueryParameter =
| Issuer
| StateController
| Governor
| UnlockableByAddress
| CommonQueryParameters;

/** Query parameters for filtering Alias Outputs */
Expand All @@ -31,6 +32,7 @@ export type AliasQueryParameter =
| Governor
| Issuer
| Sender
| UnlockableByAddress
| CommonQueryParameters;

/** Query parameters for filtering Foundry Outputs */
Expand All @@ -51,6 +53,7 @@ export type NftQueryParameter =
| Issuer
| Sender
| Tag
| UnlockableByAddress
| CommonQueryParameters;

/** Shared query parameters*/
Expand Down
8 changes: 8 additions & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 1.1.1 - 2023-MM-DD

### Added

- `QueryParameter::UnlockableByAddress` to allowed query parameters for `Client::{alias_output_ids(), basic_output_ids(), nft_output_ids()}`;

### Changed

- Use `QueryParameter::UnlockableByAddress` for syncing also without default SyncOptions;

### Fixed

- Update protocol params and addresses with correct bech32 HRP in `Wallet::set_client_options()`;
Expand Down
9 changes: 6 additions & 3 deletions sdk/src/client/node_api/indexer/query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ pub(crate) fn verify_query_parameters_basic_outputs(query_parameters: Vec<QueryP
QueryParameter::CreatedBefore,
QueryParameter::CreatedAfter,
QueryParameter::PageSize,
QueryParameter::Cursor
QueryParameter::Cursor,
QueryParameter::UnlockableByAddress
)?;

Ok(QueryParameters::new(query_parameters))
Expand All @@ -264,7 +265,8 @@ pub(crate) fn verify_query_parameters_alias_outputs(query_parameters: Vec<QueryP
QueryParameter::CreatedBefore,
QueryParameter::CreatedAfter,
QueryParameter::PageSize,
QueryParameter::Cursor
QueryParameter::Cursor,
QueryParameter::UnlockableByAddress
)?;

Ok(QueryParameters::new(query_parameters))
Expand Down Expand Up @@ -310,7 +312,8 @@ pub(crate) fn verify_query_parameters_nft_outputs(query_parameters: Vec<QueryPar
QueryParameter::CreatedBefore,
QueryParameter::CreatedAfter,
QueryParameter::PageSize,
QueryParameter::Cursor
QueryParameter::Cursor,
QueryParameter::UnlockableByAddress
)?;

Ok(QueryParameters::new(query_parameters))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

use std::collections::HashSet;

#[cfg(not(target_family = "wasm"))]
use futures::FutureExt;

use crate::{
client::{node_api::indexer::query_parameters::QueryParameter, secret::SecretManage},
types::{
Expand Down Expand Up @@ -33,63 +30,13 @@ where
sync_options: &SyncOptions,
) -> crate::wallet::Result<Vec<OutputId>> {
log::debug!("[SYNC] get_alias_and_foundry_output_ids");
let client = self.client();
let bech32_address = bech32_address.convert()?;

let mut output_ids = HashSet::new();

#[cfg(target_family = "wasm")]
{
output_ids.extend(
client
.alias_output_ids([QueryParameter::Governor(bech32_address)])
.await?
.items,
);
output_ids.extend(
client
.alias_output_ids([QueryParameter::StateController(bech32_address)])
.await?
.items,
);
}

#[cfg(not(target_family = "wasm"))]
{
let tasks = [
// Get outputs where the address is in the governor address unlock condition
async move {
let client = client.clone();
task::spawn(async move {
client
.alias_output_ids([QueryParameter::Governor(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
// Get outputs where the address is in the state controller unlock condition
async move {
let client = client.clone();
task::spawn(async move {
client
.alias_output_ids([QueryParameter::StateController(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
];

let results: Vec<crate::wallet::Result<OutputIdsResponse>> = futures::future::try_join_all(tasks).await?;

for res in results {
let found_output_ids = res?;
output_ids.extend(found_output_ids.items);
}
}
let mut output_ids = self
.client()
.alias_output_ids([QueryParameter::UnlockableByAddress(bech32_address)])
.await?
.items;

// Get all results
if sync_options.alias.foundry_outputs {
Expand All @@ -103,11 +50,11 @@ where
/// Returns output ids of foundries controlled by the provided aliases
pub(crate) async fn get_foundry_output_ids(
&self,
alias_output_ids: &HashSet<OutputId>,
alias_output_ids: &[OutputId],
) -> crate::wallet::Result<Vec<OutputId>> {
log::debug!("[SYNC] get_foundry_output_ids");
// Get alias outputs, so we can then get the foundry outputs with the alias addresses
let alias_outputs_with_meta = self.get_outputs(alias_output_ids.iter().copied().collect()).await?;
let alias_outputs_with_meta = self.get_outputs(alias_output_ids.to_vec()).await?;

let bech32_hrp = self.client().get_bech32_hrp().await?;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

#[cfg(not(target_family = "wasm"))]
use std::collections::HashSet;

#[cfg(not(target_family = "wasm"))]
use futures::FutureExt;

#[cfg(not(target_family = "wasm"))]
use crate::types::api::plugins::indexer::OutputIdsResponse;
use crate::{
client::{node_api::indexer::query_parameters::QueryParameter, secret::SecretManage},
types::block::{address::Bech32Address, output::OutputId, ConvertTo},
Expand Down Expand Up @@ -45,84 +37,11 @@ where
bech32_address: impl ConvertTo<Bech32Address>,
) -> crate::wallet::Result<Vec<OutputId>> {
let bech32_address = bech32_address.convert()?;
// aliases and foundries
#[cfg(target_family = "wasm")]
{
let mut output_ids = Vec::new();
output_ids.extend(
self.client()
.basic_output_ids([QueryParameter::Address(bech32_address)])
.await?
.items,
);
output_ids.extend(
self.client()
.basic_output_ids([QueryParameter::StorageDepositReturnAddress(bech32_address)])
.await?
.items,
);
output_ids.extend(
self.client()
.basic_output_ids([QueryParameter::ExpirationReturnAddress(bech32_address)])
.await?
.items,
);

Ok(output_ids)
}

#[cfg(not(target_family = "wasm"))]
{
let client = self.client();
let tasks = [
// Get basic outputs
async move {
let client = client.clone();
tokio::spawn(async move {
client
.basic_output_ids([QueryParameter::Address(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
// Get outputs where the address is in the storage deposit return unlock condition
async move {
let client = client.clone();
tokio::spawn(async move {
client
.basic_output_ids([QueryParameter::StorageDepositReturnAddress(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
// Get outputs where the address is in an expired expiration unlock condition
async move {
let client = client.clone();
tokio::spawn(async move {
client
.basic_output_ids([QueryParameter::ExpirationReturnAddress(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
];

// Get all results
let mut output_ids = HashSet::new();
let results: Vec<crate::wallet::Result<OutputIdsResponse>> = futures::future::try_join_all(tasks).await?;

for res in results {
let found_output_ids = res?;
output_ids.extend(found_output_ids.items);
}

Ok(output_ids.into_iter().collect())
}
Ok(self
.client()
.basic_output_ids([QueryParameter::UnlockableByAddress(bech32_address)])
.await?
.items)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

#[cfg(not(target_family = "wasm"))]
use std::collections::HashSet;

#[cfg(not(target_family = "wasm"))]
use futures::FutureExt;

#[cfg(not(target_family = "wasm"))]
use crate::types::api::plugins::indexer::OutputIdsResponse;
use crate::{
client::{node_api::indexer::query_parameters::QueryParameter, secret::SecretManage},
types::block::{address::Bech32Address, output::OutputId, ConvertTo},
Expand All @@ -25,82 +17,11 @@ where
bech32_address: impl ConvertTo<Bech32Address>,
) -> crate::wallet::Result<Vec<OutputId>> {
let bech32_address = bech32_address.convert()?;
#[cfg(target_family = "wasm")]
{
let mut output_ids = Vec::new();
output_ids.extend(
self.client()
.nft_output_ids([QueryParameter::Address(bech32_address)])
.await?
.items,
);
output_ids.extend(
self.client()
.nft_output_ids([QueryParameter::StorageDepositReturnAddress(bech32_address)])
.await?
.items,
);
output_ids.extend(
self.client()
.nft_output_ids([QueryParameter::ExpirationReturnAddress(bech32_address)])
.await?
.items,
);

Ok(output_ids)
}
#[cfg(not(target_family = "wasm"))]
{
let client = self.client();
let tasks = [
async move {
let client = client.clone();
tokio::spawn(async move {
// Get nft outputs where the address is in the address unlock condition
client
.nft_output_ids([QueryParameter::Address(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
async move {
let client = client.clone();
tokio::spawn(async move {
// Get outputs where the address is in the storage deposit return unlock condition
client
.nft_output_ids([QueryParameter::StorageDepositReturnAddress(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
async move {
let client = client.clone();
tokio::spawn(async move {
// Get outputs where the address is in the expiration unlock condition
client
.nft_output_ids([QueryParameter::ExpirationReturnAddress(bech32_address)])
.await
.map_err(From::from)
})
.await
}
.boxed(),
];

// Get all results
let mut output_ids = HashSet::new();
let results: Vec<crate::wallet::Result<OutputIdsResponse>> = futures::future::try_join_all(tasks).await?;

for res in results {
let found_output_ids = res?;
output_ids.extend(found_output_ids.items);
}

Ok(output_ids.into_iter().collect())
}
Ok(self
.client()
.nft_output_ids([QueryParameter::UnlockableByAddress(bech32_address)])
.await?
.items)
}
}

0 comments on commit 16bc460

Please sign in to comment.