From 6766ee8a40a4a9850af077d15c99bcfe592163b6 Mon Sep 17 00:00:00 2001 From: AhzamAkhtar Date: Mon, 30 Dec 2024 00:08:06 +0530 Subject: [PATCH] owner field in respose coming empty when searching for fungible-asset using getAssetByOwner bug fixed --- digital_asset_types/src/dao/scopes/asset.rs | 50 +++++++++++-- digital_asset_types/src/dapi/search_assets.rs | 1 + ...RNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx | Bin 0 -> 312 bytes ...ajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 | Bin 0 -> 824 bytes ...YnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs | Bin 0 -> 224 bytes ...RNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx | Bin 0 -> 312 bytes ...ajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 | Bin 0 -> 824 bytes ...YnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs | Bin 0 -> 224 bytes .../show_fungible_flag_tests.rs | 37 ++++++++++ ...with_show_fungible_for_fungible_asset.snap | 69 ++++++++++++++++++ 10 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/6BRNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx create mode 100644 integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/7BajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 create mode 100644 integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs create mode 100644 integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_scenario_2/6BRNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx create mode 100644 integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_scenario_2/7BajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 create mode 100644 integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_scenario_2/7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs create mode 100644 integration_tests/tests/integration_tests/snapshots/integration_tests__show_fungible_flag_tests__get_asset_by_owner_with_show_fungible_for_fungible_asset.snap diff --git a/digital_asset_types/src/dao/scopes/asset.rs b/digital_asset_types/src/dao/scopes/asset.rs index cb7bf0323..1ec170b9e 100644 --- a/digital_asset_types/src/dao/scopes/asset.rs +++ b/digital_asset_types/src/dao/scopes/asset.rs @@ -3,7 +3,7 @@ use crate::{ asset::{self}, asset_authority, asset_creators, asset_data, asset_grouping, cl_audits_v2, extensions::{self, instruction::PascalCase}, - sea_orm_active_enums::Instruction, + sea_orm_active_enums::{Instruction, OwnerType}, token_accounts, tokens, Cursor, FullAsset, GroupingSize, Pagination, }, rpc::{filter::AssetSortDirection, options::Options}, @@ -79,6 +79,7 @@ pub async fn get_by_creator( limit, show_unverified_collections, Some(creator), + false, ) .await } @@ -139,6 +140,7 @@ pub async fn get_by_grouping( limit, show_unverified_collections, None, + false, ) .await } @@ -181,6 +183,7 @@ pub async fn get_assets_by_owner( pagination, limit, options.show_unverified_collections, + true, ) .await } @@ -205,6 +208,7 @@ pub async fn get_assets( pagination, limit, false, + false, ) .await } @@ -231,6 +235,7 @@ pub async fn get_by_authority( limit, show_unverified_collections, None, + false, ) .await } @@ -246,6 +251,7 @@ async fn get_by_related_condition( limit: u64, show_unverified_collections: bool, required_creator: Option>, + show_owner_for_fungible: bool, ) -> Result, DbErr> where E: RelationTrait, @@ -263,7 +269,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, + show_unverified_collections, + required_creator, + show_owner_for_fungible, + ) + .await } pub async fn get_related_for_assets( @@ -271,11 +284,12 @@ pub async fn get_related_for_assets( assets: Vec, show_unverified_collections: bool, required_creator: Option>, + show_owner_for_fungible: bool, ) -> Result, DbErr> { let asset_ids = assets.iter().map(|a| a.id.clone()).collect::>(); let asset_data: Vec = 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| { @@ -320,6 +334,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::Entity::find() + .filter(token_accounts::Column::Mint.is_in(asset_ids)) + .all(conn) + .await?; + + 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); @@ -389,6 +422,7 @@ pub async fn get_assets_by_condition( pagination: &Pagination, limit: u64, show_unverified_collections: bool, + show_owner_for_fungible: bool, ) -> Result, DbErr> { let mut stmt = asset::Entity::find(); for def in joins { @@ -404,8 +438,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) } diff --git a/digital_asset_types/src/dapi/search_assets.rs b/digital_asset_types/src/dapi/search_assets.rs index a7ee65509..5b8aeaccb 100644 --- a/digital_asset_types/src/dapi/search_assets.rs +++ b/digital_asset_types/src/dapi/search_assets.rs @@ -24,6 +24,7 @@ pub async fn search_assets( &pagination, page_options.limit, options.show_unverified_collections, + true, ) .await?; Ok(build_asset_response( diff --git a/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/6BRNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx b/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/6BRNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx new file mode 100644 index 0000000000000000000000000000000000000000..22011a98fa1ae1370202f37fcfb839b954996b5c GIT binary patch literal 312 zcmY#jfB*@G90nE!4+a$=H-NzfNJap00)qsQ^!a+$_0g5xjz_QDue^WuW!dxf{hF%2 zHoA{B?^T}N_F*mC-ER-Cr!Jg)^YlZvbFVAc$ZTnS8y{^x(W>WbYu$f_l^Q@ZKFG=g zNd|`hP$1jUECk{QA6X$hg%QMJU|0&oG4tleFXn9XI$PT)aaqmK=3aH%eTj$DCwVbT zUL H5Q_=`P!3mY literal 0 HcmV?d00001 diff --git a/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/7BajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 b/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/7BajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 new file mode 100644 index 0000000000000000000000000000000000000000..fb1372e331d6f780eee442ccc7d5930746fd15b8 GIT binary patch literal 824 zcmY#jfB*@G90nE!4+a$=H-NzfNJap00)qsQjJ_lMFd&sh{5Dh3C!?)gLerkEciEd6 zxUD_YEPgGMV>EX`>c+G^pegV6yOP$1aT$0rgjaRNwYl#~<{Tj}ekCzhm^CsyiZ7F8DORa92#2UtXynH0E} zmQ+TVrdEa&1m+e-6&ZV&#QPLxX`K4x~+cH3WMn=YejKGi_1+)wS E0B?kFNB{r; literal 0 HcmV?d00001 diff --git a/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs b/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_for_fungible_asset/7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs new file mode 100644 index 0000000000000000000000000000000000000000..bcedc7a60a070ccd26929e37ed76bd5740d32779 GIT binary patch literal 224 zcmY#jfB*@G34$FTURSP>+0yzpKH7YuRnOPfy8jF-L1zB1-nCp4 zL_>j0N3#%+%TRTAh42(c28JLI2MFYMhTprlP=7D W*V*pz%P(A&=bQ0>mlLRm7ytkqH%XHK literal 0 HcmV?d00001 diff --git a/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_scenario_2/6BRNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx b/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_scenario_2/6BRNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx new file mode 100644 index 0000000000000000000000000000000000000000..c1ed0573d43da946bcef0af04d4362ba21ea634d GIT binary patch literal 312 zcmY#jfB*@G90nE!4+a$=H-NzfNJap00)qsQ^!a+$_0g5xjz_QDue^WuW!dxf{hF%2 zHoA{B?^T}N_F*mC-ER-Cr!Jg)^YlZvbFVAc$ZTnS8y{^x(W>WbYu$f_l^Q@ZKFG=g zNd|`hQ1HC1SqQ`ry1Gnw3L}Wcz_1jEW9H3`U(DI&b+)!q;1Di&efLcpf HAr=(?@Do@< literal 0 HcmV?d00001 diff --git a/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_scenario_2/7BajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 b/integration_tests/tests/data/accounts/get_asset_by_owner_with_show_fungible_scenario_2/7BajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6 new file mode 100644 index 0000000000000000000000000000000000000000..5affc4cc0a2379629af9f0ccf636d7a47266add8 GIT binary patch literal 824 zcmY#jfB*@G90nE!4+a$=H-NzfNJap00)qsQjJ_lMFd&sh{5Dh3C!?)gLerkEciEd6 zxUD_YEPgGMV>EX`>c+^a5jYmMLKw4&9U zp0uyC-QyQCZ*Kfz&Ni>JwT%*&)eLR!Rkz)jcsPBM7qdkEzxlRR3P6K`Gjnn(6vDl+ykiT5eY$TjectSB-KuB>u%^GnS}w`G9%jEs!`7=a-<3TPPu E0I4$FTURSP>+0yzpKH7YuRnOPfy8jF-L1zB1-nCp4 zL_@)|wq_w9m!b5^GT|wV3=Baa4iL!i48M18$t#g<$3L$45$5bU#p`R0-{rKT)tjEQ Wue06bmtVLl&o|=%FDFnBF#rH@EJ|en literal 0 HcmV?d00001 diff --git a/integration_tests/tests/integration_tests/show_fungible_flag_tests.rs b/integration_tests/tests/integration_tests/show_fungible_flag_tests.rs index 3a7626106..defacc3c7 100644 --- a/integration_tests/tests/integration_tests/show_fungible_flag_tests.rs +++ b/integration_tests/tests/integration_tests/show_fungible_flag_tests.rs @@ -190,6 +190,43 @@ async fn test_get_asset_by_owner_with_show_fungible() { insta::assert_json_snapshot!(name, response); } +#[tokio::test] +#[serial] +#[named] +async fn test_get_asset_by_owner_with_show_fungible_for_fungible_asset() { + let name = trim_test_name(function_name!()); + let setup = TestSetup::new_with_options( + name.clone(), + TestSetupOptions { + network: Some(Network::Mainnet), + }, + ) + .await; + + let seeds: Vec = seed_accounts([ + "7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs", + "7BajpcYgnxmWK91RhrfsdB3Tm83PcDwPvMC8ZinvtTY6", + "6BRNfDfdq1nKyU1TQiCEQLWyPtD8EwUH9Kt2ahsbidUx", + ]); + + apply_migrations_and_delete_data(setup.db.clone()).await; + index_seed_events(&setup, seeds.iter().collect_vec()).await; + + let request = r#" + { + "ownerAddress": "2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g", + "displayOptions": { + "showFungible": true + } + } + "#; + + let request: api::GetAssetsByOwner = serde_json::from_str(request).unwrap(); + let response = setup.das_api.get_assets_by_owner(request).await.unwrap(); + + insta::assert_json_snapshot!(name, response); +} + #[tokio::test] #[serial] #[named] diff --git a/integration_tests/tests/integration_tests/snapshots/integration_tests__show_fungible_flag_tests__get_asset_by_owner_with_show_fungible_for_fungible_asset.snap b/integration_tests/tests/integration_tests/snapshots/integration_tests__show_fungible_flag_tests__get_asset_by_owner_with_show_fungible_for_fungible_asset.snap new file mode 100644 index 000000000..e982981da --- /dev/null +++ b/integration_tests/tests/integration_tests/snapshots/integration_tests__show_fungible_flag_tests__get_asset_by_owner_with_show_fungible_for_fungible_asset.snap @@ -0,0 +1,69 @@ +--- +source: integration_tests/tests/integration_tests/show_fungible_flag_tests.rs +expression: response +snapshot_kind: text +--- +{ + "total": 1, + "limit": 1000, + "cursor": "7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs", + "items": [ + { + "interface": "FungibleToken", + "id": "7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs", + "content": { + "$schema": "https://schema.metaplex.com/nft1.0.json", + "json_uri": "https://gateway.irys.xyz/P8X64pGutyX5eyTpQmqZr3H4_Lqhm0IYxr5SyzFFNek", + "files": [], + "metadata": { + "name": "Silly Dragon", + "symbol": "SILLY", + "token_standard": "Fungible" + }, + "links": {} + }, + "authorities": [ + { + "address": "38qZKCqcphT5wDrVNJGHYcuenjEtEFPitvrqvMFQkPu7", + "scopes": [ + "full" + ] + } + ], + "compression": { + "eligible": false, + "compressed": false, + "data_hash": "", + "creator_hash": "", + "asset_hash": "", + "tree": "", + "seq": 0, + "leaf_id": 0 + }, + "grouping": [], + "royalty": { + "royalty_model": "creators", + "target": null, + "percent": 0.0, + "basis_points": 0, + "primary_sale_happened": true, + "locked": false + }, + "creators": [], + "ownership": { + "frozen": false, + "delegated": false, + "delegate": null, + "ownership_model": "token", + "owner": "2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g" + }, + "mutable": true, + "burnt": false, + "token_info": { + "supply": 999913799054684527, + "decimals": 9, + "token_program": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" + } + } + ] +}