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

Bump to cairo v0.10 #449

Merged
merged 59 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
2573ba2
refactor account contracts
andrew-fleming Sep 3, 2022
7361266
update cairo syntax
andrew-fleming Sep 3, 2022
4da51b8
bump cairo in tox
andrew-fleming Sep 3, 2022
b5c90e8
migrate mocks
andrew-fleming Sep 5, 2022
87fea7c
check version in account
andrew-fleming Sep 6, 2022
c7f4c8f
fix return vals
andrew-fleming Sep 7, 2022
f76e7b1
remove unused imports
andrew-fleming Sep 7, 2022
205756e
update syntax
andrew-fleming Sep 8, 2022
1c22acc
add static erc165 support
andrew-fleming Sep 8, 2022
edc3ba0
fix return val name
andrew-fleming Sep 8, 2022
2a7672f
update IACCOUNT_ID
andrew-fleming Sep 8, 2022
b383fec
tmp comment out nile dep
andrew-fleming Sep 8, 2022
b111e1c
fix syntax
andrew-fleming Sep 8, 2022
8006a3e
refactor event assertion, fix cached_contract
andrew-fleming Sep 8, 2022
91c8f78
update signers, add mock nile
andrew-fleming Sep 8, 2022
08b7711
remove unused function
andrew-fleming Sep 8, 2022
ff1d70d
fix tests
andrew-fleming Sep 8, 2022
470f489
update id
andrew-fleming Sep 8, 2022
3c9b67a
update account id
andrew-fleming Sep 8, 2022
2731a3f
update interface
andrew-fleming Sep 9, 2022
763fce0
update iaccount
andrew-fleming Sep 9, 2022
830e78b
remove unnecessary func
andrew-fleming Sep 9, 2022
1bb9255
prepare signer for nile integration
andrew-fleming Sep 9, 2022
c5eb699
fix comments
andrew-fleming Sep 9, 2022
7af7265
Update tests/mocks/ERC721SafeMintableMock.cairo
andrew-fleming Sep 10, 2022
b8fcf16
Update tests/mocks/ERC721SafeMintableMock.cairo
andrew-fleming Sep 10, 2022
21ce4d9
import signer from nile, add nile dep
andrew-fleming Sep 10, 2022
022dba9
remove unused import
andrew-fleming Sep 10, 2022
8dac356
update branch Merge remote-tracking branch 'refs/remotes/origin/cair…
andrew-fleming Sep 10, 2022
90adee2
replace res return val
andrew-fleming Sep 10, 2022
a88e119
update _exists
andrew-fleming Sep 10, 2022
64a59c9
adjust _is_approved_or_owner, replace res
andrew-fleming Sep 10, 2022
d359337
simplify return vals
andrew-fleming Sep 10, 2022
90c73f7
Apply suggestions from code review
andrew-fleming Sep 10, 2022
6fe7959
fix implicit args order
andrew-fleming Sep 10, 2022
4d98fd3
update branch Merge remote-tracking branch 'refs/remotes/origin/cairo…
andrew-fleming Sep 10, 2022
f08c2c0
add blank line
andrew-fleming Sep 10, 2022
282da9d
fix decimals check
andrew-fleming Sep 10, 2022
2a22e37
simplify return
andrew-fleming Sep 10, 2022
bf46b3d
simplify return, rename res
andrew-fleming Sep 10, 2022
d9df0ad
rename res
andrew-fleming Sep 10, 2022
62cf66f
remove comments
andrew-fleming Sep 10, 2022
499b387
simplify view funcs, return success from lib
andrew-fleming Sep 10, 2022
b510780
simplify return
andrew-fleming Sep 10, 2022
d49305c
remove kwarg
andrew-fleming Sep 10, 2022
09e39f8
simplify presets
andrew-fleming Sep 10, 2022
38d78d5
update return var
andrew-fleming Sep 10, 2022
7f67464
add from_address to events
andrew-fleming Sep 10, 2022
be8a45d
remove unused import
andrew-fleming Sep 10, 2022
c1418e2
simplify getters
andrew-fleming Sep 10, 2022
a6a0cae
simplify getters
andrew-fleming Sep 10, 2022
f179ca0
simplify getters
andrew-fleming Sep 10, 2022
453fd43
simplify getters
andrew-fleming Sep 10, 2022
c1339f3
fix return var
andrew-fleming Sep 10, 2022
1dc7904
simplify getters
andrew-fleming Sep 10, 2022
112df72
simplify externals
andrew-fleming Sep 10, 2022
49d58d0
bind return vals
andrew-fleming Sep 10, 2022
ca02a1a
Update src/openzeppelin/account/presets/Account.cairo
andrew-fleming Sep 11, 2022
31508a9
fix and test transferFrom
andrew-fleming Sep 11, 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
28 changes: 14 additions & 14 deletions src/openzeppelin/access/accesscontrol/IAccessControl.cairo
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# SPDX-License-Identifier: MIT
# OpenZeppelin Contracts for Cairo v0.3.2 (access/accesscontrol/IAccessControl.cairo)
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.3.2 (access/accesscontrol/IAccessControl.cairo)

