From a9894b4e5cd59efb63757e846c603a5c64b6b44a Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Sun, 11 Sep 2022 18:51:34 -0400 Subject: [PATCH] Bump to cairo v0.10 (#449) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * Update tests/mocks/ERC721SafeMintableMock.cairo Co-authored-by: Martín Triay * 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 * 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 * fix and test transferFrom Co-authored-by: Martín Triay --- .../access/accesscontrol/IAccessControl.cairo | 28 +- .../access/accesscontrol/library.cairo | 290 +++--- src/openzeppelin/access/ownable/library.cairo | 176 ++-- src/openzeppelin/account/IAccount.cairo | 42 +- src/openzeppelin/account/library.cairo | 518 ++++------ .../account/presets/Account.cairo | 168 ++- .../account/presets/AddressRegistry.cairo | 37 +- .../account/presets/EthAccount.cairo | 176 ++-- .../introspection/erc165/IERC165.cairo | 12 +- .../introspection/erc165/library.cairo | 54 +- .../security/initializable/library.cairo | 47 +- .../security/pausable/library.cairo | 133 +-- .../security/reentrancyguard/library.cairo | 46 +- .../security/safemath/library.cairo | 198 ++-- src/openzeppelin/token/erc20/IERC20.cairo | 49 +- src/openzeppelin/token/erc20/library.cairo | 612 +++++------ .../token/erc20/presets/ERC20.cairo | 187 ++-- .../token/erc20/presets/ERC20Burnable.cairo | 219 ++-- .../token/erc20/presets/ERC20Mintable.cairo | 243 ++--- .../token/erc20/presets/ERC20Pausable.cairo | 276 ++--- .../erc20/presets/ERC20Upgradeable.cairo | 217 ++-- src/openzeppelin/token/erc721/IERC721.cairo | 46 +- .../token/erc721/IERC721Metadata.cairo | 20 +- .../token/erc721/IERC721Receiver.cairo | 18 +- .../erc721/enumerable/IERC721Enumerable.cairo | 20 +- .../token/erc721/enumerable/library.cairo | 389 ++++--- .../ERC721EnumerableMintableBurnable.cairo | 339 +++---- .../presets/utils/ERC721Holder.cairo | 41 +- src/openzeppelin/token/erc721/library.cairo | 957 ++++++++---------- .../presets/ERC721MintableBurnable.cairo | 297 +++--- .../presets/ERC721MintablePausable.cairo | 330 +++--- src/openzeppelin/upgrades/library.cairo | 206 ++-- src/openzeppelin/upgrades/presets/Proxy.cairo | 79 +- .../utils/constants/library.cairo | 50 +- tests/access/test_AccessControl.py | 14 +- tests/access/test_Ownable.py | 2 +- tests/account/test_Account.py | 41 +- tests/account/test_EthAccount.py | 37 +- tests/introspection/test_ERC165.py | 5 +- tests/mocks/AccessControl.cairo | 113 +-- tests/mocks/AccountReentrancy.cairo | 74 +- tests/mocks/ERC165.cairo | 29 +- tests/mocks/ERC721SafeMintableMock.cairo | 265 ++--- tests/mocks/Initializable.cairo | 26 +- tests/mocks/Ownable.cairo | 65 +- tests/mocks/Pausable.cairo | 101 +- tests/mocks/ProxiableImplementation.cairo | 94 +- tests/mocks/ReentrancyAttackerMock.cairo | 24 +- tests/mocks/ReentrancyMock.cairo | 186 ++-- tests/mocks/SafeMathMock.cairo | 67 +- tests/mocks/UpgradesMockV1.cairo | 91 +- tests/mocks/UpgradesMockV2.cairo | 160 ++- tests/security/test_initializable.py | 4 +- tests/security/test_pausable.py | 42 +- tests/security/test_reentrancy.py | 8 +- tests/security/test_safemath.py | 34 +- tests/signers.py | 92 +- tests/token/erc20/test_ERC20.py | 78 +- tests/token/erc20/test_ERC20Burnable.py | 25 +- tests/token/erc20/test_ERC20Mintable.py | 2 +- tests/token/erc20/test_ERC20Pausable.py | 24 +- tests/token/erc20/test_ERC20Upgradeable.py | 43 +- .../test_ERC721EnumerableMintableBurnable.py | 44 +- .../erc721/test_ERC721MintableBurnable.py | 161 ++- .../erc721/test_ERC721MintablePausable.py | 4 +- .../erc721/test_ERC721SafeMintableMock.py | 2 +- tests/upgrades/test_Proxy.py | 6 +- tests/upgrades/test_upgrades.py | 12 +- tests/utils.py | 37 +- tox.ini | 4 +- 70 files changed, 3696 insertions(+), 4840 deletions(-) diff --git a/src/openzeppelin/access/accesscontrol/IAccessControl.cairo b/src/openzeppelin/access/accesscontrol/IAccessControl.cairo index 38f9f32c2..adb6dbd54 100644 --- a/src/openzeppelin/access/accesscontrol/IAccessControl.cairo +++ b/src/openzeppelin/access/accesscontrol/IAccessControl.cairo @@ -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) { + } +} diff --git a/src/openzeppelin/access/accesscontrol/library.cairo b/src/openzeppelin/access/accesscontrol/library.cairo index f85ae5e77..fb7e43122 100644 --- a/src/openzeppelin/access/accesscontrol/library.cairo +++ b/src/openzeppelin/access/accesscontrol/library.cairo @@ -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 @@ -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 (); + } +} diff --git a/src/openzeppelin/access/ownable/library.cairo b/src/openzeppelin/access/ownable/library.cairo index d3ad8340b..b8f6159f7 100644 --- a/src/openzeppelin/access/ownable/library.cairo +++ b/src/openzeppelin/access/ownable/library.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (access/ownable/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (access/ownable/library.cairo) %lang starknet @@ -7,105 +7,83 @@ from starkware.starknet.common.syscalls import get_caller_address from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.math import assert_not_zero -# -# Events -# +// +// Events +// @event -func OwnershipTransferred(previousOwner: felt, newOwner: felt): -end +func OwnershipTransferred(previousOwner: felt, newOwner: felt) { +} -# -# Storage -# +// +// Storage +// @storage_var -func Ownable_owner() -> (owner: felt): -end - -namespace Ownable: - # - # Constructor - # - - func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt): - _transfer_ownership(owner) - return () - end - - # - # Protector (Modifier) - # - - func assert_only_owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - let (owner) = Ownable.owner() - let (caller) = get_caller_address() - with_attr error_message("Ownable: caller is the zero address"): - assert_not_zero(caller) - end - with_attr error_message("Ownable: caller is not the owner"): - assert owner = caller - end - return () - end - - # - # Public - # - - func owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (owner: felt): - let (owner) = Ownable_owner.read() - return (owner=owner) - end - - func transfer_ownership{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_owner: felt): - with_attr error_message("Ownable: new owner is the zero address"): - assert_not_zero(new_owner) - end - assert_only_owner() - _transfer_ownership(new_owner) - return () - end - - func renounce_ownership{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - assert_only_owner() - _transfer_ownership(0) - return () - end - - # - # Internal - # - - func _transfer_ownership{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_owner: felt): - let (previous_owner: felt) = Ownable.owner() - Ownable_owner.write(new_owner) - OwnershipTransferred.emit(previous_owner, new_owner) - return () - end - -end +func Ownable_owner() -> (owner: felt) { +} + +namespace Ownable { + // + // Initializer + // + + func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(owner: felt) { + _transfer_ownership(owner); + return (); + } + + // + // Guards + // + + func assert_only_owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (owner) = Ownable.owner(); + let (caller) = get_caller_address(); + with_attr error_message("Ownable: caller is the zero address") { + assert_not_zero(caller); + } + with_attr error_message("Ownable: caller is not the owner") { + assert owner = caller; + } + return (); + } + + // + // Public + // + + func owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable_owner.read(); + } + + func transfer_ownership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_owner: felt + ) { + with_attr error_message("Ownable: new owner is the zero address") { + assert_not_zero(new_owner); + } + assert_only_owner(); + _transfer_ownership(new_owner); + return (); + } + + func renounce_ownership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + assert_only_owner(); + _transfer_ownership(0); + return (); + } + + // + // Internal + // + + func _transfer_ownership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_owner: felt + ) { + let (previous_owner: felt) = Ownable.owner(); + Ownable_owner.write(new_owner); + OwnershipTransferred.emit(previous_owner, new_owner); + return (); + } +} diff --git a/src/openzeppelin/account/IAccount.cairo b/src/openzeppelin/account/IAccount.cairo index 977ad53a7..f6151c478 100644 --- a/src/openzeppelin/account/IAccount.cairo +++ b/src/openzeppelin/account/IAccount.cairo @@ -1,37 +1,29 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (account/IAccount.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (account/IAccount.cairo) %lang starknet from openzeppelin.account.library import AccountCallArray @contract_interface -namespace IAccount: +namespace IAccount { - # - # Getters - # + func supportsInterface(interfaceId: felt) -> (success: felt) { + } - func get_nonce() -> (res : felt): - end + func isValidSignature(hash: felt, signature_len: felt, signature: felt*) -> (isValid: felt) { + } - # - # Business logic - # + func __validate__( + call_array_len: felt, call_array: AccountCallArray*, calldata_len: felt, calldata: felt* + ) { + } - func is_valid_signature( - hash: felt, - signature_len: felt, - signature: felt* - ) -> (is_valid: felt): - end + func __validate_declare__(class_hash: felt) { + } func __execute__( - call_array_len: felt, - call_array: AccountCallArray*, - calldata_len: felt, - calldata: felt*, - nonce: felt - ) -> (response_len: felt, response: felt*): - end -end + call_array_len: felt, call_array: AccountCallArray*, calldata_len: felt, calldata: felt* + ) -> (response_len: felt, response: felt*) { + } +} diff --git a/src/openzeppelin/account/library.cairo b/src/openzeppelin/account/library.cairo index d535da497..0c620cb71 100644 --- a/src/openzeppelin/account/library.cairo +++ b/src/openzeppelin/account/library.cairo @@ -1,334 +1,248 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (account/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (account/library.cairo) %lang starknet from starkware.cairo.common.registers import get_fp_and_pc -from starkware.starknet.common.syscalls import get_contract_address from starkware.cairo.common.signature import verify_ecdsa_signature from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin, BitwiseBuiltin from starkware.cairo.common.alloc import alloc from starkware.cairo.common.uint256 import Uint256 from starkware.cairo.common.memcpy import memcpy from starkware.cairo.common.math import split_felt -from starkware.cairo.common.bool import TRUE -from starkware.starknet.common.syscalls import call_contract, get_caller_address, get_tx_info +from starkware.cairo.common.bool import TRUE, FALSE +from starkware.starknet.common.syscalls import ( + call_contract, + get_caller_address, + get_contract_address, + get_tx_info +) from starkware.cairo.common.cairo_secp.signature import verify_eth_signature_uint256 -from openzeppelin.introspection.erc165.library import ERC165 +from openzeppelin.utils.constants.library import IACCOUNT_ID, IERC165_ID -from openzeppelin.utils.constants.library import IACCOUNT_ID - -# -# Storage -# - -@storage_var -func Account_current_nonce() -> (res: felt): -end +// +// Storage +// @storage_var -func Account_public_key() -> (res: felt): -end - -# -# Structs -# - -struct Call: - member to: felt - member selector: felt - member calldata_len: felt - member calldata: felt* -end - -# Tmp struct introduced while we wait for Cairo -# to support passing `[AccountCall]` to __execute__ -struct AccountCallArray: - member to: felt - member selector: felt - member data_offset: felt - member data_len: felt -end - -namespace Account: - - # - # Initializer - # - - func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(_public_key: felt): - Account_public_key.write(_public_key) - ERC165.register_interface(IACCOUNT_ID) - return() - end - - # - # Guards - # - - func assert_only_self{syscall_ptr : felt*}(): - let (self) = get_contract_address() - let (caller) = get_caller_address() - with_attr error_message("Account: caller is not this account"): - assert self = caller - end - return () - end - - # - # Getters - # - - func get_public_key{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Account_public_key.read() - return (res=res) - end - - func get_nonce{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Account_current_nonce.read() - return (res=res) - end - - # - # Setters - # - - func set_public_key{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_public_key: felt): - assert_only_self() - Account_public_key.write(new_public_key) - return () - end - - # - # Business logic - # +func Account_public_key() -> (public_key: felt) { +} + +// +// Structs +// + +struct Call { + to: felt, + selector: felt, + calldata_len: felt, + calldata: felt*, +} + +// Tmp struct introduced while we wait for Cairo +// to support passing `[AccountCall]` to __execute__ +struct AccountCallArray { + to: felt, + selector: felt, + data_offset: felt, + data_len: felt, +} + +namespace Account { + // + // Initializer + // + + func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + _public_key: felt + ) { + Account_public_key.write(_public_key); + return (); + } + + // + // Guards + // + + func assert_only_self{syscall_ptr: felt*}() { + let (self) = get_contract_address(); + let (caller) = get_caller_address(); + with_attr error_message("Account: caller is not this account") { + assert self = caller; + } + return (); + } + + // + // Getters + // + + func get_public_key{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + public_key: felt + ) { + return Account_public_key.read(); + } + + func supports_interface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(interface_id: felt) -> ( + success: felt + ) { + if (interface_id == IERC165_ID) { + return (success=TRUE); + } + if (interface_id == IACCOUNT_ID) { + return (success=TRUE); + } + return (success=FALSE); + } + + // + // Setters + // + + func set_public_key{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_public_key: felt + ) { + assert_only_self(); + Account_public_key.write(new_public_key); + return (); + } + + // + // Business logic + // func is_valid_signature{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin* - }( - hash: felt, - signature_len: felt, - signature: felt* - ) -> (is_valid: felt): - let (_public_key) = Account_public_key.read() - - # This interface expects a signature pointer and length to make - # no assumption about signature validation schemes. - # But this implementation does, and it expects a (sig_r, sig_s) pair. - let sig_r = signature[0] - let sig_s = signature[1] + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + ecdsa_ptr: SignatureBuiltin*, + range_check_ptr, + }(hash: felt, signature_len: felt, signature: felt*) -> (is_valid: felt) { + let (_public_key) = Account_public_key.read(); + + // This interface expects a signature pointer and length to make + // no assumption about signature validation schemes. + // But this implementation does, and it expects a (sig_r, sig_s) pair. + let sig_r = signature[0]; + let sig_s = signature[1]; verify_ecdsa_signature( - message=hash, - public_key=_public_key, - signature_r=sig_r, - signature_s=sig_s) + message=hash, public_key=_public_key, signature_r=sig_r, signature_s=sig_s + ); - return (is_valid=TRUE) - end + return (is_valid=TRUE); + } func is_valid_eth_signature{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - bitwise_ptr: BitwiseBuiltin*, - range_check_ptr - }( - hash: felt, - signature_len: felt, - signature: felt* - ) -> (is_valid: felt): - alloc_locals - let (_public_key) = get_public_key() - let (__fp__, _) = get_fp_and_pc() - - # This interface expects a signature pointer and length to make - # no assumption about signature validation schemes. - # But this implementation does, and it expects a the sig_v, sig_r, - # sig_s, and hash elements. - let sig_v : felt = signature[0] - let sig_r : Uint256 = Uint256(low=signature[1], high=signature[2]) - let sig_s : Uint256 = Uint256(low=signature[3], high=signature[4]) - let (high, low) = split_felt(hash) - let msg_hash : Uint256 = Uint256(low=low, high=high) - - let (local keccak_ptr : felt*) = alloc() - - with keccak_ptr: + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + bitwise_ptr: BitwiseBuiltin*, + range_check_ptr, + }(hash: felt, signature_len: felt, signature: felt*) -> (is_valid: felt) { + alloc_locals; + let (_public_key) = get_public_key(); + let (__fp__, _) = get_fp_and_pc(); + + // This interface expects a signature pointer and length to make + // no assumption about signature validation schemes. + // But this implementation does, and it expects a the sig_v, sig_r, + // sig_s, and hash elements. + let sig_v: felt = signature[0]; + let sig_r: Uint256 = Uint256(low=signature[1], high=signature[2]); + let sig_s: Uint256 = Uint256(low=signature[3], high=signature[4]); + let (high, low) = split_felt(hash); + let msg_hash: Uint256 = Uint256(low=low, high=high); + + let (local keccak_ptr: felt*) = alloc(); + + with keccak_ptr { verify_eth_signature_uint256( - msg_hash=msg_hash, - r=sig_r, - s=sig_s, - v=sig_v, - eth_address=_public_key) - end + msg_hash=msg_hash, r=sig_r, s=sig_s, v=sig_v, eth_address=_public_key + ); + } - return (is_valid=TRUE) - end + return (is_valid=TRUE); + } func execute{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin*, - bitwise_ptr: BitwiseBuiltin* - }( - call_array_len: felt, - call_array: AccountCallArray*, - calldata_len: felt, - calldata: felt*, - nonce: felt - ) -> (response_len: felt, response: felt*): - alloc_locals - - let (__fp__, _) = get_fp_and_pc() - let (tx_info) = get_tx_info() - - # validate transaction - with_attr error_message("Account: invalid signature"): - let (is_valid) = is_valid_signature(tx_info.transaction_hash, tx_info.signature_len, tx_info.signature) - assert is_valid = TRUE - end - - return _unsafe_execute(call_array_len, call_array, calldata_len, calldata, nonce) - end - - func eth_execute{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin*, - bitwise_ptr: BitwiseBuiltin* - }( - call_array_len: felt, - call_array: AccountCallArray*, - calldata_len: felt, - calldata: felt*, - nonce: felt - ) -> (response_len: felt, response: felt*): - alloc_locals - - let (__fp__, _) = get_fp_and_pc() - let (tx_info) = get_tx_info() - - # validate transaction - with_attr error_message("Account: invalid secp256k1 signature"): - let (is_valid) = is_valid_eth_signature(tx_info.transaction_hash, tx_info.signature_len, tx_info.signature) - assert is_valid = TRUE - end - - return _unsafe_execute(call_array_len, call_array, calldata_len, calldata, nonce) - end - - func _unsafe_execute{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin*, - bitwise_ptr: BitwiseBuiltin* - }( - call_array_len: felt, - call_array: AccountCallArray*, - calldata_len: felt, - calldata: felt*, - nonce: felt - ) -> (response_len: felt, response: felt*): - alloc_locals - - let (caller) = get_caller_address() - with_attr error_message("Account: no reentrant call"): - assert caller = 0 - end - - # validate nonce - - let (_current_nonce) = Account_current_nonce.read() - - with_attr error_message("Account: nonce is invalid"): - assert _current_nonce = nonce - end - - # bump nonce - Account_current_nonce.write(_current_nonce + 1) - - # TMP: Convert `AccountCallArray` to 'Call'. - let (calls : Call*) = alloc() - _from_call_array_to_call(call_array_len, call_array, calldata, calls) - let calls_len = call_array_len - - # execute call - let (response : felt*) = alloc() - let (response_len) = _execute_list(calls_len, calls, response) - - return (response_len=response_len, response=response) - end - - func _execute_list{syscall_ptr: felt*}( - calls_len: felt, - calls: Call*, - response: felt* - ) -> (response_len: felt): - alloc_locals - - # if no more calls - if calls_len == 0: - return (0) - end - - # do the current call - let this_call: Call = [calls] + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + ecdsa_ptr: SignatureBuiltin*, + bitwise_ptr: BitwiseBuiltin*, + range_check_ptr, + }(call_array_len: felt, call_array: AccountCallArray*, calldata_len: felt, calldata: felt*) -> ( + response_len: felt, response: felt* + ) { + alloc_locals; + + let (tx_info) = get_tx_info(); + with_attr error_message("Account: invalid tx version") { + assert tx_info.version = 1; + } + + // assert not a reentrant call + let (caller) = get_caller_address(); + with_attr error_message("Account: no reentrant call") { + assert caller = 0; + } + + // TMP: Convert `AccountCallArray` to 'Call'. + let (calls: Call*) = alloc(); + _from_call_array_to_call(call_array_len, call_array, calldata, calls); + let calls_len = call_array_len; + + // execute call + let (response: felt*) = alloc(); + let (response_len) = _execute_list(calls_len, calls, response); + + return (response_len=response_len, response=response); + } + + func _execute_list{syscall_ptr: felt*}(calls_len: felt, calls: Call*, response: felt*) -> ( + response_len: felt + ) { + alloc_locals; + + // if no more calls + if (calls_len == 0) { + return (response_len=0); + } + + // do the current call + let this_call: Call = [calls]; let res = call_contract( contract_address=this_call.to, function_selector=this_call.selector, calldata_size=this_call.calldata_len, - calldata=this_call.calldata - ) - # copy the result in response - memcpy(response, res.retdata, res.retdata_size) - # do the next calls recursively - let (response_len) = _execute_list(calls_len - 1, calls + Call.SIZE, response + res.retdata_size) - return (response_len + res.retdata_size) - end + calldata=this_call.calldata, + ); + // copy the result in response + memcpy(response, res.retdata, res.retdata_size); + // do the next calls recursively + let (response_len) = _execute_list( + calls_len - 1, calls + Call.SIZE, response + res.retdata_size + ); + return (response_len=response_len + res.retdata_size); + } func _from_call_array_to_call{syscall_ptr: felt*}( - call_array_len: felt, - call_array: AccountCallArray*, - calldata: felt*, - calls: Call* - ): - # if no more calls - if call_array_len == 0: - return () - end - - # parse the current call + call_array_len: felt, call_array: AccountCallArray*, calldata: felt*, calls: Call* + ) { + // if no more calls + if (call_array_len == 0) { + return (); + } + + // parse the current call assert [calls] = Call( - to=[call_array].to, - selector=[call_array].selector, - calldata_len=[call_array].data_len, - calldata=calldata + [call_array].data_offset - ) - # parse the remaining calls recursively - _from_call_array_to_call(call_array_len - 1, call_array + AccountCallArray.SIZE, calldata, calls + Call.SIZE) - return () - end - -end + to=[call_array].to, + selector=[call_array].selector, + calldata_len=[call_array].data_len, + calldata=calldata + [call_array].data_offset + ); + // parse the remaining calls recursively + _from_call_array_to_call( + call_array_len - 1, call_array + AccountCallArray.SIZE, calldata, calls + Call.SIZE + ); + return (); + } +} diff --git a/src/openzeppelin/account/presets/Account.cairo b/src/openzeppelin/account/presets/Account.cairo index 9cfa2e03a..64f4b6112 100644 --- a/src/openzeppelin/account/presets/Account.cairo +++ b/src/openzeppelin/account/presets/Account.cairo @@ -1,115 +1,99 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (account/presets/Account.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (account/presets/Account.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin, BitwiseBuiltin +from starkware.starknet.common.syscalls import get_tx_info from openzeppelin.account.library import Account, AccountCallArray -from openzeppelin.introspection.erc165.library import ERC165 -# -# Constructor -# +// +// Constructor +// @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(public_key: felt): - Account.initializer(public_key) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + public_key: felt +) { + Account.initializer(public_key); + return (); +} -@view -func get_public_key{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Account.get_public_key() - return (res=res) -end +// +// Getters +// @view -func get_nonce{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Account.get_nonce() - return (res=res) -end +func getPublicKey{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + publicKey: felt +) { + let (publicKey: felt) = Account.get_public_key(); + return (publicKey=publicKey); +} @view -func supportsInterface{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end - -# -# Setters -# +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return Account.supports_interface(interfaceId); +} + +// +// Setters +// @external -func set_public_key{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_public_key: felt): - Account.set_public_key(new_public_key) - return () -end - -# -# Business logic -# +func setPublicKey{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + newPublicKey: felt +) { + Account.set_public_key(newPublicKey); + return (); +} + +// +// Business logic +// @view -func is_valid_signature{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin* - }( - hash: felt, - signature_len: felt, - signature: felt* - ) -> (is_valid: felt): - let (is_valid) = Account.is_valid_signature(hash, signature_len, signature) - return (is_valid=is_valid) -end +func isValidSignature{ + syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, ecdsa_ptr: SignatureBuiltin*, range_check_ptr +}(hash: felt, signature_len: felt, signature: felt*) -> (isValid: felt) { + let (isValid: felt) = Account.is_valid_signature(hash, signature_len, signature); + return (isValid=isValid); +} + +@external +func __validate__{ + syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, ecdsa_ptr: SignatureBuiltin*, range_check_ptr +}(call_array_len: felt, call_array: AccountCallArray*, calldata_len: felt, calldata: felt*) { + let (tx_info) = get_tx_info(); + Account.is_valid_signature(tx_info.transaction_hash, tx_info.signature_len, tx_info.signature); + return (); +} + +@external +func __validate_declare__{ + syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, ecdsa_ptr: SignatureBuiltin*, range_check_ptr +}(class_hash: felt) { + let (tx_info) = get_tx_info(); + Account.is_valid_signature(tx_info.transaction_hash, tx_info.signature_len, tx_info.signature); + return (); +} @external func __execute__{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin*, - bitwise_ptr: BitwiseBuiltin* - }( - call_array_len: felt, - call_array: AccountCallArray*, - calldata_len: felt, - calldata: felt*, - nonce: felt - ) -> (response_len: felt, response: felt*): + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + ecdsa_ptr: SignatureBuiltin*, + bitwise_ptr: BitwiseBuiltin*, + range_check_ptr, +}(call_array_len: felt, call_array: AccountCallArray*, calldata_len: felt, calldata: felt*) -> ( + response_len: felt, response: felt* +) { let (response_len, response) = Account.execute( - call_array_len, - call_array, - calldata_len, - calldata, - nonce - ) - return (response_len=response_len, response=response) -end + call_array_len, call_array, calldata_len, calldata + ); + return (response_len, response); +} diff --git a/src/openzeppelin/account/presets/AddressRegistry.cairo b/src/openzeppelin/account/presets/AddressRegistry.cairo index 58d6e2ebc..0da755211 100644 --- a/src/openzeppelin/account/presets/AddressRegistry.cairo +++ b/src/openzeppelin/account/presets/AddressRegistry.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (account/presets/AddressRegistry.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (account/presets/AddressRegistry.cairo) %lang starknet @@ -7,26 +7,21 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.starknet.common.syscalls import get_caller_address @storage_var -func L1_address(L2_address: felt) -> (res: felt): -end +func L1_address(L2_address: felt) -> (address: felt) { +} @external -func get_L1_address{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(L2_address: felt) -> (res: felt): - let (res) = L1_address.read(L2_address) - return (res=res) -end +func get_L1_address{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + L2_address: felt +) -> (address: felt) { + return L1_address.read(L2_address); +} @external -func set_L1_address{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_L1_address: felt): - let (caller) = get_caller_address() - L1_address.write(caller, new_L1_address) - return () -end +func set_L1_address{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_L1_address: felt +) { + let (caller) = get_caller_address(); + L1_address.write(caller, new_L1_address); + return (); +} diff --git a/src/openzeppelin/account/presets/EthAccount.cairo b/src/openzeppelin/account/presets/EthAccount.cairo index b36e29b2c..db0423e65 100644 --- a/src/openzeppelin/account/presets/EthAccount.cairo +++ b/src/openzeppelin/account/presets/EthAccount.cairo @@ -1,114 +1,106 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (account/presets/EthAccount.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (account/presets/EthAccount.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin, BitwiseBuiltin +from starkware.starknet.common.syscalls import get_tx_info from openzeppelin.account.library import Account, AccountCallArray -from openzeppelin.introspection.erc165.library import ERC165 -# -# Constructor -# +// +// Constructor +// @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(eth_address: felt): - Account.initializer(eth_address) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + eth_address: felt +) { + Account.initializer(eth_address); + return (); +} -# -# Getters -# +// +// Getters +// @view -func get_eth_address{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Account.get_public_key() - return (res=res) -end +func getEthAddress{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + ethAddress: felt +) { + let (ethAddress: felt) = Account.get_public_key(); + return (ethAddress=ethAddress); +} @view -func get_nonce{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Account.get_nonce() - return (res=res) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return Account.supports_interface(interfaceId); +} -@view -func supportsInterface{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end - -# -# Setters -# +// +// Setters +// @external -func set_eth_address{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_eth_address: felt): - Account.set_public_key(new_eth_address) - return () -end +func setEthAddress{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + newEthAddress: felt +) { + Account.set_public_key(newEthAddress); + return (); +} -# -# Business logic -# +// +// Business logic +// @view -func is_valid_signature{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin*, - bitwise_ptr: BitwiseBuiltin* - }( - hash: felt, - signature_len: felt, - signature: felt* - ) -> (is_valid: felt): - let (is_valid) = Account.is_valid_eth_signature(hash, signature_len, signature) - return (is_valid=is_valid) -end +func isValidSignature{ + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + bitwise_ptr: BitwiseBuiltin*, + range_check_ptr, +}(hash: felt, signature_len: felt, signature: felt*) -> (isValid: felt) { + let (isValid) = Account.is_valid_eth_signature(hash, signature_len, signature); + return (isValid=isValid); +} + +@external +func __validate__{ + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + bitwise_ptr: BitwiseBuiltin*, + range_check_ptr, +}(call_array_len: felt, call_array: AccountCallArray*, calldata_len: felt, calldata: felt*) { + let (tx_info) = get_tx_info(); + Account.is_valid_eth_signature(tx_info.transaction_hash, tx_info.signature_len, tx_info.signature); + return (); +} + +@external +func __validate_declare__{ + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + bitwise_ptr: BitwiseBuiltin*, + range_check_ptr, +}(class_hash: felt) { + let (tx_info) = get_tx_info(); + Account.is_valid_eth_signature(tx_info.transaction_hash, tx_info.signature_len, tx_info.signature); + return (); +} @external func __execute__{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr, - ecdsa_ptr: SignatureBuiltin*, - bitwise_ptr: BitwiseBuiltin* - }( - call_array_len: felt, - call_array: AccountCallArray*, - calldata_len: felt, - calldata: felt*, - nonce: felt - ) -> (response_len: felt, response: felt*): - let (response_len, response) = Account.eth_execute( - call_array_len, - call_array, - calldata_len, - calldata, - nonce - ) - return (response_len=response_len, response=response) -end + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, + ecdsa_ptr: SignatureBuiltin*, + bitwise_ptr: BitwiseBuiltin*, + range_check_ptr, +}(call_array_len: felt, call_array: AccountCallArray*, calldata_len: felt, calldata: felt*) -> ( + response_len: felt, response: felt* +) { + let (response_len, response) = Account.execute( + call_array_len, call_array, calldata_len, calldata + ); + return (response_len, response); +} diff --git a/src/openzeppelin/introspection/erc165/IERC165.cairo b/src/openzeppelin/introspection/erc165/IERC165.cairo index 54f224df5..1828c1abd 100644 --- a/src/openzeppelin/introspection/erc165/IERC165.cairo +++ b/src/openzeppelin/introspection/erc165/IERC165.cairo @@ -1,10 +1,10 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (introspection/erc165/IERC165.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (introspection/erc165/IERC165.cairo) %lang starknet @contract_interface -namespace IERC165: - func supportsInterface(interfaceId: felt) -> (success: felt): - end -end +namespace IERC165 { + func supportsInterface(interfaceId: felt) -> (success: felt) { + } +} diff --git a/src/openzeppelin/introspection/erc165/library.cairo b/src/openzeppelin/introspection/erc165/library.cairo index 99e85093c..ad6da9f6c 100644 --- a/src/openzeppelin/introspection/erc165/library.cairo +++ b/src/openzeppelin/introspection/erc165/library.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (introspection/erc165/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (introspection/erc165/library.cairo) %lang starknet @@ -10,33 +10,29 @@ from starkware.cairo.common.bool import TRUE from openzeppelin.utils.constants.library import INVALID_ID, IERC165_ID @storage_var -func ERC165_supported_interfaces(interface_id: felt) -> (is_supported: felt): -end +func ERC165_supported_interfaces(interface_id: felt) -> (is_supported: felt) { +} -namespace ERC165: - func supports_interface{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (interface_id: felt) -> (success: felt): - if interface_id == IERC165_ID: - return (TRUE) - end +namespace ERC165 { + func supports_interface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interface_id: felt + ) -> (success: felt) { + if (interface_id == IERC165_ID) { + return (success=TRUE); + } - # Checks interface registry - let (is_supported) = ERC165_supported_interfaces.read(interface_id) - return (is_supported) - end + // Checks interface registry + let (is_supported) = ERC165_supported_interfaces.read(interface_id); + return (success=is_supported); + } - func register_interface{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (interface_id: felt): - with_attr error_message("ERC165: invalid interface id"): - assert_not_equal(interface_id, INVALID_ID) - end - ERC165_supported_interfaces.write(interface_id, TRUE) - return () - end -end + func register_interface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interface_id: felt + ) { + with_attr error_message("ERC165: invalid interface id") { + assert_not_equal(interface_id, INVALID_ID); + } + ERC165_supported_interfaces.write(interface_id, TRUE); + return (); + } +} diff --git a/src/openzeppelin/security/initializable/library.cairo b/src/openzeppelin/security/initializable/library.cairo index ac83239b4..30366ab57 100644 --- a/src/openzeppelin/security/initializable/library.cairo +++ b/src/openzeppelin/security/initializable/library.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (security/initializable/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (security/initializable/library.cairo) %lang starknet @@ -7,31 +7,22 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.bool import TRUE, FALSE @storage_var -func Initializable_initialized() -> (res: felt): -end +func Initializable_initialized() -> (initialized: felt) { +} -namespace Initializable: +namespace Initializable { + func initialized{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + felt + ) { + return Initializable_initialized.read(); + } - func initialized{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Initializable_initialized.read() - return (res=res) - end - - func initialize{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - let (is_initialized) = Initializable_initialized.read() - with_attr error_message("Initializable: contract already initialized"): - assert is_initialized = FALSE - end - Initializable_initialized.write(TRUE) - return () - end - -end + func initialize{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (is_initialized) = Initializable_initialized.read(); + with_attr error_message("Initializable: contract already initialized") { + assert is_initialized = FALSE; + } + Initializable_initialized.write(TRUE); + return (); + } +} diff --git a/src/openzeppelin/security/pausable/library.cairo b/src/openzeppelin/security/pausable/library.cairo index 46f013284..42c74fd98 100644 --- a/src/openzeppelin/security/pausable/library.cairo +++ b/src/openzeppelin/security/pausable/library.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (security/pausable/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (security/pausable/library.cairo) %lang starknet @@ -7,85 +7,64 @@ 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 -# -# Storage -# +// +// Storage +// @storage_var -func Pausable_paused() -> (paused: felt): -end +func Pausable_paused() -> (paused: felt) { +} -# -# Events -# +// +// Events +// @event -func Paused(account: felt): -end +func Paused(account: felt) { +} @event -func Unpaused(account: felt): -end - -namespace Pausable: - - func is_paused{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (is_paused: felt): - let (is_paused) = Pausable_paused.read() - return (is_paused) - end - - func assert_not_paused{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - let (is_paused) = Pausable_paused.read() - with_attr error_message("Pausable: paused"): - assert is_paused = FALSE - end - return () - end - - func assert_paused{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - let (is_paused) = Pausable_paused.read() - with_attr error_message("Pausable: not paused"): - assert is_paused = TRUE - end - return () - end - - func _pause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - assert_not_paused() - Pausable_paused.write(TRUE) - - let (account) = get_caller_address() - Paused.emit(account) - return () - end - - func _unpause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - assert_paused() - Pausable_paused.write(FALSE) - - let (account) = get_caller_address() - Unpaused.emit(account) - return () - end - -end +func Unpaused(account: felt) { +} + +namespace Pausable { + func is_paused{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + paused: felt + ) { + return Pausable_paused.read(); + } + + func assert_not_paused{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (is_paused) = Pausable_paused.read(); + with_attr error_message("Pausable: paused") { + assert is_paused = FALSE; + } + return (); + } + + func assert_paused{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (is_paused) = Pausable_paused.read(); + with_attr error_message("Pausable: not paused") { + assert is_paused = TRUE; + } + return (); + } + + func _pause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + assert_not_paused(); + Pausable_paused.write(TRUE); + + let (account) = get_caller_address(); + Paused.emit(account); + return (); + } + + func _unpause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + assert_paused(); + Pausable_paused.write(FALSE); + + let (account) = get_caller_address(); + Unpaused.emit(account); + return (); + } +} diff --git a/src/openzeppelin/security/reentrancyguard/library.cairo b/src/openzeppelin/security/reentrancyguard/library.cairo index eff0c06b8..54418b8fa 100644 --- a/src/openzeppelin/security/reentrancyguard/library.cairo +++ b/src/openzeppelin/security/reentrancyguard/library.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (security/reentrancyguard/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (security/reentrancyguard/library.cairo) %lang starknet @@ -7,31 +7,21 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.bool import TRUE, FALSE @storage_var -func ReentrancyGuard_entered() -> (res: felt): -end +func ReentrancyGuard_entered() -> (entered: felt) { +} -namespace ReentrancyGuard: +namespace ReentrancyGuard { + func _start{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (has_entered) = ReentrancyGuard_entered.read(); + with_attr error_message("ReentrancyGuard: reentrant call") { + assert has_entered = FALSE; + } + ReentrancyGuard_entered.write(TRUE); + return (); + } - func _start{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - let (has_entered) = ReentrancyGuard_entered.read() - with_attr error_message("ReentrancyGuard: reentrant call"): - assert has_entered = FALSE - end - ReentrancyGuard_entered.write(TRUE) - return () - end - - func _end{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - ReentrancyGuard_entered.write(FALSE) - return () - end - -end + func _end{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + ReentrancyGuard_entered.write(FALSE); + return (); + } +} diff --git a/src/openzeppelin/security/safemath/library.cairo b/src/openzeppelin/security/safemath/library.cairo index cf027b37c..d3f217a68 100644 --- a/src/openzeppelin/security/safemath/library.cairo +++ b/src/openzeppelin/security/safemath/library.cairo @@ -1,119 +1,113 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (security/safemath/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (security/safemath/library.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.bool import TRUE, FALSE from starkware.cairo.common.uint256 import ( - Uint256, uint256_check, uint256_add, uint256_sub, uint256_mul, - uint256_unsigned_div_rem, uint256_le, uint256_lt, uint256_eq + Uint256, + uint256_check, + uint256_add, + uint256_sub, + uint256_mul, + uint256_unsigned_div_rem, + uint256_le, + uint256_lt, + uint256_eq, ) -namespace SafeUint256: +namespace SafeUint256 { + // Adds two integers. + // Reverts if the sum overflows. + func add{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 + ) -> (c: Uint256) { + uint256_check(a); + uint256_check(b); + let (c: Uint256, is_overflow) = uint256_add(a, b); + with_attr error_message("SafeUint256: addition overflow") { + assert is_overflow = FALSE; + } + return (c=c); + } - # Adds two integers. - # Reverts if the sum overflows. - func add{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - uint256_check(a) - uint256_check(b) - let (c: Uint256, is_overflow) = uint256_add(a, b) - with_attr error_message("SafeUint256: addition overflow"): - assert is_overflow = FALSE - end - return (c) - end + // Subtracts two integers. + // Reverts if subtrahend (`b`) is greater than minuend (`a`). + func sub_le{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 + ) -> (c: Uint256) { + alloc_locals; + uint256_check(a); + uint256_check(b); + let (is_le) = uint256_le(b, a); + with_attr error_message("SafeUint256: subtraction overflow") { + assert is_le = TRUE; + } + let (c: Uint256) = uint256_sub(a, b); + return (c=c); + } - # Subtracts two integers. - # Reverts if minuend (`b`) is greater than subtrahend (`a`). - func sub_le{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - alloc_locals - uint256_check(a) - uint256_check(b) - let (is_le) = uint256_le(b, a) - with_attr error_message("SafeUint256: subtraction overflow"): - assert is_le = TRUE - end - let (c: Uint256) = uint256_sub(a, b) - return (c) - end + // Subtracts two integers. + // Reverts if subtrahend (`b`) is greater than or equal to minuend (`a`). + func sub_lt{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 + ) -> (c: Uint256) { + alloc_locals; + uint256_check(a); + uint256_check(b); - # Subtracts two integers. - # Reverts if minuend (`b`) is greater than or equal to subtrahend (`a`). - func sub_lt{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - alloc_locals - uint256_check(a) - uint256_check(b) + let (is_lt) = uint256_lt(b, a); + with_attr error_message("SafeUint256: subtraction overflow or the difference equals zero") { + assert is_lt = TRUE; + } + let (c: Uint256) = uint256_sub(a, b); + return (c=c); + } - let (is_lt) = uint256_lt(b, a) - with_attr error_message("SafeUint256: subtraction overflow or the difference equals zero"): - assert is_lt = TRUE - end - let (c: Uint256) = uint256_sub(a, b) - return (c) - end + // Multiplies two integers. + // Reverts if product is greater than 2^256. + func mul{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 + ) -> (c: Uint256) { + alloc_locals; + uint256_check(a); + uint256_check(b); + let (a_zero) = uint256_eq(a, Uint256(0, 0)); + if (a_zero == TRUE) { + return (c=a); + } - # Multiplies two integers. - # Reverts if product is greater than 2^256. - func mul{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - alloc_locals - uint256_check(a) - uint256_check(b) - let (a_zero) = uint256_eq(a, Uint256(0, 0)) - if a_zero == TRUE: - return (a) - end + let (b_zero) = uint256_eq(b, Uint256(0, 0)); + if (b_zero == TRUE) { + return (c=b); + } - let (b_zero) = uint256_eq(b, Uint256(0, 0)) - if b_zero == TRUE: - return (b) - end + let (c: Uint256, overflow: Uint256) = uint256_mul(a, b); + with_attr error_message("SafeUint256: multiplication overflow") { + assert overflow = Uint256(0, 0); + } + return (c=c); + } - let (c: Uint256, overflow: Uint256) = uint256_mul(a, b) - with_attr error_message("SafeUint256: multiplication overflow"): - assert overflow = Uint256(0, 0) - end - return (c) - end + // Integer division of two numbers. Returns uint256 quotient and remainder. + // Reverts if divisor is zero as per OpenZeppelin's Solidity implementation. + // Cairo's `uint256_unsigned_div_rem` already checks: + // remainder < divisor + // quotient * divisor + remainder == dividend + func div_rem{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 + ) -> (c: Uint256, rem: Uint256) { + alloc_locals; + uint256_check(a); + uint256_check(b); + let (is_zero) = uint256_eq(b, Uint256(0, 0)); + with_attr error_message("SafeUint256: divisor cannot be zero") { + assert is_zero = FALSE; + } - # Integer division of two numbers. Returns uint256 quotient and remainder. - # Reverts if divisor is zero as per OpenZeppelin's Solidity implementation. - # Cairo's `uint256_unsigned_div_rem` already checks: - # remainder < divisor - # quotient * divisor + remainder == dividend - func div_rem{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256, rem: Uint256): - alloc_locals - uint256_check(a) - uint256_check(b) - - let (is_zero) = uint256_eq(b, Uint256(0, 0)) - with_attr error_message("SafeUint256: divisor cannot be zero"): - assert is_zero = FALSE - end - - let (c: Uint256, rem: Uint256) = uint256_unsigned_div_rem(a, b) - return (c, rem) - end - -end + let (c: Uint256, rem: Uint256) = uint256_unsigned_div_rem(a, b); + return (c=c, rem=rem); + } +} diff --git a/src/openzeppelin/token/erc20/IERC20.cairo b/src/openzeppelin/token/erc20/IERC20.cairo index 4ff440559..72956aece 100644 --- a/src/openzeppelin/token/erc20/IERC20.cairo +++ b/src/openzeppelin/token/erc20/IERC20.cairo @@ -1,41 +1,36 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/IERC20.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/IERC20.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC20: - func name() -> (name: felt): - end +namespace IERC20 { + func name() -> (name: felt) { + } - func symbol() -> (symbol: felt): - end + func symbol() -> (symbol: felt) { + } - func decimals() -> (decimals: felt): - end + func decimals() -> (decimals: felt) { + } - func totalSupply() -> (totalSupply: Uint256): - end + func totalSupply() -> (totalSupply: Uint256) { + } - func balanceOf(account: felt) -> (balance: Uint256): - end + func balanceOf(account: felt) -> (balance: Uint256) { + } - func allowance(owner: felt, spender: felt) -> (remaining: Uint256): - end + func allowance(owner: felt, spender: felt) -> (remaining: Uint256) { + } - func transfer(recipient: felt, amount: Uint256) -> (success: felt): - end + func transfer(recipient: felt, amount: Uint256) -> (success: felt) { + } - func transferFrom( - sender: felt, - recipient: felt, - amount: Uint256 - ) -> (success: felt): - end - - func approve(spender: felt, amount: Uint256) -> (success: felt): - end -end + func transferFrom(sender: felt, recipient: felt, amount: Uint256) -> (success: felt) { + } + func approve(spender: felt, amount: Uint256) -> (success: felt) { + } +} diff --git a/src/openzeppelin/token/erc20/library.cairo b/src/openzeppelin/token/erc20/library.cairo index 72c3dab27..3aa2709c3 100644 --- a/src/openzeppelin/token/erc20/library.cairo +++ b/src/openzeppelin/token/erc20/library.cairo @@ -1,360 +1,306 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/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.math import assert_not_zero, assert_lt -from starkware.cairo.common.bool import FALSE +from starkware.cairo.common.math import assert_not_zero, assert_le +from starkware.cairo.common.bool import TRUE, FALSE from starkware.cairo.common.uint256 import Uint256, uint256_check, uint256_eq, uint256_not from openzeppelin.security.safemath.library import SafeUint256 from openzeppelin.utils.constants.library import UINT8_MAX -# -# Events -# +// +// Events +// @event -func Transfer(from_: felt, to: felt, value: Uint256): -end +func Transfer(from_: felt, to: felt, value: Uint256) { +} @event -func Approval(owner: felt, spender: felt, value: Uint256): -end +func Approval(owner: felt, spender: felt, value: Uint256) { +} -# -# Storage -# +// +// Storage +// @storage_var -func ERC20_name() -> (name: felt): -end +func ERC20_name() -> (name: felt) { +} @storage_var -func ERC20_symbol() -> (symbol: felt): -end +func ERC20_symbol() -> (symbol: felt) { +} @storage_var -func ERC20_decimals() -> (decimals: felt): -end +func ERC20_decimals() -> (decimals: felt) { +} @storage_var -func ERC20_total_supply() -> (total_supply: Uint256): -end +func ERC20_total_supply() -> (total_supply: Uint256) { +} @storage_var -func ERC20_balances(account: felt) -> (balance: Uint256): -end +func ERC20_balances(account: felt) -> (balance: Uint256) { +} @storage_var -func ERC20_allowances(owner: felt, spender: felt) -> (allowance: Uint256): -end - -namespace ERC20: - - # - # Initializer - # - - func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - decimals: felt - ): - ERC20_name.write(name) - ERC20_symbol.write(symbol) - with_attr error_message("ERC20: decimals exceed 2^8"): - assert_lt(decimals, UINT8_MAX) - end - ERC20_decimals.write(decimals) - return () - end - - # - # Public functions - # - - func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC20_name.read() - return (name) - end - - func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC20_symbol.read() - return (symbol) - end - - func total_supply{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (total_supply: Uint256): - let (total_supply: Uint256) = ERC20_total_supply.read() - return (total_supply) - end - - func decimals{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (decimals: felt): - let (decimals) = ERC20_decimals.read() - return (decimals) - end - - func balance_of{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(account: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC20_balances.read(account) - return (balance) - end - - func allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt) -> (remaining: Uint256): - let (remaining: Uint256) = ERC20_allowances.read(owner, spender) - return (remaining) - end - - func transfer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(recipient: felt, amount: Uint256): - let (sender) = get_caller_address() - _transfer(sender, recipient, amount) - return () - end - - func transfer_from{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - sender: felt, - recipient: felt, - amount: Uint256 - ) -> (): - let (caller) = get_caller_address() - # subtract allowance - _spend_allowance(sender, caller, amount) - # execute transfer - _transfer(sender, recipient, amount) - return () - end - - func approve{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, amount: Uint256): - with_attr error_message("ERC20: amount is not a valid Uint256"): - uint256_check(amount) - end - - let (caller) = get_caller_address() - _approve(caller, spender, amount) - return () - end - - func increase_allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, added_value: Uint256) -> (): - with_attr error("ERC20: added_value is not a valid Uint256"): - uint256_check(added_value) - end - - let (caller) = get_caller_address() - let (current_allowance: Uint256) = ERC20_allowances.read(caller, spender) - - # add allowance - with_attr error_message("ERC20: allowance overflow"): - let (new_allowance: Uint256) = SafeUint256.add(current_allowance, added_value) - end - - _approve(caller, spender, new_allowance) - return () - end - - func decrease_allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, subtracted_value: Uint256) -> (): - alloc_locals - with_attr error_message("ERC20: subtracted_value is not a valid Uint256"): - uint256_check(subtracted_value) - end - - let (caller) = get_caller_address() - let (current_allowance: Uint256) = ERC20_allowances.read(owner=caller, spender=spender) - - with_attr error_message("ERC20: allowance below zero"): - let (new_allowance: Uint256) = SafeUint256.sub_le(current_allowance, subtracted_value) - end - - _approve(caller, spender, new_allowance) - return () - end - - # - # Internal - # - - func _mint{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(recipient: felt, amount: Uint256): - with_attr error_message("ERC20: amount is not a valid Uint256"): - uint256_check(amount) - end - - with_attr error_message("ERC20: cannot mint to the zero address"): - assert_not_zero(recipient) - end - - let (supply: Uint256) = ERC20_total_supply.read() - with_attr error_message("ERC20: mint overflow"): - let (new_supply: Uint256) = SafeUint256.add(supply, amount) - end - ERC20_total_supply.write(new_supply) - - let (balance: Uint256) = ERC20_balances.read(account=recipient) - # overflow is not possible because sum is guaranteed to be less than total supply - # which we check for overflow below - let (new_balance: Uint256) = SafeUint256.add(balance, amount) - ERC20_balances.write(recipient, new_balance) - - Transfer.emit(0, recipient, amount) - return () - end - - func _burn{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(account: felt, amount: Uint256): - with_attr error_message("ERC20: amount is not a valid Uint256"): - uint256_check(amount) - end - - with_attr error_message("ERC20: cannot burn from the zero address"): - assert_not_zero(account) - end - - let (balance: Uint256) = ERC20_balances.read(account) - with_attr error_message("ERC20: burn amount exceeds balance"): - let (new_balance: Uint256) = SafeUint256.sub_le(balance, amount) - end - - ERC20_balances.write(account, new_balance) - - let (supply: Uint256) = ERC20_total_supply.read() - let (new_supply: Uint256) = SafeUint256.sub_le(supply, amount) - ERC20_total_supply.write(new_supply) - Transfer.emit(account, 0, amount) - return () - end - - func _transfer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(sender: felt, recipient: felt, amount: Uint256): - with_attr error_message("ERC20: amount is not a valid Uint256"): - uint256_check(amount) # almost surely not needed, might remove after confirmation - end - - with_attr error_message("ERC20: cannot transfer from the zero address"): - assert_not_zero(sender) - end - - with_attr error_message("ERC20: cannot transfer to the zero address"): - assert_not_zero(recipient) - end - - let (sender_balance: Uint256) = ERC20_balances.read(account=sender) - with_attr error_message("ERC20: transfer amount exceeds balance"): - let (new_sender_balance: Uint256) = SafeUint256.sub_le(sender_balance, amount) - end - - ERC20_balances.write(sender, new_sender_balance) - - # add to recipient - let (recipient_balance: Uint256) = ERC20_balances.read(account=recipient) - # overflow is not possible because sum is guaranteed by mint to be less than total supply - let (new_recipient_balance: Uint256) = SafeUint256.add(recipient_balance, amount) - ERC20_balances.write(recipient, new_recipient_balance) - Transfer.emit(sender, recipient, amount) - return () - end - - func _approve{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt, amount: Uint256): - with_attr error_message("ERC20: amount is not a valid Uint256"): - uint256_check(amount) - end - - with_attr error_message("ERC20: cannot approve from the zero address"): - assert_not_zero(owner) - end - - with_attr error_message("ERC20: cannot approve to the zero address"): - assert_not_zero(spender) - end - - ERC20_allowances.write(owner, spender, amount) - Approval.emit(owner, spender, amount) - return () - end - - func _spend_allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt, amount: Uint256): - alloc_locals - with_attr error_message("ERC20: amount is not a valid Uint256"): - uint256_check(amount) # almost surely not needed, might remove after confirmation - end - - let (current_allowance: Uint256) = ERC20_allowances.read(owner, spender) - let (infinite: Uint256) = uint256_not(Uint256(0, 0)) - let (is_infinite: felt ) = uint256_eq(current_allowance, infinite) - - if is_infinite == FALSE: - with_attr error_message("ERC20: insufficient allowance"): - let (new_allowance: Uint256) = SafeUint256.sub_le(current_allowance, amount) - end - - _approve(owner, spender, new_allowance) - return () - end - return () - end - -end +func ERC20_allowances(owner: felt, spender: felt) -> (remaining: Uint256) { +} + +namespace ERC20 { + // + // Initializer + // + + func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, decimals: felt + ) { + ERC20_name.write(name); + ERC20_symbol.write(symbol); + with_attr error_message("ERC20: decimals exceed 2^8") { + assert_le(decimals, UINT8_MAX); + } + ERC20_decimals.write(decimals); + return (); + } + + // + // Public functions + // + + func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC20_name.read(); + } + + func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + symbol: felt + ) { + return ERC20_symbol.read(); + } + + func total_supply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + total_supply: Uint256 + ) { + return ERC20_total_supply.read(); + } + + func decimals{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + decimals: felt + ) { + return ERC20_decimals.read(); + } + + func balance_of{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + account: felt + ) -> (balance: Uint256) { + return ERC20_balances.read(account); + } + + func allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt + ) -> (remaining: Uint256) { + return ERC20_allowances.read(owner, spender); + } + + func transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + recipient: felt, amount: Uint256 + ) -> (success: felt) { + let (sender) = get_caller_address(); + _transfer(sender, recipient, amount); + return (success=TRUE); + } + + func transfer_from{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + sender: felt, recipient: felt, amount: Uint256 + ) -> (success: felt) { + let (caller) = get_caller_address(); + _spend_allowance(sender, caller, amount); + _transfer(sender, recipient, amount); + return (success=TRUE); + } + + func approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, amount: Uint256 + ) -> (success: felt) { + with_attr error_message("ERC20: amount is not a valid Uint256") { + uint256_check(amount); + } + + let (caller) = get_caller_address(); + _approve(caller, spender, amount); + return (success=TRUE); + } + + func increase_allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, added_value: Uint256 + ) -> (success: felt) { + with_attr error("ERC20: added_value is not a valid Uint256") { + uint256_check(added_value); + } + + let (caller) = get_caller_address(); + let (current_allowance: Uint256) = ERC20_allowances.read(caller, spender); + + // add allowance + with_attr error_message("ERC20: allowance overflow") { + let (new_allowance: Uint256) = SafeUint256.add(current_allowance, added_value); + } + + _approve(caller, spender, new_allowance); + return (success=TRUE); + } + + func decrease_allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, subtracted_value: Uint256 + ) -> (success: felt) { + alloc_locals; + with_attr error_message("ERC20: subtracted_value is not a valid Uint256") { + uint256_check(subtracted_value); + } + + let (caller) = get_caller_address(); + let (current_allowance: Uint256) = ERC20_allowances.read(owner=caller, spender=spender); + + with_attr error_message("ERC20: allowance below zero") { + let (new_allowance: Uint256) = SafeUint256.sub_le(current_allowance, subtracted_value); + } + + _approve(caller, spender, new_allowance); + return (success=TRUE); + } + + // + // Internal + // + + func _mint{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + recipient: felt, amount: Uint256 + ) { + with_attr error_message("ERC20: amount is not a valid Uint256") { + uint256_check(amount); + } + + with_attr error_message("ERC20: cannot mint to the zero address") { + assert_not_zero(recipient); + } + + let (supply: Uint256) = ERC20_total_supply.read(); + with_attr error_message("ERC20: mint overflow") { + let (new_supply: Uint256) = SafeUint256.add(supply, amount); + } + ERC20_total_supply.write(new_supply); + + let (balance: Uint256) = ERC20_balances.read(account=recipient); + // overflow is not possible because sum is guaranteed to be less than total supply + // which we check for overflow below + let (new_balance: Uint256) = SafeUint256.add(balance, amount); + ERC20_balances.write(recipient, new_balance); + + Transfer.emit(0, recipient, amount); + return (); + } + + func _burn{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + account: felt, amount: Uint256 + ) { + with_attr error_message("ERC20: amount is not a valid Uint256") { + uint256_check(amount); + } + + with_attr error_message("ERC20: cannot burn from the zero address") { + assert_not_zero(account); + } + + let (balance: Uint256) = ERC20_balances.read(account); + with_attr error_message("ERC20: burn amount exceeds balance") { + let (new_balance: Uint256) = SafeUint256.sub_le(balance, amount); + } + + ERC20_balances.write(account, new_balance); + + let (supply: Uint256) = ERC20_total_supply.read(); + let (new_supply: Uint256) = SafeUint256.sub_le(supply, amount); + ERC20_total_supply.write(new_supply); + Transfer.emit(account, 0, amount); + return (); + } + + func _transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + sender: felt, recipient: felt, amount: Uint256 + ) { + with_attr error_message("ERC20: amount is not a valid Uint256") { + uint256_check(amount); // almost surely not needed, might remove after confirmation + } + + with_attr error_message("ERC20: cannot transfer from the zero address") { + assert_not_zero(sender); + } + + with_attr error_message("ERC20: cannot transfer to the zero address") { + assert_not_zero(recipient); + } + + let (sender_balance: Uint256) = ERC20_balances.read(account=sender); + with_attr error_message("ERC20: transfer amount exceeds balance") { + let (new_sender_balance: Uint256) = SafeUint256.sub_le(sender_balance, amount); + } + + ERC20_balances.write(sender, new_sender_balance); + + // add to recipient + let (recipient_balance: Uint256) = ERC20_balances.read(account=recipient); + // overflow is not possible because sum is guaranteed by mint to be less than total supply + let (new_recipient_balance: Uint256) = SafeUint256.add(recipient_balance, amount); + ERC20_balances.write(recipient, new_recipient_balance); + Transfer.emit(sender, recipient, amount); + return (); + } + + func _approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt, amount: Uint256 + ) { + with_attr error_message("ERC20: amount is not a valid Uint256") { + uint256_check(amount); + } + + with_attr error_message("ERC20: cannot approve from the zero address") { + assert_not_zero(owner); + } + + with_attr error_message("ERC20: cannot approve to the zero address") { + assert_not_zero(spender); + } + + ERC20_allowances.write(owner, spender, amount); + Approval.emit(owner, spender, amount); + return (); + } + + func _spend_allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt, amount: Uint256 + ) { + alloc_locals; + with_attr error_message("ERC20: amount is not a valid Uint256") { + uint256_check(amount); // almost surely not needed, might remove after confirmation + } + + let (current_allowance: Uint256) = ERC20_allowances.read(owner, spender); + let (infinite: Uint256) = uint256_not(Uint256(0, 0)); + let (is_infinite: felt) = uint256_eq(current_allowance, infinite); + + if (is_infinite == FALSE) { + with_attr error_message("ERC20: insufficient allowance") { + let (new_allowance: Uint256) = SafeUint256.sub_le(current_allowance, amount); + } + + _approve(owner, spender, new_allowance); + return (); + } + return (); + } +} diff --git a/src/openzeppelin/token/erc20/presets/ERC20.cairo b/src/openzeppelin/token/erc20/presets/ERC20.cairo index 835d411e1..a5d902b83 100644 --- a/src/openzeppelin/token/erc20/presets/ERC20.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20.cairo @@ -1,149 +1,100 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/presets/ERC20.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/presets/ERC20.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin -from starkware.cairo.common.bool import TRUE from starkware.cairo.common.uint256 import Uint256 from openzeppelin.token.erc20.library import ERC20 @constructor -func constructor{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - decimals: felt, - initial_supply: Uint256, - recipient: felt - ): - ERC20.initializer(name, symbol, decimals) - ERC20._mint(recipient, initial_supply) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, decimals: felt, initial_supply: Uint256, recipient: felt +) { + ERC20.initializer(name, symbol, decimals); + ERC20._mint(recipient, initial_supply); + return (); +} + +// +// Getters +// @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC20.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC20.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC20.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC20.symbol(); +} @view -func totalSupply{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (totalSupply: Uint256): - let (totalSupply: Uint256) = ERC20.total_supply() - return (totalSupply) -end +func totalSupply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + totalSupply: Uint256 +) { + let (totalSupply: Uint256) = ERC20.total_supply(); + return (totalSupply=totalSupply); +} @view -func decimals{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (decimals: felt): - let (decimals) = ERC20.decimals() - return (decimals) -end +func decimals{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + decimals: felt +) { + return ERC20.decimals(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(account: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC20.balance_of(account) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(account: felt) -> ( + balance: Uint256 +) { + return ERC20.balance_of(account); +} @view -func allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt) -> (remaining: Uint256): - let (remaining: Uint256) = ERC20.allowance(owner, spender) - return (remaining) -end - -# -# Externals -# +func allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt +) -> (remaining: Uint256) { + return ERC20.allowance(owner, spender); +} + +// +// Externals +// @external -func transfer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(recipient: felt, amount: Uint256) -> (success: felt): - ERC20.transfer(recipient, amount) - return (TRUE) -end +func transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer(recipient, amount); +} @external -func transferFrom{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - sender: felt, - recipient: felt, - amount: Uint256 - ) -> (success: felt): - ERC20.transfer_from(sender, recipient, amount) - return (TRUE) -end +func transferFrom{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + sender: felt, recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer_from(sender, recipient, amount); +} @external -func approve{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, amount: Uint256) -> (success: felt): - ERC20.approve(spender, amount) - return (TRUE) -end +func approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.approve(spender, amount); +} @external -func increaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, added_value: Uint256) -> (success: felt): - ERC20.increase_allowance(spender, added_value) - return (TRUE) -end +func increaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, added_value: Uint256 +) -> (success: felt) { + return ERC20.increase_allowance(spender, added_value); +} @external -func decreaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, subtracted_value: Uint256) -> (success: felt): - ERC20.decrease_allowance(spender, subtracted_value) - return (TRUE) -end +func decreaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, subtracted_value: Uint256 +) -> (success: felt) { + return ERC20.decrease_allowance(spender, subtracted_value); +} diff --git a/src/openzeppelin/token/erc20/presets/ERC20Burnable.cairo b/src/openzeppelin/token/erc20/presets/ERC20Burnable.cairo index 6fb66240d..ed6939373 100644 --- a/src/openzeppelin/token/erc20/presets/ERC20Burnable.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20Burnable.cairo @@ -1,173 +1,118 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Cairo Contracts v0.3.2 (token/erc20/presets/ERC20Burnable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Cairo Contracts v0.3.2 (token/erc20/presets/ERC20Burnable.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.uint256 import Uint256 from starkware.starknet.common.syscalls import get_caller_address -from starkware.cairo.common.bool import TRUE from openzeppelin.token.erc20.library import ERC20 @constructor -func constructor{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - decimals: felt, - initial_supply: Uint256, - recipient: felt - ): - ERC20.initializer(name, symbol, decimals) - ERC20._mint(recipient, initial_supply) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, decimals: felt, initial_supply: Uint256, recipient: felt +) { + ERC20.initializer(name, symbol, decimals); + ERC20._mint(recipient, initial_supply); + return (); +} + +// +// Getters +// @view -func name{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC20.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC20.name(); +} @view -func symbol{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC20.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC20.symbol(); +} @view -func totalSupply{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (totalSupply: Uint256): - let (totalSupply: Uint256) = ERC20.total_supply() - return (totalSupply) -end +func totalSupply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + totalSupply: Uint256 +) { + let (totalSupply: Uint256) = ERC20.total_supply(); + return (totalSupply=totalSupply); +} @view -func decimals{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (decimals: felt): - let (decimals) = ERC20.decimals() - return (decimals) -end +func decimals{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + decimals: felt +) { + return ERC20.decimals(); +} @view -func balanceOf{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(account: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC20.balance_of(account) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(account: felt) -> ( + balance: Uint256 +) { + return ERC20.balance_of(account); +} @view -func allowance{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt) -> (remaining: Uint256): - let (remaining: Uint256) = ERC20.allowance(owner, spender) - return (remaining) -end - -# -# External -# +func allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt +) -> (remaining: Uint256) { + return ERC20.allowance(owner, spender); +} + +// +// External +// @external -func transfer{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(recipient: felt, amount: Uint256) -> (success: felt): - ERC20.transfer(recipient, amount) - return (TRUE) -end +func transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer(recipient, amount); +} @external -func transferFrom{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - sender: felt, - recipient: felt, - amount: Uint256 - ) -> (success: felt): - ERC20.transfer_from(sender, recipient, amount) - return (TRUE) -end +func transferFrom{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + sender: felt, recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer_from(sender, recipient, amount); +} @external -func approve{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(spender: felt, amount: Uint256) -> (success: felt): - ERC20.approve(spender, amount) - return (TRUE) -end +func approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.approve(spender, amount); +} @external -func increaseAllowance{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(spender: felt, added_value: Uint256) -> (success: felt): - ERC20.increase_allowance(spender, added_value) - return (TRUE) -end +func increaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, added_value: Uint256 +) -> (success: felt) { + return ERC20.increase_allowance(spender, added_value); +} @external -func decreaseAllowance{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(spender: felt, subtracted_value: Uint256) -> (success: felt): - ERC20.decrease_allowance(spender, subtracted_value) - return (TRUE) -end +func decreaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, subtracted_value: Uint256 +) -> (success: felt) { + return ERC20.decrease_allowance(spender, subtracted_value); +} @external -func burn{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(amount: Uint256): - let (caller) = get_caller_address() - ERC20._burn(caller, amount) - return () -end +func burn{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(amount: Uint256) { + let (caller) = get_caller_address(); + ERC20._burn(caller, amount); + return (); +} @external -func burnFrom{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(account: felt, amount: Uint256): - let (caller) = get_caller_address() - ERC20._spend_allowance(account, caller, amount) - ERC20._burn(account, amount) - return () -end +func burnFrom{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + account: felt, amount: Uint256 +) { + let (caller) = get_caller_address(); + ERC20._spend_allowance(account, caller, amount); + ERC20._burn(account, amount); + return (); +} diff --git a/src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo b/src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo index 4f72c53d0..90aa389e7 100644 --- a/src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo @@ -1,193 +1,130 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/presets/ERC20Mintable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (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.access.ownable.library import Ownable from openzeppelin.token.erc20.library import ERC20 @constructor -func constructor{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - decimals: felt, - initial_supply: Uint256, - recipient: felt, - owner: felt - ): - ERC20.initializer(name, symbol, decimals) - ERC20._mint(recipient, initial_supply) - Ownable.initializer(owner) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, decimals: felt, initial_supply: Uint256, recipient: felt, owner: felt +) { + ERC20.initializer(name, symbol, decimals); + ERC20._mint(recipient, initial_supply); + Ownable.initializer(owner); + return (); +} + +// +// Getters +// @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC20.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC20.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC20.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC20.symbol(); +} @view -func totalSupply{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (totalSupply: Uint256): - let (totalSupply: Uint256) = ERC20.total_supply() - return (totalSupply) -end +func totalSupply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + totalSupply: Uint256 +) { + let (totalSupply: Uint256) = ERC20.total_supply(); + return (totalSupply=totalSupply); +} @view -func decimals{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (decimals: felt): - let (decimals) = ERC20.decimals() - return (decimals) -end +func decimals{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + decimals: felt +) { + return ERC20.decimals(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(account: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC20.balance_of(account) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(account: felt) -> ( + balance: Uint256 +) { + return ERC20.balance_of(account); +} @view -func allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt) -> (remaining: Uint256): - let (remaining: Uint256) = ERC20.allowance(owner, spender) - return (remaining) -end +func allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt +) -> (remaining: Uint256) { + return ERC20.allowance(owner, spender); +} @view -func owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (owner: felt): - let (owner: felt) = Ownable.owner() - return (owner) -end - -# -# Externals -# +func owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable.owner(); +} + +// +// Externals +// @external -func transfer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(recipient: felt, amount: Uint256) -> (success: felt): - ERC20.transfer(recipient, amount) - return (TRUE) -end +func transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer(recipient, amount); +} @external -func transferFrom{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - sender: felt, - recipient: felt, - amount: Uint256 - ) -> (success: felt): - ERC20.transfer_from(sender, recipient, amount) - return (TRUE) -end +func transferFrom{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + sender: felt, recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer_from(sender, recipient, amount); +} @external -func approve{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, amount: Uint256) -> (success: felt): - ERC20.approve(spender, amount) - return (TRUE) -end +func approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.approve(spender, amount); +} @external -func increaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, added_value: Uint256) -> (success: felt): - ERC20.increase_allowance(spender, added_value) - return (TRUE) -end +func increaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, added_value: Uint256 +) -> (success: felt) { + return ERC20.increase_allowance(spender, added_value); +} @external -func decreaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, subtracted_value: Uint256) -> (success: felt): - ERC20.decrease_allowance(spender, subtracted_value) - return (TRUE) -end +func decreaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, subtracted_value: Uint256 +) -> (success: felt) { + return ERC20.decrease_allowance(spender, subtracted_value); +} @external -func mint{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(to: felt, amount: Uint256): - Ownable.assert_only_owner() - ERC20._mint(to, amount) - return () -end +func mint{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + to: felt, amount: Uint256 +) { + Ownable.assert_only_owner(); + ERC20._mint(to, amount); + return (); +} @external -func transferOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(newOwner: felt): - Ownable.transfer_ownership(newOwner) - return () -end +func transferOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + newOwner: felt +) { + Ownable.transfer_ownership(newOwner); + return (); +} @external -func renounceOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.renounce_ownership() - return () -end +func renounceOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.renounce_ownership(); + return (); +} diff --git a/src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo b/src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo index 865b9ce6c..b0d42dc44 100644 --- a/src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo @@ -1,10 +1,9 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/presets/ERC20Pausable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/presets/ERC20Pausable.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin -from starkware.cairo.common.bool import TRUE from starkware.cairo.common.uint256 import Uint256 from openzeppelin.access.ownable.library import Ownable @@ -12,209 +11,136 @@ from openzeppelin.security.pausable.library import Pausable from openzeppelin.token.erc20.library import ERC20 @constructor -func constructor{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - decimals: felt, - initial_supply: Uint256, - recipient: felt, - owner: felt - ): - ERC20.initializer(name, symbol, decimals) - ERC20._mint(recipient, initial_supply) - Ownable.initializer(owner) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, decimals: felt, initial_supply: Uint256, recipient: felt, owner: felt +) { + ERC20.initializer(name, symbol, decimals); + ERC20._mint(recipient, initial_supply); + Ownable.initializer(owner); + return (); +} + +// +// Getters +// @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC20.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC20.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC20.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC20.symbol(); +} @view -func totalSupply{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (totalSupply: Uint256): - let (totalSupply: Uint256) = ERC20.total_supply() - return (totalSupply) -end +func totalSupply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + totalSupply: Uint256 +) { + let (totalSupply: Uint256) = ERC20.total_supply(); + return (totalSupply=totalSupply); +} @view -func decimals{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (decimals: felt): - let (decimals) = ERC20.decimals() - return (decimals) -end +func decimals{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + decimals: felt +) { + return ERC20.decimals(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(account: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC20.balance_of(account) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(account: felt) -> ( + balance: Uint256 +) { + return ERC20.balance_of(account); +} @view -func allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt) -> (remaining: Uint256): - let (remaining: Uint256) = ERC20.allowance(owner, spender) - return (remaining) -end +func allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt +) -> (remaining: Uint256) { + return ERC20.allowance(owner, spender); +} @view -func owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (owner: felt): - let (owner: felt) = Ownable.owner() - return (owner) -end +func owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable.owner(); +} @view -func paused{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (paused: felt): - let (paused) = Pausable.is_paused() - return (paused) -end - -# -# Externals -# +func paused{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (paused: felt) { + return Pausable.is_paused(); +} + +// +// Externals +// @external -func transfer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(recipient: felt, amount: Uint256) -> (success: felt): - Pausable.assert_not_paused() - ERC20.transfer(recipient, amount) - return (TRUE) -end +func transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + recipient: felt, amount: Uint256 +) -> (success: felt) { + Pausable.assert_not_paused(); + return ERC20.transfer(recipient, amount); +} @external -func transferFrom{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - sender: felt, - recipient: felt, - amount: Uint256 - ) -> (success: felt): - Pausable.assert_not_paused() - ERC20.transfer_from(sender, recipient, amount) - return (TRUE) -end +func transferFrom{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + sender: felt, recipient: felt, amount: Uint256 +) -> (success: felt) { + Pausable.assert_not_paused(); + return ERC20.transfer_from(sender, recipient, amount); +} @external -func approve{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, amount: Uint256) -> (success: felt): - Pausable.assert_not_paused() - ERC20.approve(spender, amount) - return (TRUE) -end +func approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, amount: Uint256 +) -> (success: felt) { + Pausable.assert_not_paused(); + return ERC20.approve(spender, amount); +} @external -func increaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, added_value: Uint256) -> (success: felt): - Pausable.assert_not_paused() - ERC20.increase_allowance(spender, added_value) - return (TRUE) -end +func increaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, added_value: Uint256 +) -> (success: felt) { + Pausable.assert_not_paused(); + return ERC20.increase_allowance(spender, added_value); +} @external -func decreaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, subtracted_value: Uint256) -> (success: felt): - Pausable.assert_not_paused() - ERC20.decrease_allowance(spender, subtracted_value) - return (TRUE) -end +func decreaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, subtracted_value: Uint256 +) -> (success: felt) { + Pausable.assert_not_paused(); + return ERC20.decrease_allowance(spender, subtracted_value); +} @external -func transferOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(newOwner: felt): - Ownable.transfer_ownership(newOwner) - return () -end +func transferOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + newOwner: felt +) { + Ownable.transfer_ownership(newOwner); + return (); +} @external -func renounceOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.renounce_ownership() - return () -end +func renounceOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.renounce_ownership(); + return (); +} @external -func pause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.assert_only_owner() - Pausable._pause() - return () -end +func pause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.assert_only_owner(); + Pausable._pause(); + return (); +} @external -func unpause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.assert_only_owner() - Pausable._unpause() - return () -end +func unpause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.assert_only_owner(); + Pausable._unpause(); + return (); +} diff --git a/src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo b/src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo index 8f1dd52b9..00f6c4dd3 100644 --- a/src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo @@ -1,168 +1,121 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/presets/ERC20Upgradeable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc20/presets/ERC20Upgradeable.cairo) %lang starknet %builtins pedersen range_check from starkware.cairo.common.cairo_builtins import HashBuiltin -from starkware.cairo.common.bool import TRUE from starkware.cairo.common.uint256 import Uint256 from openzeppelin.token.erc20.library import ERC20 from openzeppelin.upgrades.library import Proxy -# -# Initializer -# +// +// Initializer +// @external -func initializer{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - decimals: felt, - initial_supply: Uint256, - recipient: felt, - proxy_admin: felt - ): - ERC20.initializer(name, symbol, decimals) - ERC20._mint(recipient, initial_supply) - Proxy.initializer(proxy_admin) - return () -end +func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, + symbol: felt, + decimals: felt, + initial_supply: Uint256, + recipient: felt, + proxy_admin: felt, +) { + ERC20.initializer(name, symbol, decimals); + ERC20._mint(recipient, initial_supply); + Proxy.initializer(proxy_admin); + return (); +} @external -func upgrade{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(new_implementation: felt): - Proxy.assert_only_admin() - Proxy._set_implementation_hash(new_implementation) - return () -end - -# -# Getters -# +func upgrade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_implementation: felt +) { + Proxy.assert_only_admin(); + Proxy._set_implementation_hash(new_implementation); + return (); +} + +// +// Getters +// @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC20.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC20.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC20.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC20.symbol(); +} @view -func totalSupply{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (totalSupply: Uint256): - let (totalSupply: Uint256) = ERC20.total_supply() - return (totalSupply) -end +func totalSupply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + totalSupply: Uint256 +) { + let (totalSupply: Uint256) = ERC20.total_supply(); + return (totalSupply=totalSupply); +} @view -func decimals{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (decimals: felt): - let (decimals) = ERC20.decimals() - return (decimals) -end +func decimals{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + decimals: felt +) { + return ERC20.decimals(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(account: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC20.balance_of(account) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(account: felt) -> ( + balance: Uint256 +) { + return ERC20.balance_of(account); +} @view -func allowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, spender: felt) -> (remaining: Uint256): - let (remaining: Uint256) = ERC20.allowance(owner, spender) - return (remaining) -end - -# -# Externals -# +func allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, spender: felt +) -> (remaining: Uint256) { + return ERC20.allowance(owner, spender); +} + +// +// Externals +// @external -func transfer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(recipient: felt, amount: Uint256) -> (success: felt): - ERC20.transfer(recipient, amount) - return (TRUE) -end +func transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer(recipient, amount); +} @external -func transferFrom{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - sender: felt, - recipient: felt, - amount: Uint256 - ) -> (success: felt): - ERC20.transfer_from(sender, recipient, amount) - return (TRUE) -end +func transferFrom{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + sender: felt, recipient: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.transfer_from(sender, recipient, amount); +} @external -func approve{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, amount: Uint256) -> (success: felt): - ERC20.approve(spender, amount) - return (TRUE) -end +func approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, amount: Uint256 +) -> (success: felt) { + return ERC20.approve(spender, amount); +} @external -func increaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, added_value: Uint256) -> (success: felt): - ERC20.increase_allowance(spender, added_value) - return (TRUE) -end +func increaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, added_value: Uint256 +) -> (success: felt) { + return ERC20.increase_allowance(spender, added_value); +} @external -func decreaseAllowance{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(spender: felt, subtracted_value: Uint256) -> (success: felt): - ERC20.decrease_allowance(spender, subtracted_value) - return (TRUE) -end +func decreaseAllowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + spender: felt, subtracted_value: Uint256 +) -> (success: felt) { + return ERC20.decrease_allowance(spender, subtracted_value); +} diff --git a/src/openzeppelin/token/erc721/IERC721.cairo b/src/openzeppelin/token/erc721/IERC721.cairo index 630f2509c..764a00a2f 100644 --- a/src/openzeppelin/token/erc721/IERC721.cairo +++ b/src/openzeppelin/token/erc721/IERC721.cairo @@ -1,39 +1,33 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/IERC721.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/IERC721.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC721: - func balanceOf(owner: felt) -> (balance: Uint256): - end +namespace IERC721 { + func balanceOf(owner: felt) -> (balance: Uint256) { + } - func ownerOf(tokenId: Uint256) -> (owner: felt): - end + func ownerOf(tokenId: Uint256) -> (owner: felt) { + } - func safeTransferFrom( - from_: felt, - to: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ): - end + func safeTransferFrom(from_: felt, to: felt, tokenId: Uint256, data_len: felt, data: felt*) { + } - func transferFrom(from_: felt, to: felt, tokenId: Uint256): - end + func transferFrom(from_: felt, to: felt, tokenId: Uint256) { + } - func approve(approved: felt, tokenId: Uint256): - end + func approve(approved: felt, tokenId: Uint256) { + } - func setApprovalForAll(operator: felt, approved: felt): - end + func setApprovalForAll(operator: felt, approved: felt) { + } - func getApproved(tokenId: Uint256) -> (approved: felt): - end + func getApproved(tokenId: Uint256) -> (approved: felt) { + } - func isApprovedForAll(owner: felt, operator: felt) -> (isApproved: felt): - end -end + func isApprovedForAll(owner: felt, operator: felt) -> (isApproved: felt) { + } +} diff --git a/src/openzeppelin/token/erc721/IERC721Metadata.cairo b/src/openzeppelin/token/erc721/IERC721Metadata.cairo index 379707870..12e050a57 100644 --- a/src/openzeppelin/token/erc721/IERC721Metadata.cairo +++ b/src/openzeppelin/token/erc721/IERC721Metadata.cairo @@ -1,18 +1,18 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/IERC721Metadata.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/IERC721Metadata.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC721Metadata: - func name() -> (name: felt): - end +namespace IERC721Metadata { + func name() -> (name: felt) { + } - func symbol() -> (symbol: felt): - end + func symbol() -> (symbol: felt) { + } - func tokenURI(tokenId: Uint256) -> (tokenURI: felt): - end -end + func tokenURI(tokenId: Uint256) -> (tokenURI: felt) { + } +} diff --git a/src/openzeppelin/token/erc721/IERC721Receiver.cairo b/src/openzeppelin/token/erc721/IERC721Receiver.cairo index acb30f82c..77dbc29cc 100644 --- a/src/openzeppelin/token/erc721/IERC721Receiver.cairo +++ b/src/openzeppelin/token/erc721/IERC721Receiver.cairo @@ -1,18 +1,14 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/IERC721Receiver.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/IERC721Receiver.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC721Receiver: +namespace IERC721Receiver { func onERC721Received( - operator: felt, - from_: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ) -> (selector: felt): - end -end + operator: felt, from_: felt, tokenId: Uint256, data_len: felt, data: felt* + ) -> (selector: felt) { + } +} diff --git a/src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo b/src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo index fa10a5f7d..01158f1de 100644 --- a/src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo +++ b/src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo @@ -1,18 +1,18 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/enumerable/IERC721Enumerable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/enumerable/IERC721Enumerable.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC721Enumerable: - func totalSupply() -> (totalSupply: Uint256): - end +namespace IERC721Enumerable { + func totalSupply() -> (totalSupply: Uint256) { + } - func tokenByIndex(index: Uint256) -> (tokenId: Uint256): - end + func tokenByIndex(index: Uint256) -> (tokenId: Uint256) { + } - func tokenOfOwnerByIndex(owner: felt, index: Uint256) -> (tokenId: Uint256): - end -end + func tokenOfOwnerByIndex(owner: felt, index: Uint256) -> (tokenId: Uint256) { + } +} diff --git a/src/openzeppelin/token/erc721/enumerable/library.cairo b/src/openzeppelin/token/erc721/enumerable/library.cairo index b525058fa..cca1a8a4d 100644 --- a/src/openzeppelin/token/erc721/enumerable/library.cairo +++ b/src/openzeppelin/token/erc721/enumerable/library.cairo @@ -1,246 +1,201 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/enumerable/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/enumerable/library.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.bool import TRUE, FALSE -from starkware.cairo.common.uint256 import ( - Uint256, uint256_lt, uint256_eq, uint256_check -) +from starkware.cairo.common.uint256 import Uint256, uint256_lt, uint256_eq, uint256_check from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.security.safemath.library import SafeUint256 from openzeppelin.token.erc721.library import ERC721 from openzeppelin.utils.constants.library import IERC721_ENUMERABLE_ID -# -# Storage -# +// +// Storage +// @storage_var -func ERC721Enumerable_all_tokens_len() -> (res: Uint256): -end +func ERC721Enumerable_all_tokens_len() -> (total_supply: Uint256) { +} @storage_var -func ERC721Enumerable_all_tokens(index: Uint256) -> (token_id: Uint256): -end +func ERC721Enumerable_all_tokens(index: Uint256) -> (token_id: Uint256) { +} @storage_var -func ERC721Enumerable_all_tokens_index(token_id: Uint256) -> (index: Uint256): -end +func ERC721Enumerable_all_tokens_index(token_id: Uint256) -> (index: Uint256) { +} @storage_var -func ERC721Enumerable_owned_tokens(owner: felt, index: Uint256) -> (token_id: Uint256): -end +func ERC721Enumerable_owned_tokens(owner: felt, index: Uint256) -> (token_id: Uint256) { +} @storage_var -func ERC721Enumerable_owned_tokens_index(token_id: Uint256) -> (index: Uint256): -end - - -namespace ERC721Enumerable: - - # - # Constructor - # - - func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - ERC165.register_interface(IERC721_ENUMERABLE_ID) - return () - end - - # - # Getters - # - - func total_supply{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (total_supply: Uint256): - let (total_supply) = ERC721Enumerable_all_tokens_len.read() - return (total_supply) - end - - - func token_by_index{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(index: Uint256) -> (token_id: Uint256): - alloc_locals - uint256_check(index) - # Ensures index argument is less than total_supply - let (len: Uint256) = ERC721Enumerable.total_supply() - let (is_lt) = uint256_lt(index, len) - with_attr error_message("ERC721Enumerable: global index out of bounds"): - assert is_lt = TRUE - end - - let (token_id: Uint256) = ERC721Enumerable_all_tokens.read(index) - return (token_id) - end - - func token_of_owner_by_index{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(owner: felt, index: Uint256) -> (token_id: Uint256): - alloc_locals - uint256_check(index) - # Ensures index argument is less than owner's balance - let (len: Uint256) = ERC721.balance_of(owner) - let (is_lt) = uint256_lt(index, len) - with_attr error_message("ERC721Enumerable: owner index out of bounds"): - assert is_lt = TRUE - end - - let (token_id: Uint256) = ERC721Enumerable_owned_tokens.read(owner, index) - return (token_id) - end - - # - # Externals - # - func transfer_from{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(from_: felt, to: felt, token_id: Uint256): - _remove_token_from_owner_enumeration(from_, token_id) - _add_token_to_owner_enumeration(to, token_id) - ERC721.transfer_from(from_, to, token_id) - return () - end - - func safe_transfer_from{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - token_id: Uint256, - data_len: felt, - data: felt* - ): - _remove_token_from_owner_enumeration(from_, token_id) - _add_token_to_owner_enumeration(to, token_id) - ERC721.safe_transfer_from(from_, to, token_id, data_len, data) - return () - end - - # - # Internals - # - - func _mint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, token_id: Uint256): - _add_token_to_all_tokens_enumeration(token_id) - _add_token_to_owner_enumeration(to, token_id) - ERC721._mint(to, token_id) - return () - end - - func _burn{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(token_id: Uint256): - let (from_) = ERC721.owner_of(token_id) - _remove_token_from_owner_enumeration(from_, token_id) - _remove_token_from_all_tokens_enumeration(token_id) - ERC721._burn(token_id) - return () - end - -end - -# -# Private -# +func ERC721Enumerable_owned_tokens_index(token_id: Uint256) -> (index: Uint256) { +} + +namespace ERC721Enumerable { + // + // Constructor + // + + func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + ERC165.register_interface(IERC721_ENUMERABLE_ID); + return (); + } + + // + // Getters + // + + func total_supply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + total_supply: Uint256 + ) { + return ERC721Enumerable_all_tokens_len.read(); + } + + func token_by_index{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + index: Uint256 + ) -> (token_id: Uint256) { + alloc_locals; + uint256_check(index); + // Ensures index argument is less than total_supply + let (len: Uint256) = ERC721Enumerable.total_supply(); + let (is_lt) = uint256_lt(index, len); + with_attr error_message("ERC721Enumerable: global index out of bounds") { + assert is_lt = TRUE; + } + + return ERC721Enumerable_all_tokens.read(index); + } + + func token_of_owner_by_index{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, index: Uint256 + ) -> (token_id: Uint256) { + alloc_locals; + uint256_check(index); + // Ensures index argument is less than owner's balance + let (len: Uint256) = ERC721.balance_of(owner); + let (is_lt) = uint256_lt(index, len); + with_attr error_message("ERC721Enumerable: owner index out of bounds") { + assert is_lt = TRUE; + } + + return ERC721Enumerable_owned_tokens.read(owner, index); + } + + // + // Externals + // + func transfer_from{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, token_id: Uint256 + ) { + _remove_token_from_owner_enumeration(from_, token_id); + _add_token_to_owner_enumeration(to, token_id); + ERC721.transfer_from(from_, to, token_id); + return (); + } + + func safe_transfer_from{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, token_id: Uint256, data_len: felt, data: felt* + ) { + _remove_token_from_owner_enumeration(from_, token_id); + _add_token_to_owner_enumeration(to, token_id); + ERC721.safe_transfer_from(from_, to, token_id, data_len, data); + return (); + } + + // + // Internals + // + + func _mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, token_id: Uint256 + ) { + _add_token_to_all_tokens_enumeration(token_id); + _add_token_to_owner_enumeration(to, token_id); + ERC721._mint(to, token_id); + return (); + } + + func _burn{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(token_id: Uint256) { + let (from_) = ERC721.owner_of(token_id); + _remove_token_from_owner_enumeration(from_, token_id); + _remove_token_from_all_tokens_enumeration(token_id); + ERC721._burn(token_id); + return (); + } +} + +// +// Private +// func _add_token_to_all_tokens_enumeration{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(token_id: Uint256): - let (supply: Uint256) = ERC721Enumerable_all_tokens_len.read() - ERC721Enumerable_all_tokens.write(supply, token_id) - ERC721Enumerable_all_tokens_index.write(token_id, supply) - - let (new_supply: Uint256) = SafeUint256.add(supply, Uint256(1, 0)) - ERC721Enumerable_all_tokens_len.write(new_supply) - return () -end + pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr +}(token_id: Uint256) { + let (supply: Uint256) = ERC721Enumerable_all_tokens_len.read(); + ERC721Enumerable_all_tokens.write(supply, token_id); + ERC721Enumerable_all_tokens_index.write(token_id, supply); -func _remove_token_from_all_tokens_enumeration{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(token_id: Uint256): - alloc_locals - let (supply: Uint256) = ERC721Enumerable_all_tokens_len.read() - let (last_token_index: Uint256) = SafeUint256.sub_le(supply, Uint256(1, 0)) - let (token_index: Uint256) = ERC721Enumerable_all_tokens_index.read(token_id) - let (last_token_id: Uint256) = ERC721Enumerable_all_tokens.read(last_token_index) - - ERC721Enumerable_all_tokens.write(last_token_index, Uint256(0, 0)) - ERC721Enumerable_all_tokens_index.write(token_id, Uint256(0, 0)) - ERC721Enumerable_all_tokens_len.write(last_token_index) - - let (is_equal) = uint256_eq(last_token_index, token_index) - if is_equal == FALSE: - ERC721Enumerable_all_tokens_index.write(last_token_id, token_index) - ERC721Enumerable_all_tokens.write(token_index, last_token_id) - return () - end - return () -end + let (new_supply: Uint256) = SafeUint256.add(supply, Uint256(1, 0)); + ERC721Enumerable_all_tokens_len.write(new_supply); + return (); +} +func _remove_token_from_all_tokens_enumeration{ + pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr +}(token_id: Uint256) { + alloc_locals; + let (supply: Uint256) = ERC721Enumerable_all_tokens_len.read(); + let (last_token_index: Uint256) = SafeUint256.sub_le(supply, Uint256(1, 0)); + let (token_index: Uint256) = ERC721Enumerable_all_tokens_index.read(token_id); + let (last_token_id: Uint256) = ERC721Enumerable_all_tokens.read(last_token_index); + + ERC721Enumerable_all_tokens.write(last_token_index, Uint256(0, 0)); + ERC721Enumerable_all_tokens_index.write(token_id, Uint256(0, 0)); + ERC721Enumerable_all_tokens_len.write(last_token_index); + + let (is_equal) = uint256_eq(last_token_index, token_index); + if (is_equal == FALSE) { + ERC721Enumerable_all_tokens_index.write(last_token_id, token_index); + ERC721Enumerable_all_tokens.write(token_index, last_token_id); + return (); + } + return (); +} func _add_token_to_owner_enumeration{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, token_id: Uint256): - let (length: Uint256) = ERC721.balance_of(to) - ERC721Enumerable_owned_tokens.write(to, length, token_id) - ERC721Enumerable_owned_tokens_index.write(token_id, length) - return () -end - + pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr +}(to: felt, token_id: Uint256) { + let (length: Uint256) = ERC721.balance_of(to); + ERC721Enumerable_owned_tokens.write(to, length, token_id); + ERC721Enumerable_owned_tokens_index.write(token_id, length); + return (); +} func _remove_token_from_owner_enumeration{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(from_: felt, token_id: Uint256): - alloc_locals - let (last_token_index: Uint256) = ERC721.balance_of(from_) - # the index starts at zero therefore the user's last token index is their balance minus one - let (last_token_index) = SafeUint256.sub_le(last_token_index, Uint256(1, 0)) - let (token_index: Uint256) = ERC721Enumerable_owned_tokens_index.read(token_id) - - # If index is last, we can just set the return values to zero - let (is_equal) = uint256_eq(token_index, last_token_index) - if is_equal == TRUE: - ERC721Enumerable_owned_tokens_index.write(token_id, Uint256(0, 0)) - ERC721Enumerable_owned_tokens.write(from_, last_token_index, Uint256(0, 0)) - return () - end - - # If index is not last, reposition owner's last token to the removed token's index - let (last_token_id: Uint256) = ERC721Enumerable_owned_tokens.read(from_, last_token_index) - ERC721Enumerable_owned_tokens.write(from_, token_index, last_token_id) - ERC721Enumerable_owned_tokens_index.write(last_token_id, token_index) - return () -end + pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr +}(from_: felt, token_id: Uint256) { + alloc_locals; + let (last_token_index: Uint256) = ERC721.balance_of(from_); + // the index starts at zero therefore the user's last token index is their balance minus one + let (last_token_index) = SafeUint256.sub_le(last_token_index, Uint256(1, 0)); + let (token_index: Uint256) = ERC721Enumerable_owned_tokens_index.read(token_id); + + // If index is last, we can just set the return values to zero + let (is_equal) = uint256_eq(token_index, last_token_index); + if (is_equal == TRUE) { + ERC721Enumerable_owned_tokens_index.write(token_id, Uint256(0, 0)); + ERC721Enumerable_owned_tokens.write(from_, last_token_index, Uint256(0, 0)); + return (); + } + + // If index is not last, reposition owner's last token to the removed token's index + let (last_token_id: Uint256) = ERC721Enumerable_owned_tokens.read(from_, last_token_index); + ERC721Enumerable_owned_tokens.write(from_, token_index, last_token_id); + ERC721Enumerable_owned_tokens_index.write(last_token_id, token_index); + return (); +} diff --git a/src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo b/src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo index 13ae6a7e7..bf8f0eddf 100644 --- a/src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo +++ b/src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo) %lang starknet @@ -11,253 +11,178 @@ from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.token.erc721.library import ERC721 from openzeppelin.token.erc721.enumerable.library import ERC721Enumerable -# -# Constructor -# +// +// Constructor +// @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - owner: felt - ): - ERC721.initializer(name, symbol) - ERC721Enumerable.initializer() - Ownable.initializer(owner) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, owner: felt +) { + ERC721.initializer(name, symbol); + ERC721Enumerable.initializer(); + Ownable.initializer(owner); + return (); +} + +// +// Getters +// @view -func totalSupply{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }() -> (totalSupply: Uint256): - let (totalSupply: Uint256) = ERC721Enumerable.total_supply() - return (totalSupply) -end +func totalSupply{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}() -> ( + totalSupply: Uint256 +) { + let (totalSupply: Uint256) = ERC721Enumerable.total_supply(); + return (totalSupply=totalSupply); +} @view -func tokenByIndex{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(index: Uint256) -> (tokenId: Uint256): - let (tokenId: Uint256) = ERC721Enumerable.token_by_index(index) - return (tokenId) -end +func tokenByIndex{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + index: Uint256 +) -> (tokenId: Uint256) { + let (tokenId: Uint256) = ERC721Enumerable.token_by_index(index); + return (tokenId=tokenId); +} @view -func tokenOfOwnerByIndex{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(owner: felt, index: Uint256) -> (tokenId: Uint256): - let (tokenId: Uint256) = ERC721Enumerable.token_of_owner_by_index(owner, index) - return (tokenId) -end +func tokenOfOwnerByIndex{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + owner: felt, index: Uint256 +) -> (tokenId: Uint256) { + let (tokenId: Uint256) = ERC721Enumerable.token_of_owner_by_index(owner, index); + return (tokenId=tokenId); +} @view -func supportsInterface{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return ERC165.supports_interface(interfaceId); +} @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC721.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC721.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC721.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC721.symbol(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC721.balance_of(owner) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(owner: felt) -> ( + balance: Uint256 +) { + return ERC721.balance_of(owner); +} @view -func ownerOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (owner: felt): - let (owner: felt) = ERC721.owner_of(tokenId) - return (owner) -end +func ownerOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(tokenId: Uint256) -> ( + owner: felt +) { + return ERC721.owner_of(tokenId); +} @view -func getApproved{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (approved: felt): - let (approved: felt) = ERC721.get_approved(tokenId) - return (approved) -end +func getApproved{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (approved: felt) { + return ERC721.get_approved(tokenId); +} @view -func isApprovedForAll{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, operator: felt) -> (isApproved: felt): - let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator) - return (isApproved) -end +func isApprovedForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, operator: felt +) -> (isApproved: felt) { + let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator); + return (isApproved=isApproved); +} @view -func tokenURI{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (tokenURI: felt): - let (tokenURI: felt) = ERC721.token_uri(tokenId) - return (tokenURI) -end +func tokenURI{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (tokenURI: felt) { + let (tokenURI: felt) = ERC721.token_uri(tokenId); + return (tokenURI=tokenURI); +} @view -func owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (owner: felt): - let (owner: felt) = Ownable.owner() - return (owner) -end +func owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable.owner(); +} -# -# Externals -# +// +// Externals +// @external -func approve{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - ERC721.approve(to, tokenId) - return () -end +func approve{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + ERC721.approve(to, tokenId); + return (); +} @external -func setApprovalForAll{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(operator: felt, approved: felt): - ERC721.set_approval_for_all(operator, approved) - return () -end +func setApprovalForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + operator: felt, approved: felt +) { + ERC721.set_approval_for_all(operator, approved); + return (); +} @external -func transferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256 - ): - ERC721Enumerable.transfer_from(from_, to, tokenId) - return () -end +func transferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256 +) { + ERC721Enumerable.transfer_from(from_, to, tokenId); + return (); +} @external -func safeTransferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ): - ERC721Enumerable.safe_transfer_from(from_, to, tokenId, data_len, data) - return () -end +func safeTransferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256, data_len: felt, data: felt* +) { + ERC721Enumerable.safe_transfer_from(from_, to, tokenId, data_len, data); + return (); +} @external -func mint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - Ownable.assert_only_owner() - ERC721Enumerable._mint(to, tokenId) - return () -end +func mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + Ownable.assert_only_owner(); + ERC721Enumerable._mint(to, tokenId); + return (); +} @external -func burn{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(tokenId: Uint256): - ERC721.assert_only_token_owner(tokenId) - ERC721Enumerable._burn(tokenId) - return () -end +func burn{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(tokenId: Uint256) { + ERC721.assert_only_token_owner(tokenId); + ERC721Enumerable._burn(tokenId); + return (); +} @external -func setTokenURI{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(tokenId: Uint256, tokenURI: felt): - Ownable.assert_only_owner() - ERC721._set_token_uri(tokenId, tokenURI) - return () -end +func setTokenURI{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + tokenId: Uint256, tokenURI: felt +) { + Ownable.assert_only_owner(); + ERC721._set_token_uri(tokenId, tokenURI); + return (); +} @external -func transferOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(newOwner: felt): - Ownable.transfer_ownership(newOwner) - return () -end +func transferOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + newOwner: felt +) { + Ownable.transfer_ownership(newOwner); + return (); +} @external -func renounceOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.renounce_ownership() - return () -end +func renounceOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.renounce_ownership(); + return (); +} diff --git a/src/openzeppelin/token/erc721/enumerable/presets/utils/ERC721Holder.cairo b/src/openzeppelin/token/erc721/enumerable/presets/utils/ERC721Holder.cairo index 900107b36..2c6a55331 100644 --- a/src/openzeppelin/token/erc721/enumerable/presets/utils/ERC721Holder.cairo +++ b/src/openzeppelin/token/erc721/enumerable/presets/utils/ERC721Holder.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/presets/utils/ERC721Holder.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/presets/utils/ERC721Holder.cairo) %lang starknet @@ -12,31 +12,20 @@ from openzeppelin.introspection.erc165.library import ERC165 @view func onERC721Received( - operator: felt, - from_: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ) -> (selector: felt): - return (IERC721_RECEIVER_ID) -end + operator: felt, from_: felt, tokenId: Uint256, data_len: felt, data: felt* +) -> (selector: felt) { + return (selector=IERC721_RECEIVER_ID); +} @view -func supportsInterface{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return ERC165.supports_interface(interfaceId); +} @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - ERC165.register_interface(IERC721_RECEIVER_ID) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + ERC165.register_interface(IERC721_RECEIVER_ID); + return (); +} diff --git a/src/openzeppelin/token/erc721/library.cairo b/src/openzeppelin/token/erc721/library.cairo index 35627f435..9962505e1 100644 --- a/src/openzeppelin/token/erc721/library.cairo +++ b/src/openzeppelin/token/erc721/library.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/library.cairo) %lang starknet @@ -14,549 +14,458 @@ from openzeppelin.introspection.erc165.IERC165 import IERC165 from openzeppelin.security.safemath.library import SafeUint256 from openzeppelin.token.erc721.IERC721Receiver import IERC721Receiver from openzeppelin.utils.constants.library import ( - IERC721_ID, IERC721_METADATA_ID, IERC721_RECEIVER_ID, IACCOUNT_ID + IERC721_ID, + IERC721_METADATA_ID, + IERC721_RECEIVER_ID, + IACCOUNT_ID, ) -# -# Events -# +// +// Events +// @event -func Transfer(from_: felt, to: felt, tokenId: Uint256): -end +func Transfer(from_: felt, to: felt, tokenId: Uint256) { +} @event -func Approval(owner: felt, approved: felt, tokenId: Uint256): -end +func Approval(owner: felt, approved: felt, tokenId: Uint256) { +} @event -func ApprovalForAll(owner: felt, operator: felt, approved: felt): -end +func ApprovalForAll(owner: felt, operator: felt, approved: felt) { +} -# -# Storage -# +// +// Storage +// @storage_var -func ERC721_name() -> (name: felt): -end +func ERC721_name() -> (name: felt) { +} @storage_var -func ERC721_symbol() -> (symbol: felt): -end +func ERC721_symbol() -> (symbol: felt) { +} @storage_var -func ERC721_owners(token_id: Uint256) -> (owner: felt): -end +func ERC721_owners(token_id: Uint256) -> (owner: felt) { +} @storage_var -func ERC721_balances(account: felt) -> (balance: Uint256): -end +func ERC721_balances(account: felt) -> (balance: Uint256) { +} @storage_var -func ERC721_token_approvals(token_id: Uint256) -> (res: felt): -end +func ERC721_token_approvals(token_id: Uint256) -> (approved: felt) { +} @storage_var -func ERC721_operator_approvals(owner: felt, operator: felt) -> (res: felt): -end +func ERC721_operator_approvals(owner: felt, operator: felt) -> (is_approved: felt) { +} @storage_var -func ERC721_token_uri(token_id: Uint256) -> (token_uri: felt): -end - -namespace ERC721: - - # - # Constructor - # - - func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - ): - ERC721_name.write(name) - ERC721_symbol.write(symbol) - ERC165.register_interface(IERC721_ID) - ERC165.register_interface(IERC721_METADATA_ID) - return () - end - - # - # Getters - # - - func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC721_name.read() - return (name) - end - - func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC721_symbol.read() - return (symbol) - end - - func balance_of{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt) -> (balance: Uint256): - with_attr error_message("ERC721: balance query for the zero address"): - assert_not_zero(owner) - end - let (balance: Uint256) = ERC721_balances.read(owner) - return (balance) - end - - func owner_of{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(token_id: Uint256) -> (owner: felt): - with_attr error_message("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - let (owner) = ERC721_owners.read(token_id) - with_attr error_message("ERC721: owner query for nonexistent token"): - assert_not_zero(owner) - end - return (owner) - end - - func get_approved{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(token_id: Uint256) -> (approved: felt): - with_attr error_message("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - let (exists) = _exists(token_id) - with_attr error_message("ERC721: approved query for nonexistent token"): - assert exists = TRUE - end - - let (approved) = ERC721_token_approvals.read(token_id) - return (approved) - end - - func is_approved_for_all{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(owner: felt, operator: felt) -> (is_approved: felt): - let (is_approved) = ERC721_operator_approvals.read(owner=owner, operator=operator) - return (is_approved) - end - - func token_uri{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(token_id: Uint256) -> (token_uri: felt): - let (exists) = _exists(token_id) - with_attr error_message("ERC721_Metadata: URI query for nonexistent token"): - assert exists = TRUE - end - - # if tokenURI is not set, it will return 0 - let (token_uri) = ERC721_token_uri.read(token_id) - return (token_uri) - end - - # - # Externals - # - - func approve{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, token_id: Uint256): - with_attr error_mesage("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - - # Checks caller is not zero address - let (caller) = get_caller_address() - with_attr error_message("ERC721: cannot approve from the zero address"): - assert_not_zero(caller) - end - - # Ensures 'owner' does not equal 'to' - let (owner) = ERC721_owners.read(token_id) - with_attr error_message("ERC721: approval to current owner"): - assert_not_equal(owner, to) - end - - # Checks that either caller equals owner or - # caller isApprovedForAll on behalf of owner - if caller == owner: - _approve(to, token_id) - return () - else: - let (is_approved) = ERC721_operator_approvals.read(owner, caller) - with_attr error_message("ERC721: approve caller is not owner nor approved for all"): - assert_not_zero(is_approved) - end - _approve(to, token_id) - return () - end - end - - func set_approval_for_all{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(operator: felt, approved: felt): - # Ensures caller is neither zero address nor operator - let (caller) = get_caller_address() - with_attr error_message("ERC721: either the caller or operator is the zero address"): - assert_not_zero(caller * operator) - end - # note this pattern as we'll frequently use it: - # instead of making an `assert_not_zero` call for each address - # we can always briefly write `assert_not_zero(a0 * a1 * ... * aN)`. - # This is because these addresses are field elements, - # meaning that a*0==0 for all a in the field, - # and a*b==0 implies that at least one of a,b are zero in the field - with_attr error_message("ERC721: approve to caller"): - assert_not_equal(caller, operator) - end - - # Make sure `approved` is a boolean (0 or 1) - with_attr error_message("ERC721: approved is not a Cairo boolean"): - assert approved * (1 - approved) = 0 - end - - ERC721_operator_approvals.write(owner=caller, operator=operator, value=approved) - ApprovalForAll.emit(caller, operator, approved) - return () - end - - func transfer_from{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(from_: felt, to: felt, token_id: Uint256): - alloc_locals - with_attr error_message("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - let (caller) = get_caller_address() - let (is_approved) = _is_approved_or_owner(caller, token_id) - with_attr error_message("ERC721: either is not approved or the caller is the zero address"): - assert_not_zero(caller * is_approved) - end - # Note that if either `is_approved` or `caller` equals `0`, - # then this method should fail. - # The `caller` address and `is_approved` boolean are both field elements - # meaning that a*0==0 for all a in the field, - # therefore a*b==0 implies that at least one of a,b is zero in the field - - _transfer(from_, to, token_id) - return () - end - - func safe_transfer_from{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - token_id: Uint256, - data_len: felt, - data: felt* - ): - alloc_locals - with_attr error_message("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - let (caller) = get_caller_address() - let (is_approved) = _is_approved_or_owner(caller, token_id) - with_attr error_message("ERC721: either is not approved or the caller is the zero address"): - assert_not_zero(caller * is_approved) - end - # Note that if either `is_approved` or `caller` equals `0`, - # then this method should fail. - # The `caller` address and `is_approved` boolean are both field elements - # meaning that a*0==0 for all a in the field, - # therefore a*b==0 implies that at least one of a,b is zero in the field - - _safe_transfer(from_, to, token_id, data_len, data) - return () - end - - # - # Internals - # - - func assert_only_token_owner{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(token_id: Uint256): - uint256_check(token_id) - let (caller) = get_caller_address() - let (owner) = owner_of(token_id) - # Note `owner_of` checks that the owner is not the zero address - with_attr error_message("ERC721: caller is not the token owner"): - assert caller = owner - end - return () - end - - func _is_approved_or_owner{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(spender: felt, token_id: Uint256) -> (res: felt): - alloc_locals - - let (exists) = _exists(token_id) - with_attr error_message("ERC721: token id does not exist"): - assert exists = TRUE - end - - let (owner) = owner_of(token_id) - if owner == spender: - return (TRUE) - end - - let (approved_addr) = get_approved(token_id) - if approved_addr == spender: - return (TRUE) - end - - let (is_operator) = is_approved_for_all(owner, spender) - if is_operator == TRUE: - return (TRUE) - end - - return (FALSE) - end - - func _exists{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(token_id: Uint256) -> (res: felt): - let (res) = ERC721_owners.read(token_id) - - if res == 0: - return (FALSE) - else: - return (TRUE) - end - end - - func _approve{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(to: felt, token_id: Uint256): - ERC721_token_approvals.write(token_id, to) - let (owner) = owner_of(token_id) - Approval.emit(owner, to, token_id) - return () - end - - func _transfer{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(from_: felt, to: felt, token_id: Uint256): - # ownerOf ensures 'from_' is not the zero address - let (owner) = owner_of(token_id) - with_attr error_message("ERC721: transfer from incorrect owner"): - assert owner = from_ - end - - with_attr error_message("ERC721: cannot transfer to the zero address"): - assert_not_zero(to) - end - - # Clear approvals - _approve(0, token_id) - - # Decrease owner balance - let (owner_bal) = ERC721_balances.read(from_) - let (new_balance: Uint256) = SafeUint256.sub_le(owner_bal, Uint256(1, 0)) - ERC721_balances.write(from_, new_balance) - - # Increase receiver balance - let (receiver_bal) = ERC721_balances.read(to) - let (new_balance: Uint256) = SafeUint256.add(receiver_bal, Uint256(1, 0)) - ERC721_balances.write(to, new_balance) - - # Update token_id owner - ERC721_owners.write(token_id, to) - Transfer.emit(from_, to, token_id) - return () - end - - func _safe_transfer{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - from_: felt, - to: felt, - token_id: Uint256, - data_len: felt, - data: felt* - ): - _transfer(from_, to, token_id) - - let (success) = _check_onERC721Received(from_, to, token_id, data_len, data) - with_attr error_message("ERC721: transfer to non ERC721Receiver implementer"): - assert_not_zero(success) - end - return () - end - - func _mint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, token_id: Uint256): - with_attr error_message("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - with_attr error_message("ERC721: cannot mint to the zero address"): - assert_not_zero(to) - end - - # Ensures token_id is unique - let (exists) = _exists(token_id) - with_attr error_message("ERC721: token already minted"): - assert exists = FALSE - end - - let (balance: Uint256) = ERC721_balances.read(to) - let (new_balance: Uint256) = SafeUint256.add(balance, Uint256(1, 0)) - ERC721_balances.write(to, new_balance) - ERC721_owners.write(token_id, to) - Transfer.emit(0, to, token_id) - return () - end - - func _safe_mint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - to: felt, - token_id: Uint256, - data_len: felt, - data: felt* - ): - with_attr error_message("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - _mint(to, token_id) - - let (success) = _check_onERC721Received( - 0, - to, - token_id, - data_len, - data - ) - with_attr error_message("ERC721: transfer to non ERC721Receiver implementer"): - assert_not_zero(success) - end - return () - end - - func _burn{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(token_id: Uint256): - alloc_locals - with_attr error_message("ERC721: token_id is not a valid Uint256"): - uint256_check(token_id) - end - let (owner) = owner_of(token_id) - - # Clear approvals - _approve(0, token_id) - - # Decrease owner balance - let (balance: Uint256) = ERC721_balances.read(owner) - let (new_balance: Uint256) = SafeUint256.sub_le(balance, Uint256(1, 0)) - ERC721_balances.write(owner, new_balance) - - # Delete owner - ERC721_owners.write(token_id, 0) - Transfer.emit(owner, 0, token_id) - return () - end - - func _set_token_uri{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(token_id: Uint256, token_uri: felt): - uint256_check(token_id) - let (exists) = _exists(token_id) - with_attr error_message("ERC721_Metadata: set token URI for nonexistent token"): - assert exists = TRUE - end - - ERC721_token_uri.write(token_id, token_uri) - return () - end - -end - -# -# Private -# - -func _check_onERC721Received{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }( - from_: felt, - to: felt, - token_id: Uint256, - data_len: felt, - data: felt* - ) -> (success: felt): - let (caller) = get_caller_address() - let (is_supported) = IERC165.supportsInterface(to, IERC721_RECEIVER_ID) - if is_supported == TRUE: +func ERC721_token_uri(token_id: Uint256) -> (token_uri: felt) { +} + +namespace ERC721 { + // + // Constructor + // + + func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt + ) { + ERC721_name.write(name); + ERC721_symbol.write(symbol); + ERC165.register_interface(IERC721_ID); + ERC165.register_interface(IERC721_METADATA_ID); + return (); + } + + // + // Getters + // + + func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC721_name.read(); + } + + func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + symbol: felt + ) { + return ERC721_symbol.read(); + } + + func balance_of{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt + ) -> (balance: Uint256) { + with_attr error_message("ERC721: balance query for the zero address") { + assert_not_zero(owner); + } + return ERC721_balances.read(owner); + } + + func owner_of{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_id: Uint256 + ) -> (owner: felt) { + with_attr error_message("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + let (owner) = ERC721_owners.read(token_id); + with_attr error_message("ERC721: owner query for nonexistent token") { + assert_not_zero(owner); + } + return (owner=owner); + } + + func get_approved{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_id: Uint256 + ) -> (approved: felt) { + with_attr error_message("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + let exists = _exists(token_id); + with_attr error_message("ERC721: approved query for nonexistent token") { + assert exists = TRUE; + } + + return ERC721_token_approvals.read(token_id); + } + + func is_approved_for_all{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, operator: felt + ) -> (is_approved: felt) { + return ERC721_operator_approvals.read(owner, operator); + } + + func token_uri{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_id: Uint256 + ) -> (token_uri: felt) { + let exists = _exists(token_id); + with_attr error_message("ERC721_Metadata: URI query for nonexistent token") { + assert exists = TRUE; + } + + // if tokenURI is not set, it will return 0 + return ERC721_token_uri.read(token_id); + } + + // + // Externals + // + + func approve{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, token_id: Uint256 + ) { + with_attr error_mesage("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + + // Checks caller is not zero address + let (caller) = get_caller_address(); + with_attr error_message("ERC721: cannot approve from the zero address") { + assert_not_zero(caller); + } + + // Ensures 'owner' does not equal 'to' + let (owner) = ERC721_owners.read(token_id); + with_attr error_message("ERC721: approval to current owner") { + assert_not_equal(owner, to); + } + + // Checks that either caller equals owner or + // caller isApprovedForAll on behalf of owner + if (caller == owner) { + _approve(to, token_id); + return (); + } else { + let (is_approved) = ERC721_operator_approvals.read(owner, caller); + with_attr error_message("ERC721: approve caller is not owner nor approved for all") { + assert_not_zero(is_approved); + } + _approve(to, token_id); + return (); + } + } + + func set_approval_for_all{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + operator: felt, approved: felt + ) { + // Ensures caller is neither zero address nor operator + let (caller) = get_caller_address(); + with_attr error_message("ERC721: either the caller or operator is the zero address") { + assert_not_zero(caller * operator); + } + // note this pattern as we'll frequently use it: + // instead of making an `assert_not_zero` call for each address + // we can always briefly write `assert_not_zero(a0 * a1 * ... * aN)`. + // This is because these addresses are field elements, + // meaning that a*0==0 for all a in the field, + // and a*b==0 implies that at least one of a,b are zero in the field + with_attr error_message("ERC721: approve to caller") { + assert_not_equal(caller, operator); + } + + // Make sure `approved` is a boolean (0 or 1) + with_attr error_message("ERC721: approved is not a Cairo boolean") { + assert approved * (1 - approved) = 0; + } + + ERC721_operator_approvals.write(owner=caller, operator=operator, value=approved); + ApprovalForAll.emit(caller, operator, approved); + return (); + } + + func transfer_from{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, token_id: Uint256 + ) { + alloc_locals; + with_attr error_message("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + let (caller) = get_caller_address(); + let is_approved = _is_approved_or_owner(caller, token_id); + with_attr error_message( + "ERC721: either is not approved or the caller is the zero address") { + assert_not_zero(caller * is_approved); + } + // Note that if either `is_approved` or `caller` equals `0`, + // then this method should fail. + // The `caller` address and `is_approved` boolean are both field elements + // meaning that a*0==0 for all a in the field, + // therefore a*b==0 implies that at least one of a,b is zero in the field + + _transfer(from_, to, token_id); + return (); + } + + func safe_transfer_from{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, token_id: Uint256, data_len: felt, data: felt* + ) { + alloc_locals; + with_attr error_message("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + let (caller) = get_caller_address(); + let is_approved = _is_approved_or_owner(caller, token_id); + with_attr error_message( + "ERC721: either is not approved or the caller is the zero address") { + assert_not_zero(caller * is_approved); + } + // Note that if either `is_approved` or `caller` equals `0`, + // then this method should fail. + // The `caller` address and `is_approved` boolean are both field elements + // meaning that a*0==0 for all a in the field, + // therefore a*b==0 implies that at least one of a,b is zero in the field + + _safe_transfer(from_, to, token_id, data_len, data); + return (); + } + + // + // Internals + // + + func assert_only_token_owner{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + token_id: Uint256 + ) { + uint256_check(token_id); + let (caller) = get_caller_address(); + let (owner) = owner_of(token_id); + // Note `owner_of` checks that the owner is not the zero address + with_attr error_message("ERC721: caller is not the token owner") { + assert caller = owner; + } + return (); + } + + func _is_approved_or_owner{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + spender: felt, token_id: Uint256 + ) -> felt { + alloc_locals; + + let exists = _exists(token_id); + with_attr error_message("ERC721: token id does not exist") { + assert exists = TRUE; + } + + let (owner) = owner_of(token_id); + if (owner == spender) { + return TRUE; + } + + let (approved_addr) = get_approved(token_id); + if (approved_addr == spender) { + return TRUE; + } + + let (is_operator) = is_approved_for_all(owner, spender); + if (is_operator == TRUE) { + return TRUE; + } + + return FALSE; + } + + func _exists{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_id: Uint256 + ) -> felt { + let (exists) = ERC721_owners.read(token_id); + if (exists == FALSE) { + return FALSE; + } + + return TRUE; + } + + func _approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + to: felt, token_id: Uint256 + ) { + ERC721_token_approvals.write(token_id, to); + let (owner) = owner_of(token_id); + Approval.emit(owner, to, token_id); + return (); + } + + func _transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + from_: felt, to: felt, token_id: Uint256 + ) { + // ownerOf ensures 'from_' is not the zero address + let (owner) = owner_of(token_id); + with_attr error_message("ERC721: transfer from incorrect owner") { + assert owner = from_; + } + + with_attr error_message("ERC721: cannot transfer to the zero address") { + assert_not_zero(to); + } + + // Clear approvals + _approve(0, token_id); + + // Decrease owner balance + let (owner_bal) = ERC721_balances.read(from_); + let (new_balance: Uint256) = SafeUint256.sub_le(owner_bal, Uint256(1, 0)); + ERC721_balances.write(from_, new_balance); + + // Increase receiver balance + let (receiver_bal) = ERC721_balances.read(to); + let (new_balance: Uint256) = SafeUint256.add(receiver_bal, Uint256(1, 0)); + ERC721_balances.write(to, new_balance); + + // Update token_id owner + ERC721_owners.write(token_id, to); + Transfer.emit(from_, to, token_id); + return (); + } + + func _safe_transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + from_: felt, to: felt, token_id: Uint256, data_len: felt, data: felt* + ) { + _transfer(from_, to, token_id); + + let (success) = _check_onERC721Received(from_, to, token_id, data_len, data); + with_attr error_message("ERC721: transfer to non ERC721Receiver implementer") { + assert_not_zero(success); + } + return (); + } + + func _mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, token_id: Uint256 + ) { + with_attr error_message("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + with_attr error_message("ERC721: cannot mint to the zero address") { + assert_not_zero(to); + } + + // Ensures token_id is unique + let exists = _exists(token_id); + with_attr error_message("ERC721: token already minted") { + assert exists = FALSE; + } + + let (balance: Uint256) = ERC721_balances.read(to); + let (new_balance: Uint256) = SafeUint256.add(balance, Uint256(1, 0)); + ERC721_balances.write(to, new_balance); + ERC721_owners.write(token_id, to); + Transfer.emit(0, to, token_id); + return (); + } + + func _safe_mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, token_id: Uint256, data_len: felt, data: felt* + ) { + with_attr error_message("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + _mint(to, token_id); + + let (success) = _check_onERC721Received(0, to, token_id, data_len, data); + with_attr error_message("ERC721: transfer to non ERC721Receiver implementer") { + assert_not_zero(success); + } + return (); + } + + func _burn{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(token_id: Uint256) { + alloc_locals; + with_attr error_message("ERC721: token_id is not a valid Uint256") { + uint256_check(token_id); + } + let (owner) = owner_of(token_id); + + // Clear approvals + _approve(0, token_id); + + // Decrease owner balance + let (balance: Uint256) = ERC721_balances.read(owner); + let (new_balance: Uint256) = SafeUint256.sub_le(balance, Uint256(1, 0)); + ERC721_balances.write(owner, new_balance); + + // Delete owner + ERC721_owners.write(token_id, 0); + Transfer.emit(owner, 0, token_id); + return (); + } + + func _set_token_uri{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_id: Uint256, token_uri: felt + ) { + uint256_check(token_id); + let exists = _exists(token_id); + with_attr error_message("ERC721_Metadata: set token URI for nonexistent token") { + assert exists = TRUE; + } + + ERC721_token_uri.write(token_id, token_uri); + return (); + } +} + +// +// Private +// + +func _check_onERC721Received{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + from_: felt, to: felt, token_id: Uint256, data_len: felt, data: felt* +) -> (success: felt) { + let (caller) = get_caller_address(); + let (is_supported) = IERC165.supportsInterface(to, IERC721_RECEIVER_ID); + if (is_supported == TRUE) { let (selector) = IERC721Receiver.onERC721Received( - to, - caller, - from_, - token_id, - data_len, - data - ) - - with_attr error_message("ERC721: transfer to non ERC721Receiver implementer"): - assert selector = IERC721_RECEIVER_ID - end - return (TRUE) - end - - let (is_account) = IERC165.supportsInterface(to, IACCOUNT_ID) - return (is_account) -end + to, caller, from_, token_id, data_len, data + ); + + with_attr error_message("ERC721: transfer to non ERC721Receiver implementer") { + assert selector = IERC721_RECEIVER_ID; + } + return (success=TRUE); + } + + let (is_account) = IERC165.supportsInterface(to, IACCOUNT_ID); + return (success=is_account); +} diff --git a/src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo b/src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo index c4bf5ecff..a5c88c5cf 100644 --- a/src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo +++ b/src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/presets/ERC721MintableBurnable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/presets/ERC721MintableBurnable.cairo) %lang starknet @@ -10,222 +10,153 @@ from openzeppelin.access.ownable.library import Ownable from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.token.erc721.library import ERC721 -# -# Constructor -# +// +// Constructor +// @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - owner: felt - ): - ERC721.initializer(name, symbol) - Ownable.initializer(owner) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, owner: felt +) { + ERC721.initializer(name, symbol); + Ownable.initializer(owner); + return (); +} + +// +// Getters +// @view -func supportsInterface{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return ERC165.supports_interface(interfaceId); +} @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC721.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC721.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC721.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC721.symbol(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC721.balance_of(owner) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(owner: felt) -> ( + balance: Uint256 +) { + return ERC721.balance_of(owner); +} @view -func ownerOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (owner: felt): - let (owner: felt) = ERC721.owner_of(tokenId) - return (owner) -end +func ownerOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(tokenId: Uint256) -> ( + owner: felt +) { + return ERC721.owner_of(tokenId); +} @view -func getApproved{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (approved: felt): - let (approved: felt) = ERC721.get_approved(tokenId) - return (approved) -end +func getApproved{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (approved: felt) { + return ERC721.get_approved(tokenId); +} @view -func isApprovedForAll{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, operator: felt) -> (isApproved: felt): - let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator) - return (isApproved) -end +func isApprovedForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, operator: felt +) -> (isApproved: felt) { + let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator); + return (isApproved=isApproved); +} @view -func tokenURI{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (tokenURI: felt): - let (tokenURI: felt) = ERC721.token_uri(tokenId) - return (tokenURI) -end +func tokenURI{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (tokenURI: felt) { + let (tokenURI: felt) = ERC721.token_uri(tokenId); + return (tokenURI=tokenURI); +} @view -func owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (owner: felt): - let (owner: felt) = Ownable.owner() - return (owner) -end - -# -# Externals -# +func owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable.owner(); +} + +// +// Externals +// @external -func approve{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - ERC721.approve(to, tokenId) - return () -end +func approve{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + ERC721.approve(to, tokenId); + return (); +} @external -func setApprovalForAll{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(operator: felt, approved: felt): - ERC721.set_approval_for_all(operator, approved) - return () -end +func setApprovalForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + operator: felt, approved: felt +) { + ERC721.set_approval_for_all(operator, approved); + return (); +} @external -func transferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256 - ): - ERC721.transfer_from(from_, to, tokenId) - return () -end +func transferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256 +) { + ERC721.transfer_from(from_, to, tokenId); + return (); +} @external -func safeTransferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ): - ERC721.safe_transfer_from(from_, to, tokenId, data_len, data) - return () -end +func safeTransferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256, data_len: felt, data: felt* +) { + ERC721.safe_transfer_from(from_, to, tokenId, data_len, data); + return (); +} @external -func mint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - Ownable.assert_only_owner() - ERC721._mint(to, tokenId) - return () -end +func mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + Ownable.assert_only_owner(); + ERC721._mint(to, tokenId); + return (); +} @external -func burn{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(tokenId: Uint256): - ERC721.assert_only_token_owner(tokenId) - ERC721._burn(tokenId) - return () -end +func burn{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(tokenId: Uint256) { + ERC721.assert_only_token_owner(tokenId); + ERC721._burn(tokenId); + return (); +} @external -func setTokenURI{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(tokenId: Uint256, tokenURI: felt): - Ownable.assert_only_owner() - ERC721._set_token_uri(tokenId, tokenURI) - return () -end +func setTokenURI{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + tokenId: Uint256, tokenURI: felt +) { + Ownable.assert_only_owner(); + ERC721._set_token_uri(tokenId, tokenURI); + return (); +} @external -func transferOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(newOwner: felt): - Ownable.transfer_ownership(newOwner) - return () -end +func transferOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + newOwner: felt +) { + Ownable.transfer_ownership(newOwner); + return (); +} @external -func renounceOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.renounce_ownership() - return () -end \ No newline at end of file +func renounceOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.renounce_ownership(); + return (); +} diff --git a/src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo b/src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo index 7623be934..ff9c9d359 100644 --- a/src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo +++ b/src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/presets/ERC721MintablePausable.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (token/erc721/presets/ERC721MintablePausable.cairo) %lang starknet @@ -11,248 +11,170 @@ from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.security.pausable.library import Pausable from openzeppelin.token.erc721.library import ERC721 -# -# Constructor -# +// +// Constructor +// @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - owner: felt - ): - ERC721.initializer(name, symbol) - Ownable.initializer(owner) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, owner: felt +) { + ERC721.initializer(name, symbol); + Ownable.initializer(owner); + return (); +} + +// +// Getters +// @view -func supportsInterface{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return ERC165.supports_interface(interfaceId); +} @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC721.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC721.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC721.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC721.symbol(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC721.balance_of(owner) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(owner: felt) -> ( + balance: Uint256 +) { + return ERC721.balance_of(owner); +} @view -func ownerOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (owner: felt): - let (owner: felt) = ERC721.owner_of(tokenId) - return (owner) -end +func ownerOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(tokenId: Uint256) -> ( + owner: felt +) { + return ERC721.owner_of(tokenId); +} @view -func getApproved{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (approved: felt): - let (approved: felt) = ERC721.get_approved(tokenId) - return (approved) -end +func getApproved{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (approved: felt) { + return ERC721.get_approved(tokenId); +} @view -func isApprovedForAll{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, operator: felt) -> (isApproved: felt): - let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator) - return (isApproved) -end +func isApprovedForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, operator: felt +) -> (isApproved: felt) { + let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator); + return (isApproved=isApproved); +} @view -func tokenURI{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (tokenURI: felt): - let (tokenURI: felt) = ERC721.token_uri(tokenId) - return (tokenURI) -end +func tokenURI{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (tokenURI: felt) { + let (tokenURI: felt) = ERC721.token_uri(tokenId); + return (tokenURI=tokenURI); +} @view -func owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (owner: felt): - let (owner: felt) = Ownable.owner() - return (owner) -end +func owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable.owner(); +} @view -func paused{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (paused: felt): - let (paused) = Pausable.is_paused() - return (paused) -end +func paused{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (paused: felt) { + return Pausable.is_paused(); +} -# -# Externals -# +// +// Externals +// @external -func approve{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - Pausable.assert_not_paused() - ERC721.approve(to, tokenId) - return () -end +func approve{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + Pausable.assert_not_paused(); + ERC721.approve(to, tokenId); + return (); +} @external -func setApprovalForAll{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(operator: felt, approved: felt): - Pausable.assert_not_paused() - ERC721.set_approval_for_all(operator, approved) - return () -end +func setApprovalForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + operator: felt, approved: felt +) { + Pausable.assert_not_paused(); + ERC721.set_approval_for_all(operator, approved); + return (); +} @external -func transferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256 - ): - Pausable.assert_not_paused() - ERC721.transfer_from(from_, to, tokenId) - return () -end +func transferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256 +) { + Pausable.assert_not_paused(); + ERC721.transfer_from(from_, to, tokenId); + return (); +} @external -func safeTransferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ): - Pausable.assert_not_paused() - ERC721.safe_transfer_from(from_, to, tokenId, data_len, data) - return () -end +func safeTransferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256, data_len: felt, data: felt* +) { + Pausable.assert_not_paused(); + ERC721.safe_transfer_from(from_, to, tokenId, data_len, data); + return (); +} @external -func mint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - Pausable.assert_not_paused() - Ownable.assert_only_owner() - ERC721._mint(to, tokenId) - return () -end +func mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + Pausable.assert_not_paused(); + Ownable.assert_only_owner(); + ERC721._mint(to, tokenId); + return (); +} @external -func setTokenURI{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(tokenId: Uint256, tokenURI: felt): - Ownable.assert_only_owner() - ERC721._set_token_uri(tokenId, tokenURI) - return () -end +func setTokenURI{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + tokenId: Uint256, tokenURI: felt +) { + Ownable.assert_only_owner(); + ERC721._set_token_uri(tokenId, tokenURI); + return (); +} @external -func transferOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(newOwner: felt): - Ownable.transfer_ownership(newOwner) - return () -end +func transferOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + newOwner: felt +) { + Ownable.transfer_ownership(newOwner); + return (); +} @external -func renounceOwnership{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.renounce_ownership() - return () -end +func renounceOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.renounce_ownership(); + return (); +} @external -func pause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.assert_only_owner() - Pausable._pause() - return () -end +func pause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.assert_only_owner(); + Pausable._pause(); + return (); +} @external -func unpause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Ownable.assert_only_owner() - Pausable._unpause() - return () -end +func unpause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.assert_only_owner(); + Pausable._unpause(); + return (); +} diff --git a/src/openzeppelin/upgrades/library.cairo b/src/openzeppelin/upgrades/library.cairo index 3f1e88aba..baa701b1f 100644 --- a/src/openzeppelin/upgrades/library.cairo +++ b/src/openzeppelin/upgrades/library.cairo @@ -1,5 +1,5 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (upgrades/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (upgrades/library.cairo) %lang starknet @@ -8,124 +8,106 @@ 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 -# +// +// Events +// @event -func Upgraded(implementation: felt): -end +func Upgraded(implementation: felt) { +} @event -func AdminChanged(previousAdmin: felt, newAdmin: felt): -end +func AdminChanged(previousAdmin: felt, newAdmin: felt) { +} -# -# Storage variables -# +// +// Storage variables +// @storage_var -func Proxy_implementation_hash() -> (class_hash: felt): -end +func Proxy_implementation_hash() -> (implementation: felt) { +} @storage_var -func Proxy_admin() -> (proxy_admin: felt): -end +func Proxy_admin() -> (admin: felt) { +} @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 +func Proxy_initialized() -> (initialized: felt) { +} + +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; + } + + Proxy_initialized.write(TRUE); + _set_admin(proxy_admin); + return (); + } + + // + // 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; + } + return (); + } + + // + // Getters + // + + func get_implementation_hash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + ) -> (implementation: felt) { + return Proxy_implementation_hash.read(); + } + + func get_admin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + admin: felt + ) { + return Proxy_admin.read(); + } + + // + // 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 (); + } + + // + // 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); + } + + Proxy_implementation_hash.write(new_implementation); + Upgraded.emit(new_implementation); + return (); + } +} diff --git a/src/openzeppelin/upgrades/presets/Proxy.cairo b/src/openzeppelin/upgrades/presets/Proxy.cairo index b893b25ad..1310e9420 100644 --- a/src/openzeppelin/upgrades/presets/Proxy.cairo +++ b/src/openzeppelin/upgrades/presets/Proxy.cairo @@ -1,78 +1,57 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (upgrades/presets/Proxy.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (upgrades/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 starkware.starknet.common.syscalls import library_call, library_call_l1_handler from openzeppelin.upgrades.library import Proxy -# -# Constructor -# +// +// Constructor +// @constructor -func constructor{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(implementation_hash: felt): - Proxy._set_implementation_hash(implementation_hash) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + implementation_hash: felt +) { + Proxy._set_implementation_hash(implementation_hash); + return (); +} -# -# Fallback functions -# +// +// 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() +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 - + ); + return (retdata_size, retdata); +} @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() +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 + ); + return (); +} diff --git a/src/openzeppelin/utils/constants/library.cairo b/src/openzeppelin/utils/constants/library.cairo index fd9ac1881..3899c87d6 100644 --- a/src/openzeppelin/utils/constants/library.cairo +++ b/src/openzeppelin/utils/constants/library.cairo @@ -1,36 +1,36 @@ -# SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.3.2 (utils/constants/library.cairo) +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts for Cairo v0.3.2 (utils/constants/library.cairo) %lang starknet -# -# Numbers -# +// +// Numbers +// -const UINT8_MAX = 256 +const UINT8_MAX = 255; -# -# Interface Ids -# +// +// Interface Ids +// -# ERC165 -const IERC165_ID = 0x01ffc9a7 -const INVALID_ID = 0xffffffff +// ERC165 +const IERC165_ID = 0x01ffc9a7; +const INVALID_ID = 0xffffffff; -# Account -const IACCOUNT_ID = 0xf10dbd44 +// Account +const IACCOUNT_ID = 0xa66bd575; -# ERC721 -const IERC721_ID = 0x80ac58cd -const IERC721_RECEIVER_ID = 0x150b7a02 -const IERC721_METADATA_ID = 0x5b5e139f -const IERC721_ENUMERABLE_ID = 0x780e9d63 +// ERC721 +const IERC721_ID = 0x80ac58cd; +const IERC721_RECEIVER_ID = 0x150b7a02; +const IERC721_METADATA_ID = 0x5b5e139f; +const IERC721_ENUMERABLE_ID = 0x780e9d63; -# AccessControl -const IACCESSCONTROL_ID = 0x7965db0b +// AccessControl +const IACCESSCONTROL_ID = 0x7965db0b; -# -# Roles -# +// +// Roles +// -const DEFAULT_ADMIN_ROLE = 0 +const DEFAULT_ADMIN_ROLE = 0; diff --git a/tests/access/test_AccessControl.py b/tests/access/test_AccessControl.py index d180969ff..1a1b073b3 100644 --- a/tests/access/test_AccessControl.py +++ b/tests/access/test_AccessControl.py @@ -53,7 +53,7 @@ def accesscontrol_factory(contract_classes, accesscontrol_init): async def test_initializer(accesscontrol_factory): accesscontrol, _, _ = accesscontrol_factory - execution_info = await accesscontrol.supportsInterface(IACCESSCONTROL_ID).invoke() + execution_info = await accesscontrol.supportsInterface(IACCESSCONTROL_ID).execute() assert execution_info.result == (TRUE,) @@ -82,7 +82,7 @@ async def test_grant_role(accesscontrol_factory): ] ) - expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account2.contract_address).invoke() + expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account2.contract_address).execute() assert expected.result.hasRole == TRUE @@ -115,7 +115,7 @@ async def test_revoke_role(accesscontrol_factory): account2.contract_address # sender ] ) - expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account1.contract_address).invoke() + expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account1.contract_address).execute() assert expected.result.hasRole == FALSE @@ -164,7 +164,7 @@ async def test_renounce_role(accesscontrol_factory): ] ) - expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account1.contract_address).invoke() + expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account1.contract_address).execute() assert expected.result.hasRole == FALSE @@ -208,7 +208,7 @@ async def test_set_role_admin(accesscontrol_factory): ] ) - expected = await accesscontrol.getRoleAdmin(DEFAULT_ADMIN_ROLE).invoke() + expected = await accesscontrol.getRoleAdmin(DEFAULT_ADMIN_ROLE).execute() assert expected.result.admin == SOME_OTHER_ROLE # test role admin cycle @@ -219,7 +219,7 @@ async def test_set_role_admin(accesscontrol_factory): [SOME_OTHER_ROLE, account2.contract_address] ) - expected = await accesscontrol.hasRole(SOME_OTHER_ROLE, account2.contract_address).invoke() + expected = await accesscontrol.hasRole(SOME_OTHER_ROLE, account2.contract_address).execute() assert expected.result.hasRole == TRUE await signer.send_transaction( @@ -229,5 +229,5 @@ async def test_set_role_admin(accesscontrol_factory): [DEFAULT_ADMIN_ROLE, account1.contract_address] ) - expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account1.contract_address).invoke() + expected = await accesscontrol.hasRole(DEFAULT_ADMIN_ROLE, account1.contract_address).execute() assert expected.result.hasRole == FALSE diff --git a/tests/access/test_Ownable.py b/tests/access/test_Ownable.py index 346c6f794..33d9dcc5a 100644 --- a/tests/access/test_Ownable.py +++ b/tests/access/test_Ownable.py @@ -92,7 +92,7 @@ async def test_contract_without_owner(ownable_factory): # Protected function should not be called from zero address await assert_revert( - ownable.protected_function().invoke(), + ownable.protected_function().execute(), reverted_with="Ownable: caller is the zero address" ) diff --git a/tests/account/test_Account.py b/tests/account/test_Account.py index 3dd311d54..95e85f159 100644 --- a/tests/account/test_Account.py +++ b/tests/account/test_Account.py @@ -1,12 +1,12 @@ import pytest -from signers import MockSigner +from signers import MockSigner, get_raw_invoke from utils import assert_revert, get_contract_class, cached_contract, TRUE, State, Account signer = MockSigner(123456789987654321) other = MockSigner(987654321123456789) -IACCOUNT_ID = 0xf10dbd44 +IACCOUNT_ID = 0xa66bd575 @pytest.fixture(scope='module') @@ -59,7 +59,7 @@ def account_factory(contract_classes, account_init): async def test_constructor(account_factory): account, *_ = account_factory - execution_info = await account.get_public_key().call() + execution_info = await account.getPublicKey().call() assert execution_info.result == (signer.public_key,) execution_info = await account.supportsInterface(IACCOUNT_ID).call() @@ -112,7 +112,7 @@ async def test_return_value(account_factory): read_info = await signer.send_transactions(account, [(initializable.contract_address, 'initialized', [])]) call_info = await initializable.initialized().call() (call_result, ) = call_info.result - assert read_info.result.response == [call_result] # 1 + assert read_info.call_info.retdata[1] == call_result #1 @ pytest.mark.asyncio @@ -122,23 +122,30 @@ async def test_nonce(account_factory): # bump nonce await signer.send_transactions(account, [(initializable.contract_address, 'initialized', [])]) - execution_info = await account.get_nonce().call() - current_nonce = execution_info.result.res + # get nonce + hex_args = [(hex(initializable.contract_address), 'initialized', [])] + raw_invocation = get_raw_invoke(account, hex_args) + current_nonce = await raw_invocation.state.state.get_nonce_at(account.contract_address) # lower nonce await assert_revert( - signer.send_transactions(account, [(initializable.contract_address, 'initialize', [])], current_nonce - 1), - reverted_with="Account: nonce is invalid" + signer.send_transactions( + account, [(initializable.contract_address, 'initialize', [])], nonce=current_nonce - 1), + reverted_with="Invalid transaction nonce. Expected: {}, got: {}.".format( + current_nonce, current_nonce - 1 + ) ) # higher nonce await assert_revert( - signer.send_transactions(account, [(initializable.contract_address, 'initialize', [])], current_nonce + 1), - reverted_with="Account: nonce is invalid" + signer.send_transactions(account, [(initializable.contract_address, 'initialize', [])], nonce=current_nonce + 1), + reverted_with="Invalid transaction nonce. Expected: {}, got: {}.".format( + current_nonce, current_nonce + 1 + ) ) # right nonce - await signer.send_transactions(account, [(initializable.contract_address, 'initialize', [])], current_nonce) + await signer.send_transactions(account, [(initializable.contract_address, 'initialize', [])], nonce=current_nonce) execution_info = await initializable.initialized().call() assert execution_info.result == (1,) @@ -148,13 +155,13 @@ async def test_nonce(account_factory): async def test_public_key_setter(account_factory): account, *_ = account_factory - execution_info = await account.get_public_key().call() + execution_info = await account.getPublicKey().call() assert execution_info.result == (signer.public_key,) # set new pubkey - await signer.send_transactions(account, [(account.contract_address, 'set_public_key', [other.public_key])]) + await signer.send_transactions(account, [(account.contract_address, 'setPublicKey', [other.public_key])]) - execution_info = await account.get_public_key().call() + execution_info = await account.getPublicKey().call() assert execution_info.result == (other.public_key,) @@ -166,7 +173,7 @@ async def test_public_key_setter_different_account(account_factory): await assert_revert( signer.send_transactions( bad_account, - [(account.contract_address, 'set_public_key', [other.public_key])] + [(account.contract_address, 'setPublicKey', [other.public_key])] ), reverted_with="Account: caller is not this account" ) @@ -180,6 +187,6 @@ async def test_account_takeover_with_reentrant_call(account_factory): signer.send_transaction(account, attacker.contract_address, 'account_takeover', []), reverted_with="Account: no reentrant call" ) - - execution_info = await account.get_public_key().call() + + execution_info = await account.getPublicKey().call() assert execution_info.result == (signer.public_key,) diff --git a/tests/account/test_EthAccount.py b/tests/account/test_EthAccount.py index 8f14b420c..819c58abc 100644 --- a/tests/account/test_EthAccount.py +++ b/tests/account/test_EthAccount.py @@ -1,12 +1,12 @@ import pytest from utils import assert_revert, get_contract_class, cached_contract, TRUE, FALSE, State -from signers import MockEthSigner +from signers import MockEthSigner, get_raw_invoke private_key = b'\x01' * 32 signer = MockEthSigner(b'\x01' * 32) other = MockEthSigner(b'\x02' * 32) -IACCOUNT_ID = 0xf10dbd44 +IACCOUNT_ID = 0xa66bd575 @pytest.fixture(scope='module') @@ -65,7 +65,7 @@ def account_factory(contract_defs, account_init): async def test_constructor(account_factory): account, *_ = account_factory - execution_info = await account.get_eth_address().call() + execution_info = await account.getEthAddress().call() assert execution_info.result == (signer.eth_address,) execution_info = await account.supportsInterface(IACCOUNT_ID).call() @@ -81,15 +81,15 @@ async def test_execute(account_factory): _, hash, signature = await signer.send_transactions(account, [(initializable.contract_address, 'initialize', [])]) - validity_info, *_ = await signer.send_transactions(account, [(account.contract_address, 'is_valid_signature', [hash, len(signature), *signature])]) - assert validity_info.result.response[0] == TRUE + validity_info, *_ = await signer.send_transactions(account, [(account.contract_address, 'isValidSignature', [hash, len(signature), *signature])]) + assert validity_info.call_info.retdata[1] == TRUE execution_info = await initializable.initialized().call() assert execution_info.result == (TRUE,) # should revert if signature is not correct await assert_revert( - signer.send_transactions(account, [(account.contract_address, 'is_valid_signature', [ + signer.send_transactions(account, [(account.contract_address, 'isValidSignature', [ hash-1, len(signature), *signature])]), reverted_with="Invalid signature" ) @@ -128,7 +128,7 @@ async def test_return_value(account_factory): read_info, *_ = await signer.send_transactions(account, [(initializable.contract_address, 'initialized', [])]) call_info = await initializable.initialized().call() (call_result, ) = call_info.result - assert read_info.result.response == [call_result] # 1 + assert read_info.call_info.retdata[1] == call_result # 1 @ pytest.mark.asyncio @@ -138,21 +138,26 @@ async def test_nonce(account_factory): # bump nonce await signer.send_transactions(account, [(initializable.contract_address, 'initialized', [])]) - execution_info = await account.get_nonce().call() - current_nonce = execution_info.result.res + hex_args = [(hex(initializable.contract_address), 'initialized', [])] + raw_invocation = get_raw_invoke(account, hex_args) + current_nonce = await raw_invocation.state.state.get_nonce_at(account.contract_address) # lower nonce await assert_revert( signer.send_transactions( account, [(initializable.contract_address, 'initialize', [])], current_nonce - 1), - reverted_with="Account: nonce is invalid" + reverted_with="Invalid transaction nonce. Expected: {}, got: {}.".format( + current_nonce, current_nonce - 1 + ) ) # higher nonce await assert_revert( signer.send_transactions( account, [(initializable.contract_address, 'initialize', [])], current_nonce + 1), - reverted_with="Account: nonce is invalid" + reverted_with="Invalid transaction nonce. Expected: {}, got: {}.".format( + current_nonce, current_nonce + 1 + ) ) # right nonce @@ -166,13 +171,13 @@ async def test_nonce(account_factory): async def test_eth_address_setter(account_factory): account, *_ = account_factory - execution_info = await account.get_eth_address().call() + execution_info = await account.getEthAddress().call() assert execution_info.result == (signer.eth_address,) # set new pubkey - await signer.send_transactions(account, [(account.contract_address, 'set_eth_address', [other.eth_address])]) + await signer.send_transactions(account, [(account.contract_address, 'setEthAddress', [other.eth_address])]) - execution_info = await account.get_eth_address().call() + execution_info = await account.getEthAddress().call() assert execution_info.result == (other.eth_address,) @@ -184,7 +189,7 @@ async def test_eth_address_setter_different_account(account_factory): await assert_revert( signer.send_transactions( bad_account, - [(account.contract_address, 'set_eth_address', [other.eth_address])] + [(account.contract_address, 'setEthAddress', [other.eth_address])] ), reverted_with="Account: caller is not this account" ) @@ -200,5 +205,5 @@ async def test_account_takeover_with_reentrant_call(account_factory): reverted_with="Account: no reentrant call" ) - execution_info = await account.get_eth_address().call() + execution_info = await account.getEthAddress().call() assert execution_info.result == (signer.eth_address,) diff --git a/tests/introspection/test_ERC165.py b/tests/introspection/test_ERC165.py index 7a7e3ff08..495a4d322 100644 --- a/tests/introspection/test_ERC165.py +++ b/tests/introspection/test_ERC165.py @@ -1,5 +1,4 @@ import pytest -from starkware.starknet.testing.starknet import Starknet from utils import ( assert_revert, get_contract_class, @@ -55,7 +54,7 @@ async def test_register_interface(erc165_factory): assert execution_info.result == (FALSE,) # register interface - await erc165.registerInterface(OTHER_ID).invoke() + await erc165.registerInterface(OTHER_ID).execute() execution_info = await erc165.supportsInterface(OTHER_ID).call() assert execution_info.result == (TRUE,) @@ -66,6 +65,6 @@ async def test_register_invalid_interface(erc165_factory): erc165 = erc165_factory await assert_revert( - erc165.registerInterface(INVALID_ID).invoke(), + erc165.registerInterface(INVALID_ID).execute(), reverted_with="ERC165: invalid interface id" ) diff --git a/tests/mocks/AccessControl.cairo b/tests/mocks/AccessControl.cairo index c1f8cd0bf..b0edacfc1 100644 --- a/tests/mocks/AccessControl.cairo +++ b/tests/mocks/AccessControl.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -9,82 +9,63 @@ from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.utils.constants.library import DEFAULT_ADMIN_ROLE @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(admin: felt): - AccessControl.initializer() - AccessControl._grant_role(DEFAULT_ADMIN_ROLE, admin) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(admin: felt) { + AccessControl.initializer(); + AccessControl._grant_role(DEFAULT_ADMIN_ROLE, admin); + return (); +} @view -func hasRole{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(role: felt, user: felt) -> (hasRole: felt): - let (hasRole) = AccessControl.has_role(role, user) - return (hasRole) -end +func hasRole{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + role: felt, user: felt +) -> (hasRole: felt) { + let (hasRole) = AccessControl.has_role(role, user); + return (hasRole=hasRole); +} @view -func getRoleAdmin{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(role: felt) -> (admin: felt): - return AccessControl.get_role_admin(role) -end +func getRoleAdmin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(role: felt) -> ( + admin: felt +) { + return AccessControl.get_role_admin(role); +} @external -func grantRole{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(role: felt, user: felt): - AccessControl.grant_role(role, user) - return () -end +func grantRole{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + role: felt, user: felt +) { + AccessControl.grant_role(role, user); + return (); +} @external -func revokeRole{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(role: felt, user: felt): - AccessControl.revoke_role(role, user) - return () -end +func revokeRole{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + role: felt, user: felt +) { + AccessControl.revoke_role(role, user); + return (); +} @external -func renounceRole{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(role: felt, user: felt): - AccessControl.renounce_role(role, user) - return () -end +func renounceRole{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + role: felt, user: felt +) { + AccessControl.renounce_role(role, user); + return (); +} -# ONLY FOR MOCKS, DON'T EXPOSE IN PRODUCTION +// ONLY FOR MOCKS, DON'T EXPOSE IN PRODUCTION @external -func setRoleAdmin{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(role: felt, admin: felt): - AccessControl._set_role_admin(role, admin) - return () -end +func setRoleAdmin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + role: felt, admin: felt +) { + AccessControl._set_role_admin(role, admin); + return (); +} @view -func supportsInterface{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return ERC165.supports_interface(interfaceId); +} diff --git a/tests/mocks/AccountReentrancy.cairo b/tests/mocks/AccountReentrancy.cairo index 7efaab6fd..ed03f78b9 100644 --- a/tests/mocks/AccountReentrancy.cairo +++ b/tests/mocks/AccountReentrancy.cairo @@ -1,56 +1,40 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet -from starkware.starknet.common.syscalls import call_contract, get_caller_address, get_tx_info, get_contract_address +from starkware.starknet.common.syscalls import ( + call_contract, + get_caller_address, +) from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin from starkware.cairo.common.alloc import alloc -const GET_NONCE = 756703644403488948674317127005533987569832834207225504298785384568821383277 -const EXECUTE = 617075754465154585683856897856256838130216341506379215893724690153393808813 -const SET_PUBLIC_KEY = 1307260637166823203998179679098545329314629630090003875272134084395659334905 +const EXECUTE = 617075754465154585683856897856256838130216341506379215893724690153393808813; +const SET_PUBLIC_KEY = 332268845949430430346835224631316185987738351560356300584998172574125127129; @external -func account_takeover{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - alloc_locals - let (caller) = get_caller_address() - - let (empty_calldata: felt*) = alloc() - let res = call_contract( - contract_address=caller, - function_selector=GET_NONCE, # get_nonce - calldata_size=0, - calldata=empty_calldata, - ) - let nonce = res.retdata[0] - - let (call_calldata: felt*) = alloc() - - # call_array - assert call_calldata[0] = 1 - assert call_calldata[1] = caller - assert call_calldata[2] = SET_PUBLIC_KEY - assert call_calldata[3] = 0 - assert call_calldata[4] = 1 - - # calldata - assert call_calldata[5] = 1 - assert call_calldata[6] = 123 # new public key - - # nonce - assert call_calldata[7] = nonce +func account_takeover{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + alloc_locals; + let (caller) = get_caller_address(); + let (call_calldata: felt*) = alloc(); + + // call_array + assert call_calldata[0] = 1; + assert call_calldata[1] = caller; + assert call_calldata[2] = SET_PUBLIC_KEY; + assert call_calldata[3] = 0; + assert call_calldata[4] = 1; + + // calldata + assert call_calldata[5] = 1; + // new public key + assert call_calldata[6] = 123; + call_contract( - contract_address=caller, - function_selector=EXECUTE, - calldata_size=8, - calldata=call_calldata, - ) - - return () -end + contract_address=caller, function_selector=EXECUTE, calldata_size=7, calldata=call_calldata + ); + + return (); +} diff --git a/tests/mocks/ERC165.cairo b/tests/mocks/ERC165.cairo index 38a231787..8771eac32 100644 --- a/tests/mocks/ERC165.cairo +++ b/tests/mocks/ERC165.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -7,21 +7,16 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin from openzeppelin.introspection.erc165.library import ERC165 @view -func supportsInterface{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return ERC165.supports_interface(interfaceId); +} @external -func registerInterface{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (interfaceId: felt): - ERC165.register_interface(interfaceId) - return () -end +func registerInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) { + ERC165.register_interface(interfaceId); + return (); +} diff --git a/tests/mocks/ERC721SafeMintableMock.cairo b/tests/mocks/ERC721SafeMintableMock.cairo index d22978619..eaf45e797 100644 --- a/tests/mocks/ERC721SafeMintableMock.cairo +++ b/tests/mocks/ERC721SafeMintableMock.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -9,197 +9,136 @@ from openzeppelin.token.erc721.library import ERC721 from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.access.ownable.library import Ownable -# -# Constructor -# +// +// Constructor +// @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }( - name: felt, - symbol: felt, - owner: felt - ): - ERC721.initializer(name, symbol) - Ownable.initializer(owner) - return () -end - -# -# Getters -# +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + name: felt, symbol: felt, owner: felt +) { + ERC721.initializer(name, symbol); + Ownable.initializer(owner); + return (); +} + +// +// Getters +// @view -func supportsInterface{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(interfaceId: felt) -> (success: felt): - let (success) = ERC165.supports_interface(interfaceId) - return (success) -end +func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + interfaceId: felt +) -> (success: felt) { + return ERC165.supports_interface(interfaceId); +} @view -func name{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (name: felt): - let (name) = ERC721.name() - return (name) -end +func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) { + return ERC721.name(); +} @view -func symbol{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (symbol: felt): - let (symbol) = ERC721.symbol() - return (symbol) -end +func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) { + return ERC721.symbol(); +} @view -func balanceOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt) -> (balance: Uint256): - let (balance: Uint256) = ERC721.balance_of(owner) - return (balance) -end +func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(owner: felt) -> ( + balance: Uint256 +) { + return ERC721.balance_of(owner); +} @view -func ownerOf{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (owner: felt): - let (owner: felt) = ERC721.owner_of(tokenId) - return (owner) -end +func ownerOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(tokenId: Uint256) -> ( + owner: felt +) { + return ERC721.owner_of(tokenId); +} @view -func getApproved{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (approved: felt): - let (approved: felt) = ERC721.get_approved(tokenId) - return (approved) -end +func getApproved{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (approved: felt) { + return ERC721.get_approved(tokenId); +} @view -func isApprovedForAll{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt, operator: felt) -> (isApproved: felt): - let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator) - return (isApproved) -end +func isApprovedForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + owner: felt, operator: felt +) -> (isApproved: felt) { + let (isApproved: felt) = ERC721.is_approved_for_all(owner, operator); + return (isApproved=isApproved); +} @view -func tokenURI{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(tokenId: Uint256) -> (tokenURI: felt): - let (tokenURI: felt) = ERC721.token_uri(tokenId) - return (tokenURI) -end - -# -# Externals -# +func tokenURI{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + tokenId: Uint256 +) -> (tokenURI: felt) { + let (tokenURI: felt) = ERC721.token_uri(tokenId); + return (tokenURI=tokenURI); +} + +// +// Externals +// @external -func approve{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - ERC721.approve(to, tokenId) - return () -end +func approve{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + ERC721.approve(to, tokenId); + return (); +} @external -func setApprovalForAll{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(operator: felt, approved: felt): - ERC721.set_approval_for_all(operator, approved) - return () -end +func setApprovalForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + operator: felt, approved: felt +) { + ERC721.set_approval_for_all(operator, approved); + return (); +} @external -func transferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256 - ): - ERC721.transfer_from(from_, to, tokenId) - return () -end +func transferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256 +) { + ERC721.transfer_from(from_, to, tokenId); + return (); +} @external -func safeTransferFrom{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - from_: felt, - to: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ): - ERC721.safe_transfer_from(from_, to, tokenId, data_len, data) - return () -end +func safeTransferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + from_: felt, to: felt, tokenId: Uint256, data_len: felt, data: felt* +) { + ERC721.safe_transfer_from(from_, to, tokenId, data_len, data); + return (); +} @external -func mint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(to: felt, tokenId: Uint256): - Ownable.assert_only_owner() - ERC721._mint(to, tokenId) - return () -end +func mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256 +) { + Ownable.assert_only_owner(); + ERC721._mint(to, tokenId); + return (); +} @external -func safeMint{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }( - to: felt, - tokenId: Uint256, - data_len: felt, - data: felt* - ): - Ownable.assert_only_owner() - ERC721._safe_mint(to, tokenId, data_len, data) - return () -end +func safeMint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + to: felt, tokenId: Uint256, data_len: felt, data: felt* +) { + Ownable.assert_only_owner(); + ERC721._safe_mint(to, tokenId, data_len, data); + return (); +} @external -func setTokenURI{ - pedersen_ptr: HashBuiltin*, - syscall_ptr: felt*, - range_check_ptr - }(tokenId: Uint256, tokenURI: felt): - Ownable.assert_only_owner() - ERC721._set_token_uri(tokenId, tokenURI) - return () -end +func setTokenURI{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}( + tokenId: Uint256, tokenURI: felt +) { + Ownable.assert_only_owner(); + ERC721._set_token_uri(tokenId, tokenURI); + return (); +} diff --git a/tests/mocks/Initializable.cairo b/tests/mocks/Initializable.cairo index eb1aa8fd8..7cddc5fc8 100644 --- a/tests/mocks/Initializable.cairo +++ b/tests/mocks/Initializable.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -6,23 +6,13 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from openzeppelin.security.initializable.library import Initializable - @view -func initialized{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = Initializable.initialized() - return (res=res) -end +func initialized{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (initialized: felt) { + return Initializable.initialized(); +} @external -func initialize{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - Initializable.initialize() - return () -end +func initialize{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Initializable.initialize(); + return (); +} diff --git a/tests/mocks/Ownable.cairo b/tests/mocks/Ownable.cairo index bd4a57efb..04902e0ad 100644 --- a/tests/mocks/Ownable.cairo +++ b/tests/mocks/Ownable.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -8,51 +8,34 @@ from openzeppelin.access.ownable.library import Ownable from starkware.cairo.common.bool import TRUE @constructor -func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(owner: felt): - Ownable.initializer(owner) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(owner: felt) { + Ownable.initializer(owner); + return (); +} @external -func owner{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (owner: felt): - let (owner) = Ownable.owner() - return (owner=owner) -end +func owner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) { + return Ownable.owner(); +} @external -func transferOwnership{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_owner: felt): - Ownable.transfer_ownership(new_owner) - return () -end +func transferOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_owner: felt +) { + Ownable.transfer_ownership(new_owner); + return (); +} @external -func renounceOwnership{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(): - Ownable.renounce_ownership() - return () -end +func renounceOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Ownable.renounce_ownership(); + return (); +} @external -func protected_function{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (res: felt): - Ownable.assert_only_owner() - return (TRUE) -end +func protected_function{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + success: felt +) { + Ownable.assert_only_owner(); + return (success=TRUE); +} diff --git a/tests/mocks/Pausable.cairo b/tests/mocks/Pausable.cairo index 1266d4e08..cbe7be839 100644 --- a/tests/mocks/Pausable.cairo +++ b/tests/mocks/Pausable.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -9,84 +9,57 @@ from starkware.cairo.common.bool import TRUE, FALSE from openzeppelin.security.pausable.library import Pausable @storage_var -func drastic_measure_taken() -> (res: felt): -end +func drastic_measure_taken() -> (success: felt) { +} @storage_var -func count() -> (res: felt): -end +func counter() -> (count: felt) { +} @view -func isPaused{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (isPaused: felt): - let (isPaused) = Pausable.is_paused() - return (isPaused) -end +func isPaused{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + paused: felt +) { + return Pausable.is_paused(); +} @view -func getCount{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = count.read() - return (res) -end +func getCount{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (count: felt) { + return counter.read(); +} @view -func getDrasticMeasureTaken{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = drastic_measure_taken.read() - return (res) -end +func getDrasticMeasureTaken{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + success: felt +) { + return drastic_measure_taken.read(); +} @external -func normalProcess{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Pausable.assert_not_paused() +func normalProcess{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Pausable.assert_not_paused(); - let (currentCount) = count.read() - count.write(currentCount + 1) - return () -end + let (currentCount) = counter.read(); + counter.write(currentCount + 1); + return (); +} @external -func drasticMeasure{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Pausable.assert_paused() +func drasticMeasure{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Pausable.assert_paused(); - drastic_measure_taken.write(TRUE) - return () -end + drastic_measure_taken.write(TRUE); + return (); +} @external -func pause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Pausable._pause() - return () -end +func pause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Pausable._pause(); + return (); +} @external -func unpause{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - Pausable._unpause() - return () -end +func unpause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + Pausable._unpause(); + return (); +} diff --git a/tests/mocks/ProxiableImplementation.cairo b/tests/mocks/ProxiableImplementation.cairo index 69b52119e..9e5118879 100644 --- a/tests/mocks/ProxiableImplementation.cairo +++ b/tests/mocks/ProxiableImplementation.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -6,73 +6,55 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from openzeppelin.upgrades.library import Proxy -# -# Storage -# +// +// Storage +// @storage_var -func value() -> (res: felt): -end +func value() -> (val: felt) { +} -# -# Initializer -# +// +// Initializer +// @external -func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(proxy_admin: felt): - Proxy.initializer(proxy_admin) - return () -end +func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + proxy_admin: felt +) { + Proxy.initializer(proxy_admin); + return (); +} -# -# Getters -# +// +// Getters +// @view -func getValue{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (val: felt): - let (val) = value.read() - return (val) -end +func getValue{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (val: felt) { + return value.read(); +} @view -func getAdmin{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (address: felt): - let (address) = Proxy.get_admin() - return (address) -end +func getAdmin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + admin: felt +) { + return Proxy.get_admin(); +} -# -# Setters -# +// +// Setters +// @external -func setValue{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(val: felt): - value.write(val) - return () -end +func setValue{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(val: felt) { + value.write(val); + return (); +} @external -func setAdmin{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(address: felt): - Proxy.assert_only_admin() - Proxy._set_admin(address) - return () -end +func setAdmin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(address: felt) { + Proxy.assert_only_admin(); + Proxy._set_admin(address); + return (); +} diff --git a/tests/mocks/ReentrancyAttackerMock.cairo b/tests/mocks/ReentrancyAttackerMock.cairo index 1188a59a0..bda88116e 100644 --- a/tests/mocks/ReentrancyAttackerMock.cairo +++ b/tests/mocks/ReentrancyAttackerMock.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -6,18 +6,14 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.starknet.common.syscalls import get_caller_address @contract_interface -namespace IReentrancyGuard: - func callback(): - end -end +namespace IReentrancyGuard { + func callback() { + } +} @external -func call_sender{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - let (caller) = get_caller_address() - IReentrancyGuard.callback(contract_address=caller) - return () -end +func call_sender{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (caller) = get_caller_address(); + IReentrancyGuard.callback(contract_address=caller); + return (); +} diff --git a/tests/mocks/ReentrancyMock.cairo b/tests/mocks/ReentrancyMock.cairo index 45df9faf0..c9e33d937 100644 --- a/tests/mocks/ReentrancyMock.cairo +++ b/tests/mocks/ReentrancyMock.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -10,122 +10,102 @@ from starkware.starknet.common.syscalls import get_contract_address from openzeppelin.security.reentrancyguard.library import ReentrancyGuard @contract_interface -namespace IReentrancyGuardAttacker: - func call_sender(): - end -end +namespace IReentrancyGuardAttacker { + func call_sender() { + } +} @contract_interface -namespace IReentrancyGuard: - func count_this_recursive(n: felt): - end -end +namespace IReentrancyGuard { + func count_this_recursive(n: felt) { + } +} @storage_var -func counter() -> (res: felt): -end +func counter() -> (count: felt) { +} @constructor -func constructor{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(initial_number: felt): - counter.write(initial_number) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + initial_number: felt +) { + counter.write(initial_number); + return (); +} @view -func current_count{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }() -> (res: felt): - let (res) = counter.read() - return (res) -end +func current_count{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + count: felt +) { + return counter.read(); +} @external -func callback{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - ReentrancyGuard._start() - _count() - ReentrancyGuard._end() - return () -end +func callback{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + ReentrancyGuard._start(); + _count(); + ReentrancyGuard._end(); + return (); +} @external -func count_local_recursive{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (n: felt): - alloc_locals - ReentrancyGuard._start() - let (greater_zero) = is_le(1, n) - if greater_zero == TRUE: - _count() - count_local_recursive(n - 1) - tempvar syscall_ptr=syscall_ptr - tempvar pedersen_ptr=pedersen_ptr - tempvar range_check_ptr=range_check_ptr - else: - tempvar syscall_ptr=syscall_ptr - tempvar pedersen_ptr=pedersen_ptr - tempvar range_check_ptr=range_check_ptr - end - ReentrancyGuard._end() - return () -end +func count_local_recursive{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + n: felt +) { + alloc_locals; + ReentrancyGuard._start(); + let greater_zero = is_le(1, n); + if (greater_zero == TRUE) { + _count(); + count_local_recursive(n - 1); + tempvar syscall_ptr = syscall_ptr; + tempvar pedersen_ptr = pedersen_ptr; + tempvar range_check_ptr = range_check_ptr; + } else { + tempvar syscall_ptr = syscall_ptr; + tempvar pedersen_ptr = pedersen_ptr; + tempvar range_check_ptr = range_check_ptr; + } + ReentrancyGuard._end(); + return (); +} @external -func count_this_recursive{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (n: felt): - alloc_locals - ReentrancyGuard._start() - let (greater_zero) = is_le(1, n) - if greater_zero == TRUE: - _count() - let (contract_address) = get_contract_address() - IReentrancyGuard.count_this_recursive( - contract_address=contract_address, n=n -1) - tempvar syscall_ptr=syscall_ptr - tempvar pedersen_ptr=pedersen_ptr - tempvar range_check_ptr=range_check_ptr - else: - tempvar syscall_ptr=syscall_ptr - tempvar pedersen_ptr=pedersen_ptr - tempvar range_check_ptr=range_check_ptr - end - ReentrancyGuard._end() - return () -end +func count_this_recursive{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + n: felt +) { + alloc_locals; + ReentrancyGuard._start(); + let greater_zero = is_le(1, n); + if (greater_zero == TRUE) { + _count(); + let (contract_address) = get_contract_address(); + IReentrancyGuard.count_this_recursive(contract_address=contract_address, n=n - 1); + tempvar syscall_ptr = syscall_ptr; + tempvar pedersen_ptr = pedersen_ptr; + tempvar range_check_ptr = range_check_ptr; + } else { + tempvar syscall_ptr = syscall_ptr; + tempvar pedersen_ptr = pedersen_ptr; + tempvar range_check_ptr = range_check_ptr; + } + ReentrancyGuard._end(); + return (); +} @external -func count_and_call{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(attacker: felt): - ReentrancyGuard._start() - _count() - IReentrancyGuardAttacker.call_sender(contract_address=attacker) - ReentrancyGuard._end() - return() -end +func count_and_call{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + attacker: felt +) { + ReentrancyGuard._start(); + _count(); + IReentrancyGuardAttacker.call_sender(attacker); + ReentrancyGuard._end(); + return (); +} -func _count{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(): - let (current_count) = counter.read() - counter.write(current_count + 1) - return () -end +func _count{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (current_count) = counter.read(); + counter.write(current_count + 1); + return (); +} diff --git a/tests/mocks/SafeMathMock.cairo b/tests/mocks/SafeMathMock.cairo index b362acbe1..8aa3b28db 100644 --- a/tests/mocks/SafeMathMock.cairo +++ b/tests/mocks/SafeMathMock.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -8,51 +8,36 @@ from starkware.cairo.common.uint256 import Uint256 from openzeppelin.security.safemath.library import SafeUint256 @view -func uint256_add{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - let (c: Uint256) = SafeUint256.add(a, b) - return (c) -end +func uint256_add{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 +) -> (c: Uint256) { + return SafeUint256.add(a, b); +} @view -func uint256_sub_le{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - let (c: Uint256) = SafeUint256.sub_le(a, b) - return (c) -end +func uint256_sub_le{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 +) -> (c: Uint256) { + return SafeUint256.sub_le(a, b); +} @view -func uint256_sub_lt{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - let (c: Uint256) = SafeUint256.sub_lt(a, b) - return (c) -end +func uint256_sub_lt{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 +) -> (c: Uint256) { + return SafeUint256.sub_lt(a, b); +} @view -func uint256_mul{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256): - let (c: Uint256) = SafeUint256.mul(a, b) - return (c) -end +func uint256_mul{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 +) -> (c: Uint256) { + return SafeUint256.mul(a, b); +} @view -func uint256_div{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - } (a: Uint256, b: Uint256) -> (c: Uint256, rem: Uint256): - let (c: Uint256, rem: Uint256) = SafeUint256.div_rem(a, b) - return (c, rem) -end +func uint256_div{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + a: Uint256, b: Uint256 +) -> (c: Uint256, rem: Uint256) { + return SafeUint256.div_rem(a, b); +} diff --git a/tests/mocks/UpgradesMockV1.cairo b/tests/mocks/UpgradesMockV1.cairo index 15f245407..a6dc72f16 100644 --- a/tests/mocks/UpgradesMockV1.cairo +++ b/tests/mocks/UpgradesMockV1.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -6,67 +6,54 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from openzeppelin.upgrades.library import Proxy -# -# Storage -# +// +// Storage +// @storage_var -func value_1() -> (res: felt): -end +func value_1() -> (val: felt) { +} -# -# Initializer -# +// +// Initializer +// @external -func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(proxy_admin: felt): - Proxy.initializer(proxy_admin) - return () -end +func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + proxy_admin: felt +) { + Proxy.initializer(proxy_admin); + return (); +} -# -# Upgrades -# +// +// Upgrades +// @external -func upgrade{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(new_implementation: felt): - Proxy.assert_only_admin() - Proxy._set_implementation_hash(new_implementation) - return () -end - -# -# Getters -# +func upgrade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_implementation: felt +) { + Proxy.assert_only_admin(); + Proxy._set_implementation_hash(new_implementation); + return (); +} + +// +// Getters +// @view -func getValue1{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (val: felt): - let (val) = value_1.read() - return (val) -end +func getValue1{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (val: felt) { + return value_1.read(); +} -# -# Setters -# +// +// Setters +// @external -func setValue1{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(val: felt): - value_1.write(val) - return () -end +func setValue1{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(val: felt) { + value_1.write(val); + return (); +} diff --git a/tests/mocks/UpgradesMockV2.cairo b/tests/mocks/UpgradesMockV2.cairo index ac585dd78..78b49ced2 100644 --- a/tests/mocks/UpgradesMockV2.cairo +++ b/tests/mocks/UpgradesMockV2.cairo @@ -1,4 +1,4 @@ -# SPDX-License-Identifier: MIT +// SPDX-License-Identifier: MIT %lang starknet @@ -6,122 +6,88 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from openzeppelin.upgrades.library import Proxy -# -# Storage -# +// +// Storage +// @storage_var -func value_1() -> (res: felt): -end +func value_1() -> (val: felt) { +} @storage_var -func value_2() -> (res: felt): -end +func value_2() -> (val: felt) { +} -# -# Initializer -# +// +// Initializer +// @external -func initializer{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(proxy_admin: felt): - Proxy.initializer(proxy_admin) - return () -end - -# -# Upgrades -# +func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + proxy_admin: felt +) { + Proxy.initializer(proxy_admin); + return (); +} + +// +// Upgrades +// @external -func upgrade{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr - }(new_implementation: felt): - Proxy.assert_only_admin() - Proxy._set_implementation_hash(new_implementation) - return () -end - -# -# Getters -# +func upgrade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + new_implementation: felt +) { + Proxy.assert_only_admin(); + Proxy._set_implementation_hash(new_implementation); + return (); +} + +// +// Getters +// @view -func getValue1{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (val: felt): - let (val) = value_1.read() - return (val) -end +func getValue1{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (val: felt) { + return value_1.read(); +} @view -func getValue2{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (val: felt): - let (val) = value_2.read() - return (val) -end +func getValue2{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (val: felt) { + return value_2.read(); +} @view -func getImplementationHash{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (address: felt): - let (address) = Proxy.get_implementation_hash() - return (address) -end +func getImplementationHash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + implementation: felt +) { + return Proxy.get_implementation_hash(); +} @view -func getAdmin{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }() -> (admin: felt): - let (admin) = Proxy.get_admin() - return (admin) -end - -# -# Setters -# +func getAdmin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (admin: felt) { + return Proxy.get_admin(); +} + +// +// Setters +// @external -func setValue1{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(val: felt): - value_1.write(val) - return () -end +func setValue1{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(val: felt) { + value_1.write(val); + return (); +} @external -func setValue2{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(val: felt): - value_2.write(val) - return () -end +func setValue2{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(val: felt) { + value_2.write(val); + return (); +} @external -func setAdmin{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, - range_check_ptr - }(new_admin: felt): - Proxy.assert_only_admin() - Proxy._set_admin(new_admin) - return () -end +func setAdmin{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(new_admin: felt) { + Proxy.assert_only_admin(); + Proxy._set_admin(new_admin); + return (); +} diff --git a/tests/security/test_initializable.py b/tests/security/test_initializable.py index 2ea9f8761..f1ff12b3c 100644 --- a/tests/security/test_initializable.py +++ b/tests/security/test_initializable.py @@ -11,13 +11,13 @@ async def test_initializer(): expected = await initializable.initialized().call() assert expected.result == (FALSE,) - await initializable.initialize().invoke() + await initializable.initialize().execute() expected = await initializable.initialized().call() assert expected.result == (TRUE,) # second initialize invocation should revert await assert_revert( - initializable.initialize().invoke(), + initializable.initialize().execute(), reverted_with="Initializable: contract already initialized" ) diff --git a/tests/security/test_pausable.py b/tests/security/test_pausable.py index 29a7a5a8e..3a963cbae 100644 --- a/tests/security/test_pausable.py +++ b/tests/security/test_pausable.py @@ -35,19 +35,19 @@ async def test_pausable_when_unpaused(pausable_factory): contract, _ = pausable_factory execution_info = await contract.isPaused().call() - assert execution_info.result.isPaused == FALSE + assert execution_info.result.paused == FALSE execution_info = await contract.getCount().call() - assert execution_info.result.res == 0 + assert execution_info.result.count == 0 # check that function executes when unpaused - await contract.normalProcess().invoke() + await contract.normalProcess().execute() execution_info = await contract.getCount().call() - assert execution_info.result.res == 1 + assert execution_info.result.count == 1 await assert_revert( - contract.drasticMeasure().invoke(), + contract.drasticMeasure().execute(), reverted_with="Pausable: not paused" ) @@ -56,42 +56,42 @@ async def test_pausable_when_paused(pausable_factory): contract, _ = pausable_factory execution_info = await contract.isPaused().call() - assert execution_info.result.isPaused == FALSE + assert execution_info.result.paused == FALSE # pause - await contract.pause().invoke() + await contract.pause().execute() execution_info = await contract.isPaused().call() - assert execution_info.result.isPaused == TRUE + assert execution_info.result.paused == TRUE await assert_revert( - contract.normalProcess().invoke(), + contract.normalProcess().execute(), reverted_with="Pausable: paused" ) execution_info = await contract.getDrasticMeasureTaken().call() - assert execution_info.result.res == FALSE + assert execution_info.result.success == FALSE # drastic measure - await contract.drasticMeasure().invoke() + await contract.drasticMeasure().execute() execution_info = await contract.getDrasticMeasureTaken().call() - assert execution_info.result.res == TRUE + assert execution_info.result.success == TRUE # unpause - await contract.unpause().invoke() + await contract.unpause().execute() execution_info = await contract.isPaused().call() - assert execution_info.result.isPaused == FALSE + assert execution_info.result.paused == FALSE # check normal process after unpausing - await contract.normalProcess().invoke() + await contract.normalProcess().execute() execution_info = await contract.getCount().call() - assert execution_info.result.res == 1 + assert execution_info.result.count == 1 await assert_revert( - contract.drasticMeasure().invoke(), + contract.drasticMeasure().execute(), reverted_with="Pausable: not paused" ) @@ -100,20 +100,20 @@ async def test_pausable_pause_when_paused(pausable_factory): contract, _ = pausable_factory # pause - await contract.pause().invoke() + await contract.pause().execute() # re-pause await assert_revert( - contract.pause().invoke(), + contract.pause().execute(), reverted_with="Pausable: paused" ) # unpause - await contract.unpause().invoke() + await contract.unpause().execute() # re-unpause await assert_revert( - contract.unpause().invoke(), + contract.unpause().execute(), reverted_with="Pausable: not paused" ) diff --git a/tests/security/test_reentrancy.py b/tests/security/test_reentrancy.py index f0b099955..eda86c878 100644 --- a/tests/security/test_reentrancy.py +++ b/tests/security/test_reentrancy.py @@ -31,7 +31,7 @@ async def test_reentrancy_guard_remote_callback(reentrancy_mock): attacker = await starknet.deploy("tests/mocks/ReentrancyAttackerMock.cairo") # should not allow remote callback await assert_revert( - contract.count_and_call(attacker.contract_address).invoke(), + contract.count_and_call(attacker.contract_address).execute(), reverted_with="ReentrancyGuard: reentrant call" ) @@ -41,12 +41,12 @@ async def test_reentrancy_guard_local_recursion(reentrancy_mock): contract, _ = reentrancy_mock # should not allow local recursion await assert_revert( - contract.count_local_recursive(10).invoke(), + contract.count_local_recursive(10).execute(), reverted_with="ReentrancyGuard: reentrant call" ) # should not allow indirect local recursion await assert_revert( - contract.count_this_recursive(10).invoke(), + contract.count_this_recursive(10).execute(), reverted_with="ReentrancyGuard: reentrant call" ) @@ -55,7 +55,7 @@ async def test_reentrancy_guard_local_recursion(reentrancy_mock): async def test_reentrancy_guard(reentrancy_mock): contract, _ = reentrancy_mock # should allow non reentrant call - await contract.callback().invoke() + await contract.callback().execute() response = await contract.current_count().call() assert response.result == (1,) diff --git a/tests/security/test_safemath.py b/tests/security/test_safemath.py index 8c7039daf..20da705a2 100644 --- a/tests/security/test_safemath.py +++ b/tests/security/test_safemath.py @@ -24,7 +24,7 @@ async def test_add(safemath_mock): b = to_uint(1234) c = add_uint(a, b) - execution_info = await safemath.uint256_add(a, b).invoke() + execution_info = await safemath.uint256_add(a, b).execute() assert execution_info.result == (c,) @@ -36,7 +36,7 @@ async def test_add_overflow(safemath_mock): b = to_uint(1) await assert_revert( - safemath.uint256_add(a, b).invoke(), + safemath.uint256_add(a, b).execute(), reverted_with="SafeUint256: addition overflow" ) @@ -49,7 +49,7 @@ async def test_sub_lt(safemath_mock): b = to_uint(1234) c = sub_uint(a, b) - execution_info = await safemath.uint256_sub_lt(a, b).invoke() + execution_info = await safemath.uint256_sub_lt(a, b).execute() assert execution_info.result == (c,) @@ -61,7 +61,7 @@ async def test_sub_lt_equal(safemath_mock): b = MAX_UINT256 await assert_revert( - safemath.uint256_sub_lt(a, b).invoke(), + safemath.uint256_sub_lt(a, b).execute(), reverted_with="SafeUint256: subtraction overflow or the difference equals zero" ) @@ -74,7 +74,7 @@ async def test_sub_lt_overflow(safemath_mock): b = to_uint(56789) await assert_revert( - safemath.uint256_sub_lt(a, b).invoke(), + safemath.uint256_sub_lt(a, b).execute(), reverted_with="SafeUint256: subtraction overflow or the difference equals zero" ) @@ -87,7 +87,7 @@ async def test_sub_le(safemath_mock): b = to_uint(1234) c = sub_uint(a, b) - execution_info = await safemath.uint256_sub_le(a, b).invoke() + execution_info = await safemath.uint256_sub_le(a, b).execute() assert execution_info.result == (c,) @@ -99,7 +99,7 @@ async def test_sub_le_equal(safemath_mock): b = MAX_UINT256 c = sub_uint(a, b) - execution_info = await safemath.uint256_sub_le(a, b).invoke() + execution_info = await safemath.uint256_sub_le(a, b).execute() assert execution_info.result == (c,) @@ -111,10 +111,10 @@ async def test_sub_le_overflow(safemath_mock): b = to_uint(56789) await assert_revert( - safemath.uint256_sub_le(a, b).invoke(), + safemath.uint256_sub_le(a, b).execute(), reverted_with="SafeUint256: subtraction overflow" ) - await assert_revert(safemath.uint256_sub_le(a, b).invoke()) + await assert_revert(safemath.uint256_sub_le(a, b).execute()) @pytest.mark.asyncio @@ -125,7 +125,7 @@ async def test_mul(safemath_mock): b = to_uint(56789) c = mul_uint(a, b) - execution_info = await safemath.uint256_mul(a, b).invoke() + execution_info = await safemath.uint256_mul(a, b).execute() assert execution_info.result == (c,) @@ -137,10 +137,10 @@ async def test_mul_zero(safemath_mock): b = to_uint(56789) c = to_uint(0) - execution_info = await safemath.uint256_mul(a, b).invoke() + execution_info = await safemath.uint256_mul(a, b).execute() assert execution_info.result == (c,) - execution_info = await safemath.uint256_mul(b, a).invoke() + execution_info = await safemath.uint256_mul(b, a).execute() assert execution_info.result == (c,) @@ -152,7 +152,7 @@ async def test_mul_overflow(safemath_mock): b = to_uint(2) await assert_revert( - safemath.uint256_mul(a, b).invoke(), + safemath.uint256_mul(a, b).execute(), reverted_with="SafeUint256: multiplication overflow" ) @@ -165,7 +165,7 @@ async def test_div(safemath_mock): b = to_uint(56789) (c, r) = div_rem_uint(a, b) - execution_info = await safemath.uint256_div(a, b).invoke() + execution_info = await safemath.uint256_div(a, b).execute() assert execution_info.result == (c, r) @@ -177,7 +177,7 @@ async def test_div_zero_dividend(safemath_mock): b = to_uint(56789) (c, r) = div_rem_uint(a, b) - execution_info = await safemath.uint256_div(a, b).invoke() + execution_info = await safemath.uint256_div(a, b).execute() assert execution_info.result == (c, r) @@ -189,7 +189,7 @@ async def test_div_zero_divisor(safemath_mock): b = to_uint(0) await assert_revert( - safemath.uint256_div(a, b).invoke(), + safemath.uint256_div(a, b).execute(), reverted_with="SafeUint256: divisor cannot be zero" ) @@ -202,5 +202,5 @@ async def test_div_uneven_division(safemath_mock): b = to_uint(5678) (c, r) = div_rem_uint(a, b) - execution_info = await safemath.uint256_div(a, b).invoke() + execution_info = await safemath.uint256_div(a, b).execute() assert execution_info.result == (c, r) diff --git a/tests/signers.py b/tests/signers.py index 7c94d9893..f202ce7dd 100644 --- a/tests/signers.py +++ b/tests/signers.py @@ -1,4 +1,7 @@ -from nile.signer import Signer, from_call_to_call_array, get_transaction_hash +from starkware.starknet.core.os.transaction_hash.transaction_hash import TransactionHashPrefix +from starkware.starknet.services.api.gateway.transaction import InvokeFunction +from starkware.starknet.business_logic.transaction.objects import InternalTransaction, TransactionExecutionInfo +from nile.signer import Signer, from_call_to_call_array, get_transaction_hash, TRANSACTION_VERSION from utils import to_uint import eth_keys @@ -42,20 +45,44 @@ def __init__(self, private_key): async def send_transaction(self, account, to, selector_name, calldata, nonce=None, max_fee=0): return await self.send_transactions(account, [(to, selector_name, calldata)], nonce, max_fee) - async def send_transactions(self, account, calls, nonce=None, max_fee=0): - if nonce is None: - execution_info = await account.get_nonce().call() - nonce, = execution_info.result - + async def send_transactions( + self, + account, + calls, + nonce=None, + max_fee=0 + ) -> TransactionExecutionInfo: + # hexify address before passing to from_call_to_call_array build_calls = [] for call in calls: build_call = list(call) build_call[0] = hex(build_call[0]) build_calls.append(build_call) - (call_array, calldata, sig_r, sig_s) = self.signer.sign_transaction( - hex(account.contract_address), build_calls, nonce, max_fee) - return await account.__execute__(call_array, calldata, nonce).invoke(signature=[sig_r, sig_s]) + raw_invocation = get_raw_invoke(account, build_calls) + state = raw_invocation.state + + if nonce is None: + nonce = await state.state.get_nonce_at(account.contract_address) + + _, sig_r, sig_s = self.signer.sign_transaction(account.contract_address, build_calls, nonce, max_fee) + + # craft invoke and execute tx + external_tx = InvokeFunction( + contract_address=account.contract_address, + calldata=raw_invocation.calldata, + entry_point_selector=None, + signature=[sig_r, sig_s], + max_fee=max_fee, + version=TRANSACTION_VERSION, + nonce=nonce, + ) + + tx = InternalTransaction.from_external( + external_tx=external_tx, general_config=state.general_config + ) + execution_info = await state.execute_tx(tx=tx) + return execution_info class MockEthSigner(): @@ -75,27 +102,52 @@ async def send_transaction(self, account, to, selector_name, calldata, nonce=Non return await self.send_transactions(account, [(to, selector_name, calldata)], nonce, max_fee) async def send_transactions(self, account, calls, nonce=None, max_fee=0): - if nonce is None: - execution_info = await account.get_nonce().call() - nonce, = execution_info.result - build_calls = [] for call in calls: build_call = list(call) build_call[0] = hex(build_call[0]) build_calls.append(build_call) - (call_array, calldata) = from_call_to_call_array(build_calls) - message_hash = get_transaction_hash( - account.contract_address, call_array, calldata, nonce, max_fee + raw_invocation = get_raw_invoke(account, build_calls) + state = raw_invocation.state + + if nonce is None: + nonce = await state.state.get_nonce_at(account.contract_address) + + transaction_hash = get_transaction_hash( + prefix=TransactionHashPrefix.INVOKE, + account=account.contract_address, + calldata=raw_invocation.calldata, + nonce=nonce, + max_fee=max_fee ) signature = self.signer.sign_msg_hash( - (message_hash).to_bytes(32, byteorder="big")) + (transaction_hash).to_bytes(32, byteorder="big")) sig_r = to_uint(signature.r) sig_s = to_uint(signature.s) + external_tx = InvokeFunction( + contract_address=account.contract_address, + calldata=raw_invocation.calldata, + entry_point_selector=None, + signature=[signature.v, *sig_r, *sig_s], + max_fee=max_fee, + version=TRANSACTION_VERSION, + nonce=nonce, + ) + + tx = InternalTransaction.from_external( + external_tx=external_tx, general_config=state.general_config + ) + + execution_info = await state.execute_tx(tx=tx) # the hash and signature are returned for other tests to use - return await account.__execute__(call_array, calldata, nonce).invoke( - signature=[signature.v, *sig_r, *sig_s] - ), message_hash, [signature.v, *sig_r, *sig_s] + return execution_info, transaction_hash, [signature.v, *sig_r, *sig_s] + + +def get_raw_invoke(sender, calls): + """Return raw invoke, remove when test framework supports `invoke`.""" + call_array, calldata = from_call_to_call_array(calls) + raw_invocation = sender.__execute__(call_array, calldata) + return raw_invocation diff --git a/tests/token/erc20/test_ERC20.py b/tests/token/erc20/test_ERC20.py index c84ff5650..d378c573e 100644 --- a/tests/token/erc20/test_ERC20.py +++ b/tests/token/erc20/test_ERC20.py @@ -3,7 +3,7 @@ from utils import ( to_uint, add_uint, sub_uint, str_to_felt, MAX_UINT256, ZERO_ADDRESS, INVALID_UINT256, TRUE, get_contract_class, cached_contract, assert_revert, - assert_event_emitted, contract_path, State, Account + assert_event_emitted, assert_events_emitted, contract_path, State, Account ) @@ -73,11 +73,11 @@ async def test_constructor(erc20_factory): erc20, account, _ = erc20_factory # balanceOf recipient - execution_info = await erc20.balanceOf(account.contract_address).invoke() + execution_info = await erc20.balanceOf(account.contract_address).execute() assert execution_info.result.balance == INIT_SUPPLY # totalSupply - execution_info = await erc20.totalSupply().invoke() + execution_info = await erc20.totalSupply().execute() assert execution_info.result.totalSupply == INIT_SUPPLY @@ -105,21 +105,21 @@ async def test_constructor_exceed_max_decimals(erc20_factory): @pytest.mark.asyncio async def test_name(erc20_factory): erc20, _, _ = erc20_factory - execution_info = await erc20.name().invoke() + execution_info = await erc20.name().execute() assert execution_info.result.name == NAME @pytest.mark.asyncio async def test_symbol(erc20_factory): erc20, _, _ = erc20_factory - execution_info = await erc20.symbol().invoke() + execution_info = await erc20.symbol().execute() assert execution_info.result.symbol == SYMBOL @pytest.mark.asyncio async def test_decimals(erc20_factory): erc20, _, _ = erc20_factory - execution_info = await erc20.decimals().invoke() + execution_info = await erc20.decimals().execute() assert execution_info.result.decimals == DECIMALS @@ -133,7 +133,7 @@ async def test_approve(erc20_factory): erc20, account, spender = erc20_factory # check spender's allowance starts at zero - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == UINT_ZERO # set approval @@ -143,10 +143,10 @@ async def test_approve(erc20_factory): *AMOUNT ] ) - assert return_bool.result.response == [TRUE] + assert return_bool.call_info.retdata[1] == TRUE # check spender's allowance - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == AMOUNT @@ -179,7 +179,7 @@ async def test_approve_from_zero_address(erc20_factory): # Without using an account abstraction, the caller address # (get_caller_address) is zero await assert_revert( - erc20.approve(spender.contract_address, AMOUNT).invoke(), + erc20.approve(spender.contract_address, AMOUNT).execute(), reverted_with="ERC20: cannot approve from the zero address" ) @@ -221,11 +221,11 @@ async def test_transfer(erc20_factory): erc20, account, _ = erc20_factory # check original totalSupply - execution_info = await erc20.balanceOf(account.contract_address).invoke() + execution_info = await erc20.balanceOf(account.contract_address).execute() assert execution_info.result.balance == INIT_SUPPLY # check recipient original balance - execution_info = await erc20.balanceOf(RECIPIENT).invoke() + execution_info = await erc20.balanceOf(RECIPIENT).execute() assert execution_info.result.balance == UINT_ZERO # transfer @@ -235,18 +235,18 @@ async def test_transfer(erc20_factory): *AMOUNT ] ) - assert return_bool.result.response == [TRUE] + assert return_bool.call_info.retdata[1] == TRUE # check account balance - execution_info = await erc20.balanceOf(account.contract_address).invoke() + execution_info = await erc20.balanceOf(account.contract_address).execute() assert execution_info.result.balance == sub_uint(INIT_SUPPLY, AMOUNT) # check recipient balance - execution_info = await erc20.balanceOf(RECIPIENT).invoke() + execution_info = await erc20.balanceOf(RECIPIENT).execute() assert execution_info.result.balance == AMOUNT # check totalSupply - execution_info = await erc20.totalSupply().invoke() + execution_info = await erc20.totalSupply().execute() assert execution_info.result.totalSupply == INIT_SUPPLY @@ -305,7 +305,7 @@ async def test_transfer_from_zero_address(erc20_factory): # Without using an account abstraction, the caller address # (get_caller_address) is zero await assert_revert( - erc20.transfer(RECIPIENT, UINT_ONE).invoke(), + erc20.transfer(RECIPIENT, UINT_ONE).execute(), reverted_with="ERC20: cannot transfer from the zero address" ) @@ -347,18 +347,18 @@ async def test_transferFrom(erc20_factory): *AMOUNT ] ) - assert return_bool.result.response == [TRUE] + assert return_bool.call_info.retdata[1] == TRUE # check account balance - execution_info = await erc20.balanceOf(account.contract_address).invoke() + execution_info = await erc20.balanceOf(account.contract_address).execute() assert execution_info.result.balance == sub_uint(INIT_SUPPLY, AMOUNT) # check recipient balance - execution_info = await erc20.balanceOf(RECIPIENT).invoke() + execution_info = await erc20.balanceOf(RECIPIENT).execute() assert execution_info.result.balance == AMOUNT # check spender allowance after tx - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == UINT_ZERO @@ -381,14 +381,14 @@ async def test_transferFrom_emits_event(erc20_factory): *AMOUNT ]) - assert_event_emitted( + # check events + assert_events_emitted( tx_exec_info, - from_address=erc20.contract_address, - name='Transfer', - data=[ - account.contract_address, - RECIPIENT, - *AMOUNT + [ + [0, erc20.contract_address, 'Approval', [ + account.contract_address, spender.contract_address, *UINT_ZERO]], + [1, erc20.contract_address, 'Transfer', [ + account.contract_address, RECIPIENT, *AMOUNT]] ] ) @@ -486,7 +486,7 @@ async def test_transferFrom_to_zero_address(erc20_factory): async def test_increaseAllowance(erc20_factory): erc20, account, spender = erc20_factory - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == UINT_ZERO # set approve @@ -498,7 +498,7 @@ async def test_increaseAllowance(erc20_factory): ) # check allowance - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == AMOUNT # increase allowance @@ -508,10 +508,10 @@ async def test_increaseAllowance(erc20_factory): *AMOUNT ] ) - assert return_bool.result.response == [TRUE] + assert return_bool.call_info.retdata[1] == TRUE # check spender's allowance increased - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == add_uint(AMOUNT, AMOUNT) @@ -600,7 +600,7 @@ async def test_increaseAllowance_from_zero_address(erc20_factory): ) await assert_revert( - erc20.increaseAllowance(RECIPIENT, AMOUNT).invoke() + erc20.increaseAllowance(RECIPIENT, AMOUNT).execute() ) @@ -613,7 +613,7 @@ async def test_increaseAllowance_from_zero_address(erc20_factory): async def test_decreaseAllowance(erc20_factory): erc20, account, spender = erc20_factory - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == UINT_ZERO # set approve @@ -624,7 +624,7 @@ async def test_decreaseAllowance(erc20_factory): ] ) - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == AMOUNT # decrease allowance @@ -634,11 +634,11 @@ async def test_decreaseAllowance(erc20_factory): *UINT_ONE ] ) - assert return_bool.result.response == [TRUE] + assert return_bool.call_info.retdata[1] == TRUE new_allowance = sub_uint(AMOUNT, UINT_ONE) - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == new_allowance @@ -685,7 +685,7 @@ async def test_decreaseAllowance_overflow(erc20_factory): ] ) - execution_info = await erc20.allowance(account.contract_address, spender.contract_address).invoke() + execution_info = await erc20.allowance(account.contract_address, spender.contract_address).execute() assert execution_info.result.remaining == AMOUNT allowance_plus_one = add_uint(AMOUNT, UINT_ONE) @@ -731,7 +731,7 @@ async def test_decreaseAllowance_from_zero_address(erc20_factory): ) await assert_revert( - erc20.decreaseAllowance(RECIPIENT, AMOUNT).invoke() + erc20.decreaseAllowance(RECIPIENT, AMOUNT).execute() ) diff --git a/tests/token/erc20/test_ERC20Burnable.py b/tests/token/erc20/test_ERC20Burnable.py index e0f7fc4c2..1cc6cf4fc 100644 --- a/tests/token/erc20/test_ERC20Burnable.py +++ b/tests/token/erc20/test_ERC20Burnable.py @@ -3,7 +3,7 @@ from utils import ( to_uint, add_uint, sub_uint, str_to_felt, ZERO_ADDRESS, INVALID_UINT256, get_contract_class, cached_contract, assert_revert, assert_event_emitted, - State, Account + assert_events_emitted, State, Account ) @@ -12,6 +12,7 @@ # testing vars INIT_SUPPLY = to_uint(1000) AMOUNT = to_uint(200) +UINT_ZERO = to_uint(0) UINT_ONE = to_uint(1) NAME = str_to_felt("Mintable Token") SYMBOL = str_to_felt("MTKN") @@ -73,7 +74,7 @@ async def test_burn(erc20_factory): new_balance = sub_uint(INIT_SUPPLY, AMOUNT) - execution_info = await erc20.balanceOf(account.contract_address).invoke() + execution_info = await erc20.balanceOf(account.contract_address).execute() assert execution_info.result.balance == new_balance @@ -117,7 +118,7 @@ async def test_burn_from_zero_address(erc20_factory): erc20, _, _ = erc20_factory await assert_revert( - erc20.burn(UINT_ONE).invoke(), + erc20.burn(UINT_ONE).execute(), reverted_with="ERC20: cannot burn from the zero address" ) @@ -127,7 +128,7 @@ async def test_burn_invalid_uint256(erc20_factory): erc20, _, _ = erc20_factory await assert_revert( - erc20.burn(INVALID_UINT256).invoke(), + erc20.burn(INVALID_UINT256).execute(), reverted_with="ERC20: amount is not a valid Uint256" ) @@ -150,7 +151,7 @@ async def test_burn_from(erc20_factory): new_balance = sub_uint(INIT_SUPPLY, AMOUNT) - execution_info = await erc20.balanceOf(account1.contract_address).invoke() + execution_info = await erc20.balanceOf(account1.contract_address).execute() assert execution_info.result.balance == new_balance @@ -170,14 +171,14 @@ async def test_burn_from_emits_event(erc20_factory): *AMOUNT ]) - assert_event_emitted( + # events + assert_events_emitted( tx_exec_info, - from_address=erc20.contract_address, - name='Transfer', - data=[ - account1.contract_address, - ZERO_ADDRESS, - *AMOUNT + [ + [0, erc20.contract_address, 'Approval', [ + account1.contract_address, account2.contract_address, *UINT_ZERO]], + [1, erc20.contract_address, 'Transfer', [ + account1.contract_address, ZERO_ADDRESS, *AMOUNT]] ] ) diff --git a/tests/token/erc20/test_ERC20Mintable.py b/tests/token/erc20/test_ERC20Mintable.py index 20e044451..5b15895e2 100644 --- a/tests/token/erc20/test_ERC20Mintable.py +++ b/tests/token/erc20/test_ERC20Mintable.py @@ -89,7 +89,7 @@ async def test_mint(token_factory): ]) # check new supply - execution_info = await erc20.totalSupply().invoke() + execution_info = await erc20.totalSupply().execute() new_supply = execution_info.result.totalSupply assert new_supply == add_uint(INIT_SUPPLY, UINT_ONE) diff --git a/tests/token/erc20/test_ERC20Pausable.py b/tests/token/erc20/test_ERC20Pausable.py index 24630564f..be5971bc9 100644 --- a/tests/token/erc20/test_ERC20Pausable.py +++ b/tests/token/erc20/test_ERC20Pausable.py @@ -65,19 +65,19 @@ def token_factory(contract_classes, erc20_init): async def test_constructor(token_factory): token, owner, _ = token_factory - execution_info = await token.name().invoke() + execution_info = await token.name().execute() assert execution_info.result == (NAME,) - execution_info = await token.symbol().invoke() + execution_info = await token.symbol().execute() assert execution_info.result == (SYMBOL,) - execution_info = await token.decimals().invoke() + execution_info = await token.decimals().execute() assert execution_info.result.decimals == DECIMALS - execution_info = await token.balanceOf(owner.contract_address).invoke() + execution_info = await token.balanceOf(owner.contract_address).execute() assert execution_info.result.balance == INIT_SUPPLY - execution_info = await token.paused().invoke() + execution_info = await token.paused().execute() assert execution_info.result.paused == FALSE @@ -87,7 +87,7 @@ async def test_pause(token_factory): await signer.send_transaction(owner, token.contract_address, 'pause', []) - execution_info = await token.paused().invoke() + execution_info = await token.paused().execute() assert execution_info.result.paused == TRUE await assert_revert(signer.send_transaction( @@ -143,7 +143,7 @@ async def test_unpause(token_factory): await signer.send_transaction(owner, token.contract_address, 'pause', []) await signer.send_transaction(owner, token.contract_address, 'unpause', []) - execution_info = await token.paused().invoke() + execution_info = await token.paused().execute() assert execution_info.result.paused == FALSE success = await signer.send_transaction( @@ -152,7 +152,7 @@ async def test_unpause(token_factory): 'transfer', [other.contract_address, *AMOUNT] ) - assert success.result.response == [TRUE] + assert success.call_info.retdata[1] == TRUE success = await signer.send_transaction( owner, @@ -160,7 +160,7 @@ async def test_unpause(token_factory): 'approve', [other.contract_address, *AMOUNT] ) - assert success.result.response == [TRUE] + assert success.call_info.retdata[1] == TRUE success = await signer.send_transaction( other, @@ -168,7 +168,7 @@ async def test_unpause(token_factory): 'transferFrom', [owner.contract_address, other.contract_address, *AMOUNT] ) - assert success.result.response == [TRUE] + assert success.call_info.retdata[1] == TRUE success = await signer.send_transaction( owner, @@ -176,7 +176,7 @@ async def test_unpause(token_factory): 'increaseAllowance', [other.contract_address, *AMOUNT] ) - assert success.result.response == [TRUE] + assert success.call_info.retdata[1] == TRUE success = await signer.send_transaction( owner, @@ -184,7 +184,7 @@ async def test_unpause(token_factory): 'decreaseAllowance', [other.contract_address, *AMOUNT] ) - assert success.result.response == [TRUE] + assert success.call_info.retdata[1] == TRUE @pytest.mark.asyncio diff --git a/tests/token/erc20/test_ERC20Upgradeable.py b/tests/token/erc20/test_ERC20Upgradeable.py index 605b9d174..a32262e99 100644 --- a/tests/token/erc20/test_ERC20Upgradeable.py +++ b/tests/token/erc20/test_ERC20Upgradeable.py @@ -1,12 +1,11 @@ import pytest from signers import MockSigner from utils import ( - to_uint, sub_uint, str_to_felt, assert_revert, + to_uint, sub_uint, str_to_felt, assert_revert, TRUE, get_contract_class, cached_contract, State, Account ) - signer = MockSigner(123456789987654321) USER = 999 @@ -108,8 +107,8 @@ async def test_constructor(token_factory): ) # check values - expected = [NAME, SYMBOL, DECIMALS, *INIT_SUPPLY] - assert execution_info.result.response == expected + expected = [5, NAME, SYMBOL, DECIMALS, *INIT_SUPPLY] + assert execution_info.call_info.retdata == expected @pytest.mark.asyncio @@ -137,12 +136,13 @@ async def test_upgrade(after_initializer): ) expected = [ + 6, # number of return values *sub_uint(INIT_SUPPLY, AMOUNT), # balanceOf admin *AMOUNT, # balanceOf USER *INIT_SUPPLY # totalSupply ] - assert execution_info.result.response == expected + assert execution_info.call_info.retdata == expected @pytest.mark.asyncio @@ -159,3 +159,36 @@ async def test_upgrade_from_nonadmin(after_initializer): await signer.send_transaction( admin, proxy.contract_address, 'upgrade', [token_v2.class_hash] ) + + +@pytest.mark.asyncio +async def test_upgrade_transferFrom(after_initializer): + admin, non_admin, proxy, _, _ = after_initializer + + # approve + await signer.send_transaction( + admin, proxy.contract_address, 'approve', [ + non_admin.contract_address, + *AMOUNT + ] + ) + + # transferFrom + return_bool = await signer.send_transaction( + non_admin, proxy.contract_address, 'transferFrom', [ + admin.contract_address, + non_admin.contract_address, + *AMOUNT + ] + ) + assert return_bool.call_info.retdata[1] == TRUE + + # should fail + await assert_revert(signer.send_transaction( + non_admin, proxy.contract_address, 'transferFrom', [ + admin.contract_address, + non_admin.contract_address, + *AMOUNT + ] + ) + ) diff --git a/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py b/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py index 3422798a2..7044ad23c 100644 --- a/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py +++ b/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py @@ -86,7 +86,7 @@ async def erc721_minted(erc721_factory): async def test_supportsInterface(erc721_factory): erc721, _, _ = erc721_factory - execution_info = await erc721.supportsInterface(ENUMERABLE_INTERFACE_ID).invoke() + execution_info = await erc721.supportsInterface(ENUMERABLE_INTERFACE_ID).execute() assert execution_info.result == (TRUE,) # @@ -98,7 +98,7 @@ async def test_supportsInterface(erc721_factory): async def test_totalSupply(erc721_minted): erc721, _, _ = erc721_minted - execution_info = await erc721.totalSupply().invoke() + execution_info = await erc721.totalSupply().execute() assert execution_info.result == (TOTAL_TOKENS,) @@ -114,7 +114,7 @@ async def test_tokenOfOwnerByIndex(erc721_minted): # check index for i in range(0, len(TOKENS)): execution_info = await erc721.tokenOfOwnerByIndex( - account.contract_address, to_uint(i)).invoke() + account.contract_address, to_uint(i)).execute() assert execution_info.result == (TOKENS[i],) @@ -126,7 +126,7 @@ async def test_tokenOfOwnerByIndex_greater_than_supply(erc721_minted): await assert_revert( erc721.tokenOfOwnerByIndex( - account.contract_address, tokens_plus_one).invoke(), + account.contract_address, tokens_plus_one).execute(), reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -136,7 +136,7 @@ async def test_tokenOfOwnerByIndex_owner_with_no_tokens(erc721_minted): erc721, _, _ = erc721_minted await assert_revert( - erc721.tokenOfOwnerByIndex(RECIPIENT, to_uint(1)).invoke(), + erc721.tokenOfOwnerByIndex(RECIPIENT, to_uint(1)).execute(), reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -155,20 +155,20 @@ async def test_tokenOfOwnerByIndex_transfer_all_tokens(erc721_minted): ] ) - execution_info = await erc721.balanceOf(other.contract_address).invoke() + execution_info = await erc721.balanceOf(other.contract_address).execute() assert execution_info.result == (TOTAL_TOKENS,) for i in range(0, len(TOKENS)): - execution_info = await erc721.tokenOfOwnerByIndex(other.contract_address, to_uint(i)).invoke() + execution_info = await erc721.tokenOfOwnerByIndex(other.contract_address, to_uint(i)).execute() assert execution_info.result == (TOKENS[i],) - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() assert execution_info.result == (to_uint(0),) # check that queries to old owner's token ownership reverts since index is less # than the target's balance await assert_revert(erc721.tokenOfOwnerByIndex( - account.contract_address, to_uint(0)).invoke(), + account.contract_address, to_uint(0)).execute(), reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -189,20 +189,20 @@ async def test_tokenOfOwnerByIndex_safe_transfer_all_tokens(erc721_minted): ] ) - execution_info = await erc721.balanceOf(other.contract_address).invoke() + execution_info = await erc721.balanceOf(other.contract_address).execute() assert execution_info.result == (TOTAL_TOKENS,) for i in range(0, len(TOKENS)): - execution_info = await erc721.tokenOfOwnerByIndex(other.contract_address, to_uint(i)).invoke() + execution_info = await erc721.tokenOfOwnerByIndex(other.contract_address, to_uint(i)).execute() assert execution_info.result == (TOKENS[i],) - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() assert execution_info.result == (to_uint(0),) # check that queries to old owner's token ownership reverts since index is less # than the target's balance await assert_revert(erc721.tokenOfOwnerByIndex( - account.contract_address, to_uint(0)).invoke(), + account.contract_address, to_uint(0)).execute(), reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -217,7 +217,7 @@ async def test_tokenByIndex(erc721_minted): erc721, _, _ = erc721_minted for i in range(0, len(TOKENS)): - execution_info = await erc721.tokenByIndex(to_uint(i)).invoke() + execution_info = await erc721.tokenByIndex(to_uint(i)).execute() assert execution_info.result == (TOKENS[i],) @@ -226,7 +226,7 @@ async def test_tokenByIndex_greater_than_supply(erc721_minted): erc721, _, _ = erc721_minted await assert_revert( - erc721.tokenByIndex(to_uint(5)).invoke(), + erc721.tokenByIndex(to_uint(5)).execute(), reverted_with="ERC721Enumerable: global index out of bounds" ) @@ -243,15 +243,15 @@ async def test_tokenByIndex_burn_last_token(erc721_minted): *TOKENS[4]] ) - execution_info = await erc721.totalSupply().invoke() + execution_info = await erc721.totalSupply().execute() assert execution_info.result == (tokens_minus_one,) for i in range(0, 4): - execution_info = await erc721.tokenByIndex(to_uint(i)).invoke() + execution_info = await erc721.tokenByIndex(to_uint(i)).execute() assert execution_info.result == (TOKENS[i],) await assert_revert( - erc721.tokenByIndex(tokens_minus_one).invoke(), + erc721.tokenByIndex(tokens_minus_one).execute(), reverted_with="ERC721Enumerable: global index out of bounds" ) @@ -270,7 +270,7 @@ async def test_tokenByIndex_burn_first_token(erc721_minted): # to TOKEN[0]'s index new_token_order = [TOKENS[4], TOKENS[1], TOKENS[2], TOKENS[3]] for i in range(0, 3): - execution_info = await erc721.tokenByIndex(to_uint(i)).invoke() + execution_info = await erc721.tokenByIndex(to_uint(i)).execute() assert execution_info.result == (new_token_order[i],) @@ -284,11 +284,11 @@ async def test_tokenByIndex_burn_and_mint(erc721_minted): *token] ) - execution_info = await erc721.totalSupply().invoke() + execution_info = await erc721.totalSupply().execute() assert execution_info.result == (to_uint(0),) await assert_revert( - erc721.tokenByIndex(to_uint(0)).invoke(), + erc721.tokenByIndex(to_uint(0)).execute(), reverted_with="ERC721Enumerable: global index out of bounds" ) @@ -300,5 +300,5 @@ async def test_tokenByIndex_burn_and_mint(erc721_minted): ) for i in range(0, len(TOKENS)): - execution_info = await erc721.tokenByIndex(to_uint(i)).invoke() + execution_info = await erc721.tokenByIndex(to_uint(i)).execute() assert execution_info.result == (TOKENS[i],) diff --git a/tests/token/erc721/test_ERC721MintableBurnable.py b/tests/token/erc721/test_ERC721MintableBurnable.py index e0a23a733..f9f601293 100644 --- a/tests/token/erc721/test_ERC721MintableBurnable.py +++ b/tests/token/erc721/test_ERC721MintableBurnable.py @@ -2,8 +2,8 @@ from signers import MockSigner from utils import ( str_to_felt, ZERO_ADDRESS, TRUE, FALSE, assert_revert, INVALID_UINT256, - assert_event_emitted, get_contract_class, cached_contract, to_uint, sub_uint, - add_uint, State, Account + assert_event_emitted, assert_events_emitted, get_contract_class, cached_contract, + to_uint, sub_uint, add_uint, State, Account ) @@ -122,10 +122,10 @@ async def erc721_unsupported(erc721_factory): @pytest.mark.asyncio async def test_constructor(erc721_factory): erc721, _, _, _, _ = erc721_factory - execution_info = await erc721.name().invoke() + execution_info = await erc721.name().execute() assert execution_info.result == (str_to_felt("Non Fungible Token"),) - execution_info = await erc721.symbol().invoke() + execution_info = await erc721.symbol().execute() assert execution_info.result == (str_to_felt("NFT"),) @@ -145,12 +145,12 @@ async def test_balanceOf(erc721_factory): account.contract_address, *token] ) - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() n_tokens = len(TOKENS) assert execution_info.result == (to_uint(n_tokens),) # user should have zero tokens - execution_info = await erc721.balanceOf(RECIPIENT).invoke() + execution_info = await erc721.balanceOf(RECIPIENT).execute() assert execution_info.result == (to_uint(0),) @@ -166,7 +166,7 @@ async def test_balanceOf_zero_address(erc721_factory): # should revert when querying zero address await assert_revert( - erc721.balanceOf(ZERO_ADDRESS).invoke(), + erc721.balanceOf(ZERO_ADDRESS).execute(), reverted_with="ERC721: balance query for the zero address" ) @@ -188,7 +188,7 @@ async def test_ownerOf(erc721_factory): ) # should return account's address - execution_info = await erc721.ownerOf(token).invoke() + execution_info = await erc721.ownerOf(token).execute() assert execution_info.result == (account.contract_address,) @@ -204,7 +204,7 @@ async def test_ownerOf_nonexistent_token(erc721_factory): # should revert when querying nonexistent token await assert_revert( - erc721.ownerOf(NONEXISTENT_TOKEN).invoke(), + erc721.ownerOf(NONEXISTENT_TOKEN).execute(), reverted_with="ERC721: owner query for nonexistent token" ) @@ -215,7 +215,7 @@ async def test_ownerOf_invalid_uint256(erc721_factory): # should revert when querying nonexistent token await assert_revert( - erc721.ownerOf(INVALID_UINT256).invoke(), + erc721.ownerOf(INVALID_UINT256).execute(), reverted_with="ERC721: token_id is not a valid Uint256" ) @@ -253,12 +253,12 @@ async def test_mint(erc721_minted): erc721, account, _, _ = erc721_minted # checks balance - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() assert execution_info.result == (to_uint(2),) # checks that account owns correct tokens for token in TOKENS: - execution_info = await erc721.ownerOf(token).invoke() + execution_info = await erc721.ownerOf(token).execute() assert execution_info.result == (account.contract_address,) @@ -296,7 +296,7 @@ async def test_mint_approve_should_be_zero_address(erc721_minted): # approved address should be zero for newly minted tokens for token in TOKENS: - execution_info = await erc721.getApproved(token).invoke() + execution_info = await erc721.getApproved(token).execute() assert execution_info.result == (0,) @@ -323,7 +323,7 @@ async def test_mint_by_not_owner(erc721_factory): async def test_burn(erc721_minted): erc721, account, _, _ = erc721_minted - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() previous_balance = execution_info.result.balance # burn token @@ -332,7 +332,7 @@ async def test_burn(erc721_minted): ) # account balance should subtract one - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() assert execution_info.result.balance == sub_uint( previous_balance, to_uint(1) ) @@ -340,14 +340,14 @@ async def test_burn(erc721_minted): # approve should be cleared to zero, therefore, # 'getApproved()' call should fail await assert_revert( - erc721.getApproved(TOKEN).invoke(), + erc721.getApproved(TOKEN).execute(), reverted_with="ERC721: approved query for nonexistent token" ) # 'token_to_burn' owner should be zero; therefore, # 'ownerOf()' call should fail await assert_revert( - erc721.ownerOf(TOKEN).invoke(), + erc721.ownerOf(TOKEN).execute(), reverted_with="ERC721: owner query for nonexistent token" ) @@ -363,14 +363,14 @@ async def test_burn_emits_event(erc721_minted): ] ) - assert_event_emitted( + # events + assert_events_emitted( tx_exec_info, - from_address=erc721.contract_address, - name='Transfer', - data=[ - account.contract_address, - ZERO_ADDRESS, - *TOKEN + [ + [0, erc721.contract_address, 'Approval', [ + account.contract_address, ZERO_ADDRESS, *TOKEN]], + [1, erc721.contract_address, 'Transfer', [ + account.contract_address, ZERO_ADDRESS, *TOKEN]] ] ) @@ -410,7 +410,7 @@ async def test_burn_from_zero_address(erc721_minted): erc721, _, _, _ = erc721_minted await assert_revert( - erc721.burn(TOKEN).invoke(), + erc721.burn(TOKEN).execute(), reverted_with="ERC721: caller is not the token owner" ) @@ -429,7 +429,7 @@ async def test_approve(erc721_minted): spender.contract_address, *TOKEN] ) - execution_info = await erc721.getApproved(TOKEN).invoke() + execution_info = await erc721.getApproved(TOKEN).execute() assert execution_info.result == (spender.contract_address,) @@ -473,7 +473,7 @@ async def test_approve_on_setApprovalForAll(erc721_minted): RECIPIENT, *TOKEN] ) - execution_info = await erc721.getApproved(TOKEN).invoke() + execution_info = await erc721.getApproved(TOKEN).execute() assert execution_info.result == (RECIPIENT,) @@ -485,7 +485,7 @@ async def test_approve_from_zero_address(erc721_minted): # (get_caller_address) is zero await assert_revert( erc721.approve( - spender.contract_address, TOKEN).invoke(), + spender.contract_address, TOKEN).execute(), reverted_with="ERC721: cannot approve from the zero address" ) @@ -542,7 +542,7 @@ async def test_approve_on_already_approved(erc721_minted): ) # check that approval does not change - execution_info = await erc721.getApproved(TOKEN).invoke() + execution_info = await erc721.getApproved(TOKEN).execute() assert execution_info.result == (spender.contract_address,) @@ -551,7 +551,7 @@ async def test_getApproved_nonexistent_token(erc721_minted): erc721, _, _, _ = erc721_minted await assert_revert( - erc721.getApproved(NONEXISTENT_TOKEN).invoke(), + erc721.getApproved(NONEXISTENT_TOKEN).execute(), reverted_with="ERC721: approved query for nonexistent token" ) @@ -561,7 +561,7 @@ async def test_getApproved_invalid_uint256(erc721_minted): erc721, _, _, _ = erc721_minted await assert_revert( - erc721.getApproved(INVALID_UINT256).invoke(), + erc721.getApproved(INVALID_UINT256).execute(), reverted_with="ERC721: token_id is not a valid Uint256" ) @@ -581,7 +581,7 @@ async def test_setApprovalForAll(erc721_minted): ) execution_info = await erc721.isApprovedForAll( - account.contract_address, spender.contract_address).invoke() + account.contract_address, spender.contract_address).execute() assert execution_info.result == (TRUE,) @@ -621,7 +621,7 @@ async def test_setApprovalForAll_when_operator_was_set_as_not_approved(erc721_mi ) execution_info = await erc721.isApprovedForAll( - account.contract_address, spender.contract_address).invoke() + account.contract_address, spender.contract_address).execute() assert execution_info.result == (TRUE,) @@ -659,7 +659,7 @@ async def test_setApprovalForAll_from_zero_address(erc721_minted): erc721, account, _, _ = erc721_minted await assert_revert( - erc721.setApprovalForAll(account.contract_address, TRUE).invoke(), + erc721.setApprovalForAll(account.contract_address, TRUE).execute(), reverted_with="ERC721: either the caller or operator is the zero address" ) @@ -688,7 +688,7 @@ async def test_transferFrom_owner(erc721_minted): erc721, account, _, _ = erc721_minted # get account's previous balance - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() previous_balance = execution_info.result.balance # transfers token from account to recipient @@ -698,20 +698,20 @@ async def test_transferFrom_owner(erc721_minted): ) # checks recipient balance - execution_info = await erc721.balanceOf(RECIPIENT).invoke() + execution_info = await erc721.balanceOf(RECIPIENT).execute() assert execution_info.result == (to_uint(1),) # checks account balance - execution_info = await erc721.balanceOf(account.contract_address).invoke() + execution_info = await erc721.balanceOf(account.contract_address).execute() assert execution_info.result.balance == sub_uint( previous_balance, to_uint(1)) # checks token has new owner - execution_info = await erc721.ownerOf(TOKEN).invoke() + execution_info = await erc721.ownerOf(TOKEN).execute() assert execution_info.result == (RECIPIENT,) # checks approval is cleared for token_id - execution_info = await erc721.getApproved(TOKEN).invoke() + execution_info = await erc721.getApproved(TOKEN).execute() assert execution_info.result == (0,) @@ -734,25 +734,14 @@ async def test_transferFrom_emits_events(erc721_minted): ] ) - assert_event_emitted( + # events + assert_events_emitted( tx_exec_info, - from_address=erc721.contract_address, - name='Transfer', - data=[ - account.contract_address, - RECIPIENT, - *TOKEN - ] - ) - - assert_event_emitted( - tx_exec_info, - from_address=erc721.contract_address, - name='Approval', - data=[ - account.contract_address, - ZERO_ADDRESS, - *TOKEN + [ + [0, erc721.contract_address, 'Approval', [ + account.contract_address, ZERO_ADDRESS, *TOKEN]], + [1, erc721.contract_address, 'Transfer', [ + account.contract_address, RECIPIENT, *TOKEN]] ] ) @@ -774,7 +763,7 @@ async def test_transferFrom_approved_user(erc721_minted): ) # checks user balance - execution_info = await erc721.balanceOf(RECIPIENT).invoke() + execution_info = await erc721.balanceOf(RECIPIENT).execute() assert execution_info.result == (to_uint(1),) @@ -795,7 +784,7 @@ async def test_transferFrom_operator(erc721_minted): ) # checks user balance - execution_info = await erc721.balanceOf(RECIPIENT).invoke() + execution_info = await erc721.balanceOf(RECIPIENT).execute() assert execution_info.result == (to_uint(1),) @@ -866,7 +855,7 @@ async def test_transferFrom_from_zero_address(erc721_minted): account.contract_address, RECIPIENT, TOKEN - ).invoke(), + ).execute(), reverted_with="ERC721: either is not approved or the caller is the zero address" ) @@ -887,7 +876,7 @@ async def test_transferFrom_from_zero_address(erc721_minted): async def test_supportsInterface(erc721_factory, interface_id, result): erc721, _, _, _, _ = erc721_factory - execution_info = await erc721.supportsInterface(interface_id).invoke() + execution_info = await erc721.supportsInterface(interface_id).execute() assert execution_info.result == (result,) @@ -911,11 +900,11 @@ async def test_safeTransferFrom(erc721_minted): ) # check balance - execution_info = await erc721.balanceOf(erc721_holder.contract_address).invoke() + execution_info = await erc721.balanceOf(erc721_holder.contract_address).execute() assert execution_info.result == (to_uint(1),) # check owner - execution_info = await erc721.ownerOf(TOKEN).invoke() + execution_info = await erc721.ownerOf(TOKEN).execute() assert execution_info.result == (erc721_holder.contract_address,) @@ -933,25 +922,15 @@ async def test_safeTransferFrom_emits_events(erc721_minted): ] ) - assert_event_emitted( - tx_exec_info, - from_address=erc721.contract_address, - name='Transfer', - data=[ - account.contract_address, - erc721_holder.contract_address, - *TOKEN - ] - ) - assert_event_emitted( + # events + assert_events_emitted( tx_exec_info, - from_address=erc721.contract_address, - name='Approval', - data=[ - account.contract_address, - ZERO_ADDRESS, - *TOKEN + [ + [0, erc721.contract_address, 'Approval', [ + account.contract_address, ZERO_ADDRESS, *TOKEN]], + [1, erc721.contract_address, 'Transfer', [ + account.contract_address, erc721_holder.contract_address, *TOKEN]] ] ) @@ -960,7 +939,7 @@ async def test_safeTransferFrom_emits_events(erc721_minted): async def test_safeTransferFrom_from_approved(erc721_minted): erc721, account, spender, erc721_holder = erc721_minted - execution_info = await erc721.balanceOf(erc721_holder.contract_address).invoke() + execution_info = await erc721.balanceOf(erc721_holder.contract_address).execute() previous_balance = execution_info.result.balance # approve spender @@ -981,7 +960,7 @@ async def test_safeTransferFrom_from_approved(erc721_minted): ) # erc721_holder balance check - execution_info = await erc721.balanceOf(erc721_holder.contract_address).invoke() + execution_info = await erc721.balanceOf(erc721_holder.contract_address).execute() assert execution_info.result.balance == add_uint( previous_balance, to_uint(1) ) @@ -991,7 +970,7 @@ async def test_safeTransferFrom_from_approved(erc721_minted): async def test_safeTransferFrom_from_operator(erc721_minted): erc721, account, spender, erc721_holder = erc721_minted - execution_info = await erc721.balanceOf(erc721_holder.contract_address).invoke() + execution_info = await erc721.balanceOf(erc721_holder.contract_address).execute() previous_balance = execution_info.result.balance # setApprovalForAll @@ -1012,7 +991,7 @@ async def test_safeTransferFrom_from_operator(erc721_minted): ) # erc721_holder balance check - execution_info = await erc721.balanceOf(erc721_holder.contract_address).invoke() + execution_info = await erc721.balanceOf(erc721_holder.contract_address).execute() assert execution_info.result.balance == add_uint( previous_balance, to_uint(1) ) @@ -1063,7 +1042,7 @@ async def test_safeTransferFrom_from_zero_address(erc721_minted): erc721_holder.contract_address, TOKEN, DATA - ).invoke(), + ).execute(), reverted_with="ERC721: either is not approved or the caller is the zero address" ) @@ -1099,11 +1078,11 @@ async def test_safeTransferFrom_to_account(erc721_minted): ) # check balance - execution_info = await erc721.balanceOf(account2.contract_address).invoke() + execution_info = await erc721.balanceOf(account2.contract_address).execute() assert execution_info.result == (to_uint(1),) # check owner - execution_info = await erc721.ownerOf(TOKEN).invoke() + execution_info = await erc721.ownerOf(TOKEN).execute() assert execution_info.result == (account2.contract_address,) @@ -1137,7 +1116,7 @@ async def test_tokenURI(erc721_minted): token_2 = TOKENS[1] # should be zero when tokenURI is not set - execution_info = await erc721.tokenURI(token_1).invoke() + execution_info = await erc721.tokenURI(token_1).execute() assert execution_info.result == (0,) # setTokenURI for token_1 @@ -1148,7 +1127,7 @@ async def test_tokenURI(erc721_minted): ] ) - execution_info = await erc721.tokenURI(token_1).invoke() + execution_info = await erc721.tokenURI(token_1).execute() assert execution_info.result == (SAMPLE_URI_1,) # setTokenURI for token_2 @@ -1159,7 +1138,7 @@ async def test_tokenURI(erc721_minted): ] ) - execution_info = await erc721.tokenURI(token_2).invoke() + execution_info = await erc721.tokenURI(token_2).execute() assert execution_info.result == (SAMPLE_URI_2,) @@ -1169,7 +1148,7 @@ async def test_tokenURI_should_revert_for_nonexistent_token(erc721_minted): # should revert for nonexistent token await assert_revert( - erc721.tokenURI(NONEXISTENT_TOKEN).invoke(), + erc721.tokenURI(NONEXISTENT_TOKEN).execute(), reverted_with="ERC721_Metadata: URI query for nonexistent token" ) diff --git a/tests/token/erc721/test_ERC721MintablePausable.py b/tests/token/erc721/test_ERC721MintablePausable.py index 0c8329e47..2d9ad5eaa 100644 --- a/tests/token/erc721/test_ERC721MintablePausable.py +++ b/tests/token/erc721/test_ERC721MintablePausable.py @@ -84,7 +84,7 @@ async def test_pause(erc721_minted): # pause await signer.send_transaction(owner, erc721.contract_address, 'pause', []) - execution_info = await erc721.paused().invoke() + execution_info = await erc721.paused().execute() assert execution_info.result.paused == TRUE await assert_revert(signer.send_transaction( @@ -142,7 +142,7 @@ async def test_unpause(erc721_minted): # unpause await signer.send_transaction(owner, erc721.contract_address, 'unpause', []) - execution_info = await erc721.paused().invoke() + execution_info = await erc721.paused().execute() assert execution_info.result.paused == FALSE await signer.send_transaction( diff --git a/tests/token/erc721/test_ERC721SafeMintableMock.py b/tests/token/erc721/test_ERC721SafeMintableMock.py index 4da0f620b..646b77e7d 100644 --- a/tests/token/erc721/test_ERC721SafeMintableMock.py +++ b/tests/token/erc721/test_ERC721SafeMintableMock.py @@ -166,7 +166,7 @@ async def test_safeMint_from_zero_address(erc721_factory): erc721_holder.contract_address, TOKEN, DATA - ).invoke(), + ).execute(), reverted_with="Ownable: caller is the zero address" ) diff --git a/tests/upgrades/test_Proxy.py b/tests/upgrades/test_Proxy.py index 44ae2c598..ac5fd1471 100644 --- a/tests/upgrades/test_Proxy.py +++ b/tests/upgrades/test_Proxy.py @@ -85,7 +85,7 @@ async def test_initializer(proxy_factory): execution_info = await signer.send_transaction( admin, proxy.contract_address, 'getAdmin', [] ) - assert execution_info.result.response == [admin.contract_address] + assert execution_info.call_info.retdata[1] == admin.contract_address @pytest.mark.asyncio @@ -125,7 +125,7 @@ async def test_set_admin(after_initialized): execution_info = await signer.send_transaction( admin, proxy.contract_address, 'getAdmin', [] ) - assert execution_info.result.response == [VALUE] + assert execution_info.call_info.retdata[1] == VALUE @pytest.mark.asyncio @@ -155,7 +155,7 @@ async def test_default_fallback(proxy_factory): execution_info = execution_info = await signer.send_transaction( admin, proxy.contract_address, 'getValue', [] ) - assert execution_info.result.response == [VALUE] + assert execution_info.call_info.retdata[1] == VALUE @pytest.mark.asyncio diff --git a/tests/upgrades/test_upgrades.py b/tests/upgrades/test_upgrades.py index ee03c5380..a26be4eef 100644 --- a/tests/upgrades/test_upgrades.py +++ b/tests/upgrades/test_upgrades.py @@ -132,7 +132,7 @@ async def test_upgrade(proxy_factory): execution_info = await signer.send_transaction( admin, proxy.contract_address, 'getValue1', [] ) - assert execution_info.result.response == [VALUE_1] + assert execution_info.call_info.retdata[1] == VALUE_1 # upgrade await signer.send_transaction( @@ -145,7 +145,7 @@ async def test_upgrade(proxy_factory): execution_info = await signer.send_transaction( admin, proxy.contract_address, 'getValue1', [] ) - assert execution_info.result.response == [VALUE_1] + assert execution_info.call_info.retdata[1] == VALUE_1 @pytest.mark.asyncio @@ -233,12 +233,13 @@ async def test_implementation_v2(after_upgrade): ) expected = [ + 3, # number of return values v2_decl.class_hash, # getImplementationHash admin.contract_address, # getAdmin VALUE_1 # getValue1 ] - assert execution_info.result.response == expected + assert execution_info.call_info.retdata == expected # # v2 functions @@ -259,7 +260,7 @@ async def test_set_admin(after_upgrade): execution_info = await signer.send_transaction( admin, proxy.contract_address, 'getAdmin', [] ) - assert execution_info.result.response == [new_admin.contract_address] + assert execution_info.call_info.retdata[1] == new_admin.contract_address @pytest.mark.asyncio @@ -342,7 +343,8 @@ async def test_v2_functions_pre_and_post_upgrade(proxy_factory): ) expected = [ + 2, # number of return values VALUE_2, # getValue2 new_admin.contract_address # getAdmin ] - assert execution_info.result.response == expected + assert execution_info.call_info.retdata == expected diff --git a/tests/utils.py b/tests/utils.py index 8c1533e3b..690a083c0 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -8,7 +8,7 @@ from starkware.starkware_utils.error_handling import StarkException from starkware.starknet.testing.starknet import StarknetContract from starkware.starknet.testing.starknet import Starknet -from starkware.starknet.business_logic.execution.objects import Event +from starkware.starknet.business_logic.execution.objects import OrderedEvent MAX_UINT256 = (2**128 - 1, 2**128 - 1) @@ -104,12 +104,33 @@ async def assert_revert_entry_point(fun, invalid_selector): await assert_revert(fun, entry_point_msg) -def assert_event_emitted(tx_exec_info, from_address, name, data): - assert Event( - from_address=from_address, - keys=[get_selector_from_name(name)], - data=data, - ) in tx_exec_info.raw_events +def assert_event_emitted(tx_exec_info, from_address, name, data, order=0): + """Assert one single event is fired with correct data.""" + assert_events_emitted(tx_exec_info, [(order, from_address, name, data)]) + + +def assert_events_emitted(tx_exec_info, events): + """Assert events are fired with correct data.""" + for event in events: + order, from_address, name, data = event + event_obj = OrderedEvent( + order=order, + keys=[get_selector_from_name(name)], + data=data, + ) + + base = tx_exec_info.call_info.internal_calls[0] + if event_obj in base.events and from_address == base.contract_address: + return + + try: + base2 = base.internal_calls[0] + if event_obj in base2.events and from_address == base2.contract_address: + return + except IndexError: + pass + + raise BaseException("Event not fired or not fired correctly") def _get_path_from_name(name): @@ -144,7 +165,7 @@ def cached_contract(state, _class, deployed): state=state, abi=_class.abi, contract_address=deployed.contract_address, - deploy_execution_info=deployed.deploy_execution_info + deploy_call_info=deployed.deploy_call_info ) return contract diff --git a/tox.ini b/tox.ini index 3a2fb54e8..c4764eecd 100644 --- a/tox.ini +++ b/tox.ini @@ -15,8 +15,8 @@ passenv = HOME PYTHONPATH deps = - cairo-lang==0.9.1 - cairo-nile==0.8.0 + cairo-lang==0.10.0 + cairo-nile==0.9.0 pytest-xdist # See https://github.com/starkware-libs/cairo-lang/issues/52 marshmallow-dataclass==8.5.3