-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: economic model requirements change (#241)
- Loading branch information
Showing
13 changed files
with
189 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "pallet-sminer-reward" | ||
authors = ["CESS LAB"] | ||
version = "0.7.4" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/CESSProject/cess" | ||
description = "FRAME pallet for sminer management" | ||
readme = "README.md" | ||
|
||
[dependencies] | ||
scale-info = { default-features = false, features = ['derive'], version = "2.0.1" } | ||
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } | ||
|
||
#substrate pallet | ||
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/CESSProject/substrate", branch = "cess-polkadot-v0.9.42" } | ||
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/CESSProject/substrate", branch = "cess-polkadot-v0.9.42" } | ||
sp-std = { version = "5.0.0", git = 'https://github.com/CESSProject/substrate', branch = "cess-polkadot-v0.9.42", default-features = false } | ||
sp-runtime = { version = "7.0.0", git = 'https://github.com/CESSProject/substrate', branch = "cess-polkadot-v0.9.42", default-features = false } | ||
|
||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"scale-info/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"sp-std/std", | ||
"sp-runtime/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
use frame_support::{ | ||
traits::{ | ||
Currency, ReservableCurrency, | ||
}, | ||
dispatch::{DispatchResult}, | ||
pallet_prelude::{StorageValue, ValueQuery}, | ||
}; | ||
use codec::{Decode, Encode}; | ||
use scale_info::TypeInfo; | ||
// use sp_std::prelude::*; | ||
use sp_runtime::{ | ||
SaturatedConversion, | ||
traits::{CheckedAdd, CheckedSub}, | ||
}; | ||
// use frame_system::pallet_prelude::*; | ||
|
||
pub use pallet::*; | ||
|
||
type BalanceOf<T> = | ||
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance; | ||
|
||
#[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 currency trait. | ||
type Currency: ReservableCurrency<Self::AccountId>; | ||
} | ||
|
||
#[pallet::error] | ||
pub enum Error<T> { | ||
Overflow, | ||
} | ||
|
||
#[pallet::storage] | ||
#[pallet::getter(fn currency_reward)] | ||
pub(super) type CurrencyReward<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>; | ||
} | ||
|
||
pub trait RewardPool<Balance> { | ||
fn get_reward() -> Balance; | ||
fn get_reward_128() -> u128; | ||
fn add_reward(amount: Balance) -> DispatchResult; | ||
fn sub_reward(amount: Balance) -> DispatchResult; | ||
} | ||
|
||
impl<T: Config> RewardPool<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 { | ||
<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(()) | ||
})?; | ||
|
||
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.