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.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub const POLKADOT_TREASURY_PALLET_INDEX: u8 = 19;
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;
Expand Down
40 changes: 31 additions & 9 deletions parachains/runtimes/collectives/collectives-polkadot/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// &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
//
Expand All @@ -13,36 +13,58 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::constants::POLKADOT_TREASURY_PALLET_INDEX;
use frame_support::{
log,
traits::{Currency, Imbalance, OnUnbalanced, OriginTrait},
traits::{Currency, Get, Imbalance, OnUnbalanced, OriginTrait},
};
use sp_std::{boxed::Box, marker::PhantomData};
use xcm::latest::{Fungibility, Junction, Parent};
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<T, I = ()>(PhantomData<(T, I)>);
impl<T, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>> for ToParentTreasury<T, I>
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::root(),
<T as frame_system::Config>::Origin::signed(temp_account.into()),
Box::new(Parent.into()),
Box::new(Junction::PalletInstance(POLKADOT_TREASURY_PALLET_INDEX).into().into()),
Box::new((Parent, amount.peek()).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),
_ => (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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 @@ -484,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 @@ -502,7 +505,7 @@ impl pallet_alliance::Config for Runtime {
pallet_collective::EnsureProportionMoreThan<AccountId, AllianceCollective, 2, 3>,
>;
type Currency = Balances;
type Slashed = ToParentTreasury<Runtime>;
type Slashed = ToParentTreasury<RelayTreasuryAccId, SlashedImbalanceAccId, Runtime>;
type InitializeMembers = AllianceMotion;
type MembershipChanged = AllianceMotion;
type IdentityVerifier = (); // Don't block accounts on identity criteria
Expand Down