Skip to content

Commit

Permalink
chore: pause methods
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed May 3, 2024
1 parent c3311a6 commit 7a44728
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion macros/src/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn expand(meta: PauseMeta) -> Result<TokenStream, darling::Error> {
#[#near_sdk::near]
impl #imp #me::pause::PauseExternal for #ident #ty #wher {
fn paus_is_paused(&self) -> bool {
<Self as #me::pause::Pause>::is_paused()
#me::pause::Pause::is_paused(self)
}
}
})
Expand Down
22 changes: 11 additions & 11 deletions src/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub trait Pause {
fn set_is_paused(&mut self, is_paused: bool);

/// Returns `true` if the contract is paused, `false` otherwise
fn is_paused() -> bool;
fn is_paused(&self) -> bool;

/// Pauses the contract if it is currently unpaused, panics otherwise.
/// Emits a `PauseEvent::Pause` event.
Expand All @@ -107,39 +107,39 @@ pub trait Pause {
fn unpause(&mut self);

/// Rejects if the contract is unpaused.
fn require_paused();
fn require_paused(&self);

/// Rejects if the contract is paused.
fn require_unpaused();
fn require_unpaused(&self);
}

impl<T: PauseInternal> Pause for T {
fn set_is_paused(&mut self, is_paused: bool) {
Self::slot_paused().write(&is_paused);
}

fn is_paused() -> bool {
fn is_paused(&self) -> bool {
Self::slot_paused().read().unwrap_or(false)
}

fn pause(&mut self) {
Self::require_unpaused();
self.require_unpaused();
self.set_is_paused(true);
PauseEvent::Pause.emit();
}

fn unpause(&mut self) {
Self::require_paused();
self.require_paused();
self.set_is_paused(false);
PauseEvent::Unpause.emit();
}

fn require_paused() {
require!(Self::is_paused(), UNPAUSED_FAIL_MESSAGE);
fn require_paused(&self) {
require!(self.is_paused(), UNPAUSED_FAIL_MESSAGE);
}

fn require_unpaused() {
require!(!Self::is_paused(), PAUSED_FAIL_MESSAGE);
fn require_unpaused(&self) {
require!(!self.is_paused(), PAUSED_FAIL_MESSAGE);
}
}

Expand Down Expand Up @@ -172,7 +172,7 @@ pub mod hooks {
C: Pause,
{
fn hook<R>(contract: &mut C, _args: &A, f: impl FnOnce(&mut C) -> R) -> R {
C::require_unpaused();
contract.require_unpaused();
f(contract)
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod integration {
}

pub fn set_value(&mut self, value: u32) {
Self::require_unpaused();
self.require_unpaused();
Self::require_role(&Role::CanSetValue);

let old = self.value;
Expand Down Expand Up @@ -141,7 +141,7 @@ struct MigrateIntegration {
impl MigrateHook for MigrateIntegration {
fn on_migrate(old: Integration) -> Self {
old.require_owner();
Self::require_unpaused();
old.require_unpaused();

Self {
new_value: "my string".to_string(),
Expand All @@ -161,7 +161,7 @@ impl MigrateIntegration {
}

pub fn set_value(&mut self, value: u32) {
Self::require_unpaused();
self.require_unpaused();
Self::require_role(&Role::CanSetValue);

let old = self.moved_value;
Expand Down
10 changes: 5 additions & 5 deletions tests/macros/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ struct Contract {
#[near]
impl Contract {
pub fn only_when_unpaused(&mut self, value: u32) {
Self::require_unpaused();
self.require_unpaused();

self.value = value;
}

pub fn only_when_paused(&mut self, value: u32) {
Self::require_paused();
self.require_paused();

self.value = value;
}
Expand All @@ -53,19 +53,19 @@ fn derive_pause() {
"Initial state should be unpaused",
);

Contract::require_unpaused();
contract.require_unpaused();

contract.pause();

assert!(contract.paus_is_paused(), "Pausing the contract works");

Contract::require_paused();
contract.require_paused();

contract.unpause();

assert!(!contract.paus_is_paused(), "Unpausing the contract works");

Contract::require_unpaused();
contract.require_unpaused();
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions tests/macros/standard/nep171/manual_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Hook<Contract, action::Nep171Transfer<'_>> for Contract {
_args: &action::Nep171Transfer<'_>,
f: impl FnOnce(&mut Contract) -> R,
) -> R {
Contract::require_unpaused();
contract.require_unpaused();
f(contract)
}
}
Expand All @@ -53,7 +53,7 @@ impl Contract {
}

pub fn mint(&mut self) -> TokenId {
Self::require_unpaused();
self.require_unpaused();

let token_id = format!("token_{}", self.next_token_id);
self.next_token_id += 1;
Expand Down
2 changes: 1 addition & 1 deletion tests/macros/standard/nep171/non_fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Contract {
}

pub fn mint(&mut self) -> TokenId {
Self::require_unpaused();
self.require_unpaused();

let token_id = format!("token_{}", self.next_token_id);
self.next_token_id += 1;
Expand Down

0 comments on commit 7a44728

Please sign in to comment.