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

Robin/add rpc calls #15

Open
wants to merge 10 commits into
base: add-rpc-calls
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
6 changes: 3 additions & 3 deletions rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ license = "Apache-2.0"


[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", features = [
codec = { package = "parity-scale-codec", version = "3.0", features = [
"derive",
], default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = [
"derive",
] }

sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" , version = "4.0.0-dev"}
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" , version = "4.0.0-dev"}

[features]
default = ["std"]
Expand Down
3 changes: 2 additions & 1 deletion rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
use sp_std::vec::Vec;
#[cfg(not(feature = "std"))]
use sp_std::prelude::Vec;

sp_api::decl_runtime_apis! {
pub trait SuperSigApi<AccountId>
Expand Down
71 changes: 62 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pub mod pallet {
// A supersig should at least have one member
let member_length = members.len();
if member_length < 1 {
return Err(Error::<T>::InvalidNumberOfMembers.into());
return Err(Error::<T>::InvalidNumberOfMembers.into())
}

// Get it id and associated account
Expand Down Expand Up @@ -352,10 +352,10 @@ pub mod pallet {
let supersig_id = Self::get_supersig_id_from_account(&supersig_account)?;

if Self::calls(supersig_id, call_id).is_none() {
return Err(Error::<T>::CallNotFound.into());
return Err(Error::<T>::CallNotFound.into())
}
if Self::members_votes((supersig_id, call_id, who.clone())) {
return Err(Error::<T>::AlreadyVoted.into());
return Err(Error::<T>::AlreadyVoted.into())
}

// Different roles have different voting weight
Expand Down Expand Up @@ -427,7 +427,7 @@ pub mod pallet {

// Either the supersig or the user that created the vote can remove a call
if who != supersig_account && who != preimage.provider {
return Err(Error::<T>::NotAllowed.into());
return Err(Error::<T>::NotAllowed.into())
}

// Clean up storage and release reserved funds
Expand Down Expand Up @@ -569,7 +569,7 @@ pub mod pallet {
let supersig_id = Self::get_supersig_id_from_account(&supersig_account)?;

if Self::members(supersig_id, &who) == Role::NotMember {
return Err(Error::<T>::NotMember.into());
return Err(Error::<T>::NotMember.into())
}

// Remeber the storage state before we remove the members from it
Expand All @@ -586,7 +586,7 @@ pub mod pallet {
// A supersig should at least have one member
TotalMembers::<T>::try_mutate(supersig_id, |nb| {
if *nb == 1 {
return Err(Error::<T>::InvalidNumberOfMembers);
return Err(Error::<T>::InvalidNumberOfMembers)
};
*nb -= 1;
Ok(())
Expand All @@ -610,7 +610,7 @@ pub mod pallet {
) -> Result<SupersigId, pallet::Error<T>> {
if let Some((account, supersig_id)) = PalletId::try_from_sub_account(supersig_account) {
if account != T::PalletId::get() || Self::total_members(supersig_id) == 0 {
return Err(Error::<T>::NotSupersig);
return Err(Error::<T>::NotSupersig)
}
Ok(supersig_id)
} else {
Expand Down Expand Up @@ -679,7 +679,7 @@ pub mod pallet {
let new_total_members =
n.saturating_sub(removed.len().try_into().map_err(|_| Error::<T>::Conversion)?);
if new_total_members < 1 {
return Err(Error::<T>::InvalidNumberOfMembers);
return Err(Error::<T>::InvalidNumberOfMembers)
}

*n = new_total_members;
Expand Down Expand Up @@ -767,7 +767,60 @@ impl<T: Config> Pallet<T> {
.collect()
}

pub fn get_members_connected_to_supersig(which: SupersigId) -> Vec<(T::AccountId, Role)> {
pub fn get_members_connected(which: SupersigId) -> Vec<(T::AccountId, Role)> {
tdelabro marked this conversation as resolved.
Show resolved Hide resolved
Members::<T>::iter_prefix(which).collect()
}

// Return :
// Tuple :
// Vec<((Vec<u8>, T::AccountId, BalanceOf<T>), Vec<T::AccountId>)> :
// The tuple inside the vec is just a Call that is unwrap.
// The vec inside the vec is all the account id that have voted.
// The second parameter of the tuple is the total amount of members into the supersig.
pub fn get_proposals(
which: SupersigId,
) -> (
Vec<((Vec<u8>, T::AccountId, BalanceOf<T>), Vec<T::AccountId>)>,
tdelabro marked this conversation as resolved.
Show resolved Hide resolved
u32,
) {
let member_count = Self::total_members(which);
let proposal_state = Calls::<T>::iter_prefix(which)
.map(|(call_id, call)| {
(
(call.data, call.provider, call.deposit),
tdelabro marked this conversation as resolved.
Show resolved Hide resolved
tdelabro marked this conversation as resolved.
Show resolved Hide resolved
MembersVotes::<T>::iter_prefix((which, call_id))
.filter_map(
|(account_id, vote)| {
if vote { Some(account_id) } else { None }
},
)
.collect(),
)
})
.collect();
(proposal_state, member_count)
}

// Return :
// Tuple :
// The bool is to define if the Call that is asked for state still exists.
// The vec is all the account id that have voted.
// The first u32 is the total amount of members in the supersig
// The second u32 is the total number of votes (not necessary because == members_votes.len())
pub fn get_proposal_state(
which: SupersigId,
call_id: CallId,
) -> (bool, Vec<T::AccountId>, u32, u32) {
let member_count = Self::total_members(which);
let votes = Self::votes(which, call_id);
let exists = !Self::calls(which, call_id).is_none();
tdelabro marked this conversation as resolved.
Show resolved Hide resolved
let member_votes = MembersVotes::<T>::iter_prefix((which, call_id))
.filter_map(
|(account_id, vote)| {
if vote { Some(account_id) } else { None }
},
)
.collect();
(exists, member_votes, member_count, votes)
}
}
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod delete_supersig;
mod leave_supersig;
mod remove_call;
mod remove_members;
mod submit_call;
mod rpc_calls;
mod submit_call;

pub mod helper;
pub mod mock;
Loading