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

Add showCollectionMetadata option to view collection_metadata in rpc responce #217

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion digital_asset_types/src/dao/extensions/asset_grouping.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use sea_orm::{EntityTrait, EnumIter, Related, RelationDef, RelationTrait};

use crate::dao::{asset, asset_authority, asset_grouping};
use crate::dao::{asset, asset_authority, asset_data, asset_grouping};

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Asset,
AssetAuthority,
AssetData,
}

impl RelationTrait for Relation {
Expand All @@ -19,6 +20,10 @@ impl RelationTrait for Relation {
.from(asset_grouping::Column::AssetId)
.to(asset_authority::Column::Id)
.into(),
Self::AssetData => asset_grouping::Entity::belongs_to(asset_data::Entity)
.from(asset_grouping::Column::AssetId)
.to(asset_data::Column::Id)
.into(),
}
}
}
Expand All @@ -34,3 +39,9 @@ impl Related<asset_authority::Entity> for asset_grouping::Entity {
Relation::AssetAuthority.def()
}
}

impl Related<asset_data::Entity> for asset_grouping::Entity {
fn to() -> RelationDef {
Relation::AssetData.def()
}
}
12 changes: 11 additions & 1 deletion digital_asset_types/src/dao/full_asset.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use crate::dao::{asset, asset_authority, asset_creators, asset_data, asset_grouping};

pub struct FullAssetGroup {
pub id: i64,
pub asset_id: Vec<u8>,
pub group_key: String,
pub group_value: Option<String>,
pub seq: Option<i64>,
pub slot_updated: Option<i64>,
pub verified: bool,
pub group_info_seq: Option<i64>,
}
#[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 groups: Vec<(asset_grouping::Model, Option<asset_data::Model>)>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct AssetRelated {
Expand Down
88 changes: 57 additions & 31 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
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 +60,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 +76,7 @@ pub async fn get_by_creator(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
Some(creator),
)
.await
Expand Down Expand Up @@ -112,13 +112,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 +136,7 @@ pub async fn get_by_grouping(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
None,
)
.await
Expand All @@ -149,7 +149,7 @@ 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))
Expand All @@ -162,7 +162,7 @@ pub async fn get_assets_by_owner(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
)
.await
}
Expand All @@ -172,6 +172,7 @@ 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))
Expand All @@ -185,7 +186,7 @@ pub async fn get_assets(
Order::Asc,
pagination,
limit,
false,
options,
)
.await
}
Expand All @@ -197,7 +198,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 +211,7 @@ pub async fn get_by_authority(
sort_direction,
pagination,
limit,
show_unverified_collections,
options,
None,
)
.await
Expand All @@ -225,7 +226,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,13 +245,13 @@ 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<_>>();
Expand Down Expand Up @@ -325,7 +326,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 @@ -335,18 +336,30 @@ pub async fn get_related_for_assets(
.add(asset_grouping::Column::Verified.is_null())
};

let grouping = asset_grouping::Entity::find()
let grouping_base_query = asset_grouping::Entity::find()
.filter(asset_grouping::Column::AssetId.is_in(ids.clone()))
.filter(asset_grouping::Column::GroupValue.is_not_null())
.filter(cond)
.order_by_asc(asset_grouping::Column::AssetId)
.all(conn)
.await?;
for g in grouping.into_iter() {
if let Some(asset) = assets_map.get_mut(&g.asset_id) {
asset.groups.push(g);
.order_by_asc(asset_grouping::Column::AssetId);

if options.show_collection_metadata {
let combined_group_query = grouping_base_query
.find_also_related(asset_data::Entity)
.all(conn)
.await?;
for (g, a) in combined_group_query.into_iter() {
if let Some(asset) = assets_map.get_mut(&g.asset_id) {
asset.groups.push((g, a));
}
}
}
} else {
let single_group_query = grouping_base_query.all(conn).await?;
for g in single_group_query.into_iter() {
if let Some(asset) = assets_map.get_mut(&g.asset_id) {
asset.groups.push((g, None));
}
}
};

Ok(assets_map.into_iter().map(|(_, v)| v).collect())
}
Expand All @@ -360,7 +373,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,15 +389,15 @@ 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);
Expand All @@ -411,7 +424,7 @@ pub async fn get_by_id(

filter_out_stale_creators(&mut creators);

let grouping: Vec<asset_grouping::Model> = asset_grouping::Entity::find()
let grouping_query = asset_grouping::Entity::find()
.filter(asset_grouping::Column::AssetId.eq(asset.id.clone()))
.filter(asset_grouping::Column::GroupValue.is_not_null())
.filter(
Expand All @@ -421,15 +434,28 @@ pub async fn get_by_id(
// Therefore if verified is null, we can assume that the group is verified.
.add(asset_grouping::Column::Verified.is_null()),
)
.order_by_asc(asset_grouping::Column::AssetId)
.all(conn)
.await?;
.order_by_asc(asset_grouping::Column::AssetId);

let groups = if options.show_collection_metadata {
grouping_query
.find_also_related(asset_data::Entity)
.all(conn)
.await?
} else {
grouping_query
.all(conn)
.await?
.into_iter()
.map(|g| (g, None))
.collect::<Vec<_>>()
};

Ok(FullAsset {
asset,
data,
authorities,
creators,
groups: grouping,
groups,
})
}

Expand Down
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
39 changes: 32 additions & 7 deletions digital_asset_types/src/dapi/common/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,25 +310,50 @@ pub fn to_creators(creators: Vec<asset_creators::Model>) -> Vec<Creator> {
}

pub fn to_grouping(
groups: Vec<asset_grouping::Model>,
groups: Vec<(asset_grouping::Model, Option<asset_data::Model>)>,
options: &Options,
) -> Result<Vec<Group>, DbErr> {
let result: Vec<Group> = groups
.iter()
.filter_map(|model| {
.filter_map(|(asset_group, asset_data)| {
let verified = match options.show_unverified_collections {
// Null verified indicates legacy data, meaning it is verified.
true => Some(model.verified),
true => Some(asset_group.verified),
false => None,
};
// Filter out items where group_value is None.
model.group_value.clone().map(|group_value| Group {
group_key: model.group_key.clone(),
group_value: Some(group_value),
verified,
asset_group.group_value.clone().map(|group_value| {
let collection_metadata = asset_data.as_ref().map(|data| {
let mut metadata_selector_fn = jsonpath_lib::selector(&data.metadata);
let metadata_selector = &mut metadata_selector_fn;
let mut meta: MetadataMap = MetadataMap::new();

if let Some(name) = safe_select(metadata_selector, "$.name") {
meta.set_item("name", name.clone());
}
if let Some(symbol) = safe_select(metadata_selector, "$.symbol") {
meta.set_item("symbol", symbol.clone());
}
if let Some(image) = safe_select(metadata_selector, "$.image") {
meta.set_item("image", image.clone());
}
if let Some(external_url) = safe_select(metadata_selector, "$.external_url") {
meta.set_item("external_url", external_url.clone());
}

meta
});

Group {
group_key: asset_group.group_key.clone(),
group_value: Some(group_value),
verified,
collection_metadata,
}
})
})
.collect();

Ok(result)
}

Expand Down
Loading