Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Token Type Filter in search_assets fuction #186

Open
wants to merge 12 commits into
base: das-38-rebase-stale-te-branch
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion das_api/src/api/api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ impl ApiContract for DasApi {
negate,
condition_type,
interface,
token_type,
owner_address,
owner_type,
creator_address,
Expand Down Expand Up @@ -403,7 +404,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 @@ -431,8 +432,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, TransactionSignatureList};
use digital_asset_types::rpc::{filter::AssetSorting, response::GetGroupingResponse};
Expand Down Expand Up @@ -94,6 +94,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
12 changes: 12 additions & 0 deletions digital_asset_types/src/dao/extensions/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::dao::{
asset, asset_authority, asset_creators, asset_data, asset_grouping,
asset_v1_account_attachments,
sea_orm_active_enums::{OwnerType, RoyaltyTargetType},
token_accounts,
};

#[derive(Copy, Clone, Debug, EnumIter)]
Expand All @@ -13,6 +14,7 @@ pub enum Relation {
AssetAuthority,
AssetCreators,
AssetGrouping,
TokenAccounts,
}

impl RelationTrait for Relation {
Expand All @@ -22,6 +24,10 @@ impl RelationTrait for Relation {
.from(asset::Column::AssetData)
.to(asset_data::Column::Id)
.into(),
Self::TokenAccounts => asset::Entity::belongs_to(token_accounts::Entity)
.from(asset::Column::Id)
.to(token_accounts::Column::Mint)
.into(),
Self::AssetV1AccountAttachments => {
asset::Entity::has_many(asset_v1_account_attachments::Entity).into()
}
Expand Down Expand Up @@ -62,6 +68,12 @@ impl Related<asset_grouping::Entity> for asset::Entity {
}
}

impl Related<token_accounts::Entity> for asset::Entity {
fn to() -> RelationDef {
Relation::TokenAccounts.def()
}
}

impl Default for RoyaltyTargetType {
fn default() -> Self {
Self::Creators
Expand Down
1 change: 1 addition & 0 deletions digital_asset_types/src/dao/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod asset_data;
pub mod asset_grouping;
pub mod asset_v1_account_attachment;
pub mod instruction;
pub mod token_accounts;
26 changes: 26 additions & 0 deletions digital_asset_types/src/dao/extensions/token_accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use sea_orm::{EntityTrait, EnumIter, Related, RelationDef, RelationTrait};

use crate::dao::{asset, token_accounts};

#[derive(Copy, Clone, Debug, EnumIter)]

pub enum Relation {
Asset,
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Asset => token_accounts::Entity::belongs_to(asset::Entity)
.from(token_accounts::Column::Mint)
.to(asset::Column::Id)
.into(),
}
}
}

impl Related<asset::Entity> for token_accounts::Entity {
fn to() -> RelationDef {
Relation::Asset.def()
}
}
126 changes: 108 additions & 18 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,6 +79,33 @@ pub struct SearchAssetsQuery {
}

impl SearchAssetsQuery {
pub fn check_for_onwer_type_and_token_type(&self) -> Result<(), DbErr> {
if self.token_type.is_some() && self.owner_type.is_some() {
return Err(DbErr::Custom(
"`owner_type` is not supported when using `token_type` field".to_string(),
));
}
Ok(())
}

pub fn check_for_owner_address_and_token_type(&self) -> Result<(), DbErr> {
if self.owner_address.is_none() && self.token_type.is_some() {
return Err(DbErr::Custom(
"Must provide `owner_address` when using `token_type` field".to_string(),
));
}
Ok(())
}
pub fn check_for_token_type_and_interface(&self) -> Result<(), DbErr> {
if self.token_type.is_some() && 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> {
let mut conditions = match self.condition_type {
// None --> default to all when no option is provided
Expand All @@ -88,16 +119,38 @@ impl SearchAssetsQuery {
.clone()
.map(|x| asset::Column::SpecificationVersion.eq(x)),
)
.add_option(
.add_option({
self.check_for_owner_address_and_token_type()?;
self.check_for_onwer_type_and_token_type()?;
self.token_type.as_ref().map(|x| match x {
TokenTypeClass::Compressed => asset::Column::TreeId.is_not_null(),
TokenTypeClass::Nft => 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)),
),
TokenTypeClass::NonFungible => asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::Nft)
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::ProgrammableNft))
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::MplCoreAsset)),
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.check_for_token_type_and_interface()?;
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)),
)
.map(|x| asset::Column::SpecificationAssetClass.eq(x))
})
.add_option(
self.delegate
.to_owned()
Expand Down Expand Up @@ -145,16 +198,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 +265,25 @@ 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(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.
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 @@ -5,3 +5,4 @@ mod fungibles_and_token_extensions_tests;
mod general_scenario_tests;
mod mpl_core_tests;
mod regular_nft_tests;
mod token_type_test;
Loading