Skip to content

Commit

Permalink
feat: add showInscription filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Nov 19, 2024
1 parent 4a7d4c5 commit 2e01bac
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 54 deletions.
4 changes: 4 additions & 0 deletions digital_asset_types/src/dao/full_asset.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::dao::{asset, asset_authority, asset_creators, asset_data, asset_grouping};

use super::{asset_v1_account_attachments, tokens};

#[derive(Clone, Debug, PartialEq)]
pub struct FullAsset {
pub asset: asset::Model,
pub data: asset_data::Model,
pub token_info: Option<tokens::Model>,
pub authorities: Vec<asset_authority::Model>,
pub creators: Vec<asset_creators::Model>,
pub groups: Vec<asset_grouping::Model>,
pub inscription: Option<asset_v1_account_attachments::Model>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct AssetRelated {
Expand Down
132 changes: 82 additions & 50 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::{
dao::{
asset::{self},
asset_authority, asset_creators, asset_data, asset_grouping, cl_audits_v2,
extensions::{self, instruction::PascalCase},
sea_orm_active_enums::{Instruction, SpecificationAssetClass},
Cursor, FullAsset, GroupingSize, Pagination,
asset_authority, asset_creators, asset_data, asset_grouping, asset_v1_account_attachments,
cl_audits_v2,
extensions::{self, asset_v1_account_attachment, instruction::PascalCase},
sea_orm_active_enums::Instruction,
tokens, Cursor, FullAsset, GroupingSize, Pagination,
},
rpc::{
filter::AssetSortDirection,
options::{self, Options},
},
rpc::{filter::AssetSortDirection, options::Options},
};
use indexmap::IndexMap;
use sea_orm::{entity::*, query::*, ConnectionTrait, DbErr, Order};
Expand Down Expand Up @@ -60,7 +64,7 @@ pub async fn get_by_creator(
sort_direction: Order,
pagination: &Pagination,
limit: u64,
show_unverified_collections: bool,
options: &Options,
) -> Result<Vec<FullAsset>, DbErr> {
let mut condition = Condition::all()
.add(asset_creators::Column::Creator.eq(creator.clone()))
Expand All @@ -76,7 +80,7 @@ pub async fn get_by_creator(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
Some(creator),
)
.await
Expand Down Expand Up @@ -112,13 +116,13 @@ pub async fn get_by_grouping(
sort_direction: Order,
pagination: &Pagination,
limit: u64,
show_unverified_collections: bool,
options: &Options,
) -> Result<Vec<FullAsset>, 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)
Expand All @@ -136,7 +140,7 @@ pub async fn get_by_grouping(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
None,
)
.await
Expand All @@ -151,19 +155,10 @@ pub async fn get_assets_by_owner(
limit: u64,
options: &Options,
) -> Result<Vec<FullAsset>, DbErr> {
let mut cond = Condition::all()
let cond = Condition::all()
.add(asset::Column::Owner.eq(owner))
.add(asset::Column::Supply.gt(0));

if options.show_fungible {
cond = cond.add(
asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleToken)
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleAsset)),
);
}

get_assets_by_condition(
conn,
cond,
Expand All @@ -172,7 +167,7 @@ pub async fn get_assets_by_owner(
sort_direction,
pagination,
limit,
options.show_unverified_collections,
options,
)
.await
}
Expand All @@ -184,19 +179,10 @@ pub async fn get_assets(
limit: u64,
options: &Options,
) -> Result<Vec<FullAsset>, DbErr> {
let mut cond = Condition::all()
let cond = Condition::all()
.add(asset::Column::Id.is_in(asset_ids))
.add(asset::Column::Supply.gt(0));

if options.show_fungible {
cond = cond.add(
asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleToken)
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleAsset)),
);
}

get_assets_by_condition(
conn,
cond,
Expand All @@ -206,7 +192,7 @@ pub async fn get_assets(
Order::Asc,
pagination,
limit,
false,
options,
)
.await
}
Expand All @@ -218,7 +204,7 @@ pub async fn get_by_authority(
sort_direction: Order,
pagination: &Pagination,
limit: u64,
show_unverified_collections: bool,
options: &Options,
) -> Result<Vec<FullAsset>, DbErr> {
let cond = Condition::all()
.add(asset_authority::Column::Authority.eq(authority))
Expand All @@ -231,7 +217,7 @@ pub async fn get_by_authority(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
None,
)
.await
Expand All @@ -246,7 +232,7 @@ async fn get_by_related_condition<E>(
sort_direction: Order,
pagination: &Pagination,
limit: u64,
show_unverified_collections: bool,
options: &Options,
required_creator: Option<Vec<u8>>,
) -> Result<Vec<FullAsset>, DbErr>
where
Expand All @@ -265,19 +251,19 @@ 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, options, required_creator).await
}

pub async fn get_related_for_assets(
conn: &impl ConnectionTrait,
assets: Vec<asset::Model>,
show_unverified_collections: bool,
options: &Options,
required_creator: Option<Vec<u8>>,
) -> Result<Vec<FullAsset>, DbErr> {
let asset_ids = assets.iter().map(|a| a.id.clone()).collect::<Vec<_>>();

let asset_data: Vec<asset_data::Model> = asset_data::Entity::find()
.filter(asset_data::Column::Id.is_in(asset_ids))
.filter(asset_data::Column::Id.is_in(asset_ids.clone()))
.all(conn)
.await?;
let asset_data_map = asset_data.into_iter().fold(HashMap::new(), |mut acc, ad| {
Expand All @@ -299,6 +285,8 @@ pub async fn get_related_for_assets(
authorities: vec![],
creators: vec![],
groups: vec![],
token_info: None,
inscription: None,
};
acc.insert(id, fa);
};
Expand Down Expand Up @@ -346,7 +334,7 @@ pub async fn get_related_for_assets(
}
}

let cond = if show_unverified_collections {
let cond = if options.show_unverified_collections {
Condition::all()
} else {
Condition::any()
Expand All @@ -363,6 +351,20 @@ pub async fn get_related_for_assets(
.order_by_asc(asset_grouping::Column::AssetId)
.all(conn)
.await?;

if options.show_inscription {
let attachments = asset_v1_account_attachments::Entity::find()
.filter(asset_v1_account_attachments::Column::AssetId.is_in(asset_ids.clone()))
.all(conn)
.await?;

for a in attachments.into_iter() {
if let Some(asset) = assets_map.get_mut(&a.id) {
asset.inscription = Some(a);
}
}
}

for g in grouping.into_iter() {
if let Some(asset) = assets_map.get_mut(&g.asset_id) {
asset.groups.push(g);
Expand All @@ -381,7 +383,7 @@ pub async fn get_assets_by_condition(
sort_direction: Order,
pagination: &Pagination,
limit: u64,
show_unverified_collections: bool,
options: &Options,
) -> Result<Vec<FullAsset>, DbErr> {
let mut stmt = asset::Entity::find();
for def in joins {
Expand All @@ -397,8 +399,7 @@ pub async fn get_assets_by_condition(
let assets = paginate(pagination, limit, stmt, sort_direction, asset::Column::Id)
.all(conn)
.await?;
let full_assets =
get_related_for_assets(conn, assets, show_unverified_collections, None).await?;
let full_assets = get_related_for_assets(conn, assets, options, None).await?;
Ok(full_assets)
}

Expand All @@ -414,14 +415,17 @@ pub async fn get_by_id(
asset_data = asset_data.filter(Condition::all().add(asset::Column::Supply.gt(0)));
}

if options.show_fungible {
asset_data = asset_data.filter(
asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleToken)
.or(asset::Column::SpecificationAssetClass
.eq(SpecificationAssetClass::FungibleAsset)),
);
}
let token_info = if options.show_fungible {
get_token_by_id(conn, asset_id.clone()).await.ok()
} else {
None
};

let inscription = if options.show_inscription {
get_inscription_by_id(conn, asset_id.clone()).await.ok()
} else {
None
};

let asset_data: (asset::Model, asset_data::Model) =
asset_data.one(conn).await.and_then(|o| match o {
Expand Down Expand Up @@ -462,6 +466,8 @@ pub async fn get_by_id(
authorities,
creators,
groups: grouping,
token_info,
inscription,
})
}

Expand Down Expand Up @@ -585,3 +591,29 @@ fn filter_out_stale_creators(creators: &mut Vec<asset_creators::Model>) {
}
}
}

pub async fn get_token_by_id(
conn: &impl ConnectionTrait,
id: Vec<u8>,
) -> Result<tokens::Model, DbErr> {
tokens::Entity::find_by_id(id)
.one(conn)
.await
.and_then(|o| match o {
Some(t) => Ok(t),
_ => Err(DbErr::RecordNotFound("Token Not Found".to_string())),
})
}

pub async fn get_inscription_by_id(
conn: &impl ConnectionTrait,
id: Vec<u8>,
) -> Result<asset_v1_account_attachments::Model, DbErr> {
asset_v1_account_attachments::Entity::find_by_id(id)
.one(conn)
.await
.and_then(|o| match o {
Some(t) => Ok(t),
_ => Err(DbErr::RecordNotFound("Inscription Not Found".to_string())),
})
}
2 changes: 1 addition & 1 deletion digital_asset_types/src/dapi/assets_by_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion digital_asset_types/src/dapi/assets_by_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion digital_asset_types/src/dapi/assets_by_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 2e01bac

Please sign in to comment.