Implement block emissions (#551)

* add genesis liquidity implementation

* add missing deposit event

* fix CI issues

* minor fixes

* make math safer

* fix fmt

* implement block emissions

* make remove liquidity an authorized call

* implement setting initial values for coins

* add genesis liquidity test & misc fixes

* updato develop latest

* fix rotation test

* fix licencing

* add fast-epoch feature

* only create the pool when adding liquidity first time

* add initial reward era test

* test whole pre ec security emissions

* fix clippy

* add swap-to-staked-sri feature

* rebase changes

* fix tests

* Remove accidentally commited ETH ABI files

* fix some pr comments

* Finish up fixing pr comments

* exclude SRI from is_allowed check

* Misc changes

---------

Co-authored-by: akildemir <aeg_asd@hotmail.com>
Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
akildemir
2024-08-15 06:12:04 +03:00
committed by GitHub
parent bf1c493d9a
commit cccc1fc7e6
36 changed files with 1279 additions and 302 deletions

View File

@@ -0,0 +1,61 @@
[package]
name = "serai-emissions-pallet"
version = "0.1.0"
description = "Emissions pallet for Serai"
license = "AGPL-3.0-only"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/emissions/pallet"
authors = ["Akil Demir <aeg_asd@hotmail.com>"]
edition = "2021"
rust-version = "1.77"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.cargo-machete]
ignored = ["scale", "scale-info"]
[lints]
workspace = true
[dependencies]
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"] }
frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false }
frame-support = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-runtime = { git = "https://github.com/serai-dex/substrate", default-features = false }
coins-pallet = { package = "serai-coins-pallet", path = "../../coins/pallet", default-features = false }
validator-sets-pallet = { package = "serai-validator-sets-pallet", path = "../../validator-sets/pallet", default-features = false }
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
genesis-liquidity-pallet = { package = "serai-genesis-liquidity-pallet", path = "../../genesis-liquidity/pallet", default-features = false }
serai-primitives = { path = "../../primitives", default-features = false }
validator-sets-primitives = { package = "serai-validator-sets-primitives", path = "../../validator-sets/primitives", default-features = false }
emissions-primitives = { package = "serai-emissions-primitives", path = "../primitives", default-features = false }
[features]
std = [
"scale/std",
"scale-info/std",
"frame-system/std",
"frame-support/std",
"sp-std/std",
"sp-runtime/std",
"coins-pallet/std",
"validator-sets-pallet/std",
"dex-pallet/std",
"genesis-liquidity-pallet/std",
"serai-primitives/std",
"emissions-primitives/std",
]
fast-epoch = []
try-runtime = [] # TODO
default = ["std"]

View File

@@ -0,0 +1,15 @@
AGPL-3.0-only license
Copyright (c) 2024 Luke Parker
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License Version 3 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

View File

