Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
feat: use only local assets in pallet-asset-coversion
Browse files Browse the repository at this point in the history
  • Loading branch information
José Molina committed Dec 7, 2023
1 parent fdbd320 commit 4346a71
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 245 deletions.
5 changes: 2 additions & 3 deletions node/src/chain_spec/stout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use sc_service::ChainType;
use sp_core::sr25519;
use stout_runtime::{
constants::currency::EXISTENTIAL_DEPOSIT, AccountId, AssetsConfig, AuraId, BalancesConfig,
CouncilConfig, ForeignAssetsConfig, PoolAssetsConfig, RuntimeGenesisConfig, SessionConfig,
SessionKeys, SudoConfig, SystemConfig,
CouncilConfig, PoolAssetsConfig, RuntimeGenesisConfig, SessionConfig, SessionKeys, SudoConfig,
SystemConfig,
};

const DEFAULT_PROTOCOL_ID: &str = "stout";
Expand Down Expand Up @@ -157,7 +157,6 @@ pub fn testnet_genesis(
members: invulnerables.into_iter().map(|x| x.0).collect::<Vec<_>>(),
phantom: Default::default(),
},
foreign_assets: ForeignAssetsConfig { assets: vec![], accounts: vec![], metadata: vec![] },
pool_assets: PoolAssetsConfig { assets: vec![], accounts: vec![], metadata: vec![] },
}
}
6 changes: 2 additions & 4 deletions node/src/chain_spec/trappist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use sc_service::ChainType;
use sp_core::{crypto::UncheckedInto, sr25519};
use trappist_runtime::{
constants::currency::EXISTENTIAL_DEPOSIT, AccountId, AssetsConfig, AuraId, BalancesConfig,
CouncilConfig, ForeignAssetsConfig, PoolAssetsConfig, RuntimeGenesisConfig, SessionConfig,
SessionKeys, SudoConfig, SystemConfig,
CouncilConfig, PoolAssetsConfig, RuntimeGenesisConfig, SessionConfig, SessionKeys, SudoConfig,
SystemConfig,
};

const DEFAULT_PROTOCOL_ID: &str = "hop";
Expand Down Expand Up @@ -215,7 +215,6 @@ pub fn testnet_genesis(
treasury: Default::default(),
safe_mode: Default::default(),
tx_pause: Default::default(),
foreign_assets: ForeignAssetsConfig { assets: vec![], accounts: vec![], metadata: vec![] },
pool_assets: PoolAssetsConfig { assets: vec![], accounts: vec![], metadata: vec![] },
transaction_payment: Default::default(),
}
Expand Down Expand Up @@ -334,7 +333,6 @@ fn trappist_live_genesis(
treasury: Default::default(),
safe_mode: Default::default(),
tx_pause: Default::default(),
foreign_assets: ForeignAssetsConfig { assets: vec![], accounts: vec![], metadata: vec![] },
pool_assets: PoolAssetsConfig { assets: vec![], accounts: vec![], metadata: vec![] },
transaction_payment: Default::default(),
}
Expand Down
196 changes: 78 additions & 118 deletions runtime/stout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
#[macro_use]
extern crate frame_benchmarking;

use assets_common::foreign_creators::ForeignCreators;
use assets_common::local_and_foreign_assets::{LocalAndForeignAssets, MultiLocationConverter};
use assets_common::matching::FromSiblingParachain;
use assets_common::{AssetIdForTrustBackedAssetsConvert, MultiLocationForAssetId};
use common::AssetIdForTrustBackedAssets;
use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
use sp_api::impl_runtime_apis;
Expand All @@ -38,6 +34,8 @@ use sp_runtime::{
};

