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

Integrate hooks #4

Merged
merged 29 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions astra/src/bounties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1)]),
vec![],
accounts(1)
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
);
add_bounty(&mut context, &mut contract, 2);

Expand Down Expand Up @@ -303,6 +305,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1)]),
vec![],
accounts(1)
);
let id = add_bounty(&mut context, &mut contract, 1);
contract.bounty_claim(id, U64::from(500));
Expand Down
71 changes: 69 additions & 2 deletions astra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum StorageKeys {
BountyClaimers,
BountyClaimCounts,
Blobs,
AllowedHooks,
}

/// After payouts, allows a callback
Expand Down Expand Up @@ -78,12 +79,22 @@ pub struct Contract {

/// Large blob storage.
pub blobs: LookupMap<CryptoHash, AccountId>,

/// map of the hook name --> list of accounts authorized to execute a hook.
pub allowed_hooks: LookupMap<String, Vec<AccountId>>,

/// Trust address
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
pub trust: AccountId,
}

#[near_bindgen]
impl Contract {
#[init]
pub fn new(config: Config, policy: VersionedPolicy) -> Self {
pub fn new(config: Config, policy: VersionedPolicy, allowed_hooks: Vec<(String, Vec<AccountId>)>, trust: AccountId) -> Self {
let mut allowed: LookupMap<String, Vec<AccountId>> = LookupMap::new(StorageKeys::AllowedHooks);
for hook in allowed_hooks.iter() {
allowed.insert(&hook.0,&hook.1);
}
let this = Self {
config: LazyOption::new(StorageKeys::Config, Some(&config)),
policy: LazyOption::new(StorageKeys::Policy, Some(&policy.upgrade())),
Expand All @@ -98,6 +109,8 @@ impl Contract {
bounty_claims_count: LookupMap::new(StorageKeys::BountyClaimCounts),
blobs: LookupMap::new(StorageKeys::Blobs),
locked_amount: 0,
allowed_hooks: allowed,
trust,
};
internal_set_factory_info(&FactoryInfo {
factory_id: env::predecessor_account_id(),
Expand Down Expand Up @@ -137,6 +150,48 @@ impl Contract {
pub fn get_factory_info(&self) -> FactoryInfo {
internal_get_factory_info()
}

/// Veto proposal hook
/// Check for authorities and remove proposal
/// * `id`: proposal id
pub fn veto_hook(&mut self, id: u64) {
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
let authorities = self.allowed_hooks.get(&"veto".to_string()).expect("Not allowed");
if !authorities.contains(&env::predecessor_account_id()) {
panic!("not authorized")
}

// Check if the proposal exist and is not finalized
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
let proposal: Proposal = self.proposals.get(&id).expect("Proposal doesn't exist").into();
if proposal.status == ProposalStatus::InProgress || proposal.status == ProposalStatus::Failed {
self.proposals.remove(&id);
} else {
panic!("proposal finalized")
}
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Dissolve proposal hook
/// Check for authorities and remove all the members
/// Transfer funds to trust
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
pub fn dissolve_hook(&self) {
let authorities = match self.allowed_hooks.get(&"dissolve".to_string()) {
None => panic!("unknown hook"),
Some(a) => a
};
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
// dissolve hook must be called by authorized contract (Voting Body)
if !authorities.contains(&env::predecessor_account_id()) {
panic!("not authorized")
}

// All the members are removed so operations are in freeze state
let mut policy = self.policy.get().unwrap().to_policy();
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
while !policy.roles.is_empty() {
policy.roles.pop();
}
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved

// TODO: Check what to do with bond amount
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
let funds = env::account_balance() - self.locked_amount;
Promise::new(self.trust.clone()).transfer(funds);
}
}

/// Stores attached data into blob store and returns hash of it.
Expand Down Expand Up @@ -201,6 +256,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1)]),
vec![],
accounts(1)
);
let id = create_proposal(&mut context, &mut contract);
assert_eq!(contract.get_proposal(id).proposal.description, "test");
Expand Down Expand Up @@ -246,6 +303,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1)]),
vec![],
accounts(1)
);
let id = create_proposal(&mut context, &mut contract);
assert_eq!(contract.get_proposal(id).proposal.description, "test");
Expand All @@ -260,7 +319,7 @@ mod tests {
policy.to_policy_mut().roles[1]
.permissions
.insert("*:RemoveProposal".to_string());
let mut contract = Contract::new(Config::test_config(), policy);
let mut contract = Contract::new(Config::test_config(), policy, vec![], accounts(1));
let id = create_proposal(&mut context, &mut contract);
assert_eq!(contract.get_proposal(id).proposal.description, "test");
contract.act_proposal(id, Action::RemoveProposal, None);
Expand All @@ -274,6 +333,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1)]),
vec![],
accounts(1)
);
let id = create_proposal(&mut context, &mut contract);
testing_env!(context
Expand All @@ -290,6 +351,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1), accounts(2)]),
vec![],
accounts(1)
);
let id = create_proposal(&mut context, &mut contract);
contract.act_proposal(id, Action::VoteApprove, None);
Expand All @@ -303,6 +366,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1)]),
vec![],
accounts(1)
);
testing_env!(context.attached_deposit(parse_near!("1 N")).build());
let id = contract.add_proposal(ProposalInput {
Expand All @@ -326,6 +391,8 @@ mod tests {
let mut contract = Contract::new(
Config::test_config(),
VersionedPolicy::Default(vec![accounts(1)]),
vec![],
accounts(1)
);
testing_env!(context.attached_deposit(parse_near!("1 N")).build());
let _id = contract.add_proposal(ProposalInput {
Expand Down
Loading