Skip to content

Commit

Permalink
Migrate security/initializable (#592)
Browse files Browse the repository at this point in the history
* update cairo

* fix path

* add security mod

* add initializable

* add tests

* Update src/openzeppelin/security/initializable.cairo

Co-authored-by: Martín Triay <martriay@gmail.com>

* remove underscore

* add internal macros

---------

Co-authored-by: Martín Triay <martriay@gmail.com>
  • Loading branch information
andrew-fleming and martriay authored Mar 31, 2023
1 parent a54e98c commit 91dcad3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:
cargo build

test:
cargo run --bin cairo-test -- --starknet --path src/openzeppelin
cargo run --bin cairo-test -- --starknet --path $(SOURCE_FOLDER)

format:
cargo run --bin cairo-format -- --recursive $(SOURCE_FOLDER) --print-parsing-errors
Expand Down
1 change: 1 addition & 0 deletions src/openzeppelin/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod security;
mod token;
mod tests;
1 change: 1 addition & 0 deletions src/openzeppelin/security.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod initializable;
17 changes: 17 additions & 0 deletions src/openzeppelin/security/initializable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[contract]
mod Initializable {
struct Storage {
initialized: bool,
}

#[internal]
fn is_initialized() -> bool {
initialized::read()
}

#[internal]
fn initialize() {
assert(!is_initialized(), 'Contract already initialized');
initialized::write(true);
}
}
1 change: 1 addition & 0 deletions src/openzeppelin/tests.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod test_erc20;
mod test_initializable;
17 changes: 17 additions & 0 deletions src/openzeppelin/tests/test_initializable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use openzeppelin::security::initializable::Initializable;

#[test]
#[available_gas(2000000)]
fn test_initialize() {
assert(!Initializable::is_initialized(),'Should not be initialized');
Initializable::initialize();
assert(Initializable::is_initialized(),'Should be initialized');
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('Contract already initialized', ))]
fn test_initialize_when_initialized() {
Initializable::initialize();
Initializable::initialize();
}

0 comments on commit 91dcad3

Please sign in to comment.