@@ -0,0 +1,460 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[allow(clippy::cast_possible_truncation, clippy::no_effect_underscore_binding, clippy::empty_docs)]
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_system::{pallet_prelude::*, RawOrigin};
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion};
use sp_std::{vec, vec::Vec, ops::Mul, 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 genesis_liquidity_pallet::{Pallet as GenesisLiquidity, Config as GenesisLiquidityConfig};
use serai_primitives::*;
use validator_sets_primitives::{MAX_KEY_SHARES_PER_SET, Session};
pub use emissions_primitives as primitives;
use primitives::*;
#[pallet::config]
pub trait Config:
frame_system::Config<AccountId = PublicKey>
+ ValidatorSetsConfig
+ CoinsConfig
+ DexConfig
+ GenesisLiquidityConfig
{
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.
pub networks: Vec<(NetworkId, Amount)>,
/// 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> {
NetworkHasEconomicSecurity,
NoValueForCoin,
InsufficientAllocation,
}
#[pallet::event]
pub enum Event<T: Config> {}
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
// TODO: Remove this. This should be the sole domain of validator-sets
#[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,
>;
// TODO: Remove this too
#[pallet::storage]
#[pallet::getter(fn session)]
pub type CurrentSession<T: Config> = StorageMap<_, Identity, NetworkId, u32, ValueQuery>;
// TODO: Find a better place for this
#[pallet::storage]
#[pallet::getter(fn economic_security_reached)]
pub(crate) type EconomicSecurityReached<T: Config> =
StorageMap<_, Identity, NetworkId, bool, ValueQuery>;
// TODO: Find a better place for this
#[pallet::storage]
pub(crate) type GenesisCompleteBlock<T: Config> = StorageValue<_, u64, OptionQuery>;
#[pallet::storage]
pub(crate) type LastSwapVolume<T: Config> = StorageMap<_, Identity, Coin, u64, OptionQuery>;
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
for (id, stake) in self.networks.clone() {
let mut participants = vec![];
for p in self.participants.clone() {
participants.push((p, stake.0));
}
Participants::<T>::set(id, Some(participants.try_into().unwrap()));
CurrentSession::<T>::set(id, 0);
EconomicSecurityReached::<T>::set(id, false);
}
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
if GenesisCompleteBlock::<T>::get().is_none() &&
GenesisLiquidity::<T>::genesis_complete().is_some()
{
GenesisCompleteBlock::<T>::set(Some(n.saturated_into::<u64>()));
}
// we wait 1 extra block after genesis ended to see the changes. We only need this extra
// block in dev&test networks where we start the chain with accounts that already has some
// staked SRI. So when we check for ec-security pre-genesis we look like we are economically
// secure. The reason for this although we only check for it once the genesis is complete(so
// if the genesis complete we shouldn't be economically secure because the funds are not
// enough) is because ValidatorSets pallet runs before the genesis pallet in runtime.
// So ValidatorSets pallet sees the old state until next block.
// TODO: revisit this when mainnet genesis validator stake code is done.
let gcb = GenesisCompleteBlock::<T>::get();
let genesis_ended = gcb.is_some() && (n.saturated_into::<u64>() > gcb.unwrap());
// we accept we reached economic security once we can mint smallest amount of a network's coin
for coin in COINS {
let check = genesis_ended && !Self::economic_security_reached(coin.network());
if check && <T as CoinsConfig>::AllowMint::is_allowed(&Balance { coin, amount: Amount(1) })
{
EconomicSecurityReached::<T>::set(coin.network(), true);
}
}
// check if we got a new session
let mut session_changed = false;
let session = ValidatorSets::<T>::session(NetworkId::Serai).unwrap_or(Session(0));
if session.0 > Self::session(NetworkId::Serai) {
session_changed = true;
CurrentSession::<T>::set(NetworkId::Serai, session.0);
}
// update participants per session before the genesis
// after the genesis, we update them after reward distribution.
if (!genesis_ended) && session_changed {
Self::update_participants();
}
// We only want to distribute emissions if the genesis period is over AND the session has
// ended
if !(genesis_ended && session_changed) {
return Weight::zero(); // TODO
}
// figure out the amount of blocks in the last session
// Since the session has changed, we're now at least at session 1
let block_count = ValidatorSets::<T>::session_begin_block(NetworkId::Serai, session) -
ValidatorSets::<T>::session_begin_block(NetworkId::Serai, Session(session.0 - 1));
// 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;
let reward_this_epoch = if pre_ec_security {
// 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;
}
let distance = required - current;
distances.insert(n, distance);
total_distance = total_distance.saturating_add(distance);
}
// add serai network portion (20%)
let new_total_distance =
total_distance.saturating_mul(100) / (100 - SERAI_VALIDATORS_DESIRED_PERCENTAGE);
distances.insert(NetworkId::Serai, new_total_distance - total_distance);
total_distance = new_total_distance;
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
}
} else {
// post ec security
block_count * REWARD_PER_BLOCK
};
// map epoch ec-security-distance/volume to rewards
let (rewards_per_network, volume_per_network, volume_per_coin) = if pre_ec_security {
(
distances
.into_iter()
.map(|(n, distance)| {
// calculate how much each network gets based on distance to ec-security
let reward = u64::try_from(
u128::from(reward_this_epoch).saturating_mul(u128::from(distance)) /
u128::from(total_distance),
)
.unwrap();
(n, reward)
})
.collect::<BTreeMap<NetworkId, u64>>(),
None,
None,
)
} else {
// get swap volumes
let mut volume_per_coin: BTreeMap<Coin, 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);
let last_volume = LastSwapVolume::<T>::get(c).unwrap_or(0);
let vol_this_epoch = current_volume.saturating_sub(last_volume);
// update the current volume
LastSwapVolume::<T>::set(c, Some(current_volume));
volume_per_coin.insert(c, vol_this_epoch);
}
// aggregate per network
let mut total_volume = 0u64;
let mut volume_per_network: BTreeMap<NetworkId, u64> = BTreeMap::new();
for (c, vol) in &volume_per_coin {
volume_per_network.insert(
c.network(),
(*volume_per_network.get(&c.network()).unwrap_or(&0)).saturating_add(*vol),
);
total_volume = total_volume.saturating_add(*vol);
}
(
volume_per_network
.iter()
.map(|(n, vol)| {
// 20% of the reward goes to the Serai network and rest is distributed among others
// based on swap-volume.
let reward = if *n == NetworkId::Serai {
reward_this_epoch / 5
} else {
let reward = reward_this_epoch - (reward_this_epoch / 5);
// TODO: It is highly unlikely but what to do in case of 0 total volume?
if total_volume != 0 {
u64::try_from(
u128::from(reward).saturating_mul(u128::from(*vol)) / u128::from(total_volume),
)
.unwrap()
} else {
0
}
};
(*n, reward)
})
.collect::<BTreeMap<NetworkId, u64>>(),
Some(volume_per_network),
Some(volume_per_coin),
)
};
// distribute the rewards within the network
for (n, reward) in rewards_per_network {
let (validators_reward, network_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);
let distribution = unused_capacity.saturating_mul(ACCURACY_MULTIPLIER) / capacity;
let total = DESIRED_DISTRIBUTION.saturating_add(distribution);
let validators_reward = DESIRED_DISTRIBUTION.saturating_mul(reward) / total;
let network_pool_reward = reward.saturating_sub(validators_reward);
(validators_reward, network_pool_reward)
};
// distribute validators rewards
Self::distribute_to_validators(n, validators_reward);
// send the rest to the pool
if network_pool_reward != 0 {
// these should be available to unwrap if we have a network_pool_reward. Because that
// means we had an unused capacity hence in a post-ec era.
let vpn = volume_per_network.as_ref().unwrap();
let vpc = volume_per_coin.as_ref().unwrap();
for c in n.coins() {
let pool_reward = u64::try_from(
u128::from(network_pool_reward).saturating_mul(u128::from(vpc[c])) /
u128::from(vpn[&n]),
)
.unwrap();
if Coins::<T>::mint(
Dex::<T>::get_pool_account(*c),
Balance { coin: Coin::Serai, amount: Amount(pool_reward) },
)
.is_err()
{
// TODO: log the failure
continue;
}
}
}
}
// TODO: we have the past session participants here in the emissions pallet so that we can
// distribute rewards to them in the next session. Ideally we should be able to fetch this
// information from valiadtor sets pallet.
Self::update_participants();
Weight::zero() // TODO
}
}
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 {
#[cfg(feature = "fast-epoch")]
let initial_period_duration = FAST_EPOCH_INITIAL_PERIOD;
#[cfg(not(feature = "fast-epoch"))]
let initial_period_duration = 2 * MONTHS;
let genesis_complete_block = GenesisCompleteBlock::<T>::get();
genesis_complete_block.is_some() &&
(n.saturated_into::<u64>() < (genesis_complete_block.unwrap() + initial_period_duration))
}
/// 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;
}
if !Self::economic_security_reached(n) {
return true;
}
}
false
}
// Distribute the reward among network's set based on
// -> (key shares * stake per share) + ((stake % stake per share) / 2)
fn distribute_to_validators(n: NetworkId, reward: u64) {
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 / 2);
total_score = total_score.saturating_add(score);
scores.push((p, score));
}
// stake the rewards
for (p, score) in scores {
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) }).unwrap();
if ValidatorSets::<T>::distribute_block_rewards(n, p, Amount(p_reward)).is_err() {
// TODO: log the failure
continue;
}
}
}
pub fn swap_to_staked_sri(
to: PublicKey,
network: NetworkId,
balance: Balance,
) -> DispatchResult {
// check the network didn't reach the economic security yet
if Self::economic_security_reached(network) {
Err(Error::<T>::NetworkHasEconomicSecurity)?;
}
// swap half of the liquidity for SRI to form PoL.
let half = balance.amount.0 / 2;
let path = BoundedVec::try_from(vec![balance.coin, Coin::Serai]).unwrap();
let origin = RawOrigin::Signed(POL_ACCOUNT.into());
Dex::<T>::swap_exact_tokens_for_tokens(
origin.clone().into(),
path,
half,
1, // minimum out, so we accept whatever we get.
POL_ACCOUNT.into(),
)?;
// get how much we got for our swap
let sri_amount = Coins::<T>::balance(POL_ACCOUNT.into(), Coin::Serai).0;
// add liquidity
Dex::<T>::add_liquidity(
origin.clone().into(),
balance.coin,
half,
sri_amount,
1,
1,
POL_ACCOUNT.into(),
)?;
// use last block spot price to calculate how much SRI the balance makes.
let last_block = <frame_system::Pallet<T>>::block_number() - 1u32.into();
let value = Dex::<T>::spot_price_for_block(last_block, balance.coin)
.ok_or(Error::<T>::NoValueForCoin)?;
// TODO: may panic? It might be best for this math ops to return the result as is instead of
// doing an unwrap so that it can be properly dealt with.
let sri_amount = balance.amount.mul(value);
// Mint
Coins::<T>::mint(to, Balance { coin: Coin::Serai, amount: sri_amount })?;
// Stake the SRI for the network.
ValidatorSets::<T>::allocate(
frame_system::RawOrigin::Signed(to).into(),
network,
sri_amount,
)?;
Ok(())
}
fn update_participants() {
for n in NETWORKS {
let participants = ValidatorSets::<T>::participants_for_latest_decided_set(n)
.unwrap()
.into_iter()
.map(|(key, _)| (key, ValidatorSets::<T>::allocation((n, key)).unwrap_or(Amount(0)).0))
.collect::<Vec<_>>();
Participants::<T>::set(n, Some(participants.try_into().unwrap()));
}
}
}
}
pub use pallet::*;

