Skip to content

Commit

Permalink
Merge pull request #70 from toints/upstream-polkadot-v1.7.0
Browse files Browse the repository at this point in the history
perf: update to polkadot-v1.7.0
  • Loading branch information
Acaishiba authored Apr 1, 2024
2 parents 2bed195 + 7352585 commit 7a8fc82
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 13 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions pallets/liquidation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ frame-support = { workspace = true, default-features = false }
frame-system = { workspace = true, default-features = false }
frame-benchmarking = { workspace = true, default-features = false, optional = true}
pallet-utility = { workspace = true, default-features = false}
xcm = { workspace = true, default-features = false}
xcm-builder = { workspace = true, default-features = false}
cumulus-pallet-xcmp-queue = { workspace = true, default-features = false}
xcm-executor = { workspace = true, default-features = false}
pallet-xcm = {workspace = true, default-features = false}

[features]
default = ['std']
Expand All @@ -58,6 +63,11 @@ std = [
"mp-system/std",
"pallet-utility/std",
"frame-benchmarking/std",
"pallet-xcm/std",
"xcm/std",
"xcm-builder/std",
"cumulus-pallet-xcmp-queue/std",
"xcm-executor/std",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
101 changes: 93 additions & 8 deletions pallets/liquidation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@ mod mock;
mod tests;

use frame_support::{
dispatch::DispatchResult,
storage::types::StorageMap,
traits::{Currency, ExistenceRequirement, Get},
weights::WeightToFeePolynomial,
Twox64Concat,
};
use frame_system::pallet_prelude::BlockNumberFor;
use frame_system::pallet_prelude::{BlockNumberFor, OriginFor};
use mp_system::BASE_ACCOUNT;
pub use pallet::*;
use sp_runtime::{
traits::{StaticLookup, Zero},
AccountId32, Perbill, Saturating,
};
use sp_std::{prelude::*, vec};
use sp_std::{prelude::*, sync::Arc, vec};

use xcm::{
opaque::v4::Junctions::X1,
prelude::*,
v4::{Asset, AssetId, Junction, Location, NetworkId},
VersionedAssets, VersionedLocation,
};

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

pub type Balance = u128;

pub const PARACHAIN_TO_RELAYCHAIN_UNIT: u128 = 1_000_000;

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -54,6 +64,13 @@ pub mod pallet {
type Currency: frame_support::traits::Currency<Self::AccountId>
+ frame_support::traits::ReservableCurrency<Self::AccountId>;

/// The XCM sender
type XcmSender: SendXcm;

//Treasury account on the Relay Chain
//#[pallet::constant]
//type RelayChainTreasuryAccountId: Get<AccountId32>;

///Handles converting weight to fee value
type WeightToFee: WeightToFeePolynomial<Balance = Balance>;

Expand All @@ -76,6 +93,10 @@ pub mod pallet {
#[pallet::constant]
type ExistentialDeposit: Get<Balance>;

///minimum liquidation threshold
#[pallet::constant]
type MinLiquidationThreshold: Get<Balance>;

/// system accountId
#[pallet::constant]
type SystemAccountName: Get<&'static str>;
Expand Down Expand Up @@ -154,14 +175,18 @@ pub mod pallet {

///failed to process liquidation
ProcessLiquidationError,

///xcm error
XcmError,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T>
where
T::AccountId: From<AccountId32>,
T::AccountId: From<AccountId32> + Into<AccountId32>,
<T as pallet_utility::Config>::RuntimeCall: From<pallet_balances::Call<T>>,
<T as pallet_balances::Config>::Balance: From<BalanceOf<T>>,
T: pallet_xcm::Config,
{
fn on_finalize(n: BlockNumberFor<T>) {
let base_account = <T::AccountId>::from(BASE_ACCOUNT);
Expand Down Expand Up @@ -229,7 +254,15 @@ pub mod pallet {
*income = income.saturating_add(current_block_fee_u128)
});

if count % T::ProfitDistributionCycle::get() == Zero::zero() {
let min_liquidation_threshold: Balance =
<T as pallet::Config>::MinLiquidationThreshold::get()
.try_into()
.unwrap_or_else(|_| 0);
let profit = TotalIncome::<T>::get().saturating_sub(TotalCost::<T>::get());

if profit >= min_liquidation_threshold
&& count % T::ProfitDistributionCycle::get() == Zero::zero()
{
DistributionBlockCount::<T>::put(BlockNumberFor::<T>::zero());
match Self::distribute_profit() {
Ok(_) => {
Expand All @@ -255,9 +288,11 @@ pub mod pallet {

impl<T: Config> Pallet<T>
where
T::AccountId: From<AccountId32>,
T::AccountId: From<AccountId32> + Into<AccountId32>,
<T as pallet_utility::Config>::RuntimeCall: From<pallet_balances::Call<T>>,
<T as pallet_balances::Config>::Balance: From<BalanceOf<T>>,
T::XcmSender: SendXcm,
T: pallet_xcm::Config,
{
fn execute_batch_transfers(
transfers: Vec<(T::AccountId, BalanceOf<T>)>,
Expand Down Expand Up @@ -287,7 +322,7 @@ pub mod pallet {
calls.push(utility_call);
}

pallet_utility::Pallet::<T>::batch(
pallet_utility::Pallet::<T>::batch_all(
frame_system::RawOrigin::Signed(system_account).into(),
calls,
)
Expand Down Expand Up @@ -354,17 +389,27 @@ pub mod pallet {
let treasury_ratio = T::TreasuryRatio::get();
let operation_ratio = T::OperationRatio::get();

let treasury_amount = treasury_ratio * total_profit;
let treasury_amount = treasury_ratio * total_profit / PARACHAIN_TO_RELAYCHAIN_UNIT;
let operation_amount = operation_ratio * total_profit;
let system_amount = system_ratio * total_profit;
let total_collators_profit =
total_profit.saturating_sub(treasury_amount + operation_amount + system_amount);

let mut transfers = Vec::new();
let origin: OriginFor<T> =
frame_system::RawOrigin::Signed(treasury_account.clone()).into();

let _send_treasury_profit = Self::send_assets_to_relaychain_treasury(
origin,
treasury_account.into(),
treasury_amount,
)?;

let mut transfers = Vec::new();
/*
let treasury_account_profit =
treasury_amount.try_into().unwrap_or_else(|_| Zero::zero());
transfers.push((treasury_account, treasury_account_profit));
*/

let operation_account_profit =
operation_amount.try_into().unwrap_or_else(|_| Zero::zero());
Expand Down Expand Up @@ -395,5 +440,45 @@ pub mod pallet {
}
Self::execute_batch_transfers(transfers)
}

fn send_assets_to_relaychain_treasury(
origin: OriginFor<T>,
recipient: AccountId32,
amount: u128,
) -> DispatchResult {
let recipient_account_id = recipient.into();

let junction = Junction::AccountId32 {
id: recipient_account_id,
network: Some(NetworkId::Rococo),
};
let arc_junctions = Arc::new([junction]);

let beneficiary = Location::new(0, X1(arc_junctions));

let asset =
Asset { id: AssetId(Location::new(1, Here)), fun: Fungibility::Fungible(amount) };

let assets = Assets::from(vec![asset]);
let versioned_assets = VersionedAssets::from(assets);

match pallet_xcm::Pallet::<T>::reserve_transfer_assets(
origin,
Box::new(VersionedLocation::from(Location::parent())),
Box::new(VersionedLocation::from(beneficiary)),
Box::new(versioned_assets),
0,
) {
Ok(_) => {
frame_support::runtime_print!("reserve_transfer_assets executed successfully.");
},
Err(e) => {
log::error!("Error occurred while executing reserve_transfer_assets: {:?}", e);
Self::deposit_event(Event::Error(Error::<T>::XcmError.into()));
return Err(Error::<T>::XcmError.into());
},
}
Ok(())
}
}
}
Loading

0 comments on commit 7a8fc82

Please sign in to comment.