Skip to content

Commit

Permalink
0.7.4 treasury (#247)
Browse files Browse the repository at this point in the history
* feat: temp save

* feat: temp save

* feat: add cess treasury

* fix: fix some warning
  • Loading branch information
ytqaljn authored Oct 23, 2023
1 parent 757b44f commit 0c1b261
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 175 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions c-pallets/audit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cp-bloom-filter = { path = '../../primitives/bloom-filter', default-features = f
cp-scheduler-credit = { path = '../../primitives/scheduler-credit', default-features = false }
cp-cess-common = { path = '../../primitives/common', default-features = false }
cp-enclave-verify = { path = '../../primitives/enclave-verify', default-features = false }
pallet-sminer-reward = { default-features = false, path = "../sminer-reward" }
pallet-cess-treasury = { default-features = false, path = "../cess-treasury" }

[dependencies.frame-benchmarking]
default-features = false
Expand Down Expand Up @@ -145,7 +145,7 @@ std = [
"pallet-tee-worker/std",
"cp-cess-common/std",
"cp-cess-common/std",
"pallet-sminer-reward/std",
"pallet-cess-treasury/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pallet-sminer-reward"
name = "pallet-cess-treasury"
authors = ["CESS LAB"]
version = "0.7.4"
edition = "2021"
Expand Down
239 changes: 239 additions & 0 deletions c-pallets/cess-treasury/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{
traits::{
Currency, ReservableCurrency, WithdrawReasons, Imbalance,
ExistenceRequirement::KeepAlive, OnUnbalanced,
},
dispatch::{DispatchResult}, PalletId,
pallet_prelude::{Weight, StorageValue, ValueQuery, Get, IsType},
};
// use sp_std::prelude::*;
use sp_runtime::{
SaturatedConversion,
traits::{CheckedAdd, CheckedSub, AccountIdConversion},
};
use frame_system::{
pallet_prelude::OriginFor,
ensure_signed, ensure_root,
};

pub use pallet::*;

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

type AccountOf<T> = <T as frame_system::Config>::AccountId;

pub type PositiveImbalanceOf<T> = <<T as Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::PositiveImbalance;

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

#[frame_support::pallet]
pub mod pallet {
use super::*;

#[pallet::pallet]
pub struct Pallet<T>(sp_std::marker::PhantomData<T>);

#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// The currency trait.
type Currency: ReservableCurrency<Self::AccountId>;

type MinerRewardId: Get<PalletId>;

type PunishTreasuryId: Get<PalletId>;

type SpaceTreasuryId: Get<PalletId>;

type BurnDestination: OnUnbalanced<NegativeImbalanceOf<Self>>;
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Deposit {
balance: BalanceOf<T>,
},
}

#[pallet::error]
pub enum Error<T> {
Overflow,
}

#[pallet::storage]
#[pallet::getter(fn currency_reward)]
pub(super) type CurrencyReward<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
// FIX ME!
#[pallet::weight(Weight::zero())]
pub fn send_funds_to_pid(
origin: OriginFor<T>,
funds: BalanceOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;

Self::send_to_pid(sender, funds)?;

Ok(())
}

#[pallet::call_index(1)]
// FIX ME!
#[pallet::weight(Weight::zero())]
pub fn send_funds_to_sid(
origin: OriginFor<T>,
funds: BalanceOf<T>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;

Self::send_to_sid(sender, funds)?;

Ok(())
}

#[pallet::call_index(2)]
// FIX ME!
#[pallet::weight(Weight::zero())]
pub fn pid_burn_funds(
origin: OriginFor<T>,
burn_amount: BalanceOf<T>,
) -> DispatchResult {
let _ = ensure_root(origin)?;

let pid = T::PunishTreasuryId::get().into_account_truncating();

let (debit, credit) = T::Currency::pair(burn_amount);

T::BurnDestination::on_unbalanced(credit);

if let Err(problem) = T::Currency::settle(&pid, debit, WithdrawReasons::TRANSFER, KeepAlive) {
drop(problem);
}

Ok(())
}

#[pallet::call_index(3)]
// FIX ME!
#[pallet::weight(Weight::zero())]
pub fn sid_burn_funds(
origin: OriginFor<T>,
burn_amount: BalanceOf<T>,
) -> DispatchResult {
let _ = ensure_root(origin)?;

let sid = T::SpaceTreasuryId::get().into_account_truncating();

let (debit, credit) = T::Currency::pair(burn_amount);

T::BurnDestination::on_unbalanced(credit);

if let Err(problem) = T::Currency::settle(&sid, debit, WithdrawReasons::TRANSFER, KeepAlive) {
drop(problem);
}

Ok(())
}
}
}

impl<T: Config> Pallet<T> {
pub fn add_miner_reward_pool(amount: BalanceOf<T>) -> DispatchResult {
<CurrencyReward<T>>::mutate(|v| -> DispatchResult {
// The total issuance amount will not exceed u128::Max, so there is no overflow risk
*v = v.checked_add(&amount).ok_or(Error::<T>::Overflow)?;

Ok(())
})
}

pub fn send_to_pid(acc: AccountOf<T>, amount: BalanceOf<T>) -> DispatchResult {
let pid = T::PunishTreasuryId::get().into_account_truncating();
<T as pallet::Config>::Currency::transfer(&acc, &pid, amount, KeepAlive)
}

pub fn send_to_sid(acc: AccountOf<T>, amount: BalanceOf<T>) -> DispatchResult {
let sid = T::SpaceTreasuryId::get().into_account_truncating();
<T as pallet::Config>::Currency::transfer(&acc, &sid, amount, KeepAlive)
}
}

pub trait RewardPool<AccountId, Balance> {
fn get_reward() -> Balance;
fn get_reward_128() -> u128;
fn add_reward(amount: Balance) -> DispatchResult;
fn sub_reward(amount: Balance) -> DispatchResult;
fn send_reward_to_miner(miner: AccountId, amount: Balance) -> DispatchResult;
}

impl<T: Config> RewardPool<AccountOf<T>, BalanceOf<T>> for Pallet<T> {
fn get_reward() -> BalanceOf<T> {
<CurrencyReward<T>>::get()
}

fn get_reward_128() -> u128 {
<CurrencyReward<T>>::get().saturated_into()
}

fn add_reward(amount: BalanceOf<T>) -> DispatchResult {
Self::add_miner_reward_pool(amount)?;

Ok(())
}

fn sub_reward(amount: BalanceOf<T>) -> DispatchResult {
<CurrencyReward<T>>::mutate(|v| -> DispatchResult {
// The total issuance amount will not exceed u128::Max, so there is no overflow risk
*v = v.checked_sub(&amount).ok_or(Error::<T>::Overflow)?;

Ok(())
})?;

Ok(())
}

fn send_reward_to_miner(beneficiary: AccountOf<T>, amount: BalanceOf<T>) -> DispatchResult {
let reward_acc = T::MinerRewardId::get().into_account_truncating();
<T as pallet::Config>::Currency::transfer(&reward_acc, &beneficiary, amount, KeepAlive)
}
}

pub trait TreasuryHandle<AccountId, Balance> {
fn send_to_pid(acc: AccountId, amount: Balance) -> DispatchResult;
fn send_to_sid(acc: AccountId, amount: Balance) -> DispatchResult;
}

impl<T: Config> TreasuryHandle<AccountOf<T>, BalanceOf<T>> for Pallet<T> {
fn send_to_pid(acc: AccountOf<T>, amount: BalanceOf<T>) -> DispatchResult {
Self::send_to_pid(acc, amount)
}

fn send_to_sid(acc: AccountOf<T>, amount: BalanceOf<T>) -> DispatchResult {
Self::send_to_sid(acc, amount)
}
}

impl<T: Config> OnUnbalanced<NegativeImbalanceOf<T>> for Pallet<T> {
fn on_nonzero_unbalanced(amount: NegativeImbalanceOf<T>) {
let numeric_amount = amount.peek();

// Must resolve into existing but better to be safe.
let _ = T::Currency::resolve_creating(&T::MinerRewardId::get().into_account_truncating(), amount);
// The total issuance amount will not exceed u128::Max, so there is no overflow risk
Self::add_miner_reward_pool(numeric_amount).unwrap();

Self::deposit_event(Event::Deposit { balance: numeric_amount });
}
}
5 changes: 3 additions & 2 deletions c-pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ pub mod pallet {
/// - `file_hash`: The unique hash identifier of the file to be transferred
#[pallet::call_index(2)]
#[transactional]
#[pallet::weight(1_000_000_000)]
/// FIX ME
#[pallet::weight(Weight::zero())]
pub fn ownership_transfer(
origin: OriginFor<T>,
target_brief: UserBrief<T>,
Expand Down Expand Up @@ -1185,7 +1186,7 @@ pub mod pallet {

#[pallet::call_index(21)]
#[transactional]
#[pallet::weight(10_000_000_000)]
#[pallet::weight(Weight::zero())]
pub fn miner_clear_failed_count(origin: OriginFor<T>) -> DispatchResult {
let sender = ensure_signed(origin)?;

Expand Down
Loading

0 comments on commit 0c1b261

Please sign in to comment.