From d6b96d7d30e5dbcd72ccc193decfb9f2fec1df4e Mon Sep 17 00:00:00 2001 From: AhzamAkhtar Date: Wed, 25 Dec 2024 00:15:52 +0530 Subject: [PATCH] cleanup --- das_api/src/api/mod.rs | 2 +- .../src/dao/extensions/asset.rs | 4 +- digital_asset_types/src/dao/extensions/mod.rs | 2 +- .../src/dao/extensions/token_accounts.rs | 14 +-- digital_asset_types/src/dao/mod.rs | 109 +++++++++--------- digital_asset_types/src/rpc/filter.rs | 6 +- .../integration_tests/token_type_test.rs | 2 - 7 files changed, 70 insertions(+), 69 deletions(-) diff --git a/das_api/src/api/mod.rs b/das_api/src/api/mod.rs index 9e0d02dd7..cbfc18e9a 100644 --- a/das_api/src/api/mod.rs +++ b/das_api/src/api/mod.rs @@ -94,7 +94,7 @@ pub struct SearchAssets { pub negate: Option, pub condition_type: Option, pub interface: Option, - pub token_type : Option, + pub token_type: Option, pub owner_address: Option, pub owner_type: Option, pub creator_address: Option, diff --git a/digital_asset_types/src/dao/extensions/asset.rs b/digital_asset_types/src/dao/extensions/asset.rs index eeb31fed8..1d8e7f1d3 100644 --- a/digital_asset_types/src/dao/extensions/asset.rs +++ b/digital_asset_types/src/dao/extensions/asset.rs @@ -2,11 +2,11 @@ use sea_orm::{EntityTrait, EnumIter, Related, RelationDef, RelationTrait}; use crate::dao::{ asset, asset_authority, asset_creators, asset_data, asset_grouping, - asset_v1_account_attachments,token_accounts, + asset_v1_account_attachments, sea_orm_active_enums::{OwnerType, RoyaltyTargetType}, + token_accounts, }; - #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { AssetData, diff --git a/digital_asset_types/src/dao/extensions/mod.rs b/digital_asset_types/src/dao/extensions/mod.rs index cdc57c34c..af3baad66 100644 --- a/digital_asset_types/src/dao/extensions/mod.rs +++ b/digital_asset_types/src/dao/extensions/mod.rs @@ -5,4 +5,4 @@ pub mod asset_data; pub mod asset_grouping; pub mod asset_v1_account_attachment; pub mod instruction; -pub mod token_accounts; \ No newline at end of file +pub mod token_accounts; diff --git a/digital_asset_types/src/dao/extensions/token_accounts.rs b/digital_asset_types/src/dao/extensions/token_accounts.rs index 2027d4556..a8c8f60eb 100644 --- a/digital_asset_types/src/dao/extensions/token_accounts.rs +++ b/digital_asset_types/src/dao/extensions/token_accounts.rs @@ -9,12 +9,12 @@ pub enum Relation { } 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(), + 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(), } } } @@ -23,4 +23,4 @@ impl Related for token_accounts::Entity { fn to() -> RelationDef { Relation::Asset.def() } -} \ No newline at end of file +} diff --git a/digital_asset_types/src/dao/mod.rs b/digital_asset_types/src/dao/mod.rs index 6a67c5701..8e6dc4ecb 100644 --- a/digital_asset_types/src/dao/mod.rs +++ b/digital_asset_types/src/dao/mod.rs @@ -79,12 +79,11 @@ 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())); + "`owner_type` is not supported when using `token_type` field".to_string(), + )); } Ok(()) } @@ -92,8 +91,8 @@ impl SearchAssetsQuery { 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())); + "Must provide `owner_address` when using `token_type` field".to_string(), + )); } Ok(()) } @@ -101,7 +100,8 @@ impl SearchAssetsQuery { 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())); + .to_string(), + )); } Ok(()) } @@ -122,31 +122,34 @@ impl SearchAssetsQuery { .add_option({ self.check_for_owner_address_and_token_type()?; self.check_for_onwer_type_and_token_type()?; - match &self.token_type { - Some(x) => Some(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(), - }), - None => None, - } + 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)) + .clone() + .map(|x| asset::Column::SpecificationAssetClass.eq(x)) }) .add_option( self.delegate @@ -198,17 +201,17 @@ impl SearchAssetsQuery { 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)) - ), - )); + 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(), - )); + conditions = conditions + .add_option(Some(asset::Column::SpecificationAssetClass.is_not_null())); } _ => { // Default to NFTs @@ -217,14 +220,13 @@ impl SearchAssetsQuery { // 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)), - )); + asset::Column::OwnerType + .eq(OwnerType::Single) + .and(asset::Column::Supply.lte(1)), + )); } } - - } + } if let Some(c) = self.creator_address.to_owned() { conditions = conditions.add(asset_creators::Column::Creator.eq(c)); @@ -264,21 +266,22 @@ impl SearchAssetsQuery { } if let Some(o) = self.owner_address.to_owned() { - if self.token_type == Some(TokenTypeClass::Fungible) || self.token_type == Some(TokenTypeClass::All) { + 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); + 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() { diff --git a/digital_asset_types/src/rpc/filter.rs b/digital_asset_types/src/rpc/filter.rs index 1c5854563..e90b4a158 100644 --- a/digital_asset_types/src/rpc/filter.rs +++ b/digital_asset_types/src/rpc/filter.rs @@ -1,6 +1,6 @@ use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "camelCase")] @@ -32,13 +32,13 @@ pub enum AssetSortBy { None, } -#[derive(Debug, Clone, PartialEq, Eq ,EnumIter, Serialize, Deserialize,JsonSchema)] +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, Serialize, Deserialize, JsonSchema)] pub enum TokenTypeClass { Fungible, NonFungible, Compressed, Nft, - All + All, } #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] diff --git a/integration_tests/tests/integration_tests/token_type_test.rs b/integration_tests/tests/integration_tests/token_type_test.rs index 201462453..260d92567 100644 --- a/integration_tests/tests/integration_tests/token_type_test.rs +++ b/integration_tests/tests/integration_tests/token_type_test.rs @@ -43,7 +43,6 @@ async fn test_search_asset_with_token_type_regular_nft() { insta::assert_json_snapshot!(name, response); } - #[tokio::test] #[serial] #[named] @@ -149,7 +148,6 @@ async fn test_search_asset_with_token_type_all() { insta::assert_json_snapshot!(name, response); } - #[tokio::test] #[serial] #[named]