Skip to content

Commit

Permalink
Add anchor indexer features
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Oct 29, 2023
1 parent 30ab60a commit f92e864
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 32 deletions.
21 changes: 17 additions & 4 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use iota_sdk::client::mqtt::Topic;
use iota_sdk::{
client::{
node_api::indexer::query_parameters::{
AccountOutputQueryParameters, BasicOutputQueryParameters, DelegationOutputQueryParameters,
FoundryOutputQueryParameters, NftOutputQueryParameters, OutputQueryParameters,
AccountOutputQueryParameters, AnchorOutputQueryParameters, BasicOutputQueryParameters,
DelegationOutputQueryParameters, FoundryOutputQueryParameters, NftOutputQueryParameters,
OutputQueryParameters,
},
node_manager::node::NodeAuth,
},
types::block::{
address::{Bech32Address, Hrp},
output::{
dto::OutputDto, feature::Feature, unlock_condition::dto::UnlockConditionDto, AccountId, DelegationId,
FoundryId, NativeToken, NftId, OutputId, TokenScheme,
dto::OutputDto, feature::Feature, unlock_condition::dto::UnlockConditionDto, AccountId, AnchorId,
DelegationId, FoundryId, NativeToken, NftId, OutputId, TokenScheme,
},
payload::{dto::PayloadDto, signed_transaction::TransactionId},
BlockId, IssuerId, SignedBlockDto,
Expand Down Expand Up @@ -236,6 +237,18 @@ pub enum ClientMethod {
/// Account id
account_id: AccountId,
},
/// Fetch anchor output IDs
#[serde(rename_all = "camelCase")]
AnchorOutputIds {
/// Query parameters for output requests
query_parameters: AnchorOutputQueryParameters,
},
/// Fetch anchor output ID
#[serde(rename_all = "camelCase")]
AnchorOutputId {
/// Anchor id
anchor_id: AnchorId,
},
/// Fetch delegation output IDs
#[serde(rename_all = "camelCase")]
DelegationOutputIds {
Expand Down
6 changes: 5 additions & 1 deletion bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,17 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
ClientMethod::AccountOutputIds { query_parameters } => {
Response::OutputIdsResponse(client.account_output_ids(query_parameters).await?)
}
ClientMethod::AccountOutputId { account_id } => Response::OutputId(client.account_output_id(account_id).await?),
ClientMethod::AnchorOutputIds { query_parameters } => {
Response::OutputIdsResponse(client.anchor_output_ids(query_parameters).await?)
}
ClientMethod::AnchorOutputId { anchor_id } => Response::OutputId(client.anchor_output_id(anchor_id).await?),
ClientMethod::DelegationOutputId { delegation_id } => {
Response::OutputId(client.delegation_output_id(delegation_id).await?)
}
ClientMethod::DelegationOutputIds { query_parameters } => {
Response::OutputIdsResponse(client.delegation_output_ids(query_parameters).await?)
}
ClientMethod::AccountOutputId { account_id } => Response::OutputId(client.account_output_id(account_id).await?),
ClientMethod::FoundryOutputIds { query_parameters } => {
Response::OutputIdsResponse(client.foundry_output_ids(query_parameters).await?)
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub enum Response {
Outputs(Vec<OutputWithMetadataResponse>),
/// Response for:
/// - [`AccountOutputId`](crate::method::ClientMethod::AccountOutputId)
/// - [`AnchorOutputId`](crate::method::ClientMethod::AnchorOutputId)
/// - [`DelegationOutputId`](crate::method::ClientMethod::DelegationOutputId)
/// - [`FoundryOutputId`](crate::method::ClientMethod::FoundryOutputId)
/// - [`NftOutputId`](crate::method::ClientMethod::NftOutputId)
Expand All @@ -138,6 +139,7 @@ pub enum Response {
/// - [`OutputIds`](crate::method::ClientMethod::OutputIds)
/// - [`BasicOutputIds`](crate::method::ClientMethod::BasicOutputIds)
/// - [`AccountOutputIds`](crate::method::ClientMethod::AccountOutputIds)
/// - [`AnchorOutputIds`](crate::method::ClientMethod::AnchorOutputIds)
/// - [`DelegationOutputIds`](crate::method::ClientMethod::DelegationOutputIds)
/// - [`FoundryOutputIds`](crate::method::ClientMethod::FoundryOutputIds)
/// - [`NftOutputIds`](crate::method::ClientMethod::NftOutputIds)
Expand Down
38 changes: 38 additions & 0 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
FoundryQueryParameter,
NftQueryParameter,
AccountQueryParameter,
AnchorQueryParameter,
GenericQueryParameter,
DelegationQueryParameter,
} from '../types/client';
Expand All @@ -37,6 +38,7 @@ import {
parseSignedBlock,
SignedBlock,
AccountId,
AnchorId,
NftId,
FoundryId,
DelegationId,
Expand Down Expand Up @@ -605,6 +607,42 @@ export class Client {
return JSON.parse(response).payload;
}

/**
* Get the corresponding output IDs given a list of anchor query parameters.
*
* @param queryParameters An array of `AnchorQueryParameter`s.
* @returns A paginated query response of corresponding output IDs.
*/
async anchorOutputIds(
queryParameters: AnchorQueryParameter[],
): Promise<IOutputsResponse> {
const response = await this.methodHandler.callMethod({
name: 'anchorOutputIds',
data: {
queryParameters,
},
});

return JSON.parse(response).payload;
}

/**
* Get the corresponding output ID from an anchor ID.
*
* @param anchorId An anchor ID.
* @returns The corresponding output ID.
*/
async anchorOutputId(anchorId: AnchorId): Promise<OutputId> {
const response = await this.methodHandler.callMethod({
name: 'anchorOutputId',
data: {
anchorId,
},
});

return JSON.parse(response).payload;
}

/**
* Get the corresponding output IDs given a list of delegation query parameters.
*
Expand Down
5 changes: 5 additions & 0 deletions bindings/nodejs/lib/types/block/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { HexEncodedString } from '../utils';
*/
export type AccountId = HexEncodedString;

/**
* An Anchor ID represented as hex-encoded string.
*/
export type AnchorId = HexEncodedString;

/**
* An NFT ID represented as hex-encoded string.
*/
Expand Down
16 changes: 16 additions & 0 deletions bindings/nodejs/lib/types/client/bridge/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
BlockId,
FoundryId,
IssuerId,
AnchorId,
NftId,
DelegationId,
Output,
Expand All @@ -20,6 +21,7 @@ import type {
import type { PreparedTransactionData } from '../prepared-transaction-data';
import type {
AccountQueryParameter,
AnchorQueryParameter,
FoundryQueryParameter,
GenericQueryParameter,
NftQueryParameter,
Expand Down Expand Up @@ -237,6 +239,20 @@ export interface __AccountOutputIdMethod__ {
};
}

export interface __AnchorOutputIdsMethod__ {
name: 'anchorOutputIds';
data: {
queryParameters: AnchorQueryParameter[];
};
}

export interface __AnchorOutputIdMethod__ {
name: 'anchorOutputId';
data: {
anchorId: AnchorId;
};
}

export interface __DelegationOutputIdsMethod__ {
name: 'delegationOutputIds';
data: {
Expand Down
4 changes: 4 additions & 0 deletions bindings/nodejs/lib/types/client/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import type {
__HexPublicKeyToBech32AddressMethod__,
__AccountOutputIdsMethod__,
__AccountOutputIdMethod__,
__AnchorOutputIdsMethod__,
__AnchorOutputIdMethod__,
__DelegationOutputIdsMethod__,
__DelegationOutputIdMethod__,
__FoundryOutputIdsMethod__,
Expand Down Expand Up @@ -84,6 +86,8 @@ export type __ClientMethods__ =
| __HexPublicKeyToBech32AddressMethod__
| __AccountOutputIdsMethod__
| __AccountOutputIdMethod__
| __AnchorOutputIdsMethod__
| __AnchorOutputIdMethod__
| __DelegationOutputIdsMethod__
| __DelegationOutputIdMethod__
| __FoundryOutputIdsMethod__
Expand Down
4 changes: 4 additions & 0 deletions bindings/nodejs/lib/types/client/query-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export type AccountQueryParameter =
| UnlockableByAddress
| CommonQueryParameters;

/** Query parameters for filtering Anchor Outputs */
// TODO https://github.com/iotaledger/iota-sdk/issues/1503
export type AnchorQueryParameter = any;

/** Query parameters for filtering Delegation Outputs */
// TODO https://github.com/iotaledger/iota-sdk/issues/1503
export type DelegationQueryParameter = any;
Expand Down
29 changes: 27 additions & 2 deletions bindings/python/iota_sdk/client/_node_indexer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,33 @@ def account_output_id(self, account_id: HexStr) -> OutputId:
'accountId': account_id
}))

def delegation_output_ids(
self, query_parameters: QueryParameters) -> OutputIdsResponse:
def anchor_output_ids(
self, query_parameters: QueryParameters) -> OutputIdsResponse:
"""Fetch anchor output IDs from the given query parameters.
Returns:
The corresponding output IDs of the anchor outputs.
"""

query_parameters_camelized = query_parameters.to_dict()

response = self._call_method('anchorOutputIds', {
'queryParameters': query_parameters_camelized,
})
return OutputIdsResponse(response)

def anchor_output_id(self, anchor_id: HexStr) -> OutputId:
"""Fetch anchor output ID from the given anchor ID.
Returns:
The output ID of the anchor output.
"""
return OutputId.from_string(self._call_method('anchorOutputId', {
'anchorId': anchor_id
}))

def delegation_output_ids(
self, query_parameters: QueryParameters) -> OutputIdsResponse:
"""Fetch delegation output IDs from the given query parameters.
Returns:
Expand Down
73 changes: 48 additions & 25 deletions sdk/src/client/node_api/indexer/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use crate::{
client::{
node_api::indexer::query_parameters::{
AccountOutputQueryParameters, BasicOutputQueryParameters, DelegationOutputQueryParameters,
FoundryOutputQueryParameters, NftOutputQueryParameters, OutputQueryParameters,
AccountOutputQueryParameters, AnchorOutputQueryParameters, BasicOutputQueryParameters,
DelegationOutputQueryParameters, FoundryOutputQueryParameters, NftOutputQueryParameters,
OutputQueryParameters,
},
ClientInner, Error, Result,
},
types::{
api::plugins::indexer::OutputIdsResponse,
block::output::{AccountId, DelegationId, FoundryId, NftId, OutputId},
block::output::{AccountId, AnchorId, DelegationId, FoundryId, NftId, OutputId},
},
};

Expand All @@ -38,54 +39,76 @@ impl ClientInner {
self.get_output_ids(route, query_parameters, true, false).await
}

/// Get delegation outputs filtered by the given parameters.
/// Get account outputs filtered by the given parameters.
/// GET with query parameter returns all outputIDs that fit these filter criteria.
/// Returns Err(Node(NotFound) if no results are found.
/// api/indexer/v2/outputs/delegation
pub async fn delegation_output_ids(
/// api/indexer/v2/outputs/account
pub async fn account_output_ids(
&self,
query_parameters: DelegationOutputQueryParameters,
query_parameters: AccountOutputQueryParameters,
) -> Result<OutputIdsResponse> {
let route = "api/indexer/v2/outputs/delegation";
let route = "api/indexer/v2/outputs/account";

self.get_output_ids(route, query_parameters, true, false).await
}

/// Get delegation output by its delegationID.
/// api/indexer/v2/outputs/delegation/:{DelegationId}
pub async fn delegation_output_id(&self, delegation_id: DelegationId) -> Result<OutputId> {
let route = format!("api/indexer/v2/outputs/delegation/{delegation_id}");
/// Get account output by its accountID.
/// api/indexer/v2/outputs/account/:{AccountId}
pub async fn account_output_id(&self, account_id: AccountId) -> Result<OutputId> {
let route = format!("api/indexer/v2/outputs/account/{account_id}");

Ok(*(self
.get_output_ids(&route, DelegationOutputQueryParameters::new(), true, false)
.get_output_ids(&route, AccountOutputQueryParameters::new(), true, false)
.await?
.first()
.ok_or_else(|| Error::NoOutput(format!("{delegation_id:?}")))?))
.ok_or_else(|| Error::NoOutput(format!("{account_id:?}")))?))
}

/// Get account outputs filtered by the given parameters.
/// Get anchor outputs filtered by the given parameters.
/// GET with query parameter returns all outputIDs that fit these filter criteria.
/// Returns Err(Node(NotFound) if no results are found.
/// api/indexer/v2/outputs/account
pub async fn account_output_ids(
/// api/indexer/v2/outputs/anchor
pub async fn anchor_output_ids(&self, query_parameters: AnchorOutputQueryParameters) -> Result<OutputIdsResponse> {
let route = "api/indexer/v2/outputs/anchor";

self.get_output_ids(route, query_parameters, true, false).await
}

/// Get anchor output by its anchorID.
/// api/indexer/v2/outputs/anchor/:{AnchorId}
pub async fn anchor_output_id(&self, anchor_id: AnchorId) -> Result<OutputId> {
let route = format!("api/indexer/v2/outputs/anchor/{anchor_id}");

Ok(*(self
.get_output_ids(&route, AnchorOutputQueryParameters::new(), true, false)
.await?
.first()
.ok_or_else(|| Error::NoOutput(format!("{anchor_id:?}")))?))
}

/// Get delegation outputs filtered by the given parameters.
/// GET with query parameter returns all outputIDs that fit these filter criteria.
/// Returns Err(Node(NotFound) if no results are found.
/// api/indexer/v2/outputs/delegation
pub async fn delegation_output_ids(
&self,
query_parameters: AccountOutputQueryParameters,
query_parameters: DelegationOutputQueryParameters,
) -> Result<OutputIdsResponse> {
let route = "api/indexer/v2/outputs/account";
let route = "api/indexer/v2/outputs/delegation";

self.get_output_ids(route, query_parameters, true, false).await
}

/// Get account output by its accountID.
/// api/indexer/v2/outputs/account/:{AccountId}
pub async fn account_output_id(&self, account_id: AccountId) -> Result<OutputId> {
let route = format!("api/indexer/v2/outputs/account/{account_id}");
/// Get delegation output by its delegationID.
/// api/indexer/v2/outputs/delegation/:{DelegationId}
pub async fn delegation_output_id(&self, delegation_id: DelegationId) -> Result<OutputId> {
let route = format!("api/indexer/v2/outputs/delegation/{delegation_id}");

Ok(*(self
.get_output_ids(&route, AccountOutputQueryParameters::new(), true, false)
.get_output_ids(&route, DelegationOutputQueryParameters::new(), true, false)
.await?
.first()
.ok_or_else(|| Error::NoOutput(format!("{account_id:?}")))?))
.ok_or_else(|| Error::NoOutput(format!("{delegation_id:?}")))?))
}

/// Get foundry outputs filtered by the given parameters.
Expand Down

0 comments on commit f92e864

Please sign in to comment.