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

Proposal Deadline introduced #102

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 22 additions & 9 deletions sputnikdao2/src/bounties.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::{U128, U64};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, near_bindgen, AccountId, Promise, PromiseOrValue};
use near_sdk::{env, near_bindgen, AccountId, Promise, PromiseOrValue, Timestamp};

use crate::*;

Expand Down Expand Up @@ -158,7 +158,13 @@ impl Contract {
/// Only creator of the claim can call `done` on bounty that is still in progress.
/// On expired, anyone can call it to free up the claim slot.
#[payable]
pub fn bounty_done(&mut self, id: u64, account_id: Option<AccountId>, description: String) {
pub fn bounty_done(
&mut self,
id: u64,
account_id: Option<AccountId>,
description: String,
deadline: Option<Timestamp>,
) {
let sender_id = account_id.unwrap_or_else(|| env::predecessor_account_id());
let (mut claims, claim_idx) = self.internal_get_claims(id, &sender_id);
assert!(!claims[claim_idx].completed, "ERR_BOUNTY_CLAIM_COMPLETED");
Expand All @@ -172,12 +178,18 @@ impl Contract {
env::predecessor_account_id(),
"ERR_BOUNTY_DONE_MUST_BE_SELF"
);
let kind = ProposalKind::BountyDone {
bounty_id: id,
receiver_id: sender_id.clone(),
};

let policy = self.policy.get().unwrap().to_policy();
proposals::assert_proposal_deadline(deadline, &kind, &policy);

self.add_proposal(ProposalInput {
description,
kind: ProposalKind::BountyDone {
bounty_id: id,
receiver_id: sender_id.clone(),
},
kind,
deadline,
});
claims[claim_idx].completed = true;
self.bounty_claimers.insert(&sender_id, &claims);
Expand Down Expand Up @@ -229,6 +241,7 @@ mod tests {
max_deadline: U64::from(1_000),
},
},
deadline: None,
});
assert_eq!(contract.get_last_bounty_id(), id);
contract.act_proposal(id, Action::VoteApprove, None);
Expand Down Expand Up @@ -261,7 +274,7 @@ mod tests {
assert_eq!(contract.get_bounty_claims(accounts(1)).len(), 1);
assert_eq!(contract.get_bounty_number_of_claims(0), 1);

contract.bounty_done(0, None, "Bounty is done".to_string());
contract.bounty_done(0, None, "Bounty is done".to_string(), None);
assert!(contract.get_bounty_claims(accounts(1))[0].completed);

assert_eq!(contract.get_last_proposal_id(), 2);
Expand All @@ -284,7 +297,7 @@ mod tests {
assert_eq!(contract.get_bounty(0).bounty.times, 1);

contract.bounty_claim(0, U64::from(500));
contract.bounty_done(0, None, "Bounty is done 2".to_string());
contract.bounty_done(0, None, "Bounty is done 2".to_string(), None);
contract.act_proposal(2, Action::VoteApprove, None);
testing_env!(
context.build(),
Expand All @@ -309,7 +322,7 @@ mod tests {
);
let id = add_bounty(&mut context, &mut contract, 1);
contract.bounty_claim(id, U64::from(500));
contract.bounty_done(id, None, "Bounty is done 2".to_string());
contract.bounty_done(id, None, "Bounty is done 2".to_string(), None);
contract.bounty_claim(id, U64::from(500));
}
}
214 changes: 205 additions & 9 deletions sputnikdao2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,18 @@ pub extern "C" fn store_blob() {
#[cfg(test)]
mod tests {
use near_sdk::test_utils::{accounts, VMContextBuilder};
use near_sdk::testing_env;
use near_sdk::{testing_env, Timestamp};
use near_sdk_sim::to_yocto;

use crate::proposals::ProposalStatus;

use super::*;

fn create_proposal(context: &mut VMContextBuilder, contract: &mut Contract) -> u64 {
fn create_proposal(
context: &mut VMContextBuilder,
contract: &mut Contract,
deadline: Option<Timestamp>,
) -> u64 {
testing_env!(context.attached_deposit(to_yocto("1")).build());
contract.add_proposal(ProposalInput {
description: "test".to_string(),
Expand All @@ -203,6 +207,7 @@ mod tests {
amount: U128(to_yocto("100")),
msg: None,
},
deadline,
})
}

Expand All @@ -214,18 +219,18 @@ mod tests {
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1).into()]),
);
let id = create_proposal(&mut context, &mut contract);
let id = create_proposal(&mut context, &mut contract, None);
assert_eq!(contract.get_proposal(id).proposal.description, "test");
assert_eq!(contract.get_proposals(0, 10).len(), 1);

let id = create_proposal(&mut context, &mut contract);
let id = create_proposal(&mut context, &mut contract, None);
contract.act_proposal(id, Action::VoteApprove, None);
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Approved
);

