-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move economic security into it's own pallet
- Loading branch information
Showing
12 changed files
with
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use serai_primitives::NetworkId; | ||
|
||
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)] | ||
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub enum Event { | ||
EconomicSecurityBlock { network: NetworkId, block: u64 }, | ||
} |
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,48 @@ | ||
[package] | ||
name = "serai-economic-security-pallet" | ||
version = "0.1.0" | ||
description = "Economic Security pallet for Serai" | ||
license = "AGPL-3.0-only" | ||
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/economic-security/pallet" | ||
authors = ["Akil Demir <[email protected]>"] | ||
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 } | ||
|
||
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false } | ||
coins-pallet = { package = "serai-coins-pallet", path = "../../coins/pallet", default-features = false } | ||
|
||
serai-primitives = { path = "../../primitives", default-features = false } | ||
|
||
[features] | ||
std = [ | ||
"scale/std", | ||
"scale-info/std", | ||
|
||
"frame-system/std", | ||
"frame-support/std", | ||
|
||
"dex-pallet/std", | ||
"coins-pallet/std", | ||
|
||
"serai-primitives/std", | ||
] | ||
try-runtime = [] # TODO | ||
|
||
default = ["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,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/>. |
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,56 @@ | ||
#![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 frame_system::pallet_prelude::*; | ||
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion}; | ||
|
||
use dex_pallet::{Config as DexConfig, Pallet as Dex}; | ||
use coins_pallet::{Config as CoinsConfig, AllowMint}; | ||
|
||
use serai_primitives::*; | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config + CoinsConfig + DexConfig { | ||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
} | ||
|
||
#[pallet::event] | ||
#[pallet::generate_deposit(fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
EconomicSecurityBlock { network: NetworkId, block: u64 }, | ||
} | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(PhantomData<T>); | ||
|
||
#[pallet::storage] | ||
#[pallet::getter(fn economic_security_block)] | ||
pub(crate) type EconomicSecurityBlock<T: Config> = | ||
StorageMap<_, Identity, NetworkId, BlockNumberFor<T>, OptionQuery>; | ||
|
||
#[pallet::hooks] | ||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { | ||
fn on_initialize(n: BlockNumberFor<T>) -> Weight { | ||
// we accept we reached economic security once we can mint smallest amount of a network's coin | ||
for coin in COINS { | ||
let existing = EconomicSecurityBlock::<T>::get(coin.network()); | ||
if existing.is_none() && | ||
Dex::<T>::security_oracle_value(coin).is_some() && | ||
<T as CoinsConfig>::AllowMint::is_allowed(&Balance { coin, amount: Amount(1) }) | ||
{ | ||
EconomicSecurityBlock::<T>::set(coin.network(), Some(n)); | ||
Self::deposit_event(Event::EconomicSecurityBlock { | ||
network: coin.network(), | ||
block: n.saturated_into::<u64>(), | ||
}); | ||
} | ||
} | ||
|
||
Weight::zero() // TODO | ||
} | ||
} | ||
} | ||
|
||
pub use pallet::*; |
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.