Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement integration test for zrml-futarchy #1383

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

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

154 changes: 154 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,33 @@ macro_rules! create_common_tests {
mod common_tests {
common_runtime::fee_tests!();

mod utility {
use crate::{Balances, BlockNumber, Futarchy, Preimage, Scheduler, System};
use frame_support::traits::Hooks;

// Beware! This only advances certain pallets.
pub(crate) fn run_to_block(to: BlockNumber) {
while System::block_number() < to {
let now = System::block_number();

Futarchy::on_finalize(now);
Balances::on_finalize(now);
Preimage::on_finalize(now);
Scheduler::on_finalize(now);
System::on_finalize(now);

let next = now + 1;
System::set_block_number(next);

System::on_initialize(next);
Scheduler::on_initialize(next);
Preimage::on_initialize(next);
Balances::on_initialize(next);
Futarchy::on_initialize(next);
}
}
}

mod dust_removal {
use crate::*;
use frame_support::PalletId;
Expand Down Expand Up @@ -2246,6 +2273,133 @@ macro_rules! create_common_tests {
});
}
}

mod futarchy {
use crate::{
common_tests::utility, AccountId, Asset, AssetManager, Balance, Balances,
Futarchy, MarketId, NeoSwaps, PredictionMarkets, Preimage, Runtime,
RuntimeCall, RuntimeOrigin, Scheduler, System,
};
use frame_support::{assert_ok, dispatch::RawOrigin, traits::StorePreimage};
use orml_traits::MultiCurrency;
use sp_runtime::{
traits::{Hash, Zero},
BuildStorage, Perbill,
};
use zeitgeist_primitives::{
math::fixed::{BaseProvider, ZeitgeistBase},
traits::MarketBuilderTrait,
types::{
Deadlines, MarketCreation, MarketPeriod, MarketType, MultiHash, ScoringRule,
},
};
use zrml_futarchy::types::Proposal;
use zrml_market_commons::types::MarketBuilder;
use zrml_neo_swaps::types::DecisionMarketOracle;

#[test]
fn futarchy_schedules_and_executes_call() {
let mut t: sp_io::TestExternalities =
frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
t.execute_with(|| {
let alice = AccountId::from([0u8; 32]);

let collateral: Asset<MarketId> = Asset::Ztg;
let one: Balance = ZeitgeistBase::get().unwrap();
let total_cost: Balance = one.saturating_mul(100_000u128);
assert_ok!(AssetManager::deposit(collateral, &alice, total_cost));

let mut metadata = [0x01; 50];
metadata[0] = 0x15;
metadata[1] = 0x30;
let multihash = MultiHash::Sha3_384(metadata);

let oracle_duration =
<Runtime as zrml_prediction_markets::Config>::MinOracleDuration::get();
let deadlines = Deadlines {
grace_period: Default::default(),
oracle_duration,
dispute_duration: Zero::zero(),
};
assert_ok!(PredictionMarkets::create_market(
RuntimeOrigin::signed(alice.clone()),
collateral,
Perbill::zero(),
alice.clone(),
MarketPeriod::Block(0..999),
deadlines,
multihash,
MarketCreation::Permissionless,
MarketType::Categorical(2),
None,
ScoringRule::AmmCdaHybrid,
));

let market_id = 0;
let amount = one * 100u128;
assert_ok!(PredictionMarkets::buy_complete_set(
RuntimeOrigin::signed(alice.clone()),
market_id,
amount,
));

assert_ok!(NeoSwaps::deploy_pool(
RuntimeOrigin::signed(alice.clone()),
market_id,
amount,
vec![one / 10u128 * 9u128, one / 10u128],
one / 100,
));

let duration = <Runtime as zrml_futarchy::Config>::MinDuration::get();

// Wrap `remark_with_event` call in `dispatch_as` so that it doesn't error
// with `BadOrigin`.
let bob = AccountId::from([0x01; 32]);
let remark = b"hullo".to_vec();
let remark_dispatched_as = pallet_utility::Call::<Runtime>::dispatch_as {
as_origin: Box::new(RawOrigin::Signed(bob.clone()).into()),
call: Box::new(
frame_system::Call::remark_with_event { remark: remark.clone() }
.into(),
),
};
let call =
Preimage::bound(RuntimeCall::from(remark_dispatched_as)).unwrap();
let oracle = DecisionMarketOracle::new(
market_id,
Asset::CategoricalOutcome(market_id, 0),
Asset::CategoricalOutcome(market_id, 1),
);
let when = duration + 10;
let proposal = Proposal { when, call, oracle };

assert_ok!(Futarchy::submit_proposal(
RawOrigin::Root.into(),
duration,
proposal.clone()
));

utility::run_to_block(when);

let hash = <Runtime as frame_system::Config>::Hashing::hash(&remark);
System::assert_has_event(
frame_system::Event::<Runtime>::Remarked { sender: bob, hash }.into(),
);
System::assert_has_event(
pallet_scheduler::Event::<Runtime>::Dispatched {
task: (when, 0),
id: None,
result: Ok(()),
}
.into(),
);
});
}
}
}
};
}
4 changes: 0 additions & 4 deletions zrml/futarchy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ zeitgeist-primitives = { workspace = true }

