-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix path * add security mod * add pausable * add mock * add tests * add security mod * update cargo * update cairo * Apply suggestions from code review Co-authored-by: Martín Triay <martriay@gmail.com> * remove underscore from _paused * remove unused imports * add test assertion for is_paused * move mocks inside tests crate * remove underscore * add blank line --------- Co-authored-by: Martín Triay <martriay@gmail.com>
- Loading branch information
1 parent
f48da2c
commit d671d4c
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod pausable; | ||
mod initializable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
|
||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod mock_pausable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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(); | ||
} |