use constants::{currency::*, fee::WeightToFee};
use frame_support::instances::{Instance1, Instance2};
use frame_support::traits::fungibles::{Balanced, Credit};
use frame_support::{
construct_runtime,
dispatch::DispatchClass,
Expand All @@ -57,6 +55,8 @@ use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot, EnsureSigned, EnsureSignedBy,
};
use pallet_asset_conversion::{NativeOrAssetId, NativeOrAssetIdConverter};
use pallet_asset_tx_payment::HandleCredit;
use pallet_xcm::{EnsureXcm, IsMajorityOfBody};
pub use parachains_common as common;
pub use parachains_common::{
Expand All @@ -74,16 +74,9 @@ use sp_std::prelude::*;
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use xcm::latest::prelude::BodyId;
use xcm::latest::MultiLocation;

use xcm_config::{CollatorSelectionUpdateOrigin, RelayLocation};

// Polkadot imports
use crate::xcm_config::{
ForeignCreatorsSovereignAccountOf, LocalAndForeignAssetsMultiLocationMatcher,
TrustBackedAssetsPalletLocation,
};

// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
Expand Down Expand Up @@ -259,12 +252,24 @@ impl pallet_transaction_payment::Config for Runtime {
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
}

/// A `HandleCredit` implementation that naively transfers the fees to the block author.
/// Will drop and burn the assets in case the transfer fails.
pub struct CreditToBlockAuthor;
impl HandleCredit<AccountId, Assets> for CreditToBlockAuthor {
fn handle_credit(credit: Credit<AccountId, Assets>) {
if let Some(author) = pallet_authorship::Pallet::<Runtime>::author() {
// Drop the result which will trigger the `OnDrop` of the imbalance in case of error.
let _ = Assets::resolve(&author, credit);
}
}
}

impl pallet_asset_tx_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = Assets;
type OnChargeAssetTransaction = pallet_asset_tx_payment::FungiblesAdapter<
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
AssetsToBlockAuthor<Runtime, ()>,
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto, Instance1>,
CreditToBlockAuthor,
>;
}

Expand Down Expand Up @@ -378,12 +383,12 @@ pub type AssetsForceOrigin =

pub type AssetBalance = Balance;

impl pallet_assets::Config for Runtime {
impl pallet_assets::Config<Instance1> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = AssetBalance;
type RemoveItemsLimit = ConstU32<1000>;
type AssetId = AssetIdForTrustBackedAssets;
type AssetIdParameter = parity_scale_codec::Compact<u32>;
type AssetIdParameter = parity_scale_codec::Compact<AssetIdForTrustBackedAssets>;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type ForceOrigin = AssetsForceOrigin;
Expand All @@ -401,6 +406,33 @@ impl pallet_assets::Config for Runtime {
type BenchmarkHelper = ();
}

ord_parameter_types! {
pub const AssetConversionOrigin: AccountId = AccountIdConversion::<AccountId>::into_account_truncating(&AssetConversionPalletId::get());
}

impl pallet_assets::Config<Instance2> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = AssetBalance;
type RemoveItemsLimit = ConstU32<1000>;
type AssetId = AssetIdForTrustBackedAssets;
type AssetIdParameter = parity_scale_codec::Compact<AssetIdForTrustBackedAssets>;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSignedBy<AssetConversionOrigin, AccountId>>;
type ForceOrigin = AssetsForceOrigin;
type AssetDeposit = ConstU128<{ UNITS }>;
type AssetAccountDeposit = ConstU128<{ UNITS }>;
type MetadataDepositBase = ConstU128<{ UNITS }>;
type MetadataDepositPerByte = ConstU128<{ 10 * CENTS }>;
type ApprovalDeposit = ConstU128<{ 10 * CENTS }>;
type StringLimit = ConstU32<50>;
type Freezer = ();
type Extra = ();
type CallbackHandle = ();
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
pub MaxProposalWeight: Weight = Perbill::from_percent(50) * RuntimeBlockWeights::get().max_block;
}
Expand Down Expand Up @@ -539,124 +571,42 @@ impl pallet_asset_registry::Config for Runtime {
}

