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

Refactor directory structure #350

Merged
merged 30 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6294925
add lib and preset dirs
andrew-fleming May 31, 2022
d154923
remove underscore from names, finish init reorganization
andrew-fleming May 31, 2022
850030a
fix mock names, adjusted paths
andrew-fleming Jun 1, 2022
751ea89
update branch Merge branch 'main' of https://github.com/andrew-flemin…
andrew-fleming Jun 1, 2022
eec911c
update paths in tests
andrew-fleming Jun 1, 2022
ad2b4ab
update attribution
andrew-fleming Jun 1, 2022
208a673
Rename safemath.cairo to SafeMath.cairo
andrew-fleming Jun 1, 2022
05b78a3
Rename initializable.cairo to Initializable.cairo
andrew-fleming Jun 1, 2022
ea9966f
Rename pausable.cairo to Pausable.cairo
andrew-fleming Jun 1, 2022
9588959
update version in attribution
andrew-fleming Jun 3, 2022
845ac1b
update branch Merge branch 'main' of https://github.com/andrew-flemin…
andrew-fleming Jun 13, 2022
3396b07
update branch Merge branch 'main' of https://github.com/andrew-flemin…
andrew-fleming Jun 15, 2022
1a6a6a5
update branch, fix paths
andrew-fleming Jun 17, 2022
a88af6a
add conventions to CONTRIBUTING.md
andrew-fleming Jun 17, 2022
3de6caa
update names and paths in docs
andrew-fleming Jun 18, 2022
87dc199
fix paths in code snippets
andrew-fleming Jun 18, 2022
b0d59f8
rename libs to lib
andrew-fleming Jul 14, 2022
2d03060
Apply suggestions from code review
andrew-fleming Jul 14, 2022
66fc6c4
Update CONTRIBUTING.md
andrew-fleming Jul 14, 2022
cc573e5
update link paths
andrew-fleming Jul 14, 2022
58fb80d
update structure
andrew-fleming Jul 14, 2022
0128e7e
fix compilation msg and import snippet
andrew-fleming Jul 15, 2022
d849fde
update branch
andrew-fleming Jul 15, 2022
cc9ccfb
Apply suggestions from code review
andrew-fleming Jul 15, 2022
a551c45
fix paths, move ERC721Holder
andrew-fleming Jul 15, 2022
7686794
remove upgrades dir
andrew-fleming Jul 16, 2022
8a22da1
fix pin
andrew-fleming Jul 17, 2022
1984e9d
update branch
andrew-fleming Jul 20, 2022
5cd09ad
update paths for proxy
andrew-fleming Jul 22, 2022
678093f
update links and code block imports
andrew-fleming Jul 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/ERC721.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Following the [contracts extensibility pattern](Extensibility.md), this implemen
Just be sure that the exposed `external` methods invoke their imported function logic a la `approve` invokes `ERC721.approve`. As an example, see below.

```python
from openzeppelin.token.erc721.library.ERC721 import ERC721
from openzeppelin.token.erc721.library import ERC721

@external
func approve{
Expand Down
4 changes: 2 additions & 2 deletions docs/Introspection.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ In order to ensure EVM/StarkNet compatibility, interface identifiers are not cal
For a contract to declare its support for a given interface, the contract should import the ERC165 library and register its support. It's recommended to register interface support upon contract deployment through a constructor either directly or indirectly (as an initializer) like this:

```cairo
from openzeppelin.introspection.library.ERC165 import ERC165
from openzeppelin.introspection.erc165.library import ERC165

INTERFACE_ID = 0x12345678

Expand All @@ -52,7 +52,7 @@ end
To query a target contract's support for an interface, the querying contract should call `supportsInterface` through IERC165 with the target contract's address and the queried interface id. Here's an example:

```cairo
from openzeppelin.introspection.interfaces.IERC165 import IERC165
from openzeppelin.introspection.erc165.IERC165 import IERC165

INTERFACE_ID = 0x12345678

Expand Down
8 changes: 4 additions & 4 deletions docs/Security.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The Initializable library provides a simple mechanism that mimics the functional
The recommended pattern with Initializable is to include a check that the Initializable state is `False` and invoke `initialize` in the target function like this:

```cairo
from openzeppelin.security.library.Initializable import Initializable
from openzeppelin.security.initializable.library import Initializable

@external
func foo{
Expand All @@ -44,7 +44,7 @@ The Pausable library allows contracts to implement an emergency stop mechanism.
To use the Pausable library, the contract should include `pause` and `unpause` functions (which should be protected). For methods that should be available only when not paused, insert `assert_not_paused`. For methods that should be available only when paused, insert `assert_paused`. For example:

```cairo
from openzeppelin.security.library.Pausable import Pausable
from openzeppelin.security.pausable.library import Pausable

@external
func whenNotPaused{
Expand Down Expand Up @@ -85,7 +85,7 @@ A [reentrancy attack](https://gus-tavo-guim.medium.com/reentrancy-attack-on-smar
Since Cairo does not support modifiers like Solidity, the [`reentrancy_guard`](../src/openzeppelin/security/reentrancyguard/library.cairo) library exposes two methods `_start` and `_end` to protect functions against reentrancy attacks. The protected function must call `ReentrancyGuard._start` before the first function statement, and `ReentrancyGuard._end` before the return statement, as shown below:

```cairo
from openzeppelin.security.library.ReentrancyGuard import ReentrancyGuard
from openzeppelin.security.reentrancyguard.library import ReentrancyGuard

func test_function{
syscall_ptr : felt*,
Expand All @@ -108,7 +108,7 @@ The SafeUint256 namespace in the [SafeMath library](../src/openzeppelin/security
Using SafeUint256 methods is rather straightforward. Simply import SafeUint256 and insert the arithmetic method like this:

```cairo
from openzeppelin.security.library.SafeMath import SafeUint256
from openzeppelin.security.safemath.library import SafeUint256

func add_two_uints{
syscall_ptr: felt*,
Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/access/accesscontrol/library.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Cairo Contracts v0.2.0 (access/accesscontrol/library.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (access/accesscontrol/library.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/access/ownable/library.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (access/ownable/library.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (access/ownable/library.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/account/presets/Account.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (account/presets/Account.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (account/presets/Account.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/account/presets/AddressRegistry.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (account/presets/AddressRegistry.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (account/presets/AddressRegistry.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/account/presets/EthAccount.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (account/presets/EthAccount.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (account/presets/EthAccount.cairo)

%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin, BitwiseBuiltin
Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/introspection/erc165/IERC165.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (introspection/erc165/IERC165.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (introspection/erc165/IERC165.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/introspection/erc165/library.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (introspection/erc165/library.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (introspection/erc165/library.cairo)

%lang starknet

Expand Down
131 changes: 131 additions & 0 deletions src/openzeppelin/proxy/library.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.1 (upgrades/proxy/library.cairo)

%lang starknet

from starkware.starknet.common.syscalls import get_caller_address
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.math import assert_not_zero

#
# Events
#

@event
func Upgraded(implementation: felt):
end

@event
func AdminChanged(previousAdmin: felt, newAdmin: felt):
end

#
# Storage variables
#

@storage_var
func Proxy_implementation_hash() -> (class_hash: felt):
end

@storage_var
func Proxy_admin() -> (proxy_admin: felt):
end

@storage_var
func Proxy_initialized() -> (initialized: felt):
end

namespace Proxy:
#
# Initializer
#

func initializer{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(proxy_admin: felt):
let (initialized) = Proxy_initialized.read()
with_attr error_message("Proxy: contract already initialized"):
assert initialized = FALSE
end

Proxy_initialized.write(TRUE)
_set_admin(proxy_admin)
return ()
end

#
# Guards
#

func assert_only_admin{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}():
let (caller) = get_caller_address()
let (admin) = Proxy_admin.read()
with_attr error_message("Proxy: caller is not admin"):
assert admin = caller
end
return ()
end

#
# Getters
#

func get_implementation_hash{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}() -> (implementation: felt):
let (implementation) = Proxy_implementation_hash.read()
return (implementation)
end

func get_admin{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}() -> (admin: felt):
let (admin) = Proxy_admin.read()
return (admin)
end

#
# Unprotected
#

func _set_admin{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(new_admin: felt):
let (previous_admin) = get_admin()
Proxy_admin.write(new_admin)
AdminChanged.emit(previous_admin, new_admin)
return ()
end

#
# Upgrade
#

func _set_implementation_hash{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(new_implementation: felt):
with_attr error_message("Proxy: implementation hash cannot be zero"):
assert_not_zero(new_implementation)
end

Proxy_implementation_hash.write(new_implementation)
Upgraded.emit(new_implementation)
return ()
end

end
78 changes: 78 additions & 0 deletions src/openzeppelin/proxy/presets/Proxy.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# SPDX-License-Identifier: MIT
martriay marked this conversation as resolved.
Show resolved Hide resolved
# OpenZeppelin Contracts for Cairo v0.2.1 (upgrades/proxy/presets/Proxy.cairo)

%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.starknet.common.syscalls import (
library_call,
library_call_l1_handler
)
from openzeppelin.upgrades.proxy.library import Proxy

#
# Constructor
#

@constructor
func constructor{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(implementation_hash: felt):
Proxy._set_implementation_hash(implementation_hash)
return ()
end

#
# Fallback functions
#

@external
@raw_input
@raw_output
func __default__{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(
selector: felt,
calldata_size: felt,
calldata: felt*
) -> (
retdata_size: felt,
retdata: felt*
):
let (class_hash) = Proxy.get_implementation_hash()

let (retdata_size: felt, retdata: felt*) = library_call(
class_hash=class_hash,
function_selector=selector,
calldata_size=calldata_size,
calldata=calldata,
)
return (retdata_size=retdata_size, retdata=retdata)
end


@l1_handler
@raw_input
func __l1_default__{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(
selector: felt,
calldata_size: felt,
calldata: felt*
):
let (class_hash) = Proxy.get_implementation_hash()

library_call_l1_handler(
class_hash=class_hash,
function_selector=selector,
calldata_size=calldata_size,
calldata=calldata,
)
return ()
end
2 changes: 1 addition & 1 deletion src/openzeppelin/security/initializable/library.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (security/initializable/library.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (security/initializable/library.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/security/pausable/library.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (security/pausable/library.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (security/pausable/library.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/security/reentrancyguard/library.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (security/reentrancyguard/library.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (security/reentrancyguard/library.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/security/safemath/library.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (security/safemath/library.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (security/safemath/library.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/token/erc20/IERC20.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (token/erc20/IERC20.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/IERC20.cairo)

%lang starknet

Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/token/erc20/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ from starkware.cairo.common.math import assert_not_zero, assert_lt
from starkware.cairo.common.bool import FALSE
from starkware.cairo.common.uint256 import Uint256, uint256_check, uint256_eq, uint256_not

from openzeppelin.utils.constants.library import UINT8_MAX
from openzeppelin.security.safemath.library import SafeUint256
from openzeppelin.utils.constants.library import UINT8_MAX

#
# Events
Expand Down
2 changes: 1 addition & 1 deletion src/openzeppelin/token/erc20/presets/ERC20.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (token/erc20/presets/ERC20.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/presets/ERC20.cairo)

%lang starknet

Expand Down
5 changes: 2 additions & 3 deletions src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.2.0 (token/erc20/presets/ERC20Mintable.cairo)
# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/presets/ERC20Mintable.cairo)

%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.uint256 import Uint256
from starkware.cairo.common.bool import TRUE

from openzeppelin.token.erc20.library import ERC20

from openzeppelin.access.ownable.library import Ownable
from openzeppelin.token.erc20.library import ERC20

@constructor
func constructor{
Expand Down
Loading