diff --git a/Cargo.lock b/Cargo.lock index f769ca613ff..adcb8e89aaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1024,7 +1024,7 @@ dependencies = [ "parachains-common", "parity-scale-codec", "polkadot-core-primitives", - "polkadot-parachain 0.9.25", + "polkadot-parachain 0.9.26", "polkadot-runtime-common", "polkadot-runtime-constants", "scale-info", @@ -6806,7 +6806,7 @@ dependencies = [ "pallet-xcm", "parachain-info", "parity-scale-codec", - "polkadot-parachain 0.9.25", + "polkadot-parachain 0.9.26", "polkadot-primitives", "polkadot-runtime-common", "scale-info", diff --git a/parachains/common/src/impls.rs b/parachains/common/src/impls.rs index 6595ee44f3b..4a2411c2cf3 100644 --- a/parachains/common/src/impls.rs +++ b/parachains/common/src/impls.rs @@ -49,23 +49,6 @@ where } } -/* -TODO:COLLECTIVES - -pub struct ToParentTreasury(PhantomData); -impl OnUnbalanced> for ToParentTreasury -where - R: pallet_balances::Config, - AccountIdOf: - From + Into, - ::Event: From>, -{ - fn on_unbalanced(amount: NegativeImbalance) { - polkadot_xcm::Pallet::::teleport(); - } -} -*/ - /// Implementation of `OnUnbalanced` that deals with the fees by combining tip and fee and passing /// the result on to `ToStakingPot`. pub struct DealWithFees(PhantomData); diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/constants.rs b/parachains/runtimes/collectives/collectives-polkadot/src/constants.rs index 51a6450b021..fea3d002a32 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/constants.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/constants.rs @@ -13,6 +13,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +pub mod account { + use frame_support::PalletId; + use sp_runtime::AccountId32; + + /// Relay Chain treasury pallet id, used to convert into AccountId + pub const RELAY_TREASURY_PALL_ID: PalletId = PalletId(*b"py/trsry"); + /// account used to temporarily deposit slashed imbalance before teleporting + pub const SLASHED_IMBALANCE_ACC_ID: AccountId32 = AccountId32::new([7u8; 32]); +} + pub mod currency { use polkadot_core_primitives::Balance; use polkadot_runtime_constants as constants; diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/impls.rs b/parachains/runtimes/collectives/collectives-polkadot/src/impls.rs new file mode 100644 index 00000000000..83863c8c475 --- /dev/null +++ b/parachains/runtimes/collectives/collectives-polkadot/src/impls.rs @@ -0,0 +1,73 @@ +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use frame_support::{ + log, + traits::{Currency, Get, Imbalance, OnUnbalanced, OriginTrait}, +}; +use sp_std::{boxed::Box, marker::PhantomData}; +use xcm::latest::{Fungibility, Junction, NetworkId, Parent}; + +type AccountIdOf = ::AccountId; + +type NegativeImbalanceOf = <>::Currency as Currency< + ::AccountId, +>>::NegativeImbalance; + +type CurrencyOf = >::Currency; + +type BalanceOf = <>::Currency as Currency< + ::AccountId, +>>::Balance; + +pub struct ToParentTreasury( + PhantomData<(TreasuryAcc, TempAcc, T, I)>, +); + +impl OnUnbalanced> + for ToParentTreasury +where + TreasuryAcc: Get>, + TempAcc: Get>, + T: pallet_xcm::Config + frame_system::Config + pallet_alliance::Config, + [u8; 32]: From>, + BalanceOf: Into, + <::Origin as OriginTrait>::AccountId: From>, +{ + fn on_unbalanced(amount: NegativeImbalanceOf) { + let temp_account: AccountIdOf = TempAcc::get(); + let treasury_acc: AccountIdOf = TreasuryAcc::get(); + let imbalance = amount.peek(); + + >::resolve_creating(&temp_account, amount); + + let result = pallet_xcm::Pallet::::teleport_assets( + ::Origin::signed(temp_account.into()), + Box::new(Parent.into()), + Box::new( + Junction::AccountId32 { network: NetworkId::Any, id: treasury_acc.into() } + .into() + .into(), + ), + Box::new((Parent, imbalance).into()), + 0, + ); + + match result { + Err(err) => log::warn!("Failed to teleport slashed assets: {:?}", err), + _ => (), + }; + } +} diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs b/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs index 1c3136cf2a5..2241a7b5a1d 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs @@ -38,15 +38,17 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); pub mod constants; +pub mod impls; mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use impls::ToParentTreasury; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, - traits::{AccountIdLookup, BlakeTwo256, Block as BlockT}, + traits::{AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT}, transaction_validity::{TransactionSource, TransactionValidity}, ApplyExtrinsicResult, }; @@ -482,6 +484,9 @@ pub const MAX_ALLIES: u32 = 100; parameter_types! { pub const AllyDeposit: Balance = 1_000 * UNITS; // 1,000 DOT bond to join as an Ally + // account used to temporarily deposit slashed imbalance before teleporting + pub SlashedImbalanceAccId: AccountId = constants::account::SLASHED_IMBALANCE_ACC_ID.into(); + pub RelayTreasuryAccId: AccountId = constants::account::RELAY_TREASURY_PALL_ID.into_account_truncating(); } impl pallet_alliance::Config for Runtime { @@ -500,7 +505,7 @@ impl pallet_alliance::Config for Runtime { pallet_collective::EnsureProportionMoreThan, >; type Currency = Balances; - type Slashed = (); // TODO:COLLECTIVES add handler to send teleport to Relay Treasury + type Slashed = ToParentTreasury; type InitializeMembers = AllianceMotion; type MembershipChanged = AllianceMotion; type IdentityVerifier = (); // Don't block accounts on identity criteria