Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Finished asset runtimes
  • Loading branch information
bkontur committed Feb 23, 2023
commit 33218cf434d405eb923d482b9481fbbbc3fa1b15
24 changes: 19 additions & 5 deletions parachains/runtimes/assets/statemine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use parachains_common::{
Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, HOURS, MAXIMUM_BLOCK_WEIGHT,
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
use xcm_config::{AssetIdForTrustBackedAssetsConvert, KsmLocation, XcmConfig};
use xcm_config::{KsmLocation, TrustBackedAssetsConvertedConcreteId, XcmConfig};

#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
Expand Down Expand Up @@ -834,13 +834,27 @@ impl_runtime_apis! {
impl assets_common::assets_api::AssetsApi<
Block,
AccountId,
Balance,
xcm::latest::MultiLocation,
> for Runtime
{
fn account_balances(account: AccountId) -> Result<Vec<(xcm::latest::MultiLocation, Balance)>, assets_common::assets_api::AssetsAccessError> {
fn query_account_balances(account: AccountId) -> Result<Vec<xcm::latest::MultiAsset>, assets_common::assets_api::AssetsAccessError> {
use assets_common::assets_api::{convert, convert_balance};
Ok([
assets_common::convert_asset_id::<_, _, AssetIdForTrustBackedAssetsConvert>(Assets::account_balances(account))?,
// collect pallet_balance
{
let balance = Balances::free_balance(account.clone());
if balance > 0 {
vec![convert_balance::<KsmLocation, Balance>(balance)?]
} else {
vec![]
}
},
// collect pallet_assets (TrustBackedAssets)
convert::<_, _, _, _, TrustBackedAssetsConvertedConcreteId>(
Assets::account_balances(account)
.iter()
.filter(|(_, balance)| balance > &0)
)?,
// collect ... e.g. pallet_assets ForeignAssets
].concat())
}
}
Expand Down
37 changes: 12 additions & 25 deletions parachains/runtimes/assets/statemine/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// limitations under the License.

use super::{
AccountId, AllPalletsWithSystem, AssetIdForTrustBackedAssets, Assets, Authorship, Balance,
Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent,
RuntimeOrigin, TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
};
use frame_support::{
match_types, parameter_types,
Expand All @@ -34,16 +34,13 @@ use sp_runtime::traits::ConvertInto;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, ConvertedConcreteId, CurrencyAdapter,
EnsureXcmOrigin, FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, ParentAsSuperuser,
ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin,
FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, ParentAsSuperuser, ParentIsPreset,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
UsingComponents, WeightInfoBounds, WithComputedOrigin,
};
use xcm_executor::{
traits::{JustTry, WithOriginFilter},
XcmExecutor,
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};

parameter_types! {
pub const KsmLocation: MultiLocation = MultiLocation::parent();
Expand Down Expand Up @@ -83,21 +80,16 @@ pub type CurrencyTransactor = CurrencyAdapter<
(),
>;

/// `AssetId` converter for [`AssetIdForTrustBackedAssets`]
pub type AssetIdForTrustBackedAssetsConvert =
assets_common::AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>;
/// `AssetId/Balancer` converter for [`TrustBackedAssets`]
pub type TrustBackedAssetsConvertedConcreteId =
assets_common::TrustBackedAssetsConvertedConcreteId<TrustBackedAssetsPalletLocation, Balance>;

/// Means for transacting assets besides the native currency on this chain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation:
Assets,
// Use this currency when it is a fungible asset matching the given location or name:
ConvertedConcreteId<
AssetIdForTrustBackedAssets,
Balance,
AssetIdForTrustBackedAssetsConvert,
JustTry,
>,
TrustBackedAssetsConvertedConcreteId,
// Convert an XCM MultiLocation into a local account id:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
Expand Down Expand Up @@ -301,12 +293,7 @@ impl xcm_executor::Config for XcmConfig {
cumulus_primitives_utility::TakeFirstAssetTrader<
AccountId,
AssetFeeAsExistentialDepositMultiplierFeeCharger,
ConvertedConcreteId<
AssetIdForTrustBackedAssets,
Balance,
AssetIdForTrustBackedAssetsConvert,
JustTry,
>,
TrustBackedAssetsConvertedConcreteId,
Assets,
cumulus_primitives_utility::XcmFeesTo32ByteAccount<
FungiblesTransactor,
Expand Down
31 changes: 25 additions & 6 deletions parachains/runtimes/assets/statemine/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use frame_support::{
assert_noop, assert_ok,
weights::{Weight, WeightToFee as WeightToFeeT},
};
use parachains_common::{AccountId, AuraId};
use parachains_common::{AccountId, AuraId, Balance};
use statemine_runtime::xcm_config::{
AssetFeeAsExistentialDepositMultiplierFeeCharger, AssetIdForTrustBackedAssetsConvert,
AssetFeeAsExistentialDepositMultiplierFeeCharger, KsmLocation, TrustBackedAssetsPalletLocation,
};
pub use statemine_runtime::{
constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit,
Expand All @@ -17,6 +17,9 @@ use xcm_executor::traits::{Convert, WeightTrader};

pub const ALICE: [u8; 32] = [1u8; 32];

type AssetIdForTrustBackedAssetsConvert =
assets_common::AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>;

#[test]
fn test_asset_xcm_trader() {
ExtBuilder::<Runtime>::default()
Expand Down Expand Up @@ -366,7 +369,13 @@ fn test_assets_balances_api_works() {

// check before
assert_eq!(Assets::balance(local_asset_id, AccountId::from(ALICE)), 0);
assert!(Runtime::account_balances(AccountId::from(ALICE)).unwrap().is_empty());
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 0);
assert!(Runtime::query_account_balances(AccountId::from(ALICE)).unwrap().is_empty());

// Drip some balance
use frame_support::traits::fungible::Mutate;
let some_currency = ExistentialDeposit::get();
Balances::mint_into(&AccountId::from(ALICE), some_currency).unwrap();

// We need root origin to create a sufficient asset
let minimum_asset_balance = 3333333_u128;
Expand All @@ -391,11 +400,21 @@ fn test_assets_balances_api_works() {
Assets::balance(local_asset_id, AccountId::from(ALICE)),
minimum_asset_balance
);
let result = Runtime::account_balances(AccountId::from(ALICE)).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), some_currency);

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

// check currency
assert!(result.iter().any(|asset| asset.eq(
&assets_common::assets_api::convert_balance::<KsmLocation, Balance>(some_currency)
.unwrap()
)));
// check trusted asset
assert!(result.iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
minimum_asset_balance
))));
)
.into())));
});
}
24 changes: 19 additions & 5 deletions parachains/runtimes/assets/statemint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use parachains_common::{
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
use xcm_config::{
AssetIdForTrustBackedAssetsConvert, DotLocation, XcmConfig, XcmOriginToTransactDispatchOrigin,
DotLocation, TrustBackedAssetsConvertedConcreteId, XcmConfig, XcmOriginToTransactDispatchOrigin,
};

#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -833,13 +833,27 @@ impl_runtime_apis! {
impl assets_common::assets_api::AssetsApi<
Block,
AccountId,
Balance,
xcm::latest::MultiLocation,
> for Runtime
{
fn account_balances(account: AccountId) -> Result<Vec<(xcm::latest::MultiLocation, Balance)>, assets_common::assets_api::AssetsAccessError> {
fn query_account_balances(account: AccountId) -> Result<Vec<xcm::latest::MultiAsset>, assets_common::assets_api::AssetsAccessError> {
use assets_common::assets_api::{convert, convert_balance};
Ok([
assets_common::convert_asset_id::<_, _, AssetIdForTrustBackedAssetsConvert>(Assets::account_balances(account))?,
// collect pallet_balance
{
let balance = Balances::free_balance(account.clone());
if balance > 0 {
vec![convert_balance::<DotLocation, Balance>(balance)?]
} else {
vec![]
}
},
// collect pallet_assets (TrustBackedAssets)
convert::<_, _, _, _, TrustBackedAssetsConvertedConcreteId>(
Assets::account_balances(account)
.iter()
.filter(|(_, balance)| balance > &0)
)?,
// collect ... e.g. pallet_assets ForeignAssets
].concat())
}
}
Expand Down
37 changes: 12 additions & 25 deletions parachains/runtimes/assets/statemint/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// limitations under the License.

use super::{
AccountId, AllPalletsWithSystem, AssetIdForTrustBackedAssets, Assets, Authorship, Balance,
Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent,
RuntimeOrigin, TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
};
use frame_support::{
match_types, parameter_types,
Expand All @@ -34,16 +34,13 @@ use sp_runtime::traits::ConvertInto;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, ConvertedConcreteId, CurrencyAdapter,
EnsureXcmOrigin, FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, ParentAsSuperuser,
ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin,
FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, ParentAsSuperuser, ParentIsPreset,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
UsingComponents, WeightInfoBounds, WithComputedOrigin,
};
use xcm_executor::{
traits::{JustTry, WithOriginFilter},
XcmExecutor,
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};

parameter_types! {
pub const DotLocation: MultiLocation = MultiLocation::parent();
Expand Down Expand Up @@ -83,21 +80,16 @@ pub type CurrencyTransactor = CurrencyAdapter<
(),
>;

/// `AssetId` converter for [`AssetIdForTrustBackedAssets`]
pub type AssetIdForTrustBackedAssetsConvert =
assets_common::AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>;
/// `AssetId/Balancer` converter for [`TrustBackedAssets`]
pub type TrustBackedAssetsConvertedConcreteId =
assets_common::TrustBackedAssetsConvertedConcreteId<TrustBackedAssetsPalletLocation, Balance>;

/// Means for transacting assets besides the native currency on this chain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation:
Assets,
// Use this currency when it is a fungible asset matching the given location or name:
ConvertedConcreteId<
AssetIdForTrustBackedAssets,
Balance,
AssetIdForTrustBackedAssetsConvert,
JustTry,
>,
TrustBackedAssetsConvertedConcreteId,
// Convert an XCM MultiLocation into a local account id:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
Expand Down Expand Up @@ -301,12 +293,7 @@ impl xcm_executor::Config for XcmConfig {
cumulus_primitives_utility::TakeFirstAssetTrader<
AccountId,
AssetFeeAsExistentialDepositMultiplierFeeCharger,
ConvertedConcreteId<
AssetIdForTrustBackedAssets,
Balance,
AssetIdForTrustBackedAssetsConvert,
JustTry,
>,
TrustBackedAssetsConvertedConcreteId,
Assets,
cumulus_primitives_utility::XcmFeesTo32ByteAccount<
FungiblesTransactor,
Expand Down
31 changes: 25 additions & 6 deletions parachains/runtimes/assets/statemint/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use frame_support::{
assert_noop, assert_ok,
weights::{Weight, WeightToFee as WeightToFeeT},
};
use parachains_common::{AccountId, StatemintAuraId as AuraId};
use parachains_common::{AccountId, Balance, StatemintAuraId as AuraId};
use statemint_runtime::xcm_config::{
AssetFeeAsExistentialDepositMultiplierFeeCharger, AssetIdForTrustBackedAssetsConvert,
AssetFeeAsExistentialDepositMultiplierFeeCharger, DotLocation, TrustBackedAssetsPalletLocation,
};
pub use statemint_runtime::{
constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit,
Expand All @@ -17,6 +17,9 @@ use xcm_executor::traits::{Convert, WeightTrader};

pub const ALICE: [u8; 32] = [1u8; 32];

type AssetIdForTrustBackedAssetsConvert =
assets_common::AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>;

#[test]
fn test_asset_xcm_trader() {
ExtBuilder::<Runtime>::default()
Expand Down Expand Up @@ -378,7 +381,13 @@ fn test_assets_balances_api_works() {

// check before
assert_eq!(Assets::balance(local_asset_id, AccountId::from(ALICE)), 0);
assert!(Runtime::account_balances(AccountId::from(ALICE)).unwrap().is_empty());
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 0);
assert!(Runtime::query_account_balances(AccountId::from(ALICE)).unwrap().is_empty());

// Drip some balance
use frame_support::traits::fungible::Mutate;
let some_currency = ExistentialDeposit::get();
Balances::mint_into(&AccountId::from(ALICE), some_currency).unwrap();

// We need root origin to create a sufficient asset
let minimum_asset_balance = 3333333_u128;
Expand All @@ -403,11 +412,21 @@ fn test_assets_balances_api_works() {
Assets::balance(local_asset_id, AccountId::from(ALICE)),
minimum_asset_balance
);
let result = Runtime::account_balances(AccountId::from(ALICE)).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), some_currency);

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

// check currency
assert!(result.iter().any(|asset| asset.eq(
&assets_common::assets_api::convert_balance::<DotLocation, Balance>(some_currency)
.unwrap()
)));
// check trusted asset
assert!(result.iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
minimum_asset_balance
))));
)
.into())));
});
}
25 changes: 20 additions & 5 deletions parachains/runtimes/assets/westmint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ use parachains_common::{
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
use xcm_config::{
AssetIdForTrustBackedAssetsConvert, XcmConfig, XcmOriginToTransactDispatchOrigin,
TrustBackedAssetsConvertedConcreteId, WestendLocation, XcmConfig,
XcmOriginToTransactDispatchOrigin,
};

#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -848,13 +849,27 @@ impl_runtime_apis! {
impl assets_common::assets_api::AssetsApi<
Block,
AccountId,
Balance,
xcm::latest::MultiLocation,
> for Runtime
{
fn account_balances(account: AccountId) -> Result<Vec<(xcm::latest::MultiLocation, Balance)>, assets_common::assets_api::AssetsAccessError> {
fn query_account_balances(account: AccountId) -> Result<Vec<xcm::latest::MultiAsset>, assets_common::assets_api::AssetsAccessError> {
use assets_common::assets_api::{convert, convert_balance};
Ok([
assets_common::convert_asset_id::<_, _, AssetIdForTrustBackedAssetsConvert>(Assets::account_balances(account))?,
// collect pallet_balance
{
let balance = Balances::free_balance(account.clone());
if balance > 0 {
vec![convert_balance::<WestendLocation, Balance>(balance)?]
} else {
vec![]
}
},
// collect pallet_assets (TrustBackedAssets)
convert::<_, _, _, _, TrustBackedAssetsConvertedConcreteId>(
Assets::account_balances(account)
.iter()
.filter(|(_, balance)| balance > &0)
)?,
// collect ... e.g. pallet_assets ForeignAssets
].concat())
}
}
Expand Down
Loading