let id = create_proposal(&mut context, &mut contract);
let id = create_proposal(&mut context, &mut contract, None);
// proposal expired, finalize.
testing_env!(context
.block_timestamp(1_000_000_000 * 24 * 60 * 60 * 8)
Expand All @@ -247,6 +252,7 @@ mod tests {
member_id: accounts(2).into(),
role: "council".to_string(),
},
deadline: None,
});
}

Expand All @@ -259,7 +265,7 @@ mod tests {
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1).into()]),
);
let id = create_proposal(&mut context, &mut contract);
let id = create_proposal(&mut context, &mut contract, None);
assert_eq!(contract.get_proposal(id).proposal.description, "test");
contract.act_proposal(id, Action::RemoveProposal, None);
}
Expand All @@ -273,7 +279,7 @@ mod tests {
.permissions
.insert("*:RemoveProposal".to_string());
let mut contract = Contract::new(Config::test_config(), policy);
let id = create_proposal(&mut context, &mut contract);
let id = create_proposal(&mut context, &mut contract, None);
assert_eq!(contract.get_proposal(id).proposal.description, "test");
contract.act_proposal(id, Action::RemoveProposal, None);
assert_eq!(contract.get_proposals(0, 10).len(), 0);
Expand All @@ -287,7 +293,7 @@ mod tests {
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1).into()]),
);
let id = create_proposal(&mut context, &mut contract);
let id = create_proposal(&mut context, &mut contract, None);
testing_env!(context
.block_timestamp(1_000_000_000 * 24 * 60 * 60 * 8)
.build());
Expand All @@ -303,7 +309,7 @@ mod tests {
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1).into(), accounts(2).into()]),
);
let id = create_proposal(&mut context, &mut contract);
let id = create_proposal(&mut context, &mut contract, None);
contract.act_proposal(id, Action::VoteApprove, None);
contract.act_proposal(id, Action::VoteApprove, None);
}
Expand All @@ -323,6 +329,7 @@ mod tests {
member_id: accounts(2).into(),
role: "missing".to_string(),
},
deadline: None,
});
contract.act_proposal(id, Action::VoteApprove, None);
let x = contract.get_policy();
Expand All @@ -345,6 +352,195 @@ mod tests {
kind: ProposalKind::ChangePolicy {
policy: VersionedPolicy::Default(vec![]),
},
deadline: None,
});
}

pub const MIN_VOTING_TIME: Timestamp = 3 * 24 * 60 * 60 * 1_000_000_000;
#[test]
fn test_deadline() {
let mut context = VMContextBuilder::new();
testing_env!(context.predecessor_account_id(accounts(1)).build());
let mut policy: Policy = policy::default_policy(vec![
accounts(1).into(),
accounts(2).into(),
accounts(3).into(),
accounts(4).into(),
accounts(5).into(),
]);
policy.min_voting_time = Some(MIN_VOTING_TIME);
let mut contract = Contract::new(Config::test_config(), VersionedPolicy::Current(policy));

// Finalize with the only 1 approval from 5 votes AFTER the deadline
let id = create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));
contract.act_proposal(id, Action::VoteApprove, None);

testing_env!(context.block_timestamp(MIN_VOTING_TIME).build());

contract.act_proposal(id, Action::Finalize, None);
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Approved
);

// Finalize with the only 1 rejection from 5 votes AFTER the deadline
testing_env!(context.block_timestamp(0).build());
let id = create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));
contract.act_proposal(id, Action::VoteReject, None);

testing_env!(context.block_timestamp(MIN_VOTING_TIME).build());

contract.act_proposal(id, Action::Finalize, None);
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Rejected
);

