Skip to content

Commit

Permalink
show_zero_balance_filter option added (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhzamAkhtar authored Dec 26, 2024
1 parent e547cd5 commit 4808636
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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,
token_accounts, 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 @@ -560,9 +560,16 @@ pub async fn get_token_accounts(
mint_address: Option<Vec<u8>>,
pagination: &Pagination,
limit: u64,
options: &Options,
) -> Result<Vec<token_accounts::Model>, DbErr> {
let mut condition = Condition::all();

if options.show_zero_balance {
condition = condition.add(token_accounts::Column::Amount.gte(0));
} else {
condition = condition.add(token_accounts::Column::Amount.gt(0));
}

if owner_address.is_none() && mint_address.is_none() {
return Err(DbErr::Custom(
"Either 'owner_address' or 'mint_address' must be provided".to_string(),
Expand Down
1 change: 1 addition & 0 deletions digital_asset_types/src/dapi/get_token_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub async fn get_token_accounts(
mint_address,
&pagination,
page_options.limit,
options,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions digital_asset_types/src/rpc/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ use serde::{Deserialize, Serialize};
pub struct Options {
#[serde(default)]
pub show_unverified_collections: bool,
#[serde(default)]
pub show_zero_balance: bool,
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions integration_tests/tests/integration_tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ mod common;
mod general_scenario_tests;
mod mpl_core_tests;
mod regular_nft_tests;
mod test_show_zero_balance_filter;
mod token_accounts_tests;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: integration_tests/tests/integration_tests/test_show_zero_balance_filter.rs
expression: response
snapshot_kind: text
---
{
"total": 1,
"limit": 1000,
"token_accounts": [
{
"address": "CyqarC6hyNYvb3EDueyeYrnGeAUjCDtMvWrbtdAnA53a",
"mint": "7Y5WQ2e3FummR2DebrqP8caC64QvXzpnhSTNWjNabxWn",
"amount": 1,
"owner": "2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g",
"frozen": true,
"delegate": "D98f1ebFe6kfZTcztLo1iPeKAwogbWHAgXzgSpdRDiu7",
"delegated_amount": 1,
"close_authority": null,
"extensions": null
}
],
"cursor": null,
"errors": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: integration_tests/tests/integration_tests/test_show_zero_balance_filter.rs
expression: response
snapshot_kind: text
---
{
"total": 2,
"limit": 1000,
"token_accounts": [
{
"address": "BE1CkzRjLTXAWcSVCaqzycwXsZ18Yuk3jMDMnPUoHjjS",
"mint": "JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN",
"amount": 0,
"owner": "2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g",
"frozen": false,
"delegate": null,
"delegated_amount": 0,
"close_authority": null,
"extensions": null
},
{
"address": "CyqarC6hyNYvb3EDueyeYrnGeAUjCDtMvWrbtdAnA53a",
"mint": "7Y5WQ2e3FummR2DebrqP8caC64QvXzpnhSTNWjNabxWn",
"amount": 1,
"owner": "2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g",
"frozen": true,
"delegate": "D98f1ebFe6kfZTcztLo1iPeKAwogbWHAgXzgSpdRDiu7",
"delegated_amount": 1,
"close_authority": null,
"extensions": null
}
],
"cursor": null,
"errors": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use function_name::named;

use das_api::api::{self, ApiContract};

use itertools::Itertools;

use serial_test::serial;

use super::common::*;

#[tokio::test]
#[serial]
#[named]
async fn test_show_zero_balance_filter_being_enabled() {
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([
"BE1CkzRjLTXAWcSVCaqzycwXsZ18Yuk3jMDMnPUoHjjS",
"CyqarC6hyNYvb3EDueyeYrnGeAUjCDtMvWrbtdAnA53a",
]);

apply_migrations_and_delete_data(setup.db.clone()).await;
index_seed_events(&setup, seeds.iter().collect_vec()).await;

let request = r#"
{
"ownerAddress":"2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g",
"displayOptions": {
"showZeroBalance": true
}
}
"#;

let request: api::GetTokenAccounts = serde_json::from_str(request).unwrap();
let response = setup.das_api.get_token_accounts(request).await.unwrap();

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

#[tokio::test]
#[serial]
#[named]
async fn test_show_zero_balance_filter_being_disabled() {
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([
"BE1CkzRjLTXAWcSVCaqzycwXsZ18Yuk3jMDMnPUoHjjS",
"CyqarC6hyNYvb3EDueyeYrnGeAUjCDtMvWrbtdAnA53a",
]);

apply_migrations_and_delete_data(setup.db.clone()).await;
index_seed_events(&setup, seeds.iter().collect_vec()).await;

let request = r#"
{
"ownerAddress":"2oerfxddTpK5hWAmCMYB6fr9WvNrjEH54CHCWK8sAq7g",
"displayOptions": {
"showZeroBalance": false
}
}
"#;

let request: api::GetTokenAccounts = serde_json::from_str(request).unwrap();
let response = setup.das_api.get_token_accounts(request).await.unwrap();

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

0 comments on commit 4808636

Please sign in to comment.