diff --git a/Cargo.lock b/Cargo.lock index bc189a2f..252f2288 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -810,7 +810,7 @@ checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -968,7 +968,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -1465,7 +1465,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2159,7 +2159,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2501,7 +2501,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2541,7 +2541,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2558,7 +2558,7 @@ checksum = "b404f596046b0bb2d903a9c786b875a126261b52b7c3a64bbb66382c41c771df" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2650,7 +2650,7 @@ checksum = "d65d7ce8132b7c0e54497a4d9a55a1c2a0912a0d786cf894472ba818fba45762" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2752,7 +2752,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2798,7 +2798,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.57", + "syn 2.0.58", "termcolor", "toml 0.8.12", "walkdir", @@ -2977,7 +2977,7 @@ checksum = "5c785274071b1b420972453b306eeca06acf4633829db4223b58a2a8c5953bc4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -2988,7 +2988,7 @@ checksum = "6fd000fd6988e73bbe993ea3db9b1aa64906ab88766d654973924340c8cddb42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -3117,7 +3117,7 @@ dependencies = [ "prettier-please", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -3175,7 +3175,7 @@ dependencies = [ [[package]] name = "fc-pallet-referenda-tracks" version = "1.0.0" -source = "git+https://github.com/virto-network/frame-contrib?branch=main#329d454b33170d81b4de8939c4135d4d8d1ae4a2" +source = "git+https://github.com/virto-network/frame-contrib?branch=main#c23a2b639c67d7775e7bd388639e87bf7081cafc" dependencies = [ "frame-benchmarking", "frame-support", @@ -3193,7 +3193,7 @@ dependencies = [ [[package]] name = "fc-traits-memberships" version = "0.1.0" -source = "git+https://github.com/virto-network/frame-contrib?branch=main#329d454b33170d81b4de8939c4135d4d8d1ae4a2" +source = "git+https://github.com/virto-network/frame-contrib?branch=main#c23a2b639c67d7775e7bd388639e87bf7081cafc" dependencies = [ "frame-support", "parity-scale-codec", @@ -3425,7 +3425,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -3553,7 +3553,7 @@ dependencies = [ "proc-macro2", "quote", "sp-core-hashing", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -3565,7 +3565,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -3575,7 +3575,7 @@ source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -3754,7 +3754,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -3934,9 +3934,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fbd2820c5e49886948654ab546d0688ff24530286bdcf8fca3cefb16d4618eb" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" dependencies = [ "bytes", "fnv", @@ -5465,7 +5465,7 @@ dependencies = [ "macro_magic_core", "macro_magic_macros", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -5479,7 +5479,7 @@ dependencies = [ "macro_magic_core_macros", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -5490,7 +5490,7 @@ checksum = "9ea73aa640dc01d62a590d48c0c3521ed739d53b27f919b25c3551e233481654" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -5501,7 +5501,7 @@ checksum = "ef9d79ae96aaba821963320eb2b6e34d17df1e5a83d8a1985c29cc5be59577b3" dependencies = [ "macro_magic_core", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -5977,9 +5977,9 @@ dependencies = [ [[package]] name = "netlink-sys" -version = "0.8.5" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6471bf08e7ac0135876a9581bf3217ef0333c191c128d34878079f42ee150411" +checksum = "416060d346fbaf1f23f9512963e3e878f1a78e707cb699ba9215761754244307" dependencies = [ "bytes", "futures", @@ -7177,7 +7177,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -7653,7 +7653,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -7694,7 +7694,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -8832,7 +8832,7 @@ dependencies = [ "polkavm-common", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -8842,7 +8842,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ba81f7b5faac81e528eb6158a6f3c9e0bb1008e0ffa19653bc8dea925ecb429" dependencies = [ "polkavm-derive-impl", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -8965,7 +8965,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22020dfcf177fcc7bf5deaf7440af371400c67c0de14c399938d8ed4fb4645d3" dependencies = [ "proc-macro2", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -8985,7 +8985,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" dependencies = [ "proc-macro2", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -9093,7 +9093,7 @@ checksum = "834da187cfe638ae8abb0203f0b33e5ccdb02a28e7199f2f47b3e2754f50edca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -9139,7 +9139,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -9464,7 +9464,7 @@ checksum = "5fddb4f8d99b0a2ebafc65a87a69a7b9875e4b1ae1f00db265d300ef7f28bccc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -10108,7 +10108,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -11077,7 +11077,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -11370,7 +11370,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -11762,7 +11762,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -12018,13 +12018,13 @@ source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto dependencies = [ "quote", "sp-core-hashing", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] name = "sp-crypto-ec-utils" version = "0.10.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ "ark-bls12-377", "ark-bls12-377-ext", @@ -12057,17 +12057,17 @@ source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] name = "sp-debug-derive" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -12084,7 +12084,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.25.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ "environmental", "parity-scale-codec", @@ -12302,7 +12302,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "24.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -12328,20 +12328,20 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ "Inflector", "expander 2.1.0", "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -12426,7 +12426,7 @@ source = "git+https://github.com/virto-network/polkadot-sdk?branch=release-virto [[package]] name = "sp-std" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" [[package]] name = "sp-storage" @@ -12444,7 +12444,7 @@ dependencies = [ [[package]] name = "sp-storage" version = "19.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ "impl-serde", "parity-scale-codec", @@ -12481,7 +12481,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "16.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ "parity-scale-codec", "tracing", @@ -12563,7 +12563,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -12582,13 +12582,11 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "20.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#5d9826c2620aff205811edf0e6a07b55a52cbf50" +source = "git+https://github.com/paritytech/polkadot-sdk#9d052b7e09d7aecf039b96cf6267e05b9b01c0ae" dependencies = [ - "anyhow", "impl-trait-for-tuples", "log", "parity-scale-codec", - "wasmtime", ] [[package]] @@ -12783,9 +12781,9 @@ dependencies = [ [[package]] name = "strsim" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "strum" @@ -12825,7 +12823,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -12956,9 +12954,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.57" +version = "2.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11a6ae1e52eb25aab8f3fb9fca13be982a373b8f1157ca14b897a825ba4a2d35" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" dependencies = [ "proc-macro2", "quote", @@ -13073,7 +13071,7 @@ checksum = "e4c60d69f36615a077cc7663b9cb8e42275722d23e58a7fa3d2c7f2915d09d04" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -13084,7 +13082,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -13228,7 +13226,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -13430,7 +13428,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -13473,7 +13471,7 @@ dependencies = [ "proc-macro-crate 2.0.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -13972,7 +13970,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", "wasm-bindgen-shared", ] @@ -14006,7 +14004,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -14853,7 +14851,7 @@ dependencies = [ "Inflector", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -14896,7 +14894,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -14916,7 +14914,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] diff --git a/pallets/communities/src/benchmarking.rs b/pallets/communities/src/benchmarking.rs index f9dfa214..9efd44c2 100644 --- a/pallets/communities/src/benchmarking.rs +++ b/pallets/communities/src/benchmarking.rs @@ -169,15 +169,7 @@ mod benchmarks { _(RawOrigin::Root, id, name.clone(), description.clone(), url.clone()); // verification code - assert_has_event::( - Event::MetadataSet { - id, - name, - description, - main_url: url, - } - .into(), - ); + assert_has_event::(Event::MetadataSet { id, name }.into()); Ok(()) } @@ -223,7 +215,7 @@ mod benchmarks { } .into(), ); - assert!(T::MemberMgmt::has_membership(&who, &membership_id).is_some()); + assert!(T::MemberMgmt::check_membership(&who, &membership_id).is_some()); Ok(()) } @@ -251,7 +243,7 @@ mod benchmarks { } .into(), ); - assert!(T::MemberMgmt::has_membership(&who, &membership_id).is_none()); + assert!(T::MemberMgmt::check_membership(&who, &membership_id).is_none()); Ok(()) } diff --git a/pallets/communities/src/functions.rs b/pallets/communities/src/functions.rs index 4e54e588..b0e59162 100644 --- a/pallets/communities/src/functions.rs +++ b/pallets/communities/src/functions.rs @@ -1,13 +1,14 @@ use crate::{ origin::DecisionMethod, types::{ - AccountIdOf, CommunityIdOf, CommunityInfo, CommunityMetadata, CommunityState, ConstSizedField, MembershipIdOf, - PalletsOriginOf, PollIndexOf, Tally, Vote, VoteOf, VoteWeight, + AccountIdOf, CommunityIdOf, CommunityInfo, CommunityState, ConstSizedField, MembershipIdOf, PalletsOriginOf, + PollIndexOf, Tally, Vote, VoteOf, VoteWeight, }, - CommunityDecisionMethod, CommunityIdFor, CommunityVotes, Config, Error, Event, HoldReason, Info, Metadata, Pallet, + CommunityDecisionMethod, CommunityIdFor, CommunityVotes, Config, Error, HoldReason, Info, Metadata, Pallet, }; use fc_traits_memberships::{GenericRank, Inspect, Rank}; use frame_support::{ + fail, pallet_prelude::*, traits::{ fungible::MutateFreeze as FunMutateFreeze, fungibles::MutateHold as FunsMutateHold, tokens::Precision, Polling, @@ -49,7 +50,7 @@ impl Pallet { /// to its default value(awaiting) pub(crate) fn do_register_community(admin: &PalletsOriginOf, community_id: &T::CommunityId) -> DispatchResult { if Self::community_exists(community_id) { - return Err(Error::::CommunityAlreadyExists.into()); + fail!(Error::::CommunityAlreadyExists); } CommunityIdFor::::insert(admin, community_id); @@ -62,15 +63,19 @@ impl Pallet { pub(crate) fn do_set_metadata( community_id: &CommunityIdOf, name: &Option>, - description: &Option>, - url: &Option>, + description: Option>, + url: Option>, ) { Metadata::::mutate(community_id, |metadata| { - *metadata = CommunityMetadata { - name: name.as_ref().unwrap_or(&metadata.name).clone(), - description: description.as_ref().unwrap_or(&metadata.description).clone(), - main_url: url.as_ref().unwrap_or(&metadata.main_url).clone(), - }; + if let Some(name) = name { + metadata.name = name.clone(); + } + if let Some(desc) = description { + metadata.description = desc; + } + if let Some(url) = url { + metadata.main_url = url; + } }) } @@ -78,14 +83,12 @@ impl Pallet { who: &AccountIdOf, membership_id: MembershipIdOf, poll_index: PollIndexOf, - vote: VoteOf, + vote: &VoteOf, ) -> DispatchResult { - let Some(community_id) = T::MemberMgmt::has_membership(who, &membership_id) else { - return Err(Error::::NotAMember.into()); + ensure!(VoteWeight::from(vote).gt(&0), TokenError::BelowMinimum); + let Some(community_id) = T::MemberMgmt::check_membership(who, &membership_id) else { + fail!(Error::::NotAMember); }; - if VoteWeight::from(vote.clone()) == 0 { - return Err(TokenError::BelowMinimum.into()); - } T::Polls::try_access_poll(poll_index, |poll_status| { let (tally, class) = poll_status.ensure_ongoing().ok_or(Error::::NotOngoing)?; @@ -93,71 +96,29 @@ impl Pallet { let decision_method = CommunityDecisionMethod::::get(community_id); - let maybe_vote = Self::community_vote_of(who, poll_index); - if let Some(vote) = maybe_vote { - Self::do_unlock_for_vote(who, &poll_index, &vote)?; - - let multiplied_vote = match CommunityDecisionMethod::::get(community_id) { - DecisionMethod::Rank => T::MemberMgmt::rank_of(&community_id, &membership_id) - .unwrap_or_default() - .into(), - _ => 1, - }; - tally.remove_vote( - vote.clone().into(), - multiplied_vote * Into::::into(vote.clone()), - vote.into(), - ); - } - - let say = match vote.clone() { - Vote::AssetBalance(say, asset_id, ..) => { - ensure!( - decision_method == DecisionMethod::CommunityAsset(asset_id.clone()), - Error::::InvalidVoteType - ); - - say - } - Vote::NativeBalance(say, ..) => { - ensure!( - decision_method == DecisionMethod::NativeToken, - Error::::InvalidVoteType - ); - - say - } - Vote::Standard(say) => { - ensure!( - decision_method == DecisionMethod::Membership || decision_method == DecisionMethod::Rank, - Error::::InvalidVoteType - ); - - say - } - }; - - Self::do_lock_for_vote(who, &poll_index, &vote)?; - - let multiplied_vote = match CommunityDecisionMethod::::get(community_id) { + let vote_multiplier = match CommunityDecisionMethod::::get(community_id) { DecisionMethod::Rank => T::MemberMgmt::rank_of(&community_id, &membership_id) .unwrap_or_default() .into(), _ => 1, }; - tally.add_vote( - say, - multiplied_vote * Into::::into(vote.clone()), - vote.clone().into(), - ); + if let Some(vote) = Self::community_vote_of(who, poll_index) { + Self::do_unlock_for_vote(who, &poll_index, &vote)?; + let vote_weight = VoteWeight::from(&vote); + tally.remove_vote(vote.say(), vote_multiplier * vote_weight, vote_weight); + } - Self::deposit_event(Event::::VoteCasted { - who: who.clone(), - poll_index, - vote, - }); + let say = *match (vote, decision_method) { + (Vote::AssetBalance(say, asset, ..), DecisionMethod::CommunityAsset(a)) if *asset == a => say, + (Vote::NativeBalance(say, ..), DecisionMethod::NativeToken) + | (Vote::Standard(say), DecisionMethod::Membership | DecisionMethod::Rank) => say, + _ => fail!(Error::::InvalidVoteType), + }; - Ok(()) + let vote_weight = VoteWeight::from(vote); + tally.add_vote(say, vote_multiplier * vote_weight, vote_weight); + + Self::do_lock_for_vote(who, &poll_index, vote) }) } @@ -166,53 +127,42 @@ impl Pallet { membership_id: MembershipIdOf, poll_index: PollIndexOf, ) -> DispatchResult { - let Some(community_id) = T::MemberMgmt::has_membership(who, &membership_id) else { - return Err(Error::::NotAMember.into()); + let Some(community_id) = T::MemberMgmt::check_membership(who, &membership_id) else { + fail!(Error::::NotAMember); }; T::Polls::try_access_poll(poll_index, |poll_status| { - let res = if let Some((tally, class)) = poll_status.ensure_ongoing() { - ensure!(community_id == class, Error::::InvalidTrack); - let vote = Self::community_vote_of(who, poll_index).ok_or(Error::::NoVoteCasted)?; - - let multiplied_vote = match CommunityDecisionMethod::::get(community_id) { - DecisionMethod::Rank => T::MemberMgmt::rank_of(&community_id, &membership_id) - .unwrap_or_default() - .into(), - _ => 1, - }; - tally.remove_vote( - vote.clone().into(), - multiplied_vote * Into::::into(vote.clone()), - vote.clone().into(), - ); - - let reason = HoldReason::VoteCasted(poll_index).into(); - CommunityVotes::::remove(who, poll_index); + let Some((tally, class)) = poll_status.ensure_ongoing() else { + fail!(Error::::NotOngoing); + }; + ensure!(community_id == class, Error::::InvalidTrack); + let vote = Self::community_vote_of(who, poll_index).ok_or(Error::::NoVoteCasted)?; - match vote { - Vote::AssetBalance(_, asset_id, amount) => { - T::Assets::release(asset_id.clone(), &reason, who, amount, Precision::BestEffort).map(|_| ()) - } - Vote::NativeBalance(..) => T::Balances::thaw(&reason, who), - _ => Ok(()), - } - } else { - Err(Error::::NotOngoing.into()) + let vote_multiplier = match CommunityDecisionMethod::::get(community_id) { + DecisionMethod::Rank => T::MemberMgmt::rank_of(&community_id, &membership_id) + .unwrap_or_default() + .into(), + _ => 1, }; + let vote_weight = VoteWeight::from(&vote); + tally.remove_vote(vote.say(), vote_multiplier * vote_weight, vote_weight); - Self::deposit_event(Event::::VoteRemoved { - who: who.clone(), - poll_index, - }); + let reason = HoldReason::VoteCasted(poll_index).into(); + CommunityVotes::::remove(who, poll_index); - res + match vote { + Vote::AssetBalance(_, asset_id, amount) => { + T::Assets::release(asset_id.clone(), &reason, who, amount, Precision::BestEffort).map(|_| ()) + } + Vote::NativeBalance(..) => T::Balances::thaw(&reason, who), + _ => Ok(()), + } }) } fn do_lock_for_vote(who: &AccountIdOf, poll_index: &PollIndexOf, vote: &VoteOf) -> DispatchResult { let reason = HoldReason::VoteCasted(*poll_index).into(); - CommunityVotes::::insert(who.clone(), poll_index, vote.clone()); + CommunityVotes::::insert(who, poll_index, vote.clone()); match vote { Vote::AssetBalance(_, asset_id, amount) => T::Assets::hold(asset_id.clone(), &reason, who, *amount), @@ -242,27 +192,21 @@ impl Pallet { } impl Tally { - pub(self) fn add_vote(&mut self, say: bool, multiplied_weight: VoteWeight, weight: VoteWeight) { - match say { - true => { - self.ayes = self.ayes.saturating_add(multiplied_weight); - self.bare_ayes = self.bare_ayes.saturating_add(weight); - } - false => { - self.nays = self.nays.saturating_add(multiplied_weight); - } + pub(self) fn add_vote(&mut self, say_aye: bool, multiplied_weight: VoteWeight, weight: VoteWeight) { + if say_aye { + self.ayes = self.ayes.saturating_add(multiplied_weight); + self.bare_ayes = self.bare_ayes.saturating_add(weight); + } else { + self.nays = self.nays.saturating_add(multiplied_weight); } } - pub(self) fn remove_vote(&mut self, say: bool, multiplied_weight: VoteWeight, weight: VoteWeight) { - match say { - true => { - self.ayes = self.ayes.saturating_sub(multiplied_weight); - self.bare_ayes = self.bare_ayes.saturating_sub(weight); - } - false => { - self.nays = self.nays.saturating_sub(multiplied_weight); - } + pub(self) fn remove_vote(&mut self, say_aye: bool, multiplied_weight: VoteWeight, weight: VoteWeight) { + if say_aye { + self.ayes = self.ayes.saturating_sub(multiplied_weight); + self.bare_ayes = self.bare_ayes.saturating_sub(weight); + } else { + self.nays = self.nays.saturating_sub(multiplied_weight); } } } diff --git a/pallets/communities/src/lib.rs b/pallets/communities/src/lib.rs index 061e9111..ed648a30 100644 --- a/pallets/communities/src/lib.rs +++ b/pallets/communities/src/lib.rs @@ -312,8 +312,6 @@ pub mod pallet { MetadataSet { id: T::CommunityId, name: Option>, - description: Option>, - main_url: Option>, }, DecisionMethodSet { id: T::CommunityId, @@ -413,13 +411,8 @@ pub mod pallet { ) -> DispatchResult { T::CommunityMgmtOrigin::ensure_origin(origin)?; - Self::do_set_metadata(&community_id, &name, &description, &url); - Self::deposit_event(Event::MetadataSet { - id: community_id, - name, - description, - main_url: url, - }); + Self::do_set_metadata(&community_id, &name, description, url); + Self::deposit_event(Event::MetadataSet { id: community_id, name }); Ok(()) } @@ -496,7 +489,7 @@ pub mod pallet { // === Governance === - /// + /// Decide the method used by the community to vote on proposals #[pallet::call_index(7)] pub fn set_decision_method( origin: OriginFor, @@ -510,7 +503,7 @@ pub mod pallet { Ok(()) } - /// + /// Cast a vote on an on-going referendum #[pallet::call_index(4)] pub fn vote( origin: OriginFor, @@ -519,7 +512,13 @@ pub mod pallet { vote: VoteOf, ) -> DispatchResult { let who = ensure_signed(origin)?; - Self::do_vote(&who, membership_id, poll_index, vote) + Self::do_vote(&who, membership_id, poll_index, &vote)?; + Self::deposit_event(Event::::VoteCasted { + who: who.clone(), + poll_index, + vote, + }); + Ok(()) } /// @@ -530,10 +529,16 @@ pub mod pallet { #[pallet::compact] poll_index: PollIndexOf, ) -> DispatchResult { let who = ensure_signed(origin)?; - Self::do_remove_vote(&who, membership_id, poll_index) + Self::do_remove_vote(&who, membership_id, poll_index)?; + Self::deposit_event(Event::::VoteRemoved { + who: who.clone(), + poll_index, + }); + Ok(()) } - /// + /// Make previously held or locked funds from a vote available + // if the refereundum has finished #[pallet::call_index(9)] pub fn unlock(origin: OriginFor, #[pallet::compact] poll_index: PollIndexOf) -> DispatchResult { let who = ensure_signed(origin)?; diff --git a/pallets/communities/src/tests/governance.rs b/pallets/communities/src/tests/governance.rs index a5c02410..9462acee 100644 --- a/pallets/communities/src/tests/governance.rs +++ b/pallets/communities/src/tests/governance.rs @@ -110,7 +110,7 @@ fn new_test_ext() -> sp_io::TestExternalities { COMMUNITY_B, DecisionMethod::CommunityAsset(COMMUNITY_B_ASSET_ID), &[BOB, CHARLIE], - &memberships_of(COMMUNITY_B), + memberships_of(COMMUNITY_B), Some(CommunityTrack::get()), ) .add_asset( diff --git a/pallets/communities/src/types.rs b/pallets/communities/src/types.rs index 55bf5ff9..f0411384 100644 --- a/pallets/communities/src/types.rs +++ b/pallets/communities/src/types.rs @@ -80,27 +80,35 @@ pub enum Vote { Standard(bool), } -impl From> for VoteWeight +impl Vote where - B: UniqueSaturatedInto, - N: UniqueSaturatedInto, + B: UniqueSaturatedInto + Clone, + N: UniqueSaturatedInto + Clone, { - fn from(value: Vote) -> Self { - match value { - Vote::AssetBalance(_, _, balance) => balance.saturated_into(), - Vote::NativeBalance(_, balance) => balance.saturated_into(), + pub fn say(&self) -> bool { + *match self { + Vote::AssetBalance(say, _, _) => say, + Vote::NativeBalance(say, _) => say, + Vote::Standard(say) => say, + } + } + + pub fn weight(&self) -> VoteWeight { + match self { + Vote::AssetBalance(_, _, balance) => balance.clone().saturated_into(), + Vote::NativeBalance(_, balance) => balance.clone().saturated_into(), Vote::Standard(_) => 1, } } } -impl From> for bool { - fn from(value: Vote) -> bool { - match value { - Vote::AssetBalance(say, _, _) => say, - Vote::NativeBalance(say, _) => say, - Vote::Standard(say) => say, - } +impl From<&Vote> for VoteWeight +where + B: UniqueSaturatedInto + Clone, + N: UniqueSaturatedInto + Clone, +{ + fn from(vote: &Vote) -> Self { + vote.weight() } }