Skip to content

Commit

Permalink
feat: add Remove variant of AgentApplication
Browse files Browse the repository at this point in the history
This variant can be submitted and approved to remove a module that was
previously added to the whitelist.
  • Loading branch information
steinerkelvin committed Jan 1, 2025
1 parent 6c1d570 commit 4bbc50e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
33 changes: 27 additions & 6 deletions pallets/governance/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ pub struct AgentApplication<T: crate::Config> {
pub data: BoundedVec<u8, T::MaxApplicationDataLength>,
pub cost: BalanceOf<T>,
pub expires_at: Block,
pub action: ApplicationAction,
pub status: ApplicationStatus,
}

// pub enum ProposalAction {
// Add,
// Remove,
// }
#[derive(DebugNoBound, Decode, Encode, TypeInfo, MaxEncodedLen, PartialEq, Eq)]
pub enum ApplicationAction {
Add,
Remove,
}

#[derive(DebugNoBound, Decode, Encode, TypeInfo, MaxEncodedLen, PartialEq, Eq)]
pub enum ApplicationStatus {
Expand All @@ -38,11 +40,16 @@ pub fn submit_application<T: crate::Config>(
payer: AccountIdOf<T>,
agent_key: AccountIdOf<T>,
data: Vec<u8>,
removing: bool,
) -> DispatchResult {
if whitelist::is_whitelisted::<T>(&agent_key) {
if !removing && whitelist::is_whitelisted::<T>(&agent_key) {
return Err(crate::Error::<T>::AlreadyWhitelisted.into());
} else if whitelist::is_whitelisted::<T>(&agent_key) {
return Err(crate::Error::<T>::NotWhitelisted.into());
}

// TODO: check if there is an application for the agent key already?

let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.agent_application_cost;

Expand Down Expand Up @@ -77,6 +84,12 @@ pub fn submit_application<T: crate::Config>(
last_id
});

let action = if removing {
ApplicationAction::Remove
} else {
ApplicationAction::Add
};

let application = AgentApplication::<T> {
id: next_id,
payer_key: payer,
Expand All @@ -85,6 +98,7 @@ pub fn submit_application<T: crate::Config>(
cost,
expires_at,
status: ApplicationStatus::Open,
action,
};

crate::AgentApplications::<T>::insert(next_id, application);
Expand All @@ -97,7 +111,14 @@ pub fn accept_application<T: crate::Config>(application_id: u32) -> DispatchResu
let application = crate::AgentApplications::<T>::get(application_id)
.ok_or(crate::Error::<T>::ApplicationNotFound)?;

whitelist::add_to_whitelist::<T>(application.agent_key.clone())?;
match application.action {
ApplicationAction::Add => {
whitelist::add_to_whitelist::<T>(application.agent_key.clone())?;
}
ApplicationAction::Remove => {
whitelist::remove_from_whitelist::<T>(application.agent_key.clone())?;
}
}

crate::AgentApplications::<T>::mutate(application_id, |application| {
if let Some(app) = application {
Expand Down
3 changes: 2 additions & 1 deletion pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ pub mod pallet {
origin: OriginFor<T>,
agent_key: AccountIdOf<T>,
metadata: Vec<u8>,
removing: bool,
) -> DispatchResult {
let payer = ensure_signed(origin)?;
application::submit_application::<T>(payer, agent_key, metadata)
application::submit_application::<T>(payer, agent_key, metadata, removing)
}

#[pallet::call_index(8)]
Expand Down

0 comments on commit 4bbc50e

Please sign in to comment.