From 253f51ec26d9b974b87210407a2208a4285978b0 Mon Sep 17 00:00:00 2001 From: Niraj Kamdar Date: Tue, 2 Apr 2024 11:49:11 +0400 Subject: [PATCH 1/3] wip: not working --- Cargo.lock | 37 +++++++++++++++++++++++++++---------- runtime/Cargo.toml | 2 ++ runtime/src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b68586c..19dfb3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4168,6 +4168,7 @@ dependencies = [ "frame", "frame-support", "frame-system", + "pallet-assets", "pallet-balances", "pallet-minimal-template", "pallet-sudo", @@ -4180,6 +4181,7 @@ dependencies = [ "sp-genesis-builder", "sp-io", "sp-runtime", + "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot-v1.9.0)", "substrate-wasm-builder", ] @@ -4620,6 +4622,21 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "pallet-assets" +version = "29.0.0" +source = "git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot-v1.9.0#3c3d6fceb82372a3019b37117aa453d564b212de" +dependencies = [ + "frame-support", + "frame-system", + "log", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-runtime", + "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot-v1.9.0)", +] + [[package]] name = "pallet-balances" version = "28.0.0" @@ -4717,7 +4734,7 @@ checksum = "4e69bf016dc406eff7d53a7d3f7cf1c2e72c82b9088aac1118591e36dd2cd3e9" dependencies = [ "bitcoin_hashes", "rand", - "rand_core 0.5.1", + "rand_core 0.6.4", "serde", "unicode-normalization", ] @@ -7596,7 +7613,7 @@ dependencies = [ [[package]] name = "sp-crypto-ec-utils" version = "0.10.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "ark-bls12-377", "ark-bls12-377-ext", @@ -7658,7 +7675,7 @@ dependencies = [ [[package]] name = "sp-debug-derive" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "proc-macro2", "quote", @@ -7679,7 +7696,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.25.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "environmental", "parity-scale-codec", @@ -7866,7 +7883,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "24.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -7898,7 +7915,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "Inflector", "expander", @@ -7991,7 +8008,7 @@ source = "git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot [[package]] name = "sp-std" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" [[package]] name = "sp-storage" @@ -8009,7 +8026,7 @@ dependencies = [ [[package]] name = "sp-storage" version = "19.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "impl-serde", "parity-scale-codec", @@ -8046,7 +8063,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "16.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "parity-scale-codec", "tracing", @@ -8146,7 +8163,7 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "20.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#0711729d251efebf3486db602119ecfa67d98366" +source = "git+https://github.com/paritytech/polkadot-sdk#d6f68bb9062167537211cc05286809771fc8861a" dependencies = [ "anyhow", "impl-trait-for-tuples", diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 1973b5a..a76f413 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -28,6 +28,7 @@ frame-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "r sp-core = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0" , default-features = false } sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0" , default-features = false } sp-io = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } +sp-std = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } # pallets that we want to use pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0" , default-features = false } @@ -35,6 +36,7 @@ pallet-sudo = { git = "https://github.com/paritytech/polkadot-sdk", branch = "re pallet-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } pallet-transaction-payment = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } +pallet-assets = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } # genesis builder that allows us to interacto with runtime genesis config sp-genesis-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index d612275..753e00d 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -38,6 +38,7 @@ use frame::{ }; use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, IdentityFee, Weight}; +use sp_std::vec; // Substrate FRAME #[cfg(feature = "with-paritydb-weights")] @@ -45,7 +46,7 @@ use frame_support::weights::constants::ParityDbWeight as RuntimeDbWeight; #[cfg(feature = "with-rocksdb-weights")] use frame_support::weights::constants::RocksDbWeight as RuntimeDbWeight; -use sp_runtime::{generic, traits::BlakeTwo256, Perbill}; +use sp_runtime::{generic, traits::BlakeTwo256, Perbill, AccountId32}; /// Type of block number. pub type BlockNumber = u32; @@ -122,6 +123,8 @@ parameter_types! { pub const SS58Prefix: u8 = 42; } +type AccountId = AccountId32; + impl frame_system::Config for Runtime { /// The default type for storing how many extrinsics an account has signed. type Nonce = u32; @@ -135,7 +138,7 @@ impl frame_system::Config for Runtime { type Hashing = sp_runtime::traits::BlakeTwo256; /// The default identifier used to distinguish between accounts. - type AccountId = sp_runtime::AccountId32; + type AccountId = AccountId; /// The lookup mechanism to get account ID from whatever is passed in dispatchers. type Lookup = sp_runtime::traits::AccountIdLookup; @@ -201,6 +204,38 @@ impl frame_system::Config for Runtime { type PostTransactions = (); } + +pub struct EnsureRootWithArg { + _runtime_origin_marker: PhantomData, + _account_id_marker: PhantomData, +} + +impl frame_support::traits::EnsureOriginWithArg for EnsureRootWithArg { + type Success = AccountId; + + fn try_origin(origin: RuntimeOrigin, _arg: AccountId) -> Result { + // First, check if the origin is root + if let Ok(_) = frame_system::EnsureRoot::::try_origin(origin.clone()) { + // If it is, return some specific account ID as the success type + // You need to define what this account ID will be + return Ok(_arg); + } + + // If not root, you could check for other conditions or specific accounts + // For this example, we're just rejecting if not root + Err(origin) + } +} + +#[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)] +impl pallet_assets::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type CreateOrigin = EnsureRootWithArg<::RuntimeOrigin, ::AccountId>; + type ForceOrigin = frame_system::EnsureRoot; + type Freezer = (); +} + #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] impl pallet_balances::Config for Runtime { type AccountStore = System; From 55460cef72b951a7a6aae4212937219b91b52527 Mon Sep 17 00:00:00 2001 From: Niraj Kamdar Date: Tue, 2 Apr 2024 12:18:40 +0400 Subject: [PATCH 2/3] wip: issue with accountId mismatch --- Cargo.lock | 1 + runtime/Cargo.toml | 2 ++ runtime/src/lib.rs | 23 ++++++++++------------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19dfb3c..233c227 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4627,6 +4627,7 @@ name = "pallet-assets" version = "29.0.0" source = "git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot-v1.9.0#3c3d6fceb82372a3019b37117aa453d564b212de" dependencies = [ + "frame-benchmarking", "frame-support", "frame-system", "log", diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index a76f413..f95e405 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -61,12 +61,14 @@ std = [ "sp-core/std", "sp-runtime/std", + "sp-io/std", "pallet-balances/std", "pallet-sudo/std", "pallet-timestamp/std", "pallet-transaction-payment-rpc-runtime-api/std", "pallet-transaction-payment/std", + "pallet-assets/std", "pallet-minimal-template/std", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 753e00d..56bb629 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -37,8 +37,7 @@ use frame::{ }, }; -use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, IdentityFee, Weight}; -use sp_std::vec; +use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight}; // Substrate FRAME #[cfg(feature = "with-paritydb-weights")] @@ -101,6 +100,7 @@ construct_runtime!( Balances: pallet_balances, Sudo: pallet_sudo, TransactionPayment: pallet_transaction_payment, + Assets: pallet_assets, // our local pallet Template: pallet_minimal_template, @@ -204,21 +204,19 @@ impl frame_system::Config for Runtime { type PostTransactions = (); } - -pub struct EnsureRootWithArg { - _runtime_origin_marker: PhantomData, - _account_id_marker: PhantomData, +pub struct EnsureRootWithArg { + _marker: PhantomData, } -impl frame_support::traits::EnsureOriginWithArg for EnsureRootWithArg { - type Success = AccountId; +impl frame_support::traits::EnsureOriginWithArg for EnsureRootWithArg { + type Success = T::AccountId; - fn try_origin(origin: RuntimeOrigin, _arg: AccountId) -> Result { + fn try_origin(origin: T::RuntimeOrigin, _arg: &T::AccountId) -> Result { // First, check if the origin is root - if let Ok(_) = frame_system::EnsureRoot::::try_origin(origin.clone()) { + if let Ok(_) = ::AccountId> as frame::prelude::EnsureOrigin>::try_origin(origin.clone()) { // If it is, return some specific account ID as the success type // You need to define what this account ID will be - return Ok(_arg); + return Ok(_arg.clone()); } // If not root, you could check for other conditions or specific accounts @@ -231,7 +229,7 @@ impl frame_support::traits::EnsureOriginWithArg::RuntimeOrigin, ::AccountId>; + type CreateOrigin = EnsureRootWithArg; type ForceOrigin = frame_system::EnsureRoot; type Freezer = (); } @@ -384,7 +382,6 @@ impl_runtime_apis! { // https://github.com/paritytech/substrate/issues/10579#issuecomment-1600537558 pub mod interface { use super::Runtime; - use frame::deps::frame_system; pub type Block = super::Block; pub use frame::runtime::types_common::OpaqueBlock; From 5e5f04d84b8042e22bc5648f540de1b37c0d9f3d Mon Sep 17 00:00:00 2001 From: Niraj Kamdar Date: Tue, 2 Apr 2024 13:47:13 +0400 Subject: [PATCH 3/3] feat: add tests --- runtime/src/lib.rs | 47 ++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 56bb629..b38540d 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -38,6 +38,7 @@ use frame::{ }; use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight}; +use frame_support::traits::AsEnsureOriginWithArg; // Substrate FRAME #[cfg(feature = "with-paritydb-weights")] @@ -204,32 +205,12 @@ impl frame_system::Config for Runtime { type PostTransactions = (); } -pub struct EnsureRootWithArg { - _marker: PhantomData, -} - -impl frame_support::traits::EnsureOriginWithArg for EnsureRootWithArg { - type Success = T::AccountId; - - fn try_origin(origin: T::RuntimeOrigin, _arg: &T::AccountId) -> Result { - // First, check if the origin is root - if let Ok(_) = ::AccountId> as frame::prelude::EnsureOrigin>::try_origin(origin.clone()) { - // If it is, return some specific account ID as the success type - // You need to define what this account ID will be - return Ok(_arg.clone()); - } - - // If not root, you could check for other conditions or specific accounts - // For this example, we're just rejecting if not root - Err(origin) - } -} #[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)] impl pallet_assets::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; - type CreateOrigin = EnsureRootWithArg; + type CreateOrigin = AsEnsureOriginWithArg>; type ForceOrigin = frame_system::EnsureRoot; type Freezer = (); } @@ -394,7 +375,8 @@ pub mod interface { #[cfg(test)] mod tests { - use super::{interface, Balances, Runtime, RuntimeOrigin, System}; + use super::{interface, Balances, Runtime, RuntimeOrigin, System, Assets}; + use frame_support::assert_ok; use sp_io; use sp_runtime::BuildStorage; @@ -452,4 +434,25 @@ mod tests { assert_eq!(Balances::usable_balance(BOB.clone()), 1010); }) } + + #[test] + fn test_assets() { + let ALICE: AccountId32 = get_account_id(&"Alice"); + let BOB: AccountId32 = get_account_id(&"Bob"); + let asset1 = 0; + new_test_ext().execute_with(|| { + assert_ok!(Assets::create(RuntimeOrigin::signed(ALICE.clone()), asset1, Id(ALICE.clone()), 1)); + assert_eq!(Assets::total_supply(asset1), 0); + + assert_ok!(Assets::mint(RuntimeOrigin::signed(ALICE.clone()), asset1, Id(ALICE.clone()), 10000)); + assert_eq!(Assets::total_supply(asset1), 10000); + assert_eq!(Assets::balance(asset1, ALICE.clone()), 10000); + + assert_eq!(Assets::balance(asset1, BOB.clone()), 0); + assert_ok!(Assets::transfer(RuntimeOrigin::signed(ALICE.clone()), asset1, Id(BOB.clone()), 1000)); + assert_eq!(Assets::balance(asset1, BOB.clone()), 1000); + assert_eq!(Assets::balance(asset1, ALICE.clone()), 9000); + + }) + } }