// Reject after a deadline with only 2 approvals & 1 rejection from 5 votes
testing_env!(context
.block_timestamp(0)
.predecessor_account_id(accounts(1))
.build());
let id = create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));

contract.act_proposal(id, Action::VoteApprove, None);

testing_env!(context.predecessor_account_id(accounts(2)).build());
contract.act_proposal(id, Action::VoteReject, None);

testing_env!(context.predecessor_account_id(accounts(3)).build());
contract.act_proposal(id, Action::VoteApprove, None);

testing_env!(context
.block_timestamp(MIN_VOTING_TIME)
.predecessor_account_id(accounts(4))
.build());

contract.act_proposal(id, Action::VoteReject, None);
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Approved
);

// Approve after a deadline with only 1 approval & 2 rejection from 5 votes
testing_env!(context
.block_timestamp(0)
.predecessor_account_id(accounts(1))
.build());
let id = create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));

contract.act_proposal(id, Action::VoteApprove, None);

testing_env!(context.predecessor_account_id(accounts(2)).build());
contract.act_proposal(id, Action::VoteReject, None);

testing_env!(context.predecessor_account_id(accounts(3)).build());
contract.act_proposal(id, Action::VoteReject, None);

testing_env!(context
.block_timestamp(MIN_VOTING_TIME)
.predecessor_account_id(accounts(4))
.build());

contract.act_proposal(id, Action::VoteApprove, None);

assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Rejected
);

// Approve after a deadline with the only 1 approval from 5 votes
testing_env!(context
.block_timestamp(0)
.predecessor_account_id(accounts(1))
.build());
let id = create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));
contract.act_proposal(id, Action::VoteApprove, None);

testing_env!(context
.block_timestamp(MIN_VOTING_TIME)
.predecessor_account_id(accounts(2))
.build());

contract.act_proposal(id, Action::VoteApprove, None);
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Approved
);

// Reject after a deadline with the only 1 approval from 5 votes
testing_env!(context
.block_timestamp(0)
.predecessor_account_id(accounts(1))
.build());
let id = create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));
contract.act_proposal(id, Action::VoteApprove, None);

testing_env!(context
.block_timestamp(MIN_VOTING_TIME)
.predecessor_account_id(accounts(2))
.build());

contract.act_proposal(id, Action::VoteReject, None);
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Approved
);

// Proposal with 0 votes. Finalize after deadline
testing_env!(context.block_timestamp(0).build());
let id = create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));
testing_env!(context.block_timestamp(MIN_VOTING_TIME).build());

contract.act_proposal(id, Action::Finalize, None);
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Removed
);
}

#[test]
#[should_panic(expected = "ERR_DEADLINE_SET_TOO_EARLY")]
fn test_deadline_period() {
let mut context = VMContextBuilder::new();
testing_env!(context.predecessor_account_id(accounts(1)).build());
let mut policy: Policy = policy::default_policy(vec![accounts(1).into()]);
policy.min_voting_time = Some(MIN_VOTING_TIME);
let mut contract = Contract::new(Config::test_config(), VersionedPolicy::Current(policy));

create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME / 2));
}

#[test]
#[should_panic(expected = "ERR_DEADLINE_FORBIDDEN_KIND")]
fn test_deadline_type() {
let mut context = VMContextBuilder::new();
testing_env!(context.predecessor_account_id(accounts(1)).build());
let mut policy: Policy = policy::default_policy(vec![accounts(1).into()]);
policy.min_voting_time = Some(MIN_VOTING_TIME);
let mut contract = Contract::new(Config::test_config(), VersionedPolicy::Current(policy));

testing_env!(context.attached_deposit(to_yocto("1")).build());
contract.add_proposal(ProposalInput {
description: "test".to_string(),
kind: ProposalKind::ChangeConfig {
config: Config::test_config(),
},
deadline: Some(MIN_VOTING_TIME),
});
}

#[test]
#[should_panic(expected = "ERR_MIN_VOTING_TIME_IS_MISSING")]
fn test_deadline_without_min_voting_time() {
let mut context = VMContextBuilder::new();
testing_env!(context.predecessor_account_id(accounts(1)).build());
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1).into()]),
);

testing_env!(context.attached_deposit(to_yocto("1")).build());
create_proposal(&mut context, &mut contract, Some(MIN_VOTING_TIME));
}
}
Loading