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

Congress threshold update #149

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
/.idea/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
45 changes: 40 additions & 5 deletions congress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ impl Contract {
"accounts": vec![member],
"memo": "".to_owned()
})
.to_string()
.as_bytes()
.to_vec(),
.to_string()
.as_bytes()
.to_vec(),
0,
EXEC_CTR_CALL_GAS,
);
Expand Down Expand Up @@ -432,8 +432,13 @@ impl Contract {
members.remove(idx.unwrap());

emit_dismiss(&member);

// Update threshold and members_len
self.members_len = members.len() as u8;
self.threshold = (self.members_len / 2) + 1;

// If DAO doesn't have required threshold, then we dissolve.
if members.len() < self.threshold as usize {
if members.len() < 2 {
VlodkoMr marked this conversation as resolved.
Show resolved Hide resolved
self.dissolve_and_cleanup();
}

Expand Down Expand Up @@ -1188,9 +1193,39 @@ mod unit_tests {
ctx.predecessor_account_id = voting_body();
testing_env!(ctx);

// Remove more members to check threshold update
ctr.dismiss_hook(acc(1)).unwrap();
let (members, _) = ctr.members.get().unwrap();
assert_eq!(members.len(), 2);
assert_eq!(ctr.threshold, 2);
}

#[test]
fn dismiss_hook_threshold_update() {
let (mut ctx, mut ctr, _) = setup_ctr(100);

// Initial checks to confirm setup is correct
assert_eq!(ctr.members_len, 4, "Initial members length should be 4");
assert_eq!(ctr.threshold, 3, "Initial threshold should be 3");

// Simulate calling dismiss_hook as the voting body to remove a member
ctx.predecessor_account_id = voting_body();
testing_env!(ctx);
ctr.dismiss_hook(acc(2)).unwrap();

// Check if member was successfully removed
let (members, _) = ctr.members.get().unwrap();
assert!(!members.contains(&acc(2)), "Member 2 should have been removed");

// Check if members count and threshold are updated correctly
assert_eq!(ctr.members_len, 3, "Members length should be updated to 3");
assert_eq!(ctr.threshold, 2, "Threshold should be updated to 2");

assert!(!ctr.dissolved);
// Remove more members to check dissolve
ctr.dismiss_hook(acc(1)).unwrap();
for member in &[acc(1), acc(3)] {
ctr.dismiss_hook(member.clone()).unwrap();
}
assert!(ctr.dissolved);
}

Expand Down
Loading