View File

@@ -0,0 +1,23 @@
[package]
name = "serai-emissions-primitives"
version = "0.1.0"
description = "Serai emissions primitives"
license = "MIT"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/emissions/primitives"
authors = ["Akil Demir <aeg_asd@hotmail.com>"]
edition = "2021"
rust-version = "1.77"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[lints]
workspace = true
[dependencies]
serai-primitives = { path = "../../primitives", default-features = false }
[features]
std = ["serai-primitives/std"]
default = ["std"]

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Luke Parker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,26 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
use serai_primitives::{DAYS, YEARS, SeraiAddress, system_address};
// Protocol owned liquidity account.
pub const POL_ACCOUNT: SeraiAddress = system_address(b"Serai-protocol_owned_liquidity");
/// INITIAL_REWARD = 100,000 SRI / BLOCKS_PER_DAY for 60 days
pub const INITIAL_REWARD_PER_BLOCK: u64 = (100_000 * 10u64.pow(8)) / DAYS;
/// REWARD = 20M SRI / BLOCKS_PER_YEAR
pub const REWARD_PER_BLOCK: u64 = (20_000_000 * 10u64.pow(8)) / YEARS;
/// 20% of all stake desired to be for Serai network
pub const SERAI_VALIDATORS_DESIRED_PERCENTAGE: u64 = 20;
/// Desired unused capacity ratio for a network assuming capacity is 10,000.
pub const DESIRED_DISTRIBUTION: u64 = 1_000;
/// Percentage scale for the validator vs. pool reward distribution.
pub const ACCURACY_MULTIPLIER: u64 = 10_000;
/// The block to target for economic security
pub const SECURE_BY: u64 = YEARS;