env_logger = { workspace = true, optional = true }
pallet-balances = { workspace = true, optional = true }
pallet-preimage = { workspace = true, optional = true }
pallet-scheduler = { workspace = true, optional = true }
sp-io = { workspace = true, optional = true }

[dev-dependencies]
Expand All @@ -25,8 +23,6 @@ mock = [
"env_logger/default",
"sp-io/default",
"pallet-balances/default",
"pallet-preimage/default",
"pallet-scheduler/default",
"zeitgeist-primitives/mock",
]
runtime-benchmarks = [
Expand Down
14 changes: 0 additions & 14 deletions zrml/futarchy/src/mock/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@ use crate::mock::types::MockBenchmarkHelper;
parameter_types! {
// zrml-futarchy
pub const MinDuration: BlockNumber = 10;

// pallet-preimage
pub const PreimageBaseDeposit: Balance = 0;
pub const PreimageByteDeposit: Balance = 0;
}

construct_runtime! {
pub enum Runtime {
System: frame_system,
Balances: pallet_balances,
Preimage: pallet_preimage,
Futarchy: zrml_futarchy,
}
}
Expand Down Expand Up @@ -91,15 +86,6 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = ();
}

impl pallet_preimage::Config for Runtime {
type WeightInfo = ();
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<<Runtime as frame_system::Config>::AccountId>;
type BaseDeposit = PreimageBaseDeposit;
type ByteDeposit = PreimageByteDeposit;
}

impl zrml_futarchy::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = MockBenchmarkHelper;
Expand Down
2 changes: 2 additions & 0 deletions zrml/futarchy/src/mock/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

#[cfg(feature = "runtime-benchmarks")]
mod benchmark_helper;
mod oracle;
mod scheduler;

#[cfg(feature = "runtime-benchmarks")]
pub use benchmark_helper::MockBenchmarkHelper;
pub(crate) use oracle::MockOracle;
pub(crate) use scheduler::MockScheduler;
4 changes: 1 addition & 3 deletions zrml/futarchy/src/mock/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// 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::{Balances, Futarchy, Preimage, System};
use crate::mock::runtime::{Balances, Futarchy, System};
use frame_support::traits::Hooks;
use zeitgeist_primitives::types::BlockNumber;

Expand All @@ -24,7 +24,6 @@ pub fn run_to_block(to: BlockNumber) {
let now = System::block_number();

Futarchy::on_finalize(now);
Preimage::on_finalize(now);
Balances::on_finalize(now);
System::on_finalize(now);

Expand All @@ -33,7 +32,6 @@ pub fn run_to_block(to: BlockNumber) {

System::on_initialize(next);
Balances::on_initialize(next);
Preimage::on_initialize(next);
Futarchy::on_initialize(next);
}
}
2 changes: 1 addition & 1 deletion zrml/hybrid-router/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl frame_system::Config for Runtime {
type Hashing = BlakeTwo256;
type Lookup = IdentityLookup<Self::AccountId>;
type Nonce = u64;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type MaxConsumers = ConstU32<16>;
type OnKilledAccount = ();
type OnNewAccount = ();
type RuntimeOrigin = RuntimeOrigin;
Expand Down
2 changes: 0 additions & 2 deletions zrml/neo-swaps/src/tests/combo_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

use super::*;
#[cfg(not(feature = "parachain"))]
use sp_runtime::{DispatchError, TokenError};
use test_case::test_case;
use zeitgeist_primitives::types::Asset::CategoricalOutcome;

Expand Down