Skip to content

Commit

Permalink
feat(Pausable)!: make pa_[un]pause return bool (Near-One#91)
Browse files Browse the repository at this point in the history
* feat(Pausable)!: make `pa_[un]pause` return `bool`

* test: assert return values

* test: extend coverage
  • Loading branch information
mooori authored Mar 13, 2023
1 parent 0201d44 commit 8853caf
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 74 deletions.
46 changes: 31 additions & 15 deletions near-plugins-derive/src/pausable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ pub fn derive_pausable(input: TokenStream) -> TokenStream {
}

#[#cratename::access_control_any(roles(#(#manager_roles),*))]
fn pa_pause_feature(&mut self, key: String) {
fn pa_pause_feature(&mut self, key: String) -> bool {
let mut paused_keys = self.pa_all_paused().unwrap_or_default();
paused_keys.insert(key.clone());
let newly_paused = paused_keys.insert(key.clone());

::near_sdk::log!(#cratename::events::AsEvent::event(
&#cratename::pausable::Pause {
by: ::near_sdk::env::predecessor_account_id(),
key,
}
));
if !newly_paused {
// Nothing to do since state was not modified.
return false;
}

::near_sdk::env::storage_write(
self.pa_storage_key().as_ref(),
Expand All @@ -72,20 +70,28 @@ pub fn derive_pausable(input: TokenStream) -> TokenStream {
.unwrap_or_else(|_| ::near_sdk::env::panic_str("Pausable: Unexpected error serializing keys"))
.as_ref(),
);
}

#[#cratename::access_control_any(roles(#(#manager_roles),*))]
fn pa_unpause_feature(&mut self, key: String) {
let mut paused_keys = self.pa_all_paused().unwrap_or_default();
paused_keys.remove(&key);

::near_sdk::log!(#cratename::events::AsEvent::event(
&#cratename::pausable::Unpause {
&#cratename::pausable::Pause {
by: ::near_sdk::env::predecessor_account_id(),
key,
}
));

// The feature is newly paused.
true
}

#[#cratename::access_control_any(roles(#(#manager_roles),*))]
fn pa_unpause_feature(&mut self, key: String) -> bool {
let mut paused_keys = self.pa_all_paused().unwrap_or_default();
let was_paused = paused_keys.remove(&key);

if !was_paused {
// Nothing to do since state is not modified.
return false;
}

if paused_keys.is_empty() {
::near_sdk::env::storage_remove(self.pa_storage_key().as_ref());
} else {
Expand All @@ -97,6 +103,16 @@ pub fn derive_pausable(input: TokenStream) -> TokenStream {
.as_ref(),
);
}

::near_sdk::log!(#cratename::events::AsEvent::event(
&#cratename::pausable::Unpause {
by: ::near_sdk::env::predecessor_account_id(),
key,
}
));

// The feature was paused.
true
}
}
};
Expand Down
Loading

0 comments on commit 8853caf

Please sign in to comment.