Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Changed query_account_balances return type #2455

Merged
merged 1 commit into from
Apr 17, 2023
Merged
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
5 changes: 5 additions & 0 deletions parachains/runtimes/assets/common/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ pub enum FungiblesAccessError {

sp_api::decl_runtime_apis! {
/// The API for querying account's balances from runtime.
#[api_version(2)]
pub trait FungiblesApi<AccountId>
where
AccountId: Codec,
{
/// Returns the list of all [`MultiAsset`] that an `AccountId` has.
#[changed_in(2)]
fn query_account_balances(account: AccountId) -> Result<Vec<MultiAsset>, FungiblesAccessError>;

/// Returns the list of all [`MultiAsset`] that an `AccountId` has.
fn query_account_balances(account: AccountId) -> Result<xcm::VersionedMultiAssets, FungiblesAccessError>;
}
}
4 changes: 2 additions & 2 deletions parachains/runtimes/assets/statemine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ impl_runtime_apis! {
AccountId,
> for Runtime
{
fn query_account_balances(account: AccountId) -> Result<Vec<xcm::latest::MultiAsset>, assets_common::runtime_api::FungiblesAccessError> {
fn query_account_balances(account: AccountId) -> Result<xcm::VersionedMultiAssets, assets_common::runtime_api::FungiblesAccessError> {
use assets_common::fungible_conversion::{convert, convert_balance};
Ok([
// collect pallet_balance
Expand All @@ -976,7 +976,7 @@ impl_runtime_apis! {
.filter(|(_, balance)| balance > &0)
)?,
// collect ... e.g. other tokens
].concat())
].concat().into())
}
}

Expand Down
17 changes: 12 additions & 5 deletions parachains/runtimes/assets/statemine/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,11 @@ fn test_assets_balances_api_works() {
0
);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 0);
assert!(Runtime::query_account_balances(AccountId::from(ALICE)).unwrap().is_empty());
assert!(Runtime::query_account_balances(AccountId::from(ALICE))
.unwrap()
.try_as::<MultiAssets>()
.unwrap()
.is_none());

// Drip some balance
use frame_support::traits::fungible::Mutate;
Expand Down Expand Up @@ -437,24 +441,27 @@ fn test_assets_balances_api_works() {
);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), some_currency);

let result = Runtime::query_account_balances(AccountId::from(ALICE)).unwrap();
let result: MultiAssets = Runtime::query_account_balances(AccountId::from(ALICE))
.unwrap()
.try_into()
.unwrap();
assert_eq!(result.len(), 3);

