Skip to content

Commit

Permalink
GetAssetByOwner skipping fungible-assets when using show_fungible opt…
Browse files Browse the repository at this point in the history
…ion bug fixed (#197)

* fungible tokens skipped when using showFungible option bug fixed in getAssetByOwner

* test-case and test-snapshot added

* cleanup
  • Loading branch information
AhzamAkhtar authored and kespinola committed Jan 6, 2025
1 parent 18f446b commit fe3d4f8
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 3 deletions.
12 changes: 12 additions & 0 deletions digital_asset_types/src/dao/extensions/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::dao::{
asset, asset_authority, asset_creators, asset_data, asset_grouping,
asset_v1_account_attachments,
sea_orm_active_enums::{OwnerType, RoyaltyTargetType},
token_accounts,
};

#[derive(Copy, Clone, Debug, EnumIter)]
Expand All @@ -13,6 +14,7 @@ pub enum Relation {
AssetAuthority,
AssetCreators,
AssetGrouping,
TokenAccounts,
}

impl RelationTrait for Relation {
Expand All @@ -22,6 +24,10 @@ impl RelationTrait for Relation {
.from(asset::Column::AssetData)
.to(asset_data::Column::Id)
.into(),
Self::TokenAccounts => asset::Entity::belongs_to(token_accounts::Entity)
.from(asset::Column::Id)
.to(token_accounts::Column::Mint)
.into(),
Self::AssetV1AccountAttachments => {
asset::Entity::has_many(asset_v1_account_attachments::Entity).into()
}
Expand Down Expand Up @@ -62,6 +68,12 @@ impl Related<asset_grouping::Entity> for asset::Entity {
}
}

impl Related<token_accounts::Entity> for asset::Entity {
fn to() -> RelationDef {
Relation::TokenAccounts.def()
}
}

impl Default for RoyaltyTargetType {
fn default() -> Self {
Self::Creators
Expand Down
1 change: 1 addition & 0 deletions digital_asset_types/src/dao/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod asset_data;
pub mod asset_grouping;
pub mod asset_v1_account_attachment;
pub mod instruction;
pub mod token_accounts;
25 changes: 25 additions & 0 deletions digital_asset_types/src/dao/extensions/token_accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use sea_orm::{EntityTrait, EnumIter, Related, RelationDef, RelationTrait};

use crate::dao::{asset, token_accounts};

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

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Asset => token_accounts::Entity::belongs_to(asset::Entity)
.from(token_accounts::Column::Mint)
.to(asset::Column::Id)
.into(),
}
}
}

impl Related<asset::Entity> for token_accounts::Entity {
fn to() -> RelationDef {
Relation::Asset.def()
}
}
23 changes: 20 additions & 3 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
rpc::{filter::AssetSortDirection, options::Options},
};
use indexmap::IndexMap;
use sea_orm::sea_query::{Expr, IntoCondition};
use sea_orm::{entity::*, query::*, ConnectionTrait, DbErr, Order};
use std::collections::HashMap;

Expand Down Expand Up @@ -151,14 +152,30 @@ pub async fn get_assets_by_owner(
limit: u64,
options: &Options,
) -> Result<Vec<FullAsset>, DbErr> {
let cond = Condition::all()
.add(asset::Column::Owner.eq(owner))
let mut joins = Vec::new();
let mut cond = Condition::all()
.add(asset::Column::Owner.eq(owner.clone()))
.add(asset::Column::Supply.gt(0));

if options.show_fungible {
cond = Condition::all().add(token_accounts::Column::Owner.eq(owner.clone()));

let rel = extensions::token_accounts::Relation::Asset
.def()
.rev()
.on_condition(|left, right| {
Expr::tbl(right, token_accounts::Column::Mint)
.eq(Expr::tbl(left, asset::Column::Id))
.into_condition()
});

joins.push(rel);
}

get_assets_by_condition(
conn,
cond,
vec![],
joins,
sort_by,
sort_direction,
pagination,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,40 @@ async fn test_get_asset_with_show_fungible_scenario_2() {

insta::assert_json_snapshot!(name, response);
}

#[tokio::test]
#[serial]
#[named]
async fn test_get_asset_by_owner_with_show_fungible_scenario() {
let name = trim_test_name(function_name!());
let setup = TestSetup::new_with_options(
name.clone(),
TestSetupOptions {
network: Some(Network::Mainnet),
},
)
.await;

let seeds: Vec<SeedEvent> = 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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
source: integration_tests/tests/integration_tests/show_fungible_flag_tests.rs
expression: response
snapshot_kind: text
---
{
"total": 1,
"limit": 1000,
"cursor": "7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs",
"items": [
{
"interface": "Custom",
"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": ""
},
"supply": null,
"mutable": true,
"burnt": false
}
]
}

0 comments on commit fe3d4f8

Please sign in to comment.