Skip to content

Commit

Permalink
Scaffold futarchy pallet (#1378)
Browse files Browse the repository at this point in the history
* Scaffold futarchy pallet

* Scaffold test suite

* .

* .
  • Loading branch information
maltekliemann authored Oct 16, 2024
1 parent e586f66 commit 27b3db0
Show file tree
Hide file tree
Showing 14 changed files with 476 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ default-members = [
"zrml/authorized",
"zrml/combinatorial-tokens",
"zrml/court",
"zrml/futarchy",
"zrml/hybrid-router",
"zrml/global-disputes",
"zrml/market-commons",
Expand All @@ -39,6 +40,7 @@ members = [
"zrml/authorized",
"zrml/combinatorial-tokens",
"zrml/court",
"zrml/futarchy",
"zrml/hybrid-router",
"zrml/global-disputes",
"zrml/market-commons",
Expand Down Expand Up @@ -247,6 +249,7 @@ zeitgeist-macros = { path = "macros", default-features = false }
zeitgeist-primitives = { path = "primitives", default-features = false }
zrml-authorized = { path = "zrml/authorized", default-features = false }
zrml-combinatorial-tokens = { path = "zrml/combinatorial-tokens", default-features = false }
zrml-futarchy = { path = "zrml/futarchy", default-features = false }
zrml-court = { path = "zrml/court", default-features = false }
zrml-global-disputes = { path = "zrml/global-disputes", default-features = false }
zrml-hybrid-router = { path = "zrml/hybrid-router", default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions runtime/battery-station/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ zeitgeist-primitives = { workspace = true }
zrml-authorized = { workspace = true }
zrml-combinatorial-tokens = { workspace = true }
zrml-court = { workspace = true }
zrml-futarchy = { workspace = true }
zrml-global-disputes = { workspace = true, optional = true }
zrml-hybrid-router = { workspace = true }
zrml-market-commons = { workspace = true }
Expand Down Expand Up @@ -217,6 +218,7 @@ runtime-benchmarks = [
"zrml-authorized/runtime-benchmarks",
"zrml-combinatorial-tokens/runtime-benchmarks",
"zrml-court/runtime-benchmarks",
"zrml-futarchy/runtime-benchmarks",
"zrml-hybrid-router/runtime-benchmarks",
"zrml-neo-swaps/runtime-benchmarks",
"zrml-parimutuel/runtime-benchmarks",
Expand Down Expand Up @@ -331,6 +333,7 @@ std = [
"zrml-authorized/std",
"zrml-combinatorial-tokens/std",
"zrml-court/std",
"zrml-futarchy/std",
"zrml-hybrid-router/std",
"zrml-market-commons/std",
"zrml-neo-swaps/std",
Expand Down Expand Up @@ -386,6 +389,7 @@ try-runtime = [
"zrml-authorized/try-runtime",
"zrml-combinatorial-tokens/try-runtime",
"zrml-court/try-runtime",
"zrml-futarchy/try-runtime",
"zrml-hybrid-router/try-runtime",
"zrml-market-commons/try-runtime",
"zrml-neo-swaps/try-runtime",
Expand Down
8 changes: 8 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ macro_rules! create_runtime {
Parimutuel: zrml_parimutuel::{Call, Event<T>, Pallet, Storage} = 62,
HybridRouter: zrml_hybrid_router::{Call, Event<T>, Pallet, Storage} = 64,
CombinatorialTokens: zrml_combinatorial_tokens::{Call, Event<T>, Pallet, Storage} = 65,
Futarchy: zrml_futarchy::{Call, Event<T>, Pallet, Storage} = 66,

$($additional_pallets)*
}
Expand Down Expand Up @@ -1205,6 +1206,13 @@ macro_rules! impl_config_traits {
type WeightInfo = zrml_court::weights::WeightInfo<Runtime>;
}

impl zrml_futarchy::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type MultiCurrency = AssetManager;
type Preimages = Preimage;
type SubmitOrigin = EnsureRoot<AccountId>;
}

impl zrml_market_commons::Config for Runtime {
type Balance = Balance;
type MarketId = MarketId;
Expand Down
4 changes: 4 additions & 0 deletions runtime/zeitgeist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ zeitgeist-primitives = { workspace = true }
zrml-authorized = { workspace = true }
zrml-combinatorial-tokens = { workspace = true }
zrml-court = { workspace = true }
zrml-futarchy = { workspace = true }
zrml-global-disputes = { workspace = true, optional = true }
zrml-hybrid-router = { workspace = true }
zrml-market-commons = { workspace = true }
Expand Down Expand Up @@ -214,6 +215,7 @@ runtime-benchmarks = [
"zrml-authorized/runtime-benchmarks",
"zrml-combinatorial-tokens/runtime-benchmarks",
"zrml-court/runtime-benchmarks",
"zrml-futarchy/runtime-benchmarks",
"zrml-hybrid-router/runtime-benchmarks",
"zrml-neo-swaps/runtime-benchmarks",
"zrml-parimutuel/runtime-benchmarks",
Expand Down Expand Up @@ -320,6 +322,7 @@ std = [
"zrml-authorized/std",
"zrml-combinatorial-tokens/std",
"zrml-court/std",
"zrml-futarchy/std",
"zrml-hybrid-router/std",
"zrml-market-commons/std",
"zrml-neo-swaps/std",
Expand Down Expand Up @@ -374,6 +377,7 @@ try-runtime = [
"zrml-authorized/try-runtime",
"zrml-combinatorial-tokens/try-runtime",
"zrml-court/try-runtime",
"zrml-futarchy/try-runtime",
"zrml-hybrid-router/try-runtime",
"zrml-market-commons/try-runtime",
"zrml-neo-swaps/try-runtime",
Expand Down
58 changes: 58 additions & 0 deletions zrml/futarchy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[dependencies]
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
orml-traits = { workspace = true }
pallet-preimage = { workspace = true }
parity-scale-codec = { workspace = true, features = ["derive", "max-encoded-len"] }
scale-info = { workspace = true, features = ["derive"] }
sp-runtime = { workspace = true }
zeitgeist-primitives = { workspace = true }

# mock

env_logger = { workspace = true, optional = true }
orml-currencies = { workspace = true, optional = true }
orml-tokens = { workspace = true, optional = true }
pallet-balances = { workspace = true, optional = true }
pallet-timestamp = { workspace = true, optional = true }
sp-io = { workspace = true, optional = true }

[dev-dependencies]
test-case = { workspace = true }
zrml-futarchy = { workspace = true, features = ["default", "mock"] }

[features]
default = ["std"]
mock = [
"env_logger/default",
"orml-currencies/default",
"orml-tokens/default",
"sp-io/default",
"pallet-balances/default",
"pallet-timestamp/default",
"zeitgeist-primitives/mock",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"orml-traits/std",
"parity-scale-codec/std",
"sp-runtime/std",
"zeitgeist-primitives/std",
]
try-runtime = [
"frame-support/try-runtime",
]

[package]
authors = ["Zeitgeist PM <[email protected]>"]
edition.workspace = true
name = "zrml-futarchy"
version = "0.5.5"
1 change: 1 addition & 0 deletions zrml/futarchy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Futarchy Module
99 changes: 99 additions & 0 deletions zrml/futarchy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2024 Forecasting Technologies LTD.
//
// This file is part of Zeitgeist.
//
// Zeitgeist is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// Zeitgeist 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

pub mod mock;
mod tests;
mod traits;
pub mod types;

pub use pallet::*;

#[frame_support::pallet]
mod pallet {
use core::marker::PhantomData;
use frame_support::{
pallet_prelude::{EnsureOrigin, IsType, StorageVersion},
require_transactional,
traits::{QueryPreimage, StorePreimage},
transactional,
};
use frame_system::pallet_prelude::OriginFor;
use orml_traits::MultiCurrency;
use sp_runtime::DispatchResult;

#[pallet::config]
pub trait Config: frame_system::Config {
type MultiCurrency: MultiCurrency<Self::AccountId>;

// Preimage interface for acquiring call data.
type Preimages: QueryPreimage + StorePreimage;

type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

type SubmitOrigin: EnsureOrigin<Self::RuntimeOrigin>;

// // TODO
// // The origin from which proposals may be whitelisted.
// type WhitelistOrigin: EnsureOrigin<Self::RuntimeOrigin>;

// TODO Scheduler, EnactmentPeriod
}

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(PhantomData<T>);

pub(crate) type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub(crate) type BalanceOf<T> =
<<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;

pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

// TODO Storage Items

#[pallet::event]
#[pallet::generate_deposit(fn deposit_event)]
pub enum Event<T>
where
T: Config, {}

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

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[transactional]
#[pallet::weight({0})]
pub fn submit(origin: OriginFor<T>) -> DispatchResult {
T::SubmitOrigin::ensure_origin(origin)?;
Self::do_submit()
}
}

impl<T: Config> Pallet<T> {
#[require_transactional]
fn do_submit() -> DispatchResult {
Ok(())
}
}
}
72 changes: 72 additions & 0 deletions zrml/futarchy/src/mock/ext_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 Forecasting Technologies LTD.
//
// This file is part of Zeitgeist.
//
// Zeitgeist is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// Zeitgeist 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

use crate::mock::runtime::{Runtime, System};
use sp_io::TestExternalities;
use sp_runtime::BuildStorage;

#[cfg(feature = "parachain")]
use {crate::mock::consts::FOREIGN_ASSET, zeitgeist_primitives::types::CustomMetadata};

pub struct ExtBuilder;

impl ExtBuilder {
pub fn build() -> TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();

// See the logs in tests when using `RUST_LOG=debug cargo test -- --nocapture`
let _ = env_logger::builder().is_test(true).try_init();

pallet_balances::GenesisConfig::<Runtime> { balances: vec![] }
.assimilate_storage(&mut t)
.unwrap();

#[cfg(feature = "parachain")]
{
orml_tokens::GenesisConfig::<Runtime> { balances: vec![] }
.assimilate_storage(&mut t)
.unwrap();

let custom_metadata =
CustomMetadata { allow_as_base_asset: true, ..Default::default() };

orml_asset_registry::GenesisConfig::<Runtime> {
assets: vec![(
FOREIGN_ASSET,
AssetMetadata {
decimals: 18,
name: "MKL".as_bytes().to_vec().try_into().unwrap(),
symbol: "MKL".as_bytes().to_vec().try_into().unwrap(),
existential_deposit: 0,
location: None,
additional: custom_metadata,
}
.encode(),
)],
last_asset_id: FOREIGN_ASSET,
}
.assimilate_storage(&mut t)
.unwrap();
}

let mut test_ext: sp_io::TestExternalities = t.into();

test_ext.execute_with(|| System::set_block_number(1));

test_ext
}
}
Loading

0 comments on commit 27b3db0

Please sign in to comment.