diff --git a/digital_asset_types/src/dao/extensions/mod.rs b/digital_asset_types/src/dao/extensions/mod.rs index 024e1ba5d..bcfc3e130 100644 --- a/digital_asset_types/src/dao/extensions/mod.rs +++ b/digital_asset_types/src/dao/extensions/mod.rs @@ -5,5 +5,3 @@ pub mod asset_data; pub mod asset_grouping; pub mod asset_v1_account_attachment; pub mod instruction; -pub mod token_accounts; -pub mod tokens; diff --git a/digital_asset_types/src/dao/extensions/token_accounts.rs b/digital_asset_types/src/dao/extensions/token_accounts.rs deleted file mode 100644 index 4db719d35..000000000 --- a/digital_asset_types/src/dao/extensions/token_accounts.rs +++ /dev/null @@ -1,25 +0,0 @@ -use sea_orm::{EntityTrait, EnumIter, Related, RelationDef, RelationTrait}; - -use crate::dao::{token_accounts, tokens}; - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Tokens, -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Tokens => token_accounts::Entity::belongs_to(tokens::Entity) - .from(token_accounts::Column::Mint) - .to(tokens::Column::Mint) - .into(), - } - } -} - -impl Related for token_accounts::Entity { - fn to() -> RelationDef { - Relation::Tokens.def() - } -} diff --git a/digital_asset_types/src/dao/extensions/tokens.rs b/digital_asset_types/src/dao/extensions/tokens.rs deleted file mode 100644 index a6d3a29c1..000000000 --- a/digital_asset_types/src/dao/extensions/tokens.rs +++ /dev/null @@ -1,36 +0,0 @@ -use sea_orm::{EntityTrait, EnumIter, Related, RelationDef, RelationTrait}; - -use crate::dao::{asset, token_accounts, tokens}; - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - TokenAccounts, - Asset, -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::TokenAccounts => tokens::Entity::belongs_to(token_accounts::Entity) - .from(tokens::Column::Mint) - .to(token_accounts::Column::Mint) - .into(), - Self::Asset => tokens::Entity::belongs_to(asset::Entity) - .from(tokens::Column::Mint) - .to(asset::Column::Id) - .into(), - } - } -} - -impl Related for tokens::Entity { - fn to() -> RelationDef { - Relation::TokenAccounts.def() - } -} - -impl Related for tokens::Entity { - fn to() -> RelationDef { - Relation::Asset.def() - } -} diff --git a/digital_asset_types/src/dao/full_asset.rs b/digital_asset_types/src/dao/full_asset.rs index 135031ac2..7d24a9cce 100644 --- a/digital_asset_types/src/dao/full_asset.rs +++ b/digital_asset_types/src/dao/full_asset.rs @@ -1,5 +1,5 @@ use crate::dao::{ - asset, asset_authority, asset_creators, asset_data, asset_grouping, token_accounts, + asset, asset_authority, asset_creators, asset_data, asset_grouping, }; use super::tokens; @@ -11,7 +11,7 @@ pub struct FullAsset { pub authorities: Vec, pub creators: Vec, pub groups: Vec, - pub token_info: Option<(tokens::Model, Option)>, + pub token_info: Option, } #[derive(Clone, Debug, PartialEq)] pub struct AssetRelated { diff --git a/digital_asset_types/src/dao/mod.rs b/digital_asset_types/src/dao/mod.rs index d060d6830..c218d092c 100644 --- a/digital_asset_types/src/dao/mod.rs +++ b/digital_asset_types/src/dao/mod.rs @@ -77,7 +77,7 @@ pub struct SearchAssetsQuery { } impl SearchAssetsQuery { - pub fn conditions(&self, options: &Options) -> Result<(Condition, Vec), DbErr> { + pub fn conditions(&self) -> Result<(Condition, Vec), DbErr> { let mut conditions = match self.condition_type { // None --> default to all when no option is provided None | Some(ConditionType::All) => Condition::all(), @@ -147,24 +147,16 @@ impl SearchAssetsQuery { if let Some(o) = self.owner_type.clone() { conditions = conditions.add(asset::Column::OwnerType.eq(o)); } else { - // Default to NFTs + // By default, we ignore malformed tokens by ignoring tokens with supply=0 + // unless they are burnt. // - // 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. - if options.show_zero_balance { - conditions = conditions.add( - token_accounts::Column::Amount - .eq(0) - .or(token_accounts::Column::Amount.ne(0)), - ); - } else { - conditions = conditions.add( - tokens::Column::Supply - .ne(0) - .or(asset::Column::Burnt.eq(true)), - ); - } + // cNFTs keep supply=1 after they are burnt. + // Regular NFTs go to supply=0 after they are burnt. + conditions = conditions.add( + asset::Column::Supply + .ne(0) + .or(asset::Column::Burnt.eq(true)), + ) } if let Some(c) = self.creator_address.to_owned() { @@ -191,25 +183,6 @@ impl SearchAssetsQuery { joins.push(rel); } - let rel = extensions::tokens::Relation::Asset - .def() - .rev() - .on_condition(|left, right| { - Expr::tbl(right, tokens::Column::Mint) - .eq(Expr::tbl(left, asset::Column::Id)) - .into_condition() - }); - joins.push(rel); - - let rel = extensions::token_accounts::Relation::Tokens - .def() - .rev() - .on_condition(|left, right| { - Expr::tbl(right, token_accounts::Column::Mint) - .eq(Expr::tbl(left, tokens::Column::Mint)) - .into_condition() - }); - joins.push(rel); if let Some(a) = self.authority_address.to_owned() { conditions = conditions.add(asset_authority::Column::Authority.eq(a)); diff --git a/digital_asset_types/src/dao/scopes/asset.rs b/digital_asset_types/src/dao/scopes/asset.rs index e9f303f8e..085db2c73 100644 --- a/digital_asset_types/src/dao/scopes/asset.rs +++ b/digital_asset_types/src/dao/scopes/asset.rs @@ -4,7 +4,7 @@ use crate::{ asset_authority, asset_creators, asset_data, asset_grouping, cl_audits_v2, extensions::{self, instruction::PascalCase}, sea_orm_active_enums::Instruction, - token_accounts, tokens, Cursor, FullAsset, GroupingSize, Pagination, + tokens, Cursor, FullAsset, GroupingSize, Pagination, }, rpc::{filter::AssetSortDirection, options::Options}, }; @@ -60,7 +60,7 @@ pub async fn get_by_creator( sort_direction: Order, pagination: &Pagination, limit: u64, - show_unverified_collections: bool, + options: &Options, ) -> Result, DbErr> { let mut condition = Condition::all() .add(asset_creators::Column::Creator.eq(creator.clone())) @@ -76,7 +76,7 @@ pub async fn get_by_creator( sort_direction, pagination, limit, - show_unverified_collections, + options, Some(creator), ) .await @@ -112,13 +112,13 @@ pub async fn get_by_grouping( sort_direction: Order, pagination: &Pagination, limit: u64, - show_unverified_collections: bool, + options: &Options, ) -> Result, DbErr> { let mut condition = asset_grouping::Column::GroupKey .eq(group_key) .and(asset_grouping::Column::GroupValue.eq(group_value)); - if !show_unverified_collections { + if !options.show_unverified_collections { condition = condition.and( asset_grouping::Column::Verified .eq(true) @@ -136,7 +136,7 @@ pub async fn get_by_grouping( sort_direction, pagination, limit, - show_unverified_collections, + options, None, ) .await @@ -163,7 +163,7 @@ pub async fn get_assets_by_owner( sort_direction, pagination, limit, - options.show_unverified_collections, + options, ) .await } @@ -173,6 +173,7 @@ pub async fn get_assets( asset_ids: Vec>, pagination: &Pagination, limit: u64, + options: &Options, ) -> Result, DbErr> { let cond = Condition::all() .add(asset::Column::Id.is_in(asset_ids)) @@ -187,7 +188,7 @@ pub async fn get_assets( Order::Asc, pagination, limit, - false, + options, ) .await } @@ -199,7 +200,7 @@ pub async fn get_by_authority( sort_direction: Order, pagination: &Pagination, limit: u64, - show_unverified_collections: bool, + options: &Options, ) -> Result, DbErr> { let cond = Condition::all() .add(asset_authority::Column::Authority.eq(authority)) @@ -212,7 +213,7 @@ pub async fn get_by_authority( sort_direction, pagination, limit, - show_unverified_collections, + options, None, ) .await @@ -227,7 +228,7 @@ async fn get_by_related_condition( sort_direction: Order, pagination: &Pagination, limit: u64, - show_unverified_collections: bool, + options: &Options, required_creator: Option>, ) -> Result, DbErr> where @@ -246,14 +247,14 @@ where let assets = paginate(pagination, limit, stmt, sort_direction, asset::Column::Id) .all(conn) .await?; - get_related_for_assets(conn, assets, show_unverified_collections, required_creator).await + get_related_for_assets(conn, assets, required_creator , options).await } pub async fn get_related_for_assets( conn: &impl ConnectionTrait, assets: Vec, - show_unverified_collections: bool, required_creator: Option>, + options: &Options, ) -> Result, DbErr> { let asset_ids = assets.iter().map(|a| a.id.clone()).collect::>(); @@ -328,16 +329,18 @@ pub async fn get_related_for_assets( } } - for id in ids.clone() { - let id_clone = id.clone(); - if let Ok(token_info) = get_token_by_id(conn, id_clone).await { - if let Some(asset) = assets_map.get_mut(&id) { - asset.token_info = Some(token_info); + if options.show_fungible { + for id in ids.clone() { + let id_clone = id.clone(); + if let Ok(token_info) = get_token_by_id(conn, id_clone).await { + if let Some(asset) = assets_map.get_mut(&id) { + asset.token_info = Some(token_info); + } } } } - let cond = if show_unverified_collections { + let cond = if options.show_unverified_collections { Condition::all() } else { Condition::any() @@ -372,7 +375,7 @@ pub async fn get_assets_by_condition( sort_direction: Order, pagination: &Pagination, limit: u64, - show_unverified_collections: bool, + options: &Options, ) -> Result, DbErr> { let mut stmt = asset::Entity::find(); for def in joins { @@ -389,7 +392,7 @@ pub async fn get_assets_by_condition( .all(conn) .await?; let full_assets = - get_related_for_assets(conn, assets, show_unverified_collections, None).await?; + get_related_for_assets(conn, assets, None,options).await?; Ok(full_assets) } @@ -397,6 +400,7 @@ pub async fn get_by_id( conn: &impl ConnectionTrait, asset_id: Vec, include_no_supply: bool, + options: &Options, ) -> Result { let mut asset_data = asset::Entity::find_by_id(asset_id.clone()).find_also_related(asset_data::Entity); @@ -404,9 +408,10 @@ pub async fn get_by_id( asset_data = asset_data.filter(Condition::all().add(asset::Column::Supply.gt(0))); } - let token_info = match get_token_by_id(conn, asset_id.clone()).await { - Ok(info) => Some(info), - Err(_) => return Err(DbErr::RecordNotFound("Token Not Found".to_string())), + let token_info = if options.show_fungible { + get_token_by_id(conn, asset_id.clone()).await.ok() + } else { + None }; let asset_data: (asset::Model, asset_data::Model) = @@ -576,10 +581,8 @@ fn filter_out_stale_creators(creators: &mut Vec) { pub async fn get_token_by_id( conn: &impl ConnectionTrait, id: Vec, -) -> Result<(tokens::Model, Option), DbErr> { +) -> Result { tokens::Entity::find_by_id(id) - .find_also_related(token_accounts::Entity) - .order_by_asc(tokens::Column::Mint) .one(conn) .await .and_then(|o| match o { diff --git a/digital_asset_types/src/dapi/assets_by_authority.rs b/digital_asset_types/src/dapi/assets_by_authority.rs index 59404f3e0..b52891062 100644 --- a/digital_asset_types/src/dapi/assets_by_authority.rs +++ b/digital_asset_types/src/dapi/assets_by_authority.rs @@ -24,7 +24,7 @@ pub async fn get_assets_by_authority( sort_direction, &pagination, page_options.limit, - options.show_unverified_collections, + options, ) .await?; Ok(build_asset_response( diff --git a/digital_asset_types/src/dapi/assets_by_creator.rs b/digital_asset_types/src/dapi/assets_by_creator.rs index 9ce5de591..3a2b1e53e 100644 --- a/digital_asset_types/src/dapi/assets_by_creator.rs +++ b/digital_asset_types/src/dapi/assets_by_creator.rs @@ -27,7 +27,7 @@ pub async fn get_assets_by_creator( sort_direction, &pagination, page_options.limit, - options.show_unverified_collections, + options, ) .await?; Ok(build_asset_response( diff --git a/digital_asset_types/src/dapi/assets_by_group.rs b/digital_asset_types/src/dapi/assets_by_group.rs index 68784b9f4..36f4ae534 100644 --- a/digital_asset_types/src/dapi/assets_by_group.rs +++ b/digital_asset_types/src/dapi/assets_by_group.rs @@ -27,7 +27,7 @@ pub async fn get_assets_by_group( sort_direction, &pagination, page_options.limit, - options.show_unverified_collections, + options, ) .await?; Ok(build_asset_response( diff --git a/digital_asset_types/src/dapi/common/asset.rs b/digital_asset_types/src/dapi/common/asset.rs index 3c352991e..74c86862a 100644 --- a/digital_asset_types/src/dapi/common/asset.rs +++ b/digital_asset_types/src/dapi/common/asset.rs @@ -379,24 +379,18 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result None, }; - let token_info = if let Some(token_info) = token_info { + let token_info = if options.show_fungible && token_info.is_some() { + let token_info = token_info.unwrap(); Some(TokenInfo { - balance: token_info - .1 - .as_ref() - .map_or(0, |info| info.amount.try_into().unwrap_or(0)), - supply: token_info.0.supply.try_into().unwrap_or(0), - decimals: token_info.0.decimals as u8, + supply: token_info.supply.try_into().unwrap_or(0), + decimals: token_info.decimals as u8, mint_authority: token_info - .0 .mint_authority .map(|s| bs58::encode(s).into_string()), freeze_authority: token_info - .0 .freeze_authority .map(|s| bs58::encode(s).into_string()), - token_program: bs58::encode(token_info.0.token_program).into_string(), - associated_token_address : token_info.1.as_ref().map(|info| bs58::encode(info.pubkey.clone()).into_string()), + token_program: bs58::encode(token_info.token_program).into_string(), }) } else { None diff --git a/digital_asset_types/src/dapi/get_asset.rs b/digital_asset_types/src/dapi/get_asset.rs index 3740562c3..fd7c38027 100644 --- a/digital_asset_types/src/dapi/get_asset.rs +++ b/digital_asset_types/src/dapi/get_asset.rs @@ -11,7 +11,7 @@ pub async fn get_asset( id: Vec, options: &Options, ) -> Result { - let asset = scopes::asset::get_by_id(db, id, false).await?; + let asset = scopes::asset::get_by_id(db, id, false,options).await?; asset_to_rpc(asset, options) } @@ -22,7 +22,7 @@ pub async fn get_assets( options: &Options, ) -> Result, DbErr> { let pagination = Pagination::Page { page: 1 }; - let assets = scopes::asset::get_assets(db, ids, &pagination, limit).await?; + let assets = scopes::asset::get_assets(db, ids, &pagination, limit,options).await?; let asset_list = build_asset_response(assets, limit, &pagination, options); let asset_map = asset_list .items diff --git a/digital_asset_types/src/dapi/search_assets.rs b/digital_asset_types/src/dapi/search_assets.rs index 94e729226..85a0f190d 100644 --- a/digital_asset_types/src/dapi/search_assets.rs +++ b/digital_asset_types/src/dapi/search_assets.rs @@ -14,7 +14,7 @@ pub async fn search_assets( ) -> Result { let pagination = create_pagination(page_options)?; let (sort_direction, sort_column) = create_sorting(sorting); - let (condition, joins) = search_assets_query.conditions(options)?; + let (condition, joins) = search_assets_query.conditions()?; let assets = scopes::asset::get_assets_by_condition( db, condition, @@ -23,7 +23,7 @@ pub async fn search_assets( sort_direction, &pagination, page_options.limit, - options.show_unverified_collections, + options, ) .await?; Ok(build_asset_response( diff --git a/digital_asset_types/src/rpc/asset.rs b/digital_asset_types/src/rpc/asset.rs index 445edf640..18c7bdd3c 100644 --- a/digital_asset_types/src/rpc/asset.rs +++ b/digital_asset_types/src/rpc/asset.rs @@ -368,13 +368,10 @@ pub struct MplCoreInfo { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct TokenInfo { - pub balance: u64, pub supply: u64, pub decimals: u8, pub token_program: String, #[serde(skip_serializing_if = "Option::is_none")] - pub associated_token_address: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub mint_authority: Option, #[serde(skip_serializing_if = "Option::is_none")] pub freeze_authority: Option,