From c1348ddc21692c9b9b7fe54090b97115a9a9c083 Mon Sep 17 00:00:00 2001 From: Oleg Kalenik Date: Wed, 12 Oct 2022 11:49:56 +0300 Subject: [PATCH] *added assets related pallets: - pallet-assets - pallet-asset-tx-payment --- Cargo.lock | 34 ++++++++++++++++++++++++++++ node/Cargo.toml | 2 ++ node/src/benchmarking.rs | 2 ++ node/src/chain_spec.rs | 1 + node/src/command_helper.rs | 1 + runtime/Cargo.toml | 4 ++++ runtime/src/impls.rs | 15 +++++++++++++ runtime/src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++- 8 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 runtime/src/impls.rs diff --git a/Cargo.lock b/Cargo.lock index b87f6bf..faed2b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3791,6 +3791,8 @@ dependencies = [ "frame-system", "jsonrpsee", "node-template-runtime", + "pallet-asset-tx-payment", + "pallet-assets", "pallet-contracts", "pallet-contracts-rpc", "pallet-im-online", @@ -3846,6 +3848,8 @@ dependencies = [ "frame-try-runtime", "hex-literal", "node-primitives", + "pallet-asset-tx-payment", + "pallet-assets", "pallet-aura", "pallet-authority-discovery", "pallet-authorship", @@ -4063,6 +4067,36 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "pallet-asset-tx-payment" +version = "4.0.0-dev" +source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.28#ce10b9f29353e89fc3e59d447041bb29622def3f" +dependencies = [ + "frame-support", + "frame-system", + "pallet-transaction-payment", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + +[[package]] +name = "pallet-assets" +version = "4.0.0-dev" +source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.28#ce10b9f29353e89fc3e59d447041bb29622def3f" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "parity-scale-codec", + "scale-info", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-aura" version = "4.0.0-dev" diff --git a/node/Cargo.toml b/node/Cargo.toml index ea910b1..0d8ed92 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -51,6 +51,8 @@ sp-timestamp = { version = "4.0.0-dev", git = "https://github.com/paritytech/sub # frame dependencies frame-system = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } +pallet-assets = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } +pallet-asset-tx-payment = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } pallet-contracts = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } pallet-im-online = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } diff --git a/node/src/benchmarking.rs b/node/src/benchmarking.rs index f0e3210..f4fb371 100644 --- a/node/src/benchmarking.rs +++ b/node/src/benchmarking.rs @@ -142,6 +142,7 @@ pub fn create_benchmark_extrinsic( frame_system::CheckNonce::::from(nonce), frame_system::CheckWeight::::new(), pallet_transaction_payment::ChargeTransactionPayment::::from(0), + pallet_asset_tx_payment::ChargeAssetTxPayment::::from(10_u128, None), ); let raw_payload = runtime::SignedPayload::from_raw( @@ -156,6 +157,7 @@ pub fn create_benchmark_extrinsic( (), (), (), + (), ), ); let signature = raw_payload.using_encoded(|e| sender.sign(e)); diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index 0425374..5964586 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -256,6 +256,7 @@ pub fn testnet_genesis( .collect(), phantom: Default::default(), }, + assets: Default::default(), transaction_payment: Default::default(), } } diff --git a/node/src/command_helper.rs b/node/src/command_helper.rs index 287e81b..cb8f8a1 100644 --- a/node/src/command_helper.rs +++ b/node/src/command_helper.rs @@ -90,6 +90,7 @@ pub fn create_benchmark_extrinsic( frame_system::CheckNonce::::from(nonce), frame_system::CheckWeight::::new(), pallet_transaction_payment::ChargeTransactionPayment::::from(0), + pallet_asset_tx_payment::ChargeAssetTxPayment::from(0), ); let raw_payload = runtime::SignedPayload::from_raw( diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 6eddcf9..546a9ea 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -41,6 +41,8 @@ frame-executive = { version = "4.0.0-dev", default-features = false, git = "http frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } frame-try-runtime = { version = "0.10.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", optional = true , branch = "polkadot-v0.9.28" } +pallet-assets = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } +pallet-asset-tx-payment = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } pallet-aura = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } pallet-authority-discovery = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } pallet-authorship = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } @@ -97,6 +99,7 @@ std = [ "frame-support/std", "frame-system-rpc-runtime-api/std", "frame-system/std", + "pallet-assets/std", "pallet-aura/std", "pallet-authority-discovery/std", "pallet-authorship/std", @@ -160,6 +163,7 @@ try-runtime = [ "frame-executive/try-runtime", "frame-try-runtime", "frame-system/try-runtime", + "pallet-assets/try-runtime", "pallet-aura/try-runtime", "pallet-balances/try-runtime", "pallet-grandpa/try-runtime", diff --git a/runtime/src/impls.rs b/runtime/src/impls.rs new file mode 100644 index 0000000..826fc7e --- /dev/null +++ b/runtime/src/impls.rs @@ -0,0 +1,15 @@ +use crate::{AccountId, Assets, Runtime}; +use frame_support::traits::fungibles::{Balanced, CreditOf}; +use pallet_asset_tx_payment::HandleCredit; + +/// 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 for CreditToBlockAuthor { + fn handle_credit(credit: CreditOf) { + if let Some(author) = pallet_authorship::Pallet::::author() { + // Drop the result which will trigger the `OnDrop` of the imbalance in case of error. + let _ = Assets::resolve(&author, credit); + } + } +} diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 72c20de..c5a105a 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -38,7 +38,7 @@ use sp_runtime::{ create_runtime_str, curve::PiecewiseLinear, generic, impl_opaque_keys, - traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, NumberFor, OpaqueKeys}, + traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto, NumberFor, OpaqueKeys}, transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity}, ApplyExtrinsicResult, FixedU128, Percent, }; @@ -75,6 +75,10 @@ use pallet_transaction_payment::CurrencyAdapter; pub use sp_runtime::BuildStorage; pub use sp_runtime::{Perbill, Permill}; +/// Implementations of some helper traits passed into runtime modules as associated types. +pub mod impls; +use impls::CreditToBlockAuthor; + use pallet_election_provider_multi_phase::SolutionAccuracyOf; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use pallet_session::historical as pallet_session_historical; @@ -955,6 +959,42 @@ impl pallet_referenda::Config for Runtime { type Tracks = TracksInfo; } +parameter_types! { + pub const AssetDeposit: Balance = 100 * DOLLARS; + pub const ApprovalDeposit: Balance = 1 * DOLLARS; + pub const StringLimit: u32 = 50; + pub const MetadataDepositBase: Balance = 10 * DOLLARS; + pub const MetadataDepositPerByte: Balance = 1 * DOLLARS; +} + +// TODO - Update settings +impl pallet_assets::Config for Runtime { + type Event = Event; + type Balance = u128; + type AssetId = u32; + type Currency = Balances; + type ForceOrigin = EnsureRoot; + type AssetDeposit = AssetDeposit; + type AssetAccountDeposit = ConstU128; + type MetadataDepositBase = MetadataDepositBase; + type MetadataDepositPerByte = MetadataDepositPerByte; + type ApprovalDeposit = ApprovalDeposit; + type StringLimit = StringLimit; + type Freezer = (); + type Extra = (); + type WeightInfo = pallet_assets::weights::SubstrateWeight; +} + +// TODO - Update settings +impl pallet_asset_tx_payment::Config for Runtime { + type Event = Event; + type Fungibles = Assets; + type OnChargeAssetTransaction = pallet_asset_tx_payment::FungiblesAdapter< + pallet_assets::BalanceToAssetBalance, + CreditToBlockAuthor, + >; +} + impl pallet_sudo::Config for Runtime { type Event = Event; type Call = Call; @@ -1020,6 +1060,8 @@ construct_runtime!( TechnicalMembership: pallet_membership::, Referenda: pallet_referenda, ConvictionVoting: pallet_conviction_voting, + Assets: pallet_assets, + AssetTxPayment: pallet_asset_tx_payment, } ); @@ -1039,6 +1081,7 @@ pub type SignedExtra = ( frame_system::CheckNonce, frame_system::CheckWeight, pallet_transaction_payment::ChargeTransactionPayment, + pallet_asset_tx_payment::ChargeAssetTxPayment, ); /// Unchecked extrinsic type as expected by this runtime. pub type UncheckedExtrinsic = generic::UncheckedExtrinsic;