Skip to content

Commit

Permalink
token_type filter added in search_asset rpc method (#233)
Browse files Browse the repository at this point in the history
* Search assets by token_type

Add token_type to search asset filter. Apply conditional filters on
database queries based on token_type.

* resolve comments

---------

Co-authored-by: Nagaprasadvr <nagaprasadvr246@gmail.com>
  • Loading branch information
AhzamAkhtar and Nagaprasadvr authored Jan 21, 2025
1 parent dee1c95 commit 06297e1
Show file tree
Hide file tree
Showing 42 changed files with 1,013 additions and 19 deletions.
6 changes: 4 additions & 2 deletions das_api/src/api/api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ impl ApiContract for DasApi {
let GetAssets { ids, options } = payload;

let mut ids: Vec<String> = ids.into_iter().collect();
ids.sort();
ids.dedup();

let batch_size = ids.len();
Expand Down Expand Up @@ -379,6 +378,7 @@ impl ApiContract for DasApi {
negate,
condition_type,
interface,
token_type,
owner_address,
owner_type,
creator_address,
Expand Down Expand Up @@ -408,7 +408,7 @@ impl ApiContract for DasApi {

// Deserialize search assets query
let spec: Option<(SpecificationVersions, SpecificationAssetClass)> =
interface.map(|x| x.into());
interface.clone().map(|x| x.into());
let specification_version = spec.clone().map(|x| x.0);
let specification_asset_class = spec.map(|x| x.1);
let condition_type = condition_type.map(|x| match x {
Expand Down Expand Up @@ -436,8 +436,10 @@ impl ApiContract for DasApi {
let saq = SearchAssetsQuery {
negate,
condition_type,
interface,
specification_version,
specification_asset_class,
token_type,
owner_address,
owner_type,
creator_address,
Expand Down
3 changes: 2 additions & 1 deletion das_api/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::DasApiError;
use async_trait::async_trait;
use digital_asset_types::rpc::filter::{AssetSortDirection, SearchConditionType};
use digital_asset_types::rpc::filter::{AssetSortDirection, SearchConditionType, TokenTypeClass};
use digital_asset_types::rpc::options::Options;
use digital_asset_types::rpc::response::{
AssetList, NftEditions, TokenAccountList, TransactionSignatureList,
Expand Down Expand Up @@ -96,6 +96,7 @@ pub struct SearchAssets {
pub negate: Option<bool>,
pub condition_type: Option<SearchConditionType>,
pub interface: Option<Interface>,
pub token_type: Option<TokenTypeClass>,
pub owner_address: Option<String>,
pub owner_type: Option<OwnershipModel>,
pub creator_address: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion digital_asset_types/src/dao/extensions/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl RelationTrait for Relation {
.from(asset::Column::AssetData)
.to(asset_data::Column::Id)
.into(),
Self::TokenAccounts => asset::Entity::belongs_to(token_accounts::Entity)
Self::TokenAccounts => asset::Entity::has_many(token_accounts::Entity)
.from(asset::Column::Id)
.to(token_accounts::Column::Mint)
.into(),
Expand Down
117 changes: 102 additions & 15 deletions digital_asset_types/src/dao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
mod full_asset;
mod generated;
pub mod scopes;
use crate::rpc::{filter::TokenTypeClass, Interface};

use self::sea_orm_active_enums::{
OwnerType, RoyaltyTargetType, SpecificationAssetClass, SpecificationVersions,
};
Expand Down Expand Up @@ -52,8 +54,10 @@ pub struct SearchAssetsQuery {
pub negate: Option<bool>,
/// Defaults to [ConditionType::All]
pub condition_type: Option<ConditionType>,
pub interface: Option<Interface>,
pub specification_version: Option<SpecificationVersions>,
pub specification_asset_class: Option<SpecificationAssetClass>,
pub token_type: Option<TokenTypeClass>,
pub owner_address: Option<Vec<u8>>,
pub owner_type: Option<OwnerType>,
pub creator_address: Option<Vec<u8>>,
Expand All @@ -75,7 +79,31 @@ pub struct SearchAssetsQuery {
}

impl SearchAssetsQuery {
fn validate(&self) -> Result<(), DbErr> {
if self.token_type.is_some() {
if self.owner_type.is_some() {
return Err(DbErr::Custom(
"`owner_type` is not supported when using `token_type` field".to_string(),
));
}
if self.owner_address.is_none() {
return Err(DbErr::Custom(
"Must provide `owner_address` when using `token_type` field".to_string(),
));
}
if self.interface.is_some() {
return Err(DbErr::Custom(
"`specification_asset_class` is not supported when using `token_type` field"
.to_string(),
));
}
}
Ok(())
}

pub fn conditions(&self) -> Result<(Condition, Vec<RelationDef>), DbErr> {
self.validate()?;

let mut conditions = match self.condition_type {
// None --> default to all when no option is provided
None | Some(ConditionType::All) => Condition::all(),
Expand All @@ -88,16 +116,33 @@ impl SearchAssetsQuery {
.clone()
.map(|x| asset::Column::SpecificationVersion.eq(x)),
)
.add_option(self.token_type.as_ref().map(|x| {
match x {
TokenTypeClass::Compressed => asset::Column::TreeId.is_not_null(),
TokenTypeClass::Nft | TokenTypeClass::NonFungible => {
asset::Column::TreeId.is_null().and(
asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::Nft)
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::MplCoreAsset))
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::ProgrammableNft))
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::MplCoreCollection)),
)
}
TokenTypeClass::Fungible => asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleAsset)
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleToken)),
TokenTypeClass::All => asset::Column::SpecificationAssetClass.is_not_null(),
}
}))
.add_option(
self.specification_asset_class
.clone()
.map(|x| asset::Column::SpecificationAssetClass.eq(x)),
)
.add_option(
self.owner_address
.to_owned()
.map(|x| asset::Column::Owner.eq(x)),
)
.add_option(
self.delegate
.to_owned()
Expand Down Expand Up @@ -145,16 +190,34 @@ impl SearchAssetsQuery {
if let Some(o) = self.owner_type.clone() {
conditions = conditions.add(asset::Column::OwnerType.eq(o));
} else {
// Default to NFTs
//
// In theory, the owner_type=single check should be sufficient,
// however there is an old bug that has marked some non-NFTs as "single" with supply > 1.
// The supply check guarentees we do not include those.
conditions = conditions.add_option(Some(
asset::Column::OwnerType
.eq(OwnerType::Single)
.and(asset::Column::Supply.lte(1)),
));
match self.token_type {
Some(TokenTypeClass::Fungible) => {
conditions = conditions.add_option(Some(
asset::Column::OwnerType.eq(OwnerType::Token).and(
(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleToken))
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleAsset)),
),
));
}
Some(TokenTypeClass::All) => {
conditions = conditions
.add_option(Some(asset::Column::SpecificationAssetClass.is_not_null()));
}
_ => {
// Default to NFTs
//
// In theory, the owner_type=single check should be sufficient,
// however there is an old bug that has marked some non-NFTs as "single" with supply > 1.
// The supply check guarentees we do not include those.
conditions = conditions.add_option(Some(
asset::Column::OwnerType
.eq(OwnerType::Single)
.and(asset::Column::Supply.lte(1)),
));
}
}
}

if let Some(c) = self.creator_address.to_owned() {
Expand Down Expand Up @@ -194,6 +257,30 @@ impl SearchAssetsQuery {
joins.push(rel);
}

if let Some(o) = self.owner_address.to_owned() {
if self.token_type == Some(TokenTypeClass::Fungible)
|| self.token_type == Some(TokenTypeClass::All)
{
conditions = conditions.add(
asset::Column::Owner
.eq(o.clone())
.or(token_accounts::Column::Owner.eq(o)),
);

let rel = extensions::token_accounts::Relation::Asset
.def()
.rev()
.on_condition(|left, right| {
Expr::tbl(right, token_accounts::Column::Mint)
.eq(Expr::tbl(left, asset::Column::Id))
.into_condition()
});
joins.push(rel);
} else {
conditions = conditions.add(asset::Column::Owner.eq(o));
}
}

if let Some(g) = self.grouping.to_owned() {
let cond = Condition::all()
.add(asset_grouping::Column::GroupKey.eq(g.0))
Expand Down
10 changes: 10 additions & 0 deletions digital_asset_types/src/rpc/filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use schemars::JsonSchema;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
Expand Down Expand Up @@ -31,6 +32,15 @@ pub enum AssetSortBy {
None,
}

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, Serialize, Deserialize, JsonSchema)]
pub enum TokenTypeClass {
Fungible,
NonFungible,
Compressed,
Nft,
All,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum AssetSortDirection {
#[serde(rename = "asc")]
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

�Ÿ���9����mR�i\���꠽�]�
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
e 9;�:�$f��E�v�KA�
��\�%.���
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.B�Q�*pp-���U������g����>�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions integration_tests/tests/integration_tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ mod show_inscription_flag_tests;
mod test_get_assets_with_multiple_same_ids;
mod test_show_zero_balance_filter;
mod token_accounts_tests;
mod token_type_test;
Loading

0 comments on commit 06297e1

Please sign in to comment.