%lang starknet

@contract_interface
namespace IAccessControl:
func hasRole(role: felt, account: felt) -> (hasRole: felt):
end
namespace IAccessControl {
func hasRole(role: felt, account: felt) -> (hasRole: felt) {
}

func getRoleAdmin(role: felt) -> (admin: felt):
end
func getRoleAdmin(role: felt) -> (admin: felt) {
}

func grantRole(role: felt, account: felt):
end
func grantRole(role: felt, account: felt) {
}

func revokeRole(role: felt, account: felt):
end
func revokeRole(role: felt, account: felt) {
}

func renounceRole(role: felt, account: felt):
end
end
func renounceRole(role: felt, account: felt) {
}
}
292 changes: 135 additions & 157 deletions 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 Contracts for Cairo v0.3.2 (access/accesscontrol/library.cairo)
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.3.2 (access/accesscontrol/library.cairo)

%lang starknet

Expand All @@ -10,169 +10,147 @@ from starkware.cairo.common.bool import TRUE, FALSE
from openzeppelin.introspection.erc165.library import ERC165
from openzeppelin.utils.constants.library import IACCESSCONTROL_ID

#
# Events
#
//
// Events
//

@event
func RoleGranted(role: felt, account: felt, sender: felt):
end
func RoleGranted(role: felt, account: felt, sender: felt) {
}

@event
func RoleRevoked(role: felt, account: felt, sender: felt):
end
func RoleRevoked(role: felt, account: felt, sender: felt) {
}

@event
func RoleAdminChanged(role: felt, previousAdminRole: felt, newAdminRole: felt):
end
func RoleAdminChanged(role: felt, previousAdminRole: felt, newAdminRole: felt) {
}

#
# Storage
#
//
// Storage
//

@storage_var
func AccessControl_role_admin(role: felt) -> (admin: felt):
end
func AccessControl_role_admin(role: felt) -> (admin: felt) {
}

@storage_var
func AccessControl_role_member(role: felt, account: felt) -> (has_role: felt):
end

namespace AccessControl:
#
# Initializer
#

func initializer{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}():
ERC165.register_interface(IACCESSCONTROL_ID)
return ()
end

#
# Modifier
#

func assert_only_role{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt):
alloc_locals
let (caller) = get_caller_address()
let (authorized) = has_role(role, caller)
with_attr error_message("AccessControl: caller is missing role {role}"):
assert authorized = TRUE
end
return ()
end

#
# Getters
#

func has_role{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt, user: felt) -> (has_role: felt):
let (authorized: felt) = AccessControl_role_member.read(role, user)
return (authorized)
end

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

#
# Externals
#

