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

When searching for fungible-asset using searchAssets for particular owner_address, empty owner field in the response bug fixed #202

Closed
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
52 changes: 46 additions & 6 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
asset::{self},
asset_authority, asset_creators, asset_data, asset_grouping, cl_audits_v2,
extensions::{self, instruction::PascalCase},
sea_orm_active_enums::Instruction,
Cursor, FullAsset, GroupingSize, Pagination,
sea_orm_active_enums::{Instruction, OwnerType},
token_accounts, Cursor, FullAsset, GroupingSize, Pagination,
},
rpc::filter::AssetSortDirection,
};
Expand Down Expand Up @@ -78,6 +78,7 @@ pub async fn get_by_creator(
limit,
show_unverified_collections,
Some(creator),
false,
)
.await
}
Expand Down Expand Up @@ -138,6 +139,7 @@ pub async fn get_by_grouping(
limit,
show_unverified_collections,
None,
false,
)
.await
}
Expand All @@ -163,6 +165,7 @@ pub async fn get_assets_by_owner(
pagination,
limit,
show_unverified_collections,
true,
)
.await
}
Expand All @@ -186,6 +189,7 @@ pub async fn get_assets(
pagination,
limit,
false,
false,
)
.await
}
Expand All @@ -212,6 +216,7 @@ pub async fn get_by_authority(
limit,
show_unverified_collections,
None,
false,
)
.await
}
Expand All @@ -227,6 +232,7 @@ async fn get_by_related_condition<E>(
limit: u64,
show_unverified_collections: bool,
required_creator: Option<Vec<u8>>,
show_owner_for_fungible: bool,
) -> Result<Vec<FullAsset>, DbErr>
where
E: RelationTrait,
Expand All @@ -244,19 +250,27 @@ 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,
show_unverified_collections,
required_creator,
show_owner_for_fungible,
)
.await
}

pub async fn get_related_for_assets(
conn: &impl ConnectionTrait,
assets: Vec<asset::Model>,
show_unverified_collections: bool,
required_creator: Option<Vec<u8>>,
show_owner_for_fungible: bool,
) -> 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 Down Expand Up @@ -300,6 +314,25 @@ pub async fn get_related_for_assets(
}
}

// When using getAssetByOwner or searchAssets with an owner_address to retrieve fungible assets, the 'owner' field was previously returned as empty.
// Instead of an empty value, we should now display the actual owner of the token account associated with the asset.

if show_owner_for_fungible {
let token_account_data: Vec<token_accounts::Model> = token_accounts::Entity::find()
.filter(token_accounts::Column::Mint.is_in(asset_ids))
.all(conn)
.await?;
Comment on lines +321 to +324
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mints don't have owners they have mint authority so owner doesn't make sense on fungible assets. I think we should just keep this as null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this idea was inspired from helius, as in place of owner they also show the owner of the token-account for that corresponding fungible-asset which is being searched using searchAsset or getAssetByOwner for a given owner_address.
But happy to close this one.


for t in token_account_data.into_iter() {
if let Some(asset) = assets_map.get_mut(&t.mint) {
if asset.asset.owner.clone().is_none() && asset.asset.owner_type == OwnerType::Token
{
asset.asset.owner = Some(t.owner);
}
}
}
}

// Filter out stale creators from each asset.
for (_id, asset) in assets_map.iter_mut() {
filter_out_stale_creators(&mut asset.creators);
Expand Down Expand Up @@ -361,6 +394,7 @@ pub async fn get_assets_by_condition(
pagination: &Pagination,
limit: u64,
show_unverified_collections: bool,
show_owner_for_fungible: bool,
) -> Result<Vec<FullAsset>, DbErr> {
let mut stmt = asset::Entity::find();
for def in joins {
Expand All @@ -376,8 +410,14 @@ 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,
show_unverified_collections,
None,
show_owner_for_fungible,
)
.await?;
Ok(full_assets)
}

Expand Down
1 change: 1 addition & 0 deletions digital_asset_types/src/dapi/search_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub async fn search_assets(
&pagination,
page_options.limit,
options.show_unverified_collections,
true,
)
.await?;
Ok(build_asset_response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ snapshot_kind: text
"delegated": false,
"delegate": null,
"ownership_model": "token",
"owner": ""
"owner": "2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g"
},
"mutable": true,
"burnt": false
Expand Down