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

token_type filter added in search_asset rpc method #233

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -379,6 +379,7 @@ impl ApiContract for DasApi {
negate,
condition_type,
interface,
token_type,
owner_address,
owner_type,
creator_address,
Expand Down Expand Up @@ -408,7 +409,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 +437,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
119 changes: 104 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,35 @@ 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 => 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)),
kespinola marked this conversation as resolved.
Show resolved Hide resolved
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 +192,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 +259,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;
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
---
source: integration_tests/tests/integration_tests/regular_nft_tests.rs
expression: response
snapshot_kind: text
---
[
{
"interface": "ProgrammableNFT",
"id": "HTKAVZZrDdyecCxzm3WEkCsG1GUmiqKm73PvngfuYRNK",
"id": "2NqdYX6kJmMUoChnDXU2UrP9BsoPZivRw3uJG8iDhRRd",
"content": {
"$schema": "https://schema.metaplex.com/nft1.0.json",
"json_uri": "https://madlads.s3.us-west-2.amazonaws.com/json/9965.json",
"json_uri": "https://madlads.s3.us-west-2.amazonaws.com/json/9959.json",
"files": [],
"metadata": {
"name": "Mad Lads #9965",
"name": "Mad Lads #9959",
Nagaprasadvr marked this conversation as resolved.
Show resolved Hide resolved
"symbol": "MAD",
"token_standard": "ProgrammableNonFungible"
},
Expand Down Expand Up @@ -65,22 +64,22 @@ snapshot_kind: text
"ownership": {
"frozen": true,
"delegated": true,
"delegate": "Hys4KoEw32y4sLHb96K2PqYT5jUHqUWdh6qS9EACEB4Y",
"delegate": "CimD28VvDSHcGdErmLbbPimnkfwrKHvstNiQUutZDxWS",
"ownership_model": "single",
"owner": "BaBQKh34KrqZzd4ifSHQYMf86HiBGASN6TWUi1ZwfyKv"
"owner": "9PacVenjPyQYiWBha89UYRM1nn6mf9bGY7vi32zY6DLn"
},
"mutable": true,
"burnt": false
},
{
"interface": "ProgrammableNFT",
"id": "2NqdYX6kJmMUoChnDXU2UrP9BsoPZivRw3uJG8iDhRRd",
"id": "HTKAVZZrDdyecCxzm3WEkCsG1GUmiqKm73PvngfuYRNK",
"content": {
"$schema": "https://schema.metaplex.com/nft1.0.json",
"json_uri": "https://madlads.s3.us-west-2.amazonaws.com/json/9959.json",
"json_uri": "https://madlads.s3.us-west-2.amazonaws.com/json/9965.json",
"files": [],
"metadata": {
"name": "Mad Lads #9959",
"name": "Mad Lads #9965",
"symbol": "MAD",
"token_standard": "ProgrammableNonFungible"
},
Expand Down Expand Up @@ -133,9 +132,9 @@ snapshot_kind: text
"ownership": {
"frozen": true,
"delegated": true,
"delegate": "CimD28VvDSHcGdErmLbbPimnkfwrKHvstNiQUutZDxWS",
"delegate": "Hys4KoEw32y4sLHb96K2PqYT5jUHqUWdh6qS9EACEB4Y",
"ownership_model": "single",
"owner": "9PacVenjPyQYiWBha89UYRM1nn6mf9bGY7vi32zY6DLn"
"owner": "BaBQKh34KrqZzd4ifSHQYMf86HiBGASN6TWUi1ZwfyKv"
},
"mutable": true,
"burnt": false
Expand Down
Loading