func grant_role{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt, user: felt):
let (admin: felt) = get_role_admin(role)
assert_only_role(admin)
_grant_role(role, user)
return ()
end

func revoke_role{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt, user: felt):
let (admin: felt) = get_role_admin(role)
assert_only_role(admin)
_revoke_role(role, user)
return ()
end

func renounce_role{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt, user: felt):
let (caller: felt) = get_caller_address()
with_attr error_message("AccessControl: can only renounce roles for self"):
assert user = caller
end
_revoke_role(role, user)
return ()
end

#
# Unprotected
#

func _grant_role{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt, user: felt):
let (user_has_role: felt) = has_role(role, user)
if user_has_role == FALSE:
let (caller: felt) = get_caller_address()
AccessControl_role_member.write(role, user, TRUE)
RoleGranted.emit(role, user, caller)
return ()
end
return ()
end

func _revoke_role{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt, user: felt):
let (user_has_role: felt) = has_role(role, user)
if user_has_role == TRUE:
let (caller: felt) = get_caller_address()
AccessControl_role_member.write(role, user, FALSE)
RoleRevoked.emit(role, user, caller)
return ()
end
return ()
end

func _set_role_admin{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(role: felt, admin_role: felt):
let (previous_admin_role: felt) = get_role_admin(role)
AccessControl_role_admin.write(role, admin_role)
RoleAdminChanged.emit(role, previous_admin_role, admin_role)
return ()
end
end
func AccessControl_role_member(role: felt, account: felt) -> (has_role: felt) {
}

namespace AccessControl {
//
// Initializer
//

func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
ERC165.register_interface(IACCESSCONTROL_ID);
return ();
}

//
// Modifier
//

func assert_only_role{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt
) {
alloc_locals;
let (caller) = get_caller_address();
let (authorized) = has_role(role, caller);
with_attr error_message("AccessControl: caller is missing role {role}") {
assert authorized = TRUE;
}
return ();
}

//
// Getters
//

func has_role{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt, user: felt
) -> (has_role: felt) {
let (authorized: felt) = AccessControl_role_member.read(role, user);
return (authorized,);
martriay marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

still need to resolve this

}

func get_role_admin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt
) -> (admin: felt) {
let (admin: felt) = AccessControl_role_admin.read(role);
return (admin=admin);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return (admin=admin);
return (admin);

does this work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does if we add a comma

}

//
// Externals
//

func grant_role{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt, user: felt
) {
let (admin: felt) = get_role_admin(role);
assert_only_role(admin);
_grant_role(role, user);
return ();
}

func revoke_role{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt, user: felt
) {
let (admin: felt) = get_role_admin(role);
assert_only_role(admin);
_revoke_role(role, user);
return ();
}

func renounce_role{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt, user: felt
) {
let (caller: felt) = get_caller_address();
with_attr error_message("AccessControl: can only renounce roles for self") {
assert user = caller;
}
_revoke_role(role, user);
return ();
}

//
// Unprotected
//

func _grant_role{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt, user: felt
) {
let (user_has_role: felt) = has_role(role, user);
if (user_has_role == FALSE) {
let (caller: felt) = get_caller_address();
AccessControl_role_member.write(role, user, TRUE);
RoleGranted.emit(role, user, caller);
return ();
}
return ();
}

func _revoke_role{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt, user: felt
) {
let (user_has_role: felt) = has_role(role, user);
if (user_has_role == TRUE) {
let (caller: felt) = get_caller_address();
AccessControl_role_member.write(role, user, FALSE);
RoleRevoked.emit(role, user, caller);
return ();
}
return ();
}

func _set_role_admin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
role: felt, admin_role: felt
martriay marked this conversation as resolved.
Show resolved Hide resolved
) {
let (previous_admin_role: felt) = get_role_admin(role);
AccessControl_role_admin.write(role, admin_role);
RoleAdminChanged.emit(role, previous_admin_role, admin_role);
return ();
}
}
Loading