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

Migrate security/pausable #593

Merged
merged 17 commits into from
Mar 31, 2023
1 change: 1 addition & 0 deletions src/openzeppelin/security.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod pausable;
mod initializable;
39 changes: 39 additions & 0 deletions src/openzeppelin/security/pausable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[contract]
mod Pausable {
use starknet::ContractAddress;
use starknet::get_caller_address;

struct Storage {
paused: bool,
}

#[event]
fn Paused(account: ContractAddress) {}

#[event]
fn Unpaused(account: ContractAddress) {}
Amxx marked this conversation as resolved.
Show resolved Hide resolved

fn is_paused() -> bool {
paused::read()
}

fn assert_not_paused() {
assert(!is_paused(), 'Pausable: paused');
}

fn assert_paused() {
assert(is_paused(), 'Pausable: not paused');
}

fn pause() {
assert_not_paused();
paused::write(true);
Paused(get_caller_address());
}

fn unpause() {
assert_paused();
paused::write(false);
Unpaused(get_caller_address());
}
}
2 changes: 2 additions & 0 deletions src/openzeppelin/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
mod test_erc20;
mod test_pausable;
mod test_initializable;
mod mocks;
1 change: 1 addition & 0 deletions src/openzeppelin/tests/mocks.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod mock_pausable;
34 changes: 34 additions & 0 deletions src/openzeppelin/tests/mocks/mock_pausable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[contract]
mod MockPausable {
use openzeppelin::security::pausable::Pausable;

struct Storage {
counter: felt252
}

#[view]
fn is_paused() -> bool {
Pausable::is_paused()
}

#[view]
fn get_count() -> felt252 {
counter::read()
}

#[external]
fn assert_unpaused_and_increment() {
Pausable::assert_not_paused();
counter::write(counter::read() + 1);
}

#[external]
fn pause() {
Pausable::pause();
}

#[external]
fn unpause() {
Pausable::unpause();
}
}
47 changes: 47 additions & 0 deletions src/openzeppelin/tests/test_pausable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use openzeppelin::tests::mocks::mock_pausable::MockPausable;

#[test]
#[available_gas(2000000)]
fn test_pause_when_unpaused() {
assert(! MockPausable::is_paused(), 'Should not be paused');
assert(MockPausable::get_count() == 0, 'Should be 0');
MockPausable::assert_unpaused_and_increment();
assert(MockPausable::get_count() == 1, 'Should increment');
MockPausable::pause();
assert(MockPausable::is_paused(), 'Should be paused');
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('Pausable: paused', ))]
fn test_pause_when_paused() {
MockPausable::pause();
MockPausable::pause();
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('Pausable: paused', ))]
fn test_pause_increment() {
MockPausable::pause();
MockPausable::assert_unpaused_and_increment();
}

#[test]
#[available_gas(2000000)]
fn test_unpause_when_paused() {
MockPausable::pause();
assert(MockPausable::is_paused(), 'Should be paused');
MockPausable::unpause();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we explicitly test is_paused() is false after this?

assert(! MockPausable::is_paused(), 'Should not be paused');
MockPausable::assert_unpaused_and_increment();
assert(MockPausable::get_count() == 1, 'Should increment');
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('Pausable: not paused', ))]
fn test_unpause_when_unpaused() {
assert(! MockPausable::is_paused(), 'Should be unpaused');
MockPausable::unpause();
}