From 0a76d861551cf8fc5d322eb1290e20687b46f17c Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Tue, 16 Jan 2024 21:25:30 +0100 Subject: [PATCH] Adjust prediction markets tests --- Cargo.lock | 2 + primitives/src/constants/mock.rs | 21 ++++- zrml/prediction-markets/Cargo.toml | 6 +- zrml/prediction-markets/src/mock.rs | 125 +++++++++++++++++++++++---- zrml/prediction-markets/src/tests.rs | 44 +++++----- 5 files changed, 157 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f6bbad3d..0c96890d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14755,6 +14755,7 @@ dependencies = [ "orml-currencies", "orml-tokens", "orml-traits", + "pallet-assets", "pallet-balances", "pallet-randomness-collective-flip", "pallet-timestamp", @@ -14770,6 +14771,7 @@ dependencies = [ "test-case", "xcm", "zeitgeist-primitives", + "zrml-asset-router", "zrml-authorized", "zrml-court", "zrml-global-disputes", diff --git a/primitives/src/constants/mock.rs b/primitives/src/constants/mock.rs index 8f8c49b9b..5d044f42d 100644 --- a/primitives/src/constants/mock.rs +++ b/primitives/src/constants/mock.rs @@ -21,12 +21,22 @@ pub use super::*; use crate::{ assets::Asset, - types::{Assets, Balance, Moment}, + types::{Assets, Balance, Moment, Currencies}, }; use frame_support::{parameter_types, traits::LockIdentifier, PalletId}; use orml_traits::parameter_type_with_key; use sp_arithmetic::Perbill; +// Assets +parameter_types! { + pub const AssetsAccountDeposit: Balance = 0; + pub const AssetsApprovalDeposit: Balance = 0; + pub const AssetsDeposit: Balance = 0; + pub const AssetsStringLimit: u32 = 256; + pub const AssetsMetadataDepositBase: Balance = 0; + pub const AssetsMetadataDepositPerByte: Balance = 0; +} + // Authorized parameter_types! { pub const AuthorizedPalletId: PalletId = PalletId(*b"zge/atzd"); @@ -164,6 +174,7 @@ parameter_types! { pub const GetNativeCurrencyId: Assets = Asset::Ztg; } +// Will be removed once asset-system is completely integerated parameter_type_with_key! { pub ExistentialDeposits: |currency_id: Assets| -> Balance { match currency_id { @@ -173,6 +184,14 @@ parameter_type_with_key! { }; } +parameter_type_with_key! { + pub ExistentialDepositsNew: |currency_id: Currencies| -> Balance { + match currency_id { + _ => 1 + } + }; +} + // System parameter_types! { pub const BlockHashCount: u64 = 250; diff --git a/zrml/prediction-markets/Cargo.toml b/zrml/prediction-markets/Cargo.toml index d88f30b77..34f42a102 100644 --- a/zrml/prediction-markets/Cargo.toml +++ b/zrml/prediction-markets/Cargo.toml @@ -21,6 +21,7 @@ zrml-simple-disputes = { workspace = true } orml-asset-registry = { workspace = true, optional = true } orml-currencies = { workspace = true, optional = true } orml-tokens = { workspace = true, optional = true } +pallet-assets = { workspace = true } pallet-balances = { workspace = true, optional = true } pallet-randomness-collective-flip = { workspace = true, optional = true } pallet-timestamp = { workspace = true, optional = true } @@ -29,6 +30,7 @@ sp-api = { workspace = true, optional = true } sp-io = { workspace = true, optional = true } substrate-fixed = { workspace = true, optional = true } xcm = { workspace = true, optional = true } +zrml-asset-router = { workspace = true, optional = true } zrml-prediction-markets-runtime-api = { workspace = true, optional = true } zrml-rikiddo = { workspace = true, optional = true } zrml-swaps = { workspace = true, optional = true } @@ -41,8 +43,10 @@ zrml-prediction-markets = { workspace = true, features = ["mock", "default"] } [features] default = ["std"] mock = [ + "orml-asset-registry/default", "orml-currencies/default", "orml-tokens/default", + "pallet-assets/default", "pallet-balances", "pallet-randomness-collective-flip/default", "pallet-timestamp/default", @@ -52,11 +56,11 @@ mock = [ "sp-io/default", "substrate-fixed", "zeitgeist-primitives/mock", + "zrml-asset-router/default", "zrml-prediction-markets-runtime-api/default", "zrml-rikiddo/default", "zrml-swaps/default", "xcm/default", - "orml-asset-registry/default", ] parachain = [] runtime-benchmarks = [ diff --git a/zrml/prediction-markets/src/mock.rs b/zrml/prediction-markets/src/mock.rs index fcb9f71fa..0ba3c46b7 100644 --- a/zrml/prediction-markets/src/mock.rs +++ b/zrml/prediction-markets/src/mock.rs @@ -24,18 +24,19 @@ use crate as prediction_markets; use frame_support::{ - construct_runtime, ord_parameter_types, parameter_types, - traits::{Everything, NeverEnsureOrigin, OnFinalize, OnInitialize}, + construct_runtime, ord_parameter_types, parameter_types, + traits::{Everything, NeverEnsureOrigin, OnFinalize, OnInitialize, AsEnsureOriginWithArg}, }; -use frame_system::{EnsureRoot, EnsureSignedBy}; +use frame_system::{EnsureRoot, EnsureSignedBy, EnsureSigned}; #[cfg(feature = "parachain")] use orml_asset_registry::AssetMetadata; use sp_arithmetic::per_things::Percent; use sp_runtime::{ testing::Header, - traits::{BlakeTwo256, IdentityLookup}, + traits::{ConstU32, BlakeTwo256, IdentityLookup}, DispatchError, DispatchResult, }; +use parity_scale_codec::Compact; use std::cell::RefCell; use substrate_fixed::{types::extra::U33, FixedI128, FixedU128}; use zeitgeist_primitives::{ @@ -44,7 +45,7 @@ use zeitgeist_primitives::{ BalanceFractionalDecimals, BlockHashCount, BlocksPerYear, CloseEarlyBlockPeriod, CloseEarlyDisputeBond, CloseEarlyProtectionBlockPeriod, CloseEarlyProtectionTimeFramePeriod, CloseEarlyRequestBond, CloseEarlyTimeFramePeriod, - CorrectionPeriod, CourtPalletId, ExistentialDeposit, ExistentialDeposits, ExitFee, + CorrectionPeriod, CourtPalletId, ExistentialDeposit, ExistentialDepositsNew, ExitFee, GdVotingPeriod, GetNativeCurrencyId, GlobalDisputeLockId, GlobalDisputesPalletId, InflationPeriod, LiquidityMiningPalletId, LockId, MaxAppeals, MaxApprovals, MaxAssets, MaxCategories, MaxCourtParticipants, MaxCreatorFee, MaxDelegations, MaxDisputeDuration, @@ -55,12 +56,12 @@ use zeitgeist_primitives::{ MinOutcomeVoteAmount, MinSubsidy, MinSubsidyPeriod, MinWeight, MinimumPeriod, OutcomeBond, OutcomeFactor, OutsiderBond, PmPalletId, RemoveKeysLimit, RequestInterval, SimpleDisputesPalletId, SwapsPalletId, TreasuryPalletId, VotePeriod, VotingOutcomeFee, - BASE, CENT, MILLISECS_PER_BLOCK, + BASE, CENT, MILLISECS_PER_BLOCK, AssetsApprovalDeposit, AssetsAccountDeposit, AssetsDeposit, AssetsMetadataDepositBase, AssetsMetadataDepositPerByte, AssetsStringLimit, }, traits::DeployPoolApi, types::{ - AccountIdTest, Amount, Asset, Assets, Balance, BasicCurrencyAdapter, BlockNumber, - BlockTest, Hash, Index, MarketId, Moment, PoolId, UncheckedExtrinsicTest, + AccountIdTest, Amount, Assets, Currencies, Balance, BasicCurrencyAdapter, BlockNumber, + BlockTest, Hash, Index, MarketId, Moment, PoolId, UncheckedExtrinsicTest, CampaignAsset, CustomAsset, MarketAsset, CustomAssetId, CampaignAssetId }, }; use zrml_rikiddo::types::{EmaMarketVolume, FeeSigmoid, RikiddoSigmoidMV}; @@ -157,6 +158,10 @@ parameter_types! { pub const DisputeBond: Balance = 109 * CENT; } +type CustomAssetsInstance = pallet_assets::Instance1; +type CampaignAssetsInstance = pallet_assets::Instance2; +type MarketAssetsInstance = pallet_assets::Instance3; + construct_runtime!( pub enum Runtime where @@ -164,11 +169,15 @@ construct_runtime!( NodeBlock = BlockTest, UncheckedExtrinsic = UncheckedExtrinsicTest, { + AssetRouter: zrml_asset_router::{Pallet}, Authorized: zrml_authorized::{Event, Pallet, Storage}, Balances: pallet_balances::{Call, Config, Event, Pallet, Storage}, + CampaignAssets: pallet_assets::::{Call, Pallet, Storage, Event}, + CustomAssets: pallet_assets::::{Call, Pallet, Storage, Event}, Court: zrml_court::{Event, Pallet, Storage}, AssetManager: orml_currencies::{Call, Pallet, Storage}, LiquidityMining: zrml_liquidity_mining::{Config, Event, Pallet}, + MarketAssets: pallet_assets::::{Call, Pallet, Storage, Event}, MarketCommons: zrml_market_commons::{Pallet, Storage}, PredictionMarkets: prediction_markets::{Event, Pallet, Storage}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage}, @@ -263,7 +272,7 @@ impl frame_system::Config for Runtime { impl orml_currencies::Config for Runtime { type GetNativeCurrencyId = GetNativeCurrencyId; - type MultiCurrency = Tokens; + type MultiCurrency = AssetRouter; type NativeCurrency = BasicCurrencyAdapter; type WeightInfo = (); } @@ -271,10 +280,10 @@ impl orml_currencies::Config for Runtime { impl orml_tokens::Config for Runtime { type Amount = Amount; type Balance = Balance; - type CurrencyId = Assets; + type CurrencyId = Currencies; type DustRemovalWhitelist = Everything; type RuntimeEvent = RuntimeEvent; - type ExistentialDeposits = ExistentialDeposits; + type ExistentialDeposits = ExistentialDepositsNew; type MaxLocks = (); type MaxReserves = MaxReserves; type CurrencyHooks = (); @@ -285,7 +294,7 @@ impl orml_tokens::Config for Runtime { #[cfg(feature = "parachain")] crate::orml_asset_registry::impl_mock_registry! { MockRegistry, - Assets, + Currencies, Balance, zeitgeist_primitives::types::CustomMetadata } @@ -315,6 +324,88 @@ ord_parameter_types! { pub const AuthorizedDisputeResolutionUser: AccountIdTest = ALICE; } +impl pallet_assets::Config for Runtime { + type ApprovalDeposit = AssetsApprovalDeposit; + type AssetAccountDeposit = AssetsAccountDeposit; + type AssetDeposit = AssetsDeposit; + type AssetId = CampaignAsset; + type AssetIdParameter = Compact; + type Balance = Balance; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = (); + type CallbackHandle = (); + type CreateOrigin = AsEnsureOriginWithArg>; + type Currency = Balances; + type Extra = (); + type ForceOrigin = EnsureRoot; + type Freezer = (); + type MetadataDepositBase = AssetsMetadataDepositBase; + type MetadataDepositPerByte = AssetsMetadataDepositPerByte; + type RemoveItemsLimit = ConstU32<50>; + type RuntimeEvent = RuntimeEvent; + type StringLimit = AssetsStringLimit; + type WeightInfo = (); +} + +impl pallet_assets::Config for Runtime { + type ApprovalDeposit = AssetsApprovalDeposit; + type AssetAccountDeposit = AssetsAccountDeposit; + type AssetDeposit = AssetsDeposit; + type AssetId = CustomAsset; + type AssetIdParameter = Compact; + type Balance = Balance; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = (); + type CallbackHandle = (); + type CreateOrigin = AsEnsureOriginWithArg>; + type Currency = Balances; + type Extra = (); + type ForceOrigin = EnsureRoot; + type Freezer = (); + type MetadataDepositBase = AssetsMetadataDepositBase; + type MetadataDepositPerByte = AssetsMetadataDepositPerByte; + type RemoveItemsLimit = ConstU32<50>; + type RuntimeEvent = RuntimeEvent; + type StringLimit = AssetsStringLimit; + type WeightInfo = (); +} + +impl pallet_assets::Config for Runtime { + type ApprovalDeposit = AssetsApprovalDeposit; + type AssetAccountDeposit = AssetsAccountDeposit; + type AssetDeposit = AssetsDeposit; + type AssetId = MarketAsset; + type AssetIdParameter = MarketAsset; + type Balance = Balance; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = (); + type CallbackHandle = (); + type CreateOrigin = AsEnsureOriginWithArg>; + type Currency = Balances; + type Extra = (); + type ForceOrigin = EnsureRoot; + type Freezer = (); + type MetadataDepositBase = AssetsMetadataDepositBase; + type MetadataDepositPerByte = AssetsMetadataDepositPerByte; + type RemoveItemsLimit = ConstU32<50>; + type RuntimeEvent = RuntimeEvent; + type StringLimit = AssetsStringLimit; + type WeightInfo = (); +} + +impl zrml_asset_router::Config for Runtime { + type AssetType = Assets; + type Balance = Balance; + type CurrencyType = Currencies; + type Currencies = Tokens; + type CampaignAssetType = CampaignAsset; + type CampaignAssets = CampaignAssets; + type CustomAssetType = CustomAsset; + type CustomAssets = CustomAssets; + type MarketAssetType = MarketAsset; + type MarketAssets = MarketAssets; +} + impl zrml_authorized::Config for Runtime { type AuthorizedDisputeResolutionOrigin = EnsureSignedBy; @@ -488,7 +579,7 @@ impl ExtBuilder { #[cfg(feature = "parachain")] orml_tokens::GenesisConfig:: { balances: (0..69) - .map(|idx| (idx, Assets::ForeignAsset(100), INITIAL_BALANCE)) + .map(|idx| (idx, Currencies::ForeignAsset(100), INITIAL_BALANCE)) .collect(), } .assimilate_storage(&mut t) @@ -502,7 +593,7 @@ impl ExtBuilder { orml_asset_registry_mock::GenesisConfig { metadata: vec![ ( - Assets::ForeignAsset(100), + Currencies::ForeignAsset(100), AssetMetadata { decimals: 18, name: "ACALA USD".as_bytes().to_vec(), @@ -513,7 +604,7 @@ impl ExtBuilder { }, ), ( - Assets::ForeignAsset(420), + Currencies::ForeignAsset(420), AssetMetadata { decimals: 18, name: "FANCY_TOKEN".as_bytes().to_vec(), @@ -558,8 +649,8 @@ pub fn set_timestamp_for_on_initialize(time: Moment) { sp_api::mock_impl_runtime_apis! { impl zrml_prediction_markets_runtime_api::PredictionMarketsApi, MarketId, Hash> for Runtime { - fn market_outcome_share_id(_: MarketId, _: u16) -> Asset { - Asset::PoolShare(1) + fn market_outcome_share_id(_: MarketId, _: u16) -> Assets { + Assets::PoolShare(1) } } } diff --git a/zrml/prediction-markets/src/tests.rs b/zrml/prediction-markets/src/tests.rs index 0fbe22b84..b8b928e1e 100644 --- a/zrml/prediction-markets/src/tests.rs +++ b/zrml/prediction-markets/src/tests.rs @@ -1535,7 +1535,7 @@ fn it_allows_to_buy_a_complete_set() { // Check the outcome balances let assets = PredictionMarkets::outcome_assets(0, &market); for asset in assets.iter() { - let bal = Tokens::free_balance(*asset, &BOB); + let bal = AssetManager::free_balance(*asset, &BOB); assert_eq!(bal, CENT); } @@ -1817,7 +1817,7 @@ fn it_allows_to_sell_a_complete_set() { // Check the outcome balances let assets = PredictionMarkets::outcome_assets(0, &market); for asset in assets.iter() { - let bal = Tokens::free_balance(*asset, &BOB); + let bal = AssetManager::free_balance(*asset, &BOB); assert_eq!(bal, 0); } @@ -3312,22 +3312,22 @@ fn it_correctly_resolves_a_market_that_was_reported_on() { // Check balance of winning outcome asset. let share_b = Asset::CategoricalOutcome(0, 1); - let share_b_total = Tokens::total_issuance(share_b); + let share_b_total = AssetManager::total_issuance(share_b); assert_eq!(share_b_total, CENT); - let share_b_bal = Tokens::free_balance(share_b, &CHARLIE); + let share_b_bal = AssetManager::free_balance(share_b, &CHARLIE); assert_eq!(share_b_bal, CENT); // TODO(#792): Remove other assets. let share_a = Asset::CategoricalOutcome(0, 0); - let share_a_total = Tokens::total_issuance(share_a); + let share_a_total = AssetManager::total_issuance(share_a); assert_eq!(share_a_total, CENT); - let share_a_bal = Tokens::free_balance(share_a, &CHARLIE); + let share_a_bal = AssetManager::free_balance(share_a, &CHARLIE); assert_eq!(share_a_bal, CENT); let share_c = Asset::CategoricalOutcome(0, 2); - let share_c_total = Tokens::total_issuance(share_c); + let share_c_total = AssetManager::total_issuance(share_c); assert_eq!(share_c_total, 0); - let share_c_bal = Tokens::free_balance(share_c, &CHARLIE); + let share_c_bal = AssetManager::free_balance(share_c, &CHARLIE); assert_eq!(share_c_bal, 0); assert!(market.bonds.creation.unwrap().is_settled); @@ -4034,15 +4034,15 @@ fn create_market_and_deploy_assets_results_in_expected_balances_and_pool_params( let market_id = 0; let pool_account = Swaps::pool_account_id(&pool_id); - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 0), &ALICE), 0); - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 1), &ALICE), 0); - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 2), &ALICE), 0); - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 3), &ALICE), 0); - - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 0), &pool_account), amount); - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 1), &pool_account), amount); - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 2), &pool_account), amount); - assert_eq!(Tokens::free_balance(Asset::CategoricalOutcome(0, 3), &pool_account), amount); + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 0), &ALICE), 0); + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 1), &ALICE), 0); + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 2), &ALICE), 0); + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 3), &ALICE), 0); + + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 0), &pool_account), amount); + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 1), &pool_account), amount); + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 2), &pool_account), amount); + assert_eq!(AssetManager::free_balance(Asset::CategoricalOutcome(0, 3), &pool_account), amount); assert_eq!(AssetManager::free_balance(base_asset, &pool_account), amount); let pool = Pools::::get(0).unwrap(); @@ -4449,7 +4449,7 @@ fn full_scalar_market_lifecycle() { let assets = PredictionMarkets::outcome_assets(0, &MarketCommons::market(&0).unwrap()); assert_eq!(assets.len(), 2); for asset in assets.iter() { - let bal = Tokens::free_balance(*asset, &CHARLIE); + let bal = AssetManager::free_balance(*asset, &CHARLIE); assert_eq!(bal, 100 * BASE); } let market = MarketCommons::market(&0).unwrap(); @@ -4492,7 +4492,7 @@ fn full_scalar_market_lifecycle() { assert_eq!(disputes.len(), 0); // give EVE some shares - assert_ok!(Tokens::transfer( + assert_ok!(AssetManager::transfer( RuntimeOrigin::signed(CHARLIE), EVE, Asset::ScalarOutcome(0, ScalarPosition::Short), @@ -4500,13 +4500,13 @@ fn full_scalar_market_lifecycle() { )); assert_eq!( - Tokens::free_balance(Asset::ScalarOutcome(0, ScalarPosition::Short), &CHARLIE), + AssetManager::free_balance(Asset::ScalarOutcome(0, ScalarPosition::Short), &CHARLIE), 50 * BASE ); assert_ok!(PredictionMarkets::redeem_shares(RuntimeOrigin::signed(CHARLIE), 0)); for asset in assets.iter() { - let bal = Tokens::free_balance(*asset, &CHARLIE); + let bal = AssetManager::free_balance(*asset, &CHARLIE); assert_eq!(bal, 0); } @@ -6372,7 +6372,7 @@ fn scalar_market_correctly_resolves_common(base_asset: Asset, reported ScoringRule::CPMM, ); assert_ok!(PredictionMarkets::buy_complete_set(RuntimeOrigin::signed(CHARLIE), 0, 100 * BASE)); - assert_ok!(Tokens::transfer( + assert_ok!(AssetManager::transfer( RuntimeOrigin::signed(CHARLIE), EVE, Asset::ScalarOutcome(0, ScalarPosition::Short),