Skip to content

Commit

Permalink
Bump to cairo v0.10 (#449)
Browse files Browse the repository at this point in the history
* refactor account contracts

* update cairo syntax

* bump cairo in tox

* migrate mocks

* check version in account

* fix return vals

* remove unused imports

* update syntax

* add static erc165 support

* fix return val name

* update IACCOUNT_ID

* tmp comment out nile dep

* fix syntax

* refactor event assertion, fix cached_contract

* update signers, add mock nile

* remove unused function

* fix tests

* update id

* update account id

* update interface

* update iaccount

* remove unnecessary func

* prepare signer for nile integration

* fix comments

* Update tests/mocks/ERC721SafeMintableMock.cairo

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

* Update tests/mocks/ERC721SafeMintableMock.cairo

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

* import signer from nile, add nile dep

* remove unused import

* replace res return val

* update _exists

* adjust _is_approved_or_owner, replace res

* simplify return vals

* Apply suggestions from code review

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

* fix implicit args order

* add blank line

* fix decimals check

* simplify return

* simplify return, rename res

* rename res

* remove comments

* simplify view funcs, return success from lib

* simplify return

* remove kwarg

* simplify presets

* update return var

* add from_address to events

* remove unused import

* simplify getters

* simplify getters

* simplify getters

* simplify getters

* fix return var

* simplify getters

* simplify externals

* bind return vals

* Update src/openzeppelin/account/presets/Account.cairo

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

* fix and test transferFrom

Co-authored-by: Martín Triay <martriay@gmail.com>
  • Loading branch information
andrew-fleming and martriay authored Sep 11, 2022
1 parent 789ea11 commit a9894b4
Show file tree
Hide file tree
Showing 70 changed files with 3,696 additions and 4,840 deletions.
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) {
}
}
290 changes: 133 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,145 @@ 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) {
return AccessControl_role_member.read(role, user);
}

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

//
// 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
) {
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

0 comments on commit a9894b4

Please sign in to comment.