Skip to content

Commit

Permalink
Add pallet fat_tokenomic
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Oct 12, 2022
1 parent 9b1f413 commit 8a0197d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pallets/phala/src/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ pub mod pallet {
}
Ok(())
}

pub fn get_system_contract(contract: &ContractId) -> Option<ContractId> {
let contract_info = Contracts::<T>::get(&contract)?;
let cluster_info = Clusters::<T>::get(&contract_info.cluster_id)?;
Some(cluster_info.system_contract)
}
}

#[pallet::hooks]
Expand Down
127 changes: 127 additions & 0 deletions pallets/phala/src/fat_tokenomic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//! The Fat Contract tokenomic module
pub use self::pallet::*;

#[frame_support::pallet]
pub mod pallet {
use crate::mq::MessageOriginInfo;
use frame_support::{
dispatch::DispatchResult,
pallet_prelude::*,
traits::{Currency, ExistenceRequirement::*, PalletInfo, StorageVersion},
};
use frame_system::pallet_prelude::*;
use phala_types::messaging::ContractId;
use sp_core::crypto::{AccountId32, UncheckedFrom};
use sp_runtime::traits::Zero;

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

#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Currency: Currency<Self::AccountId>;
}

const STORAGE_VERSION: StorageVersion = StorageVersion::new(5);

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

/// (contract, user) -> stake
#[pallet::storage]
pub type ContractUserStakes<T: Config> =
StorageMap<_, Twox64Concat, (ContractId, T::AccountId), BalanceOf<T>, ValueQuery>;

/// contract -> stake
#[pallet::storage]
pub type ContractTotalStakes<T: Config> =
StorageMap<_, Twox64Concat, ContractId, BalanceOf<T>, ValueQuery>;

/// Minimum allowed stake
#[pallet::storage]
pub type MinStake<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ContractDepositChanged {
contract: ContractId,
deposit: BalanceOf<T>,
},
}

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

#[pallet::call]
impl<T: Config> Pallet<T>
where
T: crate::mq::Config,
T: crate::fat::Config,
T: frame_system::Config<AccountId = AccountId32>,
{
#[pallet::weight(0)]
pub fn adjust_stake(
origin: OriginFor<T>,
contract: ContractId,
amount: BalanceOf<T>,
) -> DispatchResult {
let user = ensure_signed(origin)?;
ensure!(
amount == Zero::zero() || amount >= MinStake::<T>::get(),
Error::<T>::InvalidAmountOfStake
);

let mut total = ContractTotalStakes::<T>::get(&contract);
let orig = ContractUserStakes::<T>::get((&contract, &user));
if amount == orig {
return Ok(());
}
if amount > orig {
let delta = amount - orig;
total += delta;
<T as Config>::Currency::transfer(&user, &Self::pallet_id(), delta, KeepAlive)?;
} else {
let delta = orig - amount;
total -= delta;
<T as Config>::Currency::transfer(&Self::pallet_id(), &user, delta, AllowDeath)?;
}
ContractUserStakes::<T>::insert((&contract, &user), amount);
ContractTotalStakes::<T>::insert(contract, total);

Self::deposit_event(Event::ContractDepositChanged {
contract,
deposit: total,
});

if let Some(the_system_contract) =
crate::fat::Pallet::<T>::get_system_contract(&contract)
{
let selector: u32 = 0xa24bcb44; // ContractDeposit::change_deposit()
let message = (selector.to_be_bytes(), contract, total).encode();
Self::push_ink_message(the_system_contract, message);
}
Ok(())
}
}

impl<T: Config> Pallet<T> {
fn pallet_id() -> AccountId32 {
let pallet_id: u64 = T::PalletInfo::index::<Self>()
.expect("Pallet index of fat_tokenomic not found") as _;
AccountId32::unchecked_from(sp_core::H256::from_low_u64_be(pallet_id))
}
}

impl<T: Config + crate::mq::Config> MessageOriginInfo for Pallet<T> {
type Config = T;
}
}
1 change: 1 addition & 0 deletions pallets/phala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod migrations;
pub mod utils;

pub mod fat;
pub mod fat_tokenomic;
pub mod mining;
pub mod mq;
pub mod ott;
Expand Down
8 changes: 6 additions & 2 deletions pallets/phala/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type MiningBalanceOf<T> =

/// Alias for the runtime that implements all Phala Pallets
pub trait PhalaPallets:
fat::Config + mining::Config + mq::Config + registry::Config + stakepool::Config
fat::Config + mining::Config + mq::Config + registry::Config + stakepool::Config + fat_tokenomic::Config
{}
impl<T> PhalaPallets for T where
T: fat::Config + mining::Config + mq::Config + registry::Config + stakepool::Config
T: fat::Config + mining::Config + mq::Config + registry::Config + stakepool::Config + fat_tokenomic::Config
{}

type Versions = (
Expand All @@ -26,6 +26,7 @@ type Versions = (
StorageVersion,
StorageVersion,
StorageVersion,
StorageVersion,
);

#[allow(dead_code)]
Expand All @@ -36,6 +37,7 @@ fn get_versions<T: PhalaPallets>() -> Versions {
StorageVersion::get::<mq::Pallet<T>>(),
StorageVersion::get::<registry::Pallet<T>>(),
StorageVersion::get::<stakepool::Pallet<T>>(),
StorageVersion::get::<fat_tokenomic::Pallet<T>>(),
)
}

Expand All @@ -47,6 +49,7 @@ fn unified_versions<T: PhalaPallets>(version: u16) -> Versions {
StorageVersion::new(version),
StorageVersion::new(version),
StorageVersion::new(version),
StorageVersion::new(version),
)
}

Expand All @@ -57,4 +60,5 @@ fn set_unified_version<T: PhalaPallets>(version: u16) {
StorageVersion::new(version).put::<mq::Pallet<T>>();
StorageVersion::new(version).put::<registry::Pallet<T>>();
StorageVersion::new(version).put::<stakepool::Pallet<T>>();
StorageVersion::new(version).put::<fat_tokenomic::Pallet<T>>();
}

0 comments on commit 8a0197d

Please sign in to comment.