Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Collectives: teleport slashed assets #1433

Merged
merged 11 commits into from
Jul 21, 2022
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions parachains/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,6 @@ where
}
}

/*
TODO:COLLECTIVES

pub struct ToParentTreasury<R>(PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for ToParentTreasury<R>
where
R: pallet_balances::Config,
AccountIdOf<R>:
From<polkadot_primitives::v2::AccountId> + Into<polkadot_primitives::v2::AccountId>,
<R as frame_system::Config>::Event: From<pallet_balances::Event<R>>,
{
fn on_unbalanced(amount: NegativeImbalance<R>) {
polkadot_xcm::Pallet::<R>::teleport();
}
}
*/

/// Implementation of `OnUnbalanced` that deals with the fees by combining tip and fee and passing
/// the result on to `ToStakingPot`.
pub struct DealWithFees<R>(PhantomData<R>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// realy chain treasury pallet id, used to convert into AccountId
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
73 changes: 73 additions & 0 deletions parachains/runtimes/collectives/collectives-polkadot/src/impls.rs
Original file line number Diff line number Diff line change
@@ -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");
muharem marked this conversation as resolved.
Show resolved Hide resolved
// 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<T> = <T as frame_system::Config>::AccountId;

type NegativeImbalanceOf<T, I> = <<T as pallet_alliance::Config<I>>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;

type CurrencyOf<T, I> = <T as pallet_alliance::Config<I>>::Currency;

type BalanceOf<T, I> = <<T as pallet_alliance::Config<I>>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance;

pub struct ToParentTreasury<TreasuryAcc, TempAcc, T, I = ()>(
PhantomData<(TreasuryAcc, TempAcc, T, I)>,
);

impl<TreasuryAcc, TempAcc, T, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>>
for ToParentTreasury<TreasuryAcc, TempAcc, T, I>
where
TreasuryAcc: Get<AccountIdOf<T>>,
TempAcc: Get<AccountIdOf<T>>,
T: pallet_xcm::Config + frame_system::Config + pallet_alliance::Config<I>,
[u8; 32]: From<AccountIdOf<T>>,
BalanceOf<T, I>: Into<Fungibility>,
<<T as frame_system::Config>::Origin as OriginTrait>::AccountId: From<AccountIdOf<T>>,
{
fn on_unbalanced(amount: NegativeImbalanceOf<T, I>) {
let temp_account: AccountIdOf<T> = TempAcc::get();
let treasury_acc: AccountIdOf<T> = TreasuryAcc::get();
let imbalance = amount.peek();

<CurrencyOf<T, I>>::resolve_creating(&temp_account, amount);

let result = pallet_xcm::Pallet::<T>::teleport_assets(
<T as frame_system::Config>::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),
_ => (),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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 {
Expand All @@ -500,7 +505,7 @@ impl pallet_alliance::Config for Runtime {
pallet_collective::EnsureProportionMoreThan<AccountId, AllianceCollective, 2, 3>,
>;
type Currency = Balances;
type Slashed = (); // TODO:COLLECTIVES add handler to send teleport to Relay Treasury
type Slashed = ToParentTreasury<RelayTreasuryAccId, SlashedImbalanceAccId, Runtime>;
type InitializeMembers = AllianceMotion;
type MembershipChanged = AllianceMotion;
type IdentityVerifier = (); // Don't block accounts on identity criteria
Expand Down