parameter_types! {
pub const AssetDeposit: Balance = UNITS / 10; // 1 / 10 UNITS deposit to create asset
pub const AssetDeposit: Balance = UNITS;
pub const AssetAccountDeposit: Balance = deposit(1, 16);
pub const ApprovalDeposit: Balance = EXISTENTIAL_DEPOSIT;
pub const AssetsStringLimit: u32 = 50;
pub const MetadataDepositBase: Balance = deposit(1, 68);
pub const MetadataDepositPerByte: Balance = deposit(0, 1);
}

parameter_types! {
pub const ForeignAssetsAssetDeposit: Balance = AssetDeposit::get();
pub const ForeignAssetsAssetAccountDeposit: Balance = AssetAccountDeposit::get();
pub const ForeignAssetsApprovalDeposit: Balance = ApprovalDeposit::get();
pub const ForeignAssetsAssetsStringLimit: u32 = AssetsStringLimit::get();
pub const ForeignAssetsMetadataDepositBase: Balance = MetadataDepositBase::get();
pub const ForeignAssetsMetadataDepositPerByte: Balance = MetadataDepositPerByte::get();
}

/// Assets managed by some foreign location. Note: we do not declare a `ForeignAssetsCall` type, as
/// this type is used in proxy definitions. We assume that a foreign location would not want to set
/// an individual, local account as a proxy for the issuance of their assets. This issuance should
/// be managed by the foreign location's governance.
pub type ForeignAssetsInstance = pallet_assets::Instance6;
impl pallet_assets::Config<ForeignAssetsInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = MultiLocationForAssetId;
type AssetIdParameter = MultiLocationForAssetId;
type Currency = Balances;
type CreateOrigin = ForeignCreators<
(FromSiblingParachain<parachain_info::Pallet<Runtime>>,),
ForeignCreatorsSovereignAccountOf,
AccountId,
>;
type ForceOrigin = AssetsForceOrigin;
type AssetDeposit = ForeignAssetsAssetDeposit;
type MetadataDepositBase = ForeignAssetsMetadataDepositBase;
type MetadataDepositPerByte = ForeignAssetsMetadataDepositPerByte;
type ApprovalDeposit = ForeignAssetsApprovalDeposit;
type StringLimit = ForeignAssetsAssetsStringLimit;
type Freezer = ();
type Extra = ();
type WeightInfo = ();
type CallbackHandle = ();
type AssetAccountDeposit = ForeignAssetsAssetAccountDeposit;
type RemoveItemsLimit = ConstU32<1000>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = xcm_config::XcmBenchmarkHelper;
}

ord_parameter_types! {
pub const AssetConversionOrigin: sp_runtime::AccountId32 =
AccountIdConversion::<sp_runtime::AccountId32>::into_account_truncating(&AssetConversionPalletId::get());
}

pub type PoolAssetsInstance = pallet_assets::Instance7;
impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type RemoveItemsLimit = ConstU32<1000>;
type AssetId = u32;
type AssetIdParameter = u32;
type Currency = Balances;
type CreateOrigin =
AsEnsureOriginWithArg<EnsureSignedBy<AssetConversionOrigin, sp_runtime::AccountId32>>;
type ForceOrigin = AssetsForceOrigin;
// Deposits are zero because creation/admin is limited to Asset Conversion pallet.
type AssetDeposit = ConstU128<0>;
type AssetAccountDeposit = ConstU128<0>;
type MetadataDepositBase = ConstU128<0>;
type MetadataDepositPerByte = ConstU128<0>;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = ConstU32<50>;
type Freezer = ();
type Extra = ();
type WeightInfo = ();
type CallbackHandle = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub const AllowMultiAssetPools: bool = false;
// should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
pub AllowMultiAssetPools: bool = true;
pub const PoolSetupFee: Balance = EXISTENTIAL_DEPOSIT; // should be more or equal to the existential deposit
pub const MintMinLiquidity: Balance = 100; // 100 is good enough when the main currency has 10-12 decimals.
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0); // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero.
}

impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type Balance = Balance;
type AssetBalance = Balance;
type HigherPrecisionBalance = u128;
type AssetId = MultiLocation;
type MultiAssetId = Box<MultiLocation>;
type MultiAssetIdConverter =
MultiLocationConverter<RelayLocation, LocalAndForeignAssetsMultiLocationMatcher>;
type PoolAssetId = u32;
type Assets = LocalAndForeignAssets<
Assets,
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
ForeignAssets,
>;
type Assets = Assets;
type Balance = Balance;
type PoolAssets = PoolAssets;
type LPFee = ConstU32<3>;
type PoolSetupFee = ConstU128<0>;
// Asset class deposit fees are sufficient to prevent spam
type AssetId = <Self as pallet_assets::Config<Instance1>>::AssetId;
type MultiAssetId = NativeOrAssetId<u32>;
type PoolAssetId = <Self as pallet_assets::Config<Instance2>>::AssetId;
type PalletId = AssetConversionPalletId;
type LPFee = ConstU32<3>; // means 0.3%
type PoolSetupFee = PoolSetupFee;
type PoolSetupFeeReceiver = AssetConversionOrigin;
// should be non-zero if `AllowMultiAssetPools` is true, otherwise can be zero.
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type MintMinLiquidity = ConstU128<100>;
type MaxSwapPathLength = ConstU32<4>;
type PalletId = AssetConversionPalletId;
type AllowMultiAssetPools = AllowMultiAssetPools;
type WeightInfo = pallet_asset_conversion::weights::SubstrateWeight<Runtime>;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
type MintMinLiquidity = MintMinLiquidity;
type MultiAssetIdConverter = NativeOrAssetIdConverter<u32>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper =
xcm_config::BenchmarkMultiLocationConverter<parachain_info::Pallet<Runtime>>;
type BenchmarkHelper = ();
}

// Create the runtime by composing the FRAME pallets that were previously configured.
Expand Down Expand Up @@ -693,7 +643,7 @@ construct_runtime!(
Sudo: pallet_sudo = 40,
Contracts: pallet_contracts = 41,
Council: pallet_collective::<Instance1> = 42,
Assets: pallet_assets = 43,
Assets: pallet_assets::<Instance1> = 43,
Identity: pallet_identity = 44,
Uniques: pallet_uniques = 45,
Scheduler: pallet_scheduler = 46,
Expand All @@ -705,8 +655,7 @@ construct_runtime!(

Spambot: cumulus_ping::{Pallet, Call, Storage, Event<T>} = 99,
AssetRegistry: pallet_asset_registry::{Pallet, Call, Storage, Event<T>} = 111,
ForeignAssets: pallet_assets::<Instance6> = 114,
PoolAssets: pallet_assets::<Instance7> = 115,
PoolAssets: pallet_assets::<Instance2> = 112,
}
);

Expand Down Expand Up @@ -856,6 +805,17 @@ impl_runtime_apis! {
}
}

impl pallet_asset_conversion::AssetConversionApi<Block, Balance, u128, NativeOrAssetId<u32>> for Runtime {
fn quote_price_exact_tokens_for_tokens(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_exact_tokens_for_tokens(asset1, asset2, amount, include_fee)
}
fn quote_price_tokens_for_exact_tokens(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_tokens_for_exact_tokens(asset1, asset2, amount, include_fee)
}
fn get_reserves(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>) -> Option<(Balance, Balance)> {
AssetConversion::get_reserves(&asset1, &asset2).ok()
}
}

impl pallet_contracts::ContractsApi<Block, AccountId, Balance, BlockNumber, Hash, EventRecord> for Runtime {
fn call(
Expand Down
Loading

0 comments on commit 4346a71

Please sign in to comment.