Files
serai/substrate/emissions/pallet/src/lib.rs

337 lines
12 KiB
Rust
Raw Normal View History

2024-04-09 11:14:07 +03:00
#![cfg_attr(not(feature = "std"), no_std)]
2024-05-10 12:22:33 +03:00
#[allow(clippy::cast_possible_truncation, clippy::no_effect_underscore_binding, clippy::empty_docs)]
2024-04-09 11:14:07 +03:00
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_system::pallet_prelude::*;
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion};
use sp_std::{vec, vec::Vec, collections::btree_map::BTreeMap};
use sp_runtime;
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
use dex_pallet::{Config as DexConfig, Pallet as Dex};
use validator_sets_pallet::{Pallet as ValidatorSets, Config as ValidatorSetsConfig};
use serai_primitives::{NetworkId, NETWORKS, *};
2024-05-10 12:22:33 +03:00
use validator_sets_primitives::{MAX_KEY_SHARES_PER_SET, Session};
2024-05-05 11:49:37 +03:00
use genesis_liquidity_primitives::GENESIS_PERIOD_BLOCKS;
2024-04-09 11:14:07 +03:00
use emissions_primitives::*;
#[pallet::config]
pub trait Config:
2024-05-10 12:22:33 +03:00
frame_system::Config<AccountId = PublicKey> + ValidatorSetsConfig + CoinsConfig + DexConfig
2024-04-09 11:14:07 +03:00
{
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
#[pallet::genesis_config]
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
pub struct GenesisConfig<T: Config> {
/// Networks to spawn Serai with.
2024-05-10 12:22:33 +03:00
pub networks: Vec<(NetworkId, Amount)>,
2024-04-09 11:14:07 +03:00
/// List of participants to place in the initial validator sets.
pub participants: Vec<T::AccountId>,
}
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig { networks: Default::default(), participants: Default::default() }
}
}
#[pallet::error]
pub enum Error<T> {
2024-05-10 12:22:33 +03:00
MintFailed,
2024-04-09 11:14:07 +03:00
}
#[pallet::event]
pub enum Event<T: Config> {}
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::storage]
#[pallet::getter(fn participants)]
pub(crate) type Participants<T: Config> = StorageMap<
_,
Identity,
NetworkId,
BoundedVec<(PublicKey, u64), ConstU32<{ MAX_KEY_SHARES_PER_SET }>>,
OptionQuery,
>;
#[pallet::storage]
2024-05-10 12:22:33 +03:00
#[pallet::getter(fn session_begin_block)]
pub(crate) type SessionBeginBlock<T: Config> = StorageMap<_, Identity, u32, u64, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn session)]
pub type CurrentSession<T: Config> = StorageMap<_, Identity, NetworkId, u32, ValueQuery>;
2024-04-09 11:14:07 +03:00
#[pallet::storage]
#[pallet::getter(fn economic_security_reached)]
pub(crate) type EconomicSecurityReached<T: Config> =
2024-05-10 12:22:33 +03:00
StorageMap<_, Identity, NetworkId, bool, ValueQuery>;
2024-04-09 11:14:07 +03:00
#[pallet::storage]
#[pallet::getter(fn last_swap_volume)]
pub(crate) type LastSwapVolume<T: Config> = StorageMap<_, Identity, NetworkId, u64, OptionQuery>;
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
2024-05-10 12:22:33 +03:00
for (id, stake) in self.networks.clone() {
2024-04-09 11:14:07 +03:00
let mut participants = vec![];
for p in self.participants.clone() {
2024-05-10 12:22:33 +03:00
participants.push((p, stake.0));
2024-04-09 11:14:07 +03:00
}
Participants::<T>::set(id, Some(participants.try_into().unwrap()));
2024-05-10 12:22:33 +03:00
CurrentSession::<T>::set(id, 0);
EconomicSecurityReached::<T>::set(id, false);
2024-04-09 11:14:07 +03:00
}
2024-05-10 12:22:33 +03:00
SessionBeginBlock::<T>::set(0, 0);
2024-04-09 11:14:07 +03:00
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(n: BlockNumberFor<T>) {
2024-05-10 12:22:33 +03:00
// wait 1 extra block to actually see genesis changes
let genesis_ended = n >= (GENESIS_PERIOD_BLOCKS + 1).into();
2024-04-09 11:14:07 +03:00
// we accept we reached economic security once we can mint smallest amount of a network's coin
for coin in COINS {
2024-05-10 12:22:33 +03:00
let check = !Self::economic_security_reached(coin.network()) && genesis_ended;
if check && <T as CoinsConfig>::AllowMint::is_allowed(&Balance { coin, amount: Amount(1) })
2024-04-09 11:14:07 +03:00
{
2024-05-10 12:22:33 +03:00
EconomicSecurityReached::<T>::set(coin.network(), true);
2024-04-09 11:14:07 +03:00
}
}
2024-05-10 12:22:33 +03:00
// check wif we got a new session
let mut session_changed = false;
let session = ValidatorSets::<T>::session(NetworkId::Serai).unwrap_or(Session(0)).0;
if session > Self::session(NetworkId::Serai) {
session_changed = true;
CurrentSession::<T>::set(NetworkId::Serai, session);
}
// emissions start only after genesis period and happens once per session.
2024-04-09 11:14:07 +03:00
// so we don't do anything before that time.
2024-05-10 12:22:33 +03:00
if !(genesis_ended && session_changed) {
2024-04-09 11:14:07 +03:00
return;
}
2024-05-10 12:22:33 +03:00
// figure out the amount of blocks in the last session. Session is at least 1
// if we come here.
let current_block = n.saturated_into::<u64>();
let block_count = current_block - Self::session_begin_block(session - 1);
2024-04-09 11:14:07 +03:00
// get total reward for this epoch
let pre_ec_security = Self::pre_ec_security();
let mut distances = BTreeMap::new();
let mut total_distance: u64 = 0;
2024-05-10 12:22:33 +03:00
let reward_this_epoch = if pre_ec_security {
2024-04-09 11:14:07 +03:00
// calculate distance to economic security per network
for n in NETWORKS {
if n == NetworkId::Serai {
continue;
}
let required = ValidatorSets::<T>::required_stake_for_network(n);
let mut current = ValidatorSets::<T>::total_allocated_stake(n).unwrap_or(Amount(0)).0;
if current > required {
current = required;
}
2024-05-10 12:22:33 +03:00
let distance = required - current;
distances.insert(n, distance);
total_distance = total_distance.saturating_add(distance);
2024-04-09 11:14:07 +03:00
}
// add serai network portion(20%)
let new_total_distance =
total_distance.saturating_mul(10) / (10 - SERAI_VALIDATORS_DESIRED_PERCENTAGE);
distances.insert(NetworkId::Serai, new_total_distance - total_distance);
total_distance = new_total_distance;
2024-05-10 12:22:33 +03:00
if Self::initial_period(n) {
// rewards are fixed for initial period
block_count * INITIAL_REWARD_PER_BLOCK
} else {
// rewards for pre-economic security is
// (STAKE_REQUIRED - CURRENT_STAKE) / blocks_until(SECURE_BY).
let block_reward = total_distance / Self::blocks_until(SECURE_BY);
block_count * block_reward
}
2024-04-09 11:14:07 +03:00
} else {
// post ec security
block_count * REWARD_PER_BLOCK
};
// get swap volumes
let mut volume_per_network: BTreeMap<NetworkId, u64> = BTreeMap::new();
for c in COINS {
// this should return 0 for SRI and so it shouldn't affect the total volume.
let current_volume = Dex::<T>::swap_volume(c).unwrap_or(0);
volume_per_network.insert(
c.network(),
(*volume_per_network.get(&c.network()).unwrap_or(&0)).saturating_add(current_volume),
);
}
// map current volumes to epoch volumes
let mut total_volume = 0u64;
for (n, vol) in &mut volume_per_network {
let last_volume = Self::last_swap_volume(n).unwrap_or(0);
let vol_this_epoch = vol.saturating_sub(last_volume);
// update the current volume
LastSwapVolume::<T>::set(n, Some(*vol));
total_volume = total_volume.saturating_add(vol_this_epoch);
*vol = vol_this_epoch;
}
// map epoch ec-security-distance/volume to rewards
let rewards_per_network = distances
.into_iter()
.map(|(n, distance)| {
let reward = if pre_ec_security {
// calculate how much each network gets based on distance to ec-security
2024-05-10 12:22:33 +03:00
u64::try_from(
u128::from(reward_this_epoch).saturating_mul(u128::from(distance)) /
u128::from(total_distance),
)
.unwrap()
2024-04-09 11:14:07 +03:00
} else {
// 20% of the reward goes to the Serai network and rest is distributed among others
// based on swap-volume.
if n == NetworkId::Serai {
reward_this_epoch / 5
} else {
let reward = reward_this_epoch - (reward_this_epoch / 5);
2024-05-10 12:22:33 +03:00
u64::try_from(
u128::from(reward)
.saturating_mul(u128::from(*volume_per_network.get(&n).unwrap_or(&0))) /
u128::from(total_volume),
)
.unwrap()
2024-04-09 11:14:07 +03:00
}
};
(n, reward)
})
.collect::<BTreeMap<NetworkId, u64>>();
// distribute the rewards within the network
for (n, reward) in rewards_per_network {
2024-05-10 12:22:33 +03:00
let (validators_reward, pool_reward) = if n == NetworkId::Serai {
(reward, 0)
} else {
// calculate pool vs validator share
let capacity = ValidatorSets::<T>::total_allocated_stake(n).unwrap_or(Amount(0)).0;
let required = ValidatorSets::<T>::required_stake_for_network(n);
let unused_capacity = capacity.saturating_sub(required);
2024-04-09 11:14:07 +03:00
2024-05-10 12:22:33 +03:00
let distribution = unused_capacity.saturating_mul(ACCURACY_MULTIPLIER) / capacity;
let total = DESIRED_DISTRIBUTION.saturating_add(distribution);
2024-04-09 11:14:07 +03:00
2024-05-10 12:22:33 +03:00
let validators_reward = DESIRED_DISTRIBUTION.saturating_mul(reward) / total;
let pool_reward = total - validators_reward;
(validators_reward, pool_reward)
};
2024-04-09 11:14:07 +03:00
// distribute validators rewards
2024-05-10 12:22:33 +03:00
if Self::distribute_to_validators(n, validators_reward).is_err() {
// TODO: log the failure
continue;
}
2024-04-09 11:14:07 +03:00
// send the rest to the pool
let coin_count = u64::try_from(n.coins().len()).unwrap();
for c in n.coins() {
// assumes reward is equally distributed between network coins.
2024-05-10 12:22:33 +03:00
if Coins::<T>::mint(
2024-04-09 11:14:07 +03:00
Dex::<T>::get_pool_account(*c),
Balance { coin: Coin::Serai, amount: Amount(pool_reward / coin_count) },
)
2024-05-10 12:22:33 +03:00
.is_err()
{
// TODO: log the failure
continue;
}
2024-04-09 11:14:07 +03:00
}
}
// set the begin block and participants
2024-05-10 12:22:33 +03:00
SessionBeginBlock::<T>::set(session, current_block);
2024-04-09 11:14:07 +03:00
for n in NETWORKS {
// TODO: `participants_for_latest_decided_set` returns keys with key shares but we
// store keys with actual stake amounts. Pr https://github.com/serai-dex/serai/pull/518
// supposed to change that and so this pr relies and that pr.
Participants::<T>::set(n, ValidatorSets::<T>::participants_for_latest_decided_set(n));
}
}
}
impl<T: Config> Pallet<T> {
fn blocks_until(block: u64) -> u64 {
let current = <frame_system::Pallet<T>>::block_number().saturated_into::<u64>();
block.saturating_sub(current)
}
fn initial_period(n: BlockNumberFor<T>) -> bool {
2024-05-05 11:49:37 +03:00
n >= GENESIS_PERIOD_BLOCKS.into() && n < (3 * GENESIS_PERIOD_BLOCKS).into()
2024-04-09 11:14:07 +03:00
}
/// Returns true if any of the external networks haven't reached economic security yet.
fn pre_ec_security() -> bool {
for n in NETWORKS {
if n == NetworkId::Serai {
continue;
}
2024-05-10 12:22:33 +03:00
if !Self::economic_security_reached(n) {
2024-04-09 11:14:07 +03:00
return true;
}
}
false
}
2024-05-10 12:22:33 +03:00
fn distribute_to_validators(n: NetworkId, reward: u64) -> DispatchResult {
2024-04-09 11:14:07 +03:00
// distribute among network's set based on
// -> (key shares * stake per share) + ((stake % stake per share) / 2)
let stake_per_share = ValidatorSets::<T>::allocation_per_key_share(n).unwrap().0;
let mut scores = vec![];
let mut total_score = 0u64;
for (p, amount) in Self::participants(n).unwrap() {
let remainder = amount % stake_per_share;
let score = (amount - remainder) + (remainder / 2);
total_score = total_score.saturating_add(score);
scores.push((p, score));
}
// stake the rewards
for (p, score) in scores {
2024-05-10 12:22:33 +03:00
let p_reward = u64::try_from(
u128::from(reward).saturating_mul(u128::from(score)) / u128::from(total_score),
)
.unwrap();
Coins::<T>::mint(p, Balance { coin: Coin::Serai, amount: Amount(p_reward) })
.map_err(|_| Error::<T>::MintFailed)?;
ValidatorSets::<T>::deposit_stake(n, p, Amount(p_reward))?;
2024-04-09 11:14:07 +03:00
}
2024-05-10 12:22:33 +03:00
Ok(())
2024-04-09 11:14:07 +03:00
}
}
}
pub use pallet::*;