// check currency
assert!(result.iter().any(|asset| asset.eq(
assert!(result.inner().iter().any(|asset| asset.eq(
&assets_common::fungible_conversion::convert_balance::<KsmLocation, Balance>(
some_currency
)
.unwrap()
)));
// check trusted asset
assert!(result.iter().any(|asset| asset.eq(&(
assert!(result.inner().iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
minimum_asset_balance
)
.into())));
// check foreign asset
assert!(result.iter().any(|asset| asset.eq(&(
assert!(result.inner().iter().any(|asset| asset.eq(&(
Identity::reverse_ref(foreign_asset_id_multilocation).unwrap(),
6 * foreign_asset_minimum_asset_balance
)
Expand Down
4 changes: 2 additions & 2 deletions parachains/runtimes/assets/statemint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ impl_runtime_apis! {
AccountId,
> for Runtime
{
fn query_account_balances(account: AccountId) -> Result<Vec<xcm::latest::MultiAsset>, assets_common::runtime_api::FungiblesAccessError> {
fn query_account_balances(account: AccountId) -> Result<xcm::VersionedMultiAssets, assets_common::runtime_api::FungiblesAccessError> {
use assets_common::fungible_conversion::{convert, convert_balance};
Ok([
// collect pallet_balance
Expand All @@ -874,7 +874,7 @@ impl_runtime_apis! {
.filter(|(_, balance)| balance > &0)
)?,
// collect ... e.g. pallet_assets ForeignAssets
].concat())
].concat().into())
}
}

Expand Down
15 changes: 11 additions & 4 deletions parachains/runtimes/assets/statemint/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ fn test_assets_balances_api_works() {
// check before
assert_eq!(Assets::balance(local_asset_id, AccountId::from(ALICE)), 0);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 0);
assert!(Runtime::query_account_balances(AccountId::from(ALICE)).unwrap().is_empty());
assert!(Runtime::query_account_balances(AccountId::from(ALICE))
.unwrap()
.try_as::<MultiAssets>()
.unwrap()
.is_none());

// Drip some balance
use frame_support::traits::fungible::Mutate;
Expand Down Expand Up @@ -421,18 +425,21 @@ fn test_assets_balances_api_works() {
);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), some_currency);

let result = Runtime::query_account_balances(AccountId::from(ALICE)).unwrap();
let result: MultiAssets = Runtime::query_account_balances(AccountId::from(ALICE))
.unwrap()
.try_into()
.unwrap();
assert_eq!(result.len(), 2);

// check currency
assert!(result.iter().any(|asset| asset.eq(
assert!(result.inner().iter().any(|asset| asset.eq(
&assets_common::fungible_conversion::convert_balance::<DotLocation, Balance>(
some_currency
)
.unwrap()
)));
// check trusted asset
assert!(result.iter().any(|asset| asset.eq(&(
assert!(result.inner().iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
minimum_asset_balance
)
Expand Down
4 changes: 2 additions & 2 deletions parachains/runtimes/assets/westmint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ impl_runtime_apis! {
AccountId,
> for Runtime
{
fn query_account_balances(account: AccountId) -> Result<Vec<xcm::latest::MultiAsset>, assets_common::runtime_api::FungiblesAccessError> {
fn query_account_balances(account: AccountId) -> Result<xcm::VersionedMultiAssets, assets_common::runtime_api::FungiblesAccessError> {
use assets_common::fungible_conversion::{convert, convert_balance};
Ok([
// collect pallet_balance
Expand All @@ -988,7 +988,7 @@ impl_runtime_apis! {
.filter(|(_, balance)| balance > &0)
)?,
// collect ... e.g. other tokens
].concat())
].concat().into())
}
}

Expand Down
17 changes: 12 additions & 5 deletions parachains/runtimes/assets/westmint/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ fn test_assets_balances_api_works() {
0
);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 0);
assert!(Runtime::query_account_balances(AccountId::from(ALICE)).unwrap().is_empty());
assert!(Runtime::query_account_balances(AccountId::from(ALICE))
.unwrap()
.try_as::<MultiAssets>()
.unwrap()
.is_none());

// Drip some balance
use frame_support::traits::fungible::Mutate;
Expand Down Expand Up @@ -442,24 +446,27 @@ fn test_assets_balances_api_works() {
);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), some_currency);

let result = Runtime::query_account_balances(AccountId::from(ALICE)).unwrap();
let result: MultiAssets = Runtime::query_account_balances(AccountId::from(ALICE))
.unwrap()
.try_into()
.unwrap();
assert_eq!(result.len(), 3);

// check currency
assert!(result.iter().any(|asset| asset.eq(
assert!(result.inner().iter().any(|asset| asset.eq(
&assets_common::fungible_conversion::convert_balance::<WestendLocation, Balance>(
some_currency
)
.unwrap()
)));
// check trusted asset
assert!(result.iter().any(|asset| asset.eq(&(
assert!(result.inner().iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
minimum_asset_balance
)
.into())));
// check foreign asset
assert!(result.iter().any(|asset| asset.eq(&(
assert!(result.inner().iter().any(|asset| asset.eq(&(
Identity::reverse_ref(foreign_asset_id_multilocation).unwrap(),
6 * foreign_asset_minimum_asset_balance
)
Expand Down