Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Nov 19, 2024
1 parent 634c52c commit 53c1cb3
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 29 deletions.
3 changes: 3 additions & 0 deletions digital_asset_types/src/dao/full_asset.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::dao::{asset, asset_authority, asset_creators, asset_data, asset_grouping};

use super::asset_v1_account_attachments;

#[derive(Clone, Debug, PartialEq)]
pub struct FullAsset {
pub asset: asset::Model,
pub data: asset_data::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
80 changes: 60 additions & 20 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
dao::{
asset::{self},
asset_authority, asset_creators, asset_data, asset_grouping, cl_audits_v2,
asset_authority, asset_creators, asset_data, asset_grouping, asset_v1_account_attachments,
cl_audits_v2,
extensions::{self, instruction::PascalCase},
sea_orm_active_enums::Instruction,
Cursor, FullAsset, GroupingSize, Pagination,
},
rpc::filter::AssetSortDirection,
rpc::{filter::AssetSortDirection, options::Options},
};
use indexmap::IndexMap;
use sea_orm::{entity::*, query::*, ConnectionTrait, DbErr, Order};
Expand Down Expand Up @@ -60,7 +61,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 +77,7 @@ pub async fn get_by_creator(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
Some(creator),
)
.await
Expand Down Expand Up @@ -112,13 +113,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 +137,7 @@ pub async fn get_by_grouping(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
None,
)
.await
Expand All @@ -149,11 +150,12 @@ pub async fn get_assets_by_owner(
sort_direction: Order,
pagination: &Pagination,
limit: u64,
show_unverified_collections: bool,
options: &Options,
) -> Result<Vec<FullAsset>, DbErr> {
let cond = Condition::all()
.add(asset::Column::Owner.eq(owner))
.add(asset::Column::Supply.gt(0));

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

get_assets_by_condition(
conn,
cond,
Expand All @@ -185,7 +189,7 @@ pub async fn get_assets(
Order::Asc,
pagination,
limit,
false,
options,
)
.await
}
Expand All @@ -197,7 +201,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 @@ -210,7 +214,7 @@ pub async fn get_by_authority(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
None,
)
.await
Expand All @@ -225,7 +229,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 @@ -244,19 +248,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 @@ -278,6 +282,7 @@ pub async fn get_related_for_assets(
authorities: vec![],
creators: vec![],
groups: vec![],
inscription: None,
};
acc.insert(id, fa);
};
Expand Down Expand Up @@ -325,7 +330,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 @@ -342,6 +347,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 @@ -360,7 +379,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 @@ -376,21 +395,28 @@ 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)
}

pub async fn get_by_id(
conn: &impl ConnectionTrait,
asset_id: Vec<u8>,
include_no_supply: bool,
options: &Options,
) -> Result<FullAsset, DbErr> {
let mut asset_data =
asset::Entity::find_by_id(asset_id.clone()).find_also_related(asset_data::Entity);
if !include_no_supply {
asset_data = asset_data.filter(Condition::all().add(asset::Column::Supply.gt(0)));
}

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 {
Some((a, Some(d))) => Ok((a, d)),
Expand Down Expand Up @@ -430,6 +456,7 @@ pub async fn get_by_id(
authorities,
creators,
groups: grouping,
inscription,
})
}

Expand Down Expand Up @@ -553,3 +580,16 @@ fn filter_out_stale_creators(creators: &mut Vec<asset_creators::Model>) {
}
}
}

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
2 changes: 1 addition & 1 deletion digital_asset_types/src/dapi/assets_by_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn get_assets_by_owner(
sort_direction,
&pagination,
page_options.limit,
options.show_unverified_collections,
options,
)
.await?;
Ok(build_asset_response(
Expand Down
24 changes: 24 additions & 0 deletions digital_asset_types/src/dapi/common/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use crate::rpc::filter::{AssetSortBy, AssetSortDirection, AssetSorting};
use crate::rpc::options::Options;
use crate::rpc::response::TransactionSignatureList;
use crate::rpc::response::{AssetError, AssetList};
use crate::rpc::TokenInscriptionInfo;
use crate::rpc::{
Asset as RpcAsset, Authority, Compression, Content, Creator, File, Group, Interface,
MetadataMap, MplCoreInfo, Ownership, Royalty, Scope, Supply, Uses,
};
use blockbuster::programs::token_inscriptions::InscriptionData;
use jsonpath_lib::JsonPathError;
use log::warn;
use mime_guess::Mime;
Expand Down Expand Up @@ -355,6 +357,7 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
authorities,
creators,
groups,
inscription,
} = asset;
let rpc_authorities = to_authority(authorities);
let rpc_creators = to_creators(creators);
Expand All @@ -377,6 +380,26 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
_ => None,
};

let inscription = if options.show_inscription && inscription.is_some() {
let inscription = inscription.unwrap();

inscription.data.map(|d| {
let deserialized_data: InscriptionData = serde_json::from_value(d).unwrap();
TokenInscriptionInfo {
authority: deserialized_data.authority,
root: deserialized_data.root,
content: deserialized_data.content,
encoding: deserialized_data.encoding,
inscription_data: deserialized_data.inscription_data,
order: deserialized_data.order,
size: deserialized_data.size,
validation_hash: deserialized_data.validation_hash,
}
})
} else {
None
};

Ok(RpcAsset {
interface: interface.clone(),
id: bs58::encode(asset.id).into_string(),
Expand Down Expand Up @@ -444,6 +467,7 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
remaining: u.get("remaining").and_then(|t| t.as_u64()).unwrap_or(0),
}),
burnt: asset.burnt,
inscription,
plugins: asset.mpl_core_plugins,
unknown_plugins: asset.mpl_core_unknown_plugins,
mpl_core_info,
Expand Down
4 changes: 2 additions & 2 deletions digital_asset_types/src/dapi/get_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub async fn get_asset(
id: Vec<u8>,
options: &Options,
) -> Result<Asset, DbErr> {
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)
}

Expand All @@ -22,7 +22,7 @@ pub async fn get_assets(
options: &Options,
) -> Result<HashMap<String, Asset>, 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
Expand Down
2 changes: 1 addition & 1 deletion digital_asset_types/src/dapi/search_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 53c1cb3

Please sign in to comment.