From 243df2b0935b09366ee08785c7cb3634c22c3642 Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Sat, 23 Jul 2022 16:20:13 -0400 Subject: [PATCH] Refactor directory structure (#350) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add lib and preset dirs * remove underscore from names, finish init reorganization * fix mock names, adjusted paths * update paths in tests * update attribution * Rename safemath.cairo to SafeMath.cairo * Rename initializable.cairo to Initializable.cairo * Rename pausable.cairo to Pausable.cairo * update version in attribution * add conventions to CONTRIBUTING.md * update names and paths in docs * fix paths in code snippets * Apply suggestions from code review Co-authored-by: Martín Triay * Update CONTRIBUTING.md Co-authored-by: Martín Triay * update link paths * update structure * fix compilation msg and import snippet * Apply suggestions from code review * fix paths, move ERC721Holder * remove upgrades dir * fix pin * update paths for proxy * update links and code block imports Co-authored-by: Martín Triay --- CONTRIBUTING.md | 28 +++++- README.md | 60 ++++++------ docs/Access.md | 16 ++-- docs/Account.md | 11 ++- docs/ERC20.md | 28 +++--- docs/ERC721.md | 94 +++++++++---------- docs/Extensibility.md | 16 ++-- docs/Introspection.md | 12 +-- docs/Proxies.md | 10 +- docs/Security.md | 16 ++-- docs/Utilities.md | 4 +- .../library.cairo} | 6 +- .../{ownable.cairo => ownable/library.cairo} | 2 +- src/openzeppelin/account/library.cairo | 8 +- .../account/{ => presets}/Account.cairo | 6 +- .../{ => presets}/AddressRegistry.cairo | 6 +- .../account/{ => presets}/EthAccount.cairo | 8 +- .../introspection/{ => erc165}/IERC165.cairo | 2 +- .../{ERC165.cairo => erc165/library.cairo} | 4 +- .../library.cairo} | 8 +- .../library.cairo} | 2 +- .../library.cairo} | 2 +- .../library.cairo} | 4 +- .../token/erc20/{interfaces => }/IERC20.cairo | 6 +- src/openzeppelin/token/erc20/library.cairo | 4 +- .../token/erc20/{ => presets}/ERC20.cairo | 2 +- .../ERC20Mintable.cairo} | 4 +- .../ERC20Pausable.cairo} | 6 +- .../ERC20Upgradeable.cairo} | 2 +- .../erc721/{interfaces => }/IERC721.cairo | 8 +- ...1_Metadata.cairo => IERC721Metadata.cairo} | 4 +- ...1_Receiver.cairo => IERC721Receiver.cairo} | 6 +- .../enumerable/IERC721Enumerable.cairo} | 4 +- .../enumerable}/library.cairo | 72 +++++++------- .../ERC721EnumerableMintableBurnable.cairo} | 24 ++--- .../presets/utils/ERC721Holder.cairo} | 6 +- src/openzeppelin/token/erc721/library.cairo | 12 +-- .../ERC721MintableBurnable.cairo} | 6 +- .../ERC721MintablePausable.cairo} | 8 +- .../upgrades/{ => presets}/Proxy.cairo | 2 +- .../library.cairo} | 2 +- tests/account/test_Account.py | 2 +- tests/account/test_EthAccount.py | 2 +- tests/mocks/AccessControl.cairo | 6 +- ...entrancy.cairo => AccountReentrancy.cairo} | 2 + tests/mocks/ERC165.cairo | 2 +- ...ble_mock.cairo => ERC20BurnableMock.cairo} | 0 ...ock.cairo => ERC721SafeMintableMock.cairo} | 7 +- tests/mocks/Initializable.cairo | 2 +- tests/mocks/Ownable.cairo | 2 +- tests/mocks/Pausable.cairo | 2 +- ...on.cairo => ProxiableImplementation.cairo} | 0 ...ock.cairo => ReentrancyAttackerMock.cairo} | 0 ...trancy_mock.cairo => ReentrancyMock.cairo} | 2 +- ...safemath_mock.cairo => SafeMathMock.cairo} | 2 +- ...des_v1_mock.cairo => UpgradesMockV1.cairo} | 0 ...des_v2_mock.cairo => UpgradesMockV2.cairo} | 0 tests/security/test_reentrancy.py | 4 +- tests/security/test_safemath.py | 2 +- tests/signers.py | 12 +-- tests/token/erc20/test_ERC20.py | 2 +- ...able_mock.py => test_ERC20BurnableMock.py} | 2 +- ...RC20_Mintable.py => test_ERC20Mintable.py} | 2 +- ...RC20_Pausable.py => test_ERC20Pausable.py} | 2 +- ...pgradeable.py => test_ERC20Upgradeable.py} | 2 +- .../test_ERC721EnumerableMintableBurnable.py} | 16 ++-- ...able.py => test_ERC721MintableBurnable.py} | 4 +- ...able.py => test_ERC721MintablePausable.py} | 4 +- ...mock.py => test_ERC721SafeMintableMock.py} | 4 +- tests/upgrades/test_Proxy.py | 2 +- tests/upgrades/test_upgrades.py | 4 +- tests/utils.py | 2 +- 72 files changed, 332 insertions(+), 294 deletions(-) rename src/openzeppelin/access/{accesscontrol.cairo => accesscontrol/library.cairo} (95%) rename src/openzeppelin/access/{ownable.cairo => ownable/library.cairo} (96%) rename src/openzeppelin/account/{ => presets}/Account.cairo (93%) rename src/openzeppelin/account/{ => presets}/AddressRegistry.cairo (82%) rename src/openzeppelin/account/{ => presets}/EthAccount.cairo (93%) rename src/openzeppelin/introspection/{ => erc165}/IERC165.cairo (67%) rename src/openzeppelin/introspection/{ERC165.cairo => erc165/library.cairo} (88%) rename src/openzeppelin/security/{initializable.cairo => initializable/library.cairo} (89%) rename src/openzeppelin/security/{pausable.cairo => pausable/library.cairo} (96%) rename src/openzeppelin/security/{reentrancyguard.cairo => reentrancyguard/library.cairo} (97%) rename src/openzeppelin/security/{safemath.cairo => safemath/library.cairo} (97%) rename src/openzeppelin/token/erc20/{interfaces => }/IERC20.cairo (84%) rename src/openzeppelin/token/erc20/{ => presets}/ERC20.cairo (97%) rename src/openzeppelin/token/erc20/{ERC20_Mintable.cairo => presets/ERC20Mintable.cairo} (96%) rename src/openzeppelin/token/erc20/{ERC20_Pausable.cairo => presets/ERC20Pausable.cairo} (95%) rename src/openzeppelin/token/erc20/{ERC20_Upgradeable.cairo => presets/ERC20Upgradeable.cairo} (97%) rename src/openzeppelin/token/erc721/{interfaces => }/IERC721.cairo (81%) rename src/openzeppelin/token/erc721/{interfaces/IERC721_Metadata.cairo => IERC721Metadata.cairo} (69%) rename src/openzeppelin/token/erc721/{interfaces/IERC721_Receiver.cairo => IERC721Receiver.cairo} (65%) rename src/openzeppelin/token/{erc721_enumerable/interfaces/IERC721_Enumerable.cairo => erc721/enumerable/IERC721Enumerable.cairo} (71%) rename src/openzeppelin/token/{erc721_enumerable => erc721/enumerable}/library.cairo (66%) rename src/openzeppelin/token/{erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo => erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo} (86%) rename src/openzeppelin/token/erc721/{utils/ERC721_Holder.cairo => enumerable/presets/utils/ERC721Holder.cairo} (78%) rename src/openzeppelin/token/erc721/{ERC721_Mintable_Burnable.cairo => presets/ERC721MintableBurnable.cairo} (95%) rename src/openzeppelin/token/erc721/{ERC721_Mintable_Pausable.cairo => presets/ERC721MintablePausable.cairo} (95%) rename src/openzeppelin/upgrades/{ => presets}/Proxy.cairo (95%) rename src/openzeppelin/utils/{constants.cairo => constants/library.cairo} (86%) rename tests/mocks/{account_reentrancy.cairo => AccountReentrancy.cairo} (97%) rename tests/mocks/{ERC20_Burnable_mock.cairo => ERC20BurnableMock.cairo} (100%) rename tests/mocks/{ERC721_SafeMintable_mock.cairo => ERC721SafeMintableMock.cairo} (96%) rename tests/mocks/{proxiable_implementation.cairo => ProxiableImplementation.cairo} (100%) rename tests/mocks/{reentrancy_attacker_mock.cairo => ReentrancyAttackerMock.cairo} (100%) rename tests/mocks/{reentrancy_mock.cairo => ReentrancyMock.cairo} (97%) rename tests/mocks/{safemath_mock.cairo => SafeMathMock.cairo} (95%) rename tests/mocks/{upgrades_v1_mock.cairo => UpgradesMockV1.cairo} (100%) rename tests/mocks/{upgrades_v2_mock.cairo => UpgradesMockV2.cairo} (100%) rename tests/token/erc20/{test_ERC20_Burnable_mock.py => test_ERC20BurnableMock.py} (98%) rename tests/token/erc20/{test_ERC20_Mintable.py => test_ERC20Mintable.py} (98%) rename tests/token/erc20/{test_ERC20_Pausable.py => test_ERC20Pausable.py} (99%) rename tests/token/erc20/{test_ERC20_Upgradeable.py => test_ERC20Upgradeable.py} (98%) rename tests/token/{erc721_enumerable/test_ERC721_Enumerable_Mintable_Burnable.py => erc721/test_ERC721EnumerableMintableBurnable.py} (94%) rename tests/token/erc721/{test_ERC721_Mintable_Burnable.py => test_ERC721MintableBurnable.py} (99%) rename tests/token/erc721/{test_ERC721_Mintable_Pausable.py => test_ERC721MintablePausable.py} (98%) rename tests/token/erc721/{test_ERC721_SafeMintable_mock.py => test_ERC721SafeMintableMock.py} (98%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9d46f6412..c7a7f4d38 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,7 +8,33 @@ Before starting development, please [create an issue](https://github.com/OpenZep Also, consider that snake case is used for Cairo development in general due to its strong Python bias, but in order to be compliant with the standards, EIP implementations must use camelCase. Therefore, EIP-based contracts will have their external functions written in camelCase while the rest of the codebase will be in snake_case. -And make sure to always include tests and documentation for the new developments. +And make sure to always include tests and documentation for the new developments. Please consider the following conventions: + +- Naming + - libraries should be named `library.cairo`, e.g. `erc20/library.cairo` + - contracts should be PascalCased i.e. `MyContract.cairo` + - interfaces should be prefixed with an `I`, as in `IAccount.cairo` + - test modules should begin with `test_` followed by the contract name i.e. `test_MyContract.py` + +- Structure + - libraries should cede their names to their parent directory and are named `library.cairo` instead + - interfaces should be alongside the library that the interface defines + - preset contracts should be within a `presets` directory of the library to which they are a preset + - Here are example paths: + - `openzeppelin.token.erc20.library` + - `openzeppelin.token.erc20.IERC20` + - `openzeppelin.token.erc20.presets.ERC20Mintable` + - And a visual guide: + +```python + openzeppelin + └──token + └── erc20 + ├── library.cairo + ├── IERC20.cairo + └── presets + └── ERC20Mintable.cairo +``` ## Creating Pull Requests (PRs) diff --git a/README.md b/README.md index 8abdcfbc2..e1a14b4be 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ nile deploy MyToken --al from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.uint256 import Uint256 -from openzeppelin.security.pausable import Pausable +from openzeppelin.security.pausable.library import Pausable from openzeppelin.token.erc20.library import ERC20 (...) @@ -148,32 +148,38 @@ python -m pip install . ```bash nile compile --directory src -🤖 Compiling all Cairo contracts in the openzeppelin directory -🔨 Compiling openzeppelin/introspection/ERC165.cairo -🔨 Compiling openzeppelin/introspection/IERC165.cairo -🔨 Compiling openzeppelin/token/erc721/ERC721_Mintable_Burnable.cairo -🔨 Compiling openzeppelin/token/erc721/ERC721_Mintable_Pausable.cairo -🔨 Compiling openzeppelin/token/erc721/library.cairo -🔨 Compiling openzeppelin/token/erc721/interfaces/IERC721_Metadata.cairo -🔨 Compiling openzeppelin/token/erc721/interfaces/IERC721.cairo -🔨 Compiling openzeppelin/token/erc721/interfaces/IERC721_Receiver.cairo -🔨 Compiling openzeppelin/token/erc721/utils/ERC721_Holder.cairo -🔨 Compiling openzeppelin/token/erc20/ERC20_Mintable.cairo -🔨 Compiling openzeppelin/token/erc20/ERC20.cairo -🔨 Compiling openzeppelin/token/erc20/library.cairo -🔨 Compiling openzeppelin/token/erc20/ERC20_Pausable.cairo -🔨 Compiling openzeppelin/token/erc20/interfaces/IERC20.cairo -🔨 Compiling openzeppelin/token/erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo -🔨 Compiling openzeppelin/token/erc721_enumerable/library.cairo -🔨 Compiling openzeppelin/token/erc721_enumerable/interfaces/IERC721_Enumerable.cairo -🔨 Compiling openzeppelin/security/pausable.cairo -🔨 Compiling openzeppelin/security/safemath.cairo -🔨 Compiling openzeppelin/security/initializable.cairo -🔨 Compiling openzeppelin/access/ownable.cairo -🔨 Compiling openzeppelin/account/IAccount.cairo -🔨 Compiling openzeppelin/account/Account.cairo -🔨 Compiling openzeppelin/account/AddressRegistry.cairo -🔨 Compiling openzeppelin/utils/constants.cairo +🤖 Compiling all Cairo contracts in the src directory +🔨 Compiling src/openzeppelin/token/erc20/library.cairo +🔨 Compiling src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo +🔨 Compiling src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo +🔨 Compiling src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo +🔨 Compiling src/openzeppelin/token/erc20/presets/ERC20.cairo +🔨 Compiling src/openzeppelin/token/erc20/IERC20.cairo +🔨 Compiling src/openzeppelin/token/erc721/enumerable/library.cairo +🔨 Compiling src/openzeppelin/token/erc721/library.cairo +🔨 Compiling src/openzeppelin/token/erc721/utils/ERC721Holder.cairo +🔨 Compiling src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo +🔨 Compiling src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo +🔨 Compiling src/openzeppelin/token/erc721/presets/ERC721EnumerableMintableBurnable.cairo +🔨 Compiling src/openzeppelin/token/erc721/IERC721.cairo +🔨 Compiling src/openzeppelin/token/erc721/IERC721Metadata.cairo +🔨 Compiling src/openzeppelin/token/erc721/IERC721Receiver.cairo +🔨 Compiling src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo +🔨 Compiling src/openzeppelin/access/ownable/library.cairo +🔨 Compiling src/openzeppelin/security/reentrancyguard/library.cairo +🔨 Compiling src/openzeppelin/security/safemath/library.cairo +🔨 Compiling src/openzeppelin/security/pausable/library.cairo +🔨 Compiling src/openzeppelin/security/initializable/library.cairo +🔨 Compiling src/openzeppelin/utils/constants/library.cairo +🔨 Compiling src/openzeppelin/introspection/erc165/library.cairo +🔨 Compiling src/openzeppelin/introspection/erc165/IERC165.cairo +🔨 Compiling src/openzeppelin/upgrades/library.cairo +🔨 Compiling src/openzeppelin/upgrades/presets/Proxy.cairo +🔨 Compiling src/openzeppelin/account/library.cairo +🔨 Compiling src/openzeppelin/account/presets/EthAccount.cairo +🔨 Compiling src/openzeppelin/account/presets/Account.cairo +🔨 Compiling src/openzeppelin/account/presets/AddressRegistry.cairo +🔨 Compiling src/openzeppelin/account/IAccount.cairo ✅ Done ``` diff --git a/docs/Access.md b/docs/Access.md index 860192f6c..4eec95a4a 100644 --- a/docs/Access.md +++ b/docs/Access.md @@ -41,14 +41,14 @@ Access control—that is, "who is allowed to do this thing"—is incredibly impo The most common and basic form of access control is the concept of ownership: there’s an account that is the `owner` of a contract and can do administrative tasks on it. This approach is perfectly reasonable for contracts that have a single administrative user. -OpenZeppelin Contracts for Cairo provides [Ownable](../src/openzeppelin/access/ownable.cairo) for implementing ownership in your contracts. +OpenZeppelin Contracts for Cairo provides [Ownable](../src/openzeppelin/access/ownable/library.cairo) for implementing ownership in your contracts. ### Quickstart -Integrating [Ownable](../src/openzeppelin/access/ownable.cairo) into a contract first requires assigning an owner. The implementing contract's constructor should set the initial owner by passing the owner's address to Ownable's [initializer](#initializer) like this: +Integrating [Ownable](../src/openzeppelin/access/library.cairo) into a contract first requires assigning an owner. The implementing contract's constructor should set the initial owner by passing the owner's address to Ownable's [initializer](#initializer) like this: ```cairo -from openzeppelin.access.ownable import Ownable +from openzeppelin.access.ownable.library import Ownable @constructor func constructor{ @@ -64,7 +64,7 @@ end To restrict a function's access to the owner only, add in the `assert_only_owner` method: ```cairo -from openzeppelin.access.ownable import Ownable +from openzeppelin.access.ownable.library import Ownable func protected_function{ syscall_ptr : felt*, @@ -216,12 +216,12 @@ Most software uses access control systems that are role-based: some users are re For each role that you want to define, you will create a new *role identifier* that is used to grant, revoke, and check if an account has that role (see [Creating role identifiers](#creating-role-identifiers) for information on creating identifiers). -Here's a simple example of implementing `AccessControl` on a portion of an [ERC20 token contract](../src/openzeppelin/token/erc20/ERC20.cairo) which defines and sets the 'minter' role: +Here's a simple example of implementing `AccessControl` on a portion of an [ERC20 token contract](../src/openzeppelin/token/erc20/presets/ERC20.cairo) which defines and sets the 'minter' role: ```cairo from openzeppelin.token.erc20.library import ERC20 -from openzeppelin.access.accesscontrol import AccessControl +from openzeppelin.access.accesscontrol.library import AccessControl const MINTER_ROLE = 0x4f96f87f6963bb246f2c30526628466840c642dc5c50d5a67777c6cc0e44ab5 @@ -264,7 +264,7 @@ Let’s augment our ERC20 token example by also defining a 'burner' role, which ```cairo from openzeppelin.token.erc20.library import ERC20 -from openzeppelin.access.accesscontrol import AccessControl +from openzeppelin.access.accesscontrol.library import AccessControl const MINTER_ROLE = 0x4f96f87f6963bb246f2c30526628466840c642dc5c50d5a67777c6cc0e44ab5 @@ -331,7 +331,7 @@ from openzeppelin.token.erc20.library import ERC20 from openzeppelin.access.accesscontrol import AccessControl -from openzeppelin.utils.constants import DEFAULT_ADMIN_ROLE +from openzeppelin.utils.constants.library import DEFAULT_ADMIN_ROLE const MINTER_ROLE = 0x4f96f87f6963bb246f2c30526628466840c642dc5c50d5a67777c6cc0e44ab5 diff --git a/docs/Account.md b/docs/Account.md index 7aff2a0e9..177d97f4b 100644 --- a/docs/Account.md +++ b/docs/Account.md @@ -47,12 +47,15 @@ In Python, this would look as follows: ```python from starkware.starknet.testing.starknet import Starknet +from utils import get_contract_class +from signers import MockSigner + signer = MockSigner(123456789987654321) starknet = await Starknet.empty() # 1. Deploy Account account = await starknet.deploy( - "contracts/Account.cairo", + get_contract_class("Account"), constructor_calldata=[signer.public_key] ) @@ -456,11 +459,11 @@ The following contract presets are ready to deploy and can be used as-is for qui ### Account -The [`Account`](../src/openzeppelin/account/Account.cairo) preset uses StarkNet keys to validate transactions. +The [`Account`](../src/openzeppelin/account/presets/Account.cairo) preset uses StarkNet keys to validate transactions. ### Eth Account -The [`EthAccount`](../src/openzeppelin/account/EthAccount.cairo) preset supports Ethereum addresses, validating transactions with secp256k1 keys. +The [`EthAccount`](../src/openzeppelin/account/presets/EthAccount.cairo) preset supports Ethereum addresses, validating transactions with secp256k1 keys. ## Account differentiation with ERC165 @@ -478,7 +481,7 @@ Account contract developers are encouraged to implement the [standard Account in To implement alternative `execute` functions, make sure to check their corresponding `validate` function before calling the `_unsafe_execute` building block, as each of the current presets is doing. Do not expose `_unsafe_execute` directly. -> Please note that the `ecdsa_ptr` implicit argument should be included in new methods that invoke `_unsafe_execute` (even if the `ecdsa_ptr` is not being used). Otherwise, it's possible that an account's functionalty can work in both the testing and local devnet environments; however, it could fail on public networks on account of the [SignatureBuiltinRunner](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/lang/builtins/signature/signature_builtin_runner.py). See [issue #386](https://github.com/OpenZeppelin/cairo-contracts/issues/386) for more information. +> Please note that the `ecdsa_ptr` implicit argument should be included in new methods that invoke `_unsafe_execute` (even if the `ecdsa_ptr` is not being used). Otherwise, it's possible that an account's functionality can work in both the testing and local devnet environments; however, it could fail on public networks on account of the [SignatureBuiltinRunner](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/lang/builtins/signature/signature_builtin_runner.py). See [issue #386](https://github.com/OpenZeppelin/cairo-contracts/issues/386) for more information. Some other validation schemes to look out for in the future: diff --git a/docs/ERC20.md b/docs/ERC20.md index 9c3550726..f5cb8073f 100644 --- a/docs/ERC20.md +++ b/docs/ERC20.md @@ -10,9 +10,9 @@ The ERC20 token standard is a specification for [fungible tokens](https://docs.o - [Extensibility](#extensibility) - [Presets](#presets) - [ERC20 (basic)](#erc20-basic) - - [ERC20_Mintable](#erc20_mintable) - - [ERC20_Pausable](#erc20_pausable) - - [ERC20_Upgradeable](#erc20_upgradeable) + - [ERC20Mintable](#erc20mintable) + - [ERC20Pausable](#erc20pausable) + - [ERC20Upgradeable](#erc20upgradeable) - [API Specification](#api-specification) - [Methods](#methods) - [`name`](#name) @@ -100,7 +100,7 @@ To create a token you need to deploy it like this: ```python erc20 = await starknet.deploy( - "contracts/token/ERC20.cairo", + "openzeppelin/token/erc20/presets/ERC20.cairo", constructor_calldata=[ str_to_felt("Token"), # name str_to_felt("TKN"), # symbol @@ -127,7 +127,7 @@ await signer.send_transaction(account, erc20.contract_address, 'transfer', [reci ## Extensibility -ERC20 contracts can be extended by following the [extensibility pattern](../docs/Extensibility.md#the-pattern). The basic idea behind integrating the pattern is to import the requisite ERC20 methods from the ERC20 library and incorporate the extended logic thereafter. For example, let's say you wanted to implement a pausing mechanism. The contract should first import the ERC20 methods and the extended logic from the [pausable library](../src/openzeppelin/security/pausable.cairo) i.e. `Pausable_pause`, `Pausable_unpause`. Next, the contract should expose the methods with the extended logic therein like this: +ERC20 contracts can be extended by following the [extensibility pattern](../docs/Extensibility.md#the-pattern). The basic idea behind integrating the pattern is to import the requisite ERC20 methods from the ERC20 library and incorporate the extended logic thereafter. For example, let's say you wanted to implement a pausing mechanism. The contract should first import the ERC20 methods and the extended logic from the [pausable library](../src/openzeppelin/security/pausable/library.cairo) i.e. `Pausable_pause`, `Pausable_unpause`. Next, the contract should expose the methods with the extended logic therein like this: ```python @external @@ -136,8 +136,8 @@ func transfer{ pedersen_ptr : HashBuiltin*, range_check_ptr }(recipient: felt, amount: Uint256) -> (success: felt): - Pausable_when_not_paused() # imported extended logic - ERC20_transfer(recipient, amount) # imported library method + Pausable.when_not_paused() # imported extended logic + ERC20.transfer(recipient, amount) # imported library method return (TRUE) end ``` @@ -158,19 +158,19 @@ The following contract presets are ready to deploy and can be used as-is for qui ### ERC20 (basic) -The [`ERC20`](../src/openzeppelin/token/erc20/ERC20.cairo) preset offers a quick and easy setup for deploying a basic ERC20 token. +The [`ERC20`](../src/openzeppelin/token/erc20/presets/ERC20.cairo) preset offers a quick and easy setup for deploying a basic ERC20 token. -### ERC20_Mintable +### ERC20Mintable -The [`ERC20_Mintable`](../src/openzeppelin/token/erc20/ERC20_Mintable.cairo) preset allows the contract owner to mint new tokens. +The [`ERC20Mintable`](../src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo) preset allows the contract owner to mint new tokens. -### ERC20_Pausable +### ERC20Pausable -The [`ERC20_Pausable`](../src/openzeppelin/token/erc20/ERC20_Pausable.cairo) preset allows the contract owner to pause/unpause all state-modifying methods i.e. `transfer`, `approve`, etc. This preset proves useful for scenarios such as preventing trades until the end of an evaluation period and having an emergency switch for freezing all token transfers in the event of a large bug. +The [`ERC20Pausable`](../src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo) preset allows the contract owner to pause/unpause all state-modifying methods i.e. `transfer`, `approve`, etc. This preset proves useful for scenarios such as preventing trades until the end of an evaluation period and having an emergency switch for freezing all token transfers in the event of a large bug. -### ERC20_Upgradeable +### ERC20Upgradeable -The [`ERC20_Upgradeable`](../src/openzeppelin/token/erc20/ERC20_Upgradeable.cairo) preset allows the contract owner to upgrade a contract by deploying a new ERC20 implementation contract while also maintaing the contract's state. This preset proves useful for scenarios such as eliminating bugs and adding new features. For more on upgradeability, see [Contract upgrades](Proxies.md#contract-upgrades). +The [`ERC20Upgradeable`](../src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo) preset allows the contract owner to upgrade a contract by deploying a new ERC20 implementation contract while also maintaing the contract's state. This preset proves useful for scenarios such as eliminating bugs and adding new features. For more on upgradeability, see [Contract upgrades](Proxies.md#contract-upgrades). ## API Specification diff --git a/docs/ERC721.md b/docs/ERC721.md index c43e1fa26..bcf4444a5 100644 --- a/docs/ERC721.md +++ b/docs/ERC721.md @@ -10,19 +10,19 @@ The ERC721 token standard is a specification for [non-fungible tokens](https://d - [Token Transfers](#token-transfers) - [Interpreting ERC721 URIs](#interpreting-erc721-uris) - [ERC721Received](#erc721received) - - [IERC721_Receiver](#ierc721_receiver) + - [IERC721Receiver](#ierc721receiver) - [Supporting Interfaces](#supporting-interfaces) - [Ready-to-Use Presets](#ready-to-use-presets) - [Extensibility](#extensibility) - [Presets](#presets) - - [ERC721_Mintable_Burnable](#erc721_mintable_burnable) - - [ERC721_Mintable_Pausable](#erc721_mintable_pausable) - - [ERC721_Enumerable_Mintable_Burnable](#erc721_enumerable_mintable_burnable) - - [IERC721_Enumerable](#ierc721_enumerable) - - [ERC721_Metadata](#erc721_metadata) - - [IERC721_Metadata](#ierc721_metadata) + - [ERC721MintableBurnable](#erc721mintableburnable) + - [ERC721MintablePausable](#erc721mintablepausable) + - [ERC721EnumerableMintableBurnable](#erc721enumerablemintableburnable) + - [IERC721Enumerable](#ierc721enumerable) + - [ERC721Metadata](#erc721metadata) + - [IERC721Metadata](#ierc721metadata) - [Utilities](#utilities) - - [ERC721_Holder](#erc721_holder) + - [ERC721Holder](#erc721holder) - [API Specification](#api-specification) - [`IERC721`](#ierc721-api) - [`balanceOf`](#balanceof) @@ -37,15 +37,15 @@ The ERC721 token standard is a specification for [non-fungible tokens](https://d - [`Approval (event)`](#approval-event) - [`ApprovalForAll (event)`](#approvalforall-event) - [`Transfer (event)`](#transfer-event) - - [`IERC721_Metadata`](#ierc721_metadata) + - [`IERC721Metadata`](#ierc721metadata) - [`name`](#name) - [`symbol`](#symbol) - [`tokenURI`](#tokenuri) - - [`IERC721_Enumerable`](#ierc721_enumerable) + - [`IERC721Enumerable`](#ierc721enumerable) - [`totalSupply`](#totalsupply) - [`tokenByIndex`](#tokenbyindex) - [`tokenOfOwnerByIndex`](#tokenofownerbyindex) - - [`IERC721_Receiver`](#ierc721_receiver) + - [`IERC721Receiver`](#ierc721receiver-api) - [`onERC721Received`](#onerc721received) ## IERC721 @@ -104,17 +104,17 @@ But some differences can still be found, such as: - `safeTransferFrom` can only be expressed as a single function in Cairo as opposed to the two functions declared in EIP721. The difference between both functions consists of accepting `data` as an argument. Because function overloading is currently not possible in Cairo, `safeTransferFrom` by default accepts the `data` argument. If `data` is not used, simply insert `0`. - `safeTransferFrom` is specified such that the optional `data` argument should be of type bytes. In Solidity, this means a dynamically-sized array. To be as close as possible to the standard, it accepts a dynamic array of felts. In Cairo, arrays are expressed with the array length preceding the actual array; hence, the method accepts `data_len` and `data` respectively as types `felt` and `felt*` - `ERC165.register_interface` allows contracts to set and communicate which interfaces they support. This follows OpenZeppelin's [ERC165Storage](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/introspection/ERC165Storage.sol) -- `IERC721_Receiver` compliant contracts (`ERC721_Holder`) return a hardcoded selector id according to EVM selectors, since selectors are calculated differently in Cairo. This is in line with the ERC165 interfaces design choice towards EVM compatibility. See the [Introspection docs](./Introspection.md) for more info +- `IERC721Receiver` compliant contracts (`ERC721Holder`) return a hardcoded selector id according to EVM selectors, since selectors are calculated differently in Cairo. This is in line with the ERC165 interfaces design choice towards EVM compatibility. See the [Introspection docs](./Introspection.md) for more info -- `IERC721_Receiver` compliant contracts (`ERC721_Holder`) must support ERC165 by registering the `IERC721_Receiver` selector id in its constructor and exposing the `supportsInterface` method. In doing so, recipient contracts (both accounts and non-accounts) can be verified that they support ERC721 transfers +- `IERC721Receiver` compliant contracts (`ERC721Holder`) must support ERC165 by registering the `IERC721Receiver` selector id in its constructor and exposing the `supportsInterface` method. In doing so, recipient contracts (both accounts and non-accounts) can be verified that they support ERC721 transfers -- `ERC721_Enumerable` tracks the total number of tokens with the `all_tokens` and `all_tokens_len` storage variables mimicking the array of the Solidity implementation. +- `ERC721Enumerable` tracks the total number of tokens with the `all_tokens` and `all_tokens_len` storage variables mimicking the array of the Solidity implementation. ## Usage Use cases go from artwork, digital collectibles, physical property, and many more. -To show a standard use case, we'll use the `ERC721_Mintable` preset which allows for only the owner to `mint` and `burn` tokens. To create a token you need to first deploy both Account and ERC721 contracts respectively. As most StarkNet contracts, ERC721 expects to be called by another contract and it identifies it through `get_caller_address` (analogous to Solidity's `this.address`). This is why we need an Account contract to interact with it. +To show a standard use case, we'll use the `ERC721Mintable` preset which allows for only the owner to `mint` and `burn` tokens. To create a token you need to first deploy both Account and ERC721 contracts respectively. As most StarkNet contracts, ERC721 expects to be called by another contract and it identifies it through `get_caller_address` (analogous to Solidity's `this.address`). This is why we need an Account contract to interact with it. Considering that the ERC721 constructor method looks like this: @@ -135,7 +135,7 @@ account = await starknet.deploy( ) erc721 = await starknet.deploy( - "contracts/token/ERC721_Mintable.cairo", + "contracts/token/erc721/presets/ERC721Mintable.cairo", constructor_calldata=[ str_to_felt("Token"), # name str_to_felt("TKN"), # symbol @@ -200,18 +200,18 @@ string_uri = felt_to_str(felt_uri) ### ERC721Received -In order to be sure a contract can safely accept ERC721 tokens, said contract must implement the `ERC721_Receiver` interface (as expressed in the EIP721 specification). Methods such as `safeTransferFrom` and `safeMint` call the recipient contract's `onERC721Received` method. If the contract fails to return the correct magic value, the transaction fails. +In order to be sure a contract can safely accept ERC721 tokens, said contract must implement the `ERC721Receiver` interface (as expressed in the EIP721 specification). Methods such as `safeTransferFrom` and `safeMint` call the recipient contract's `onERC721Received` method. If the contract fails to return the correct magic value, the transaction fails. StarkNet contracts that support safe transfers, however, must also support [ERC165](./Introspection.md#erc165) and include `supportsInterface` as proposed in [#100](https://github.com/OpenZeppelin/cairo-contracts/discussions/100). `safeTransferFrom` requires a means of differentiating between account and non-account contracts. Currently, StarkNet does not support error handling from the contract level; -therefore, the current ERC721 implementation requires that all contracts that support safe ERC721 transfers (both accounts and non-accounts) include the `supportsInterface` method. Further, `supportsInterface` should return `TRUE` if the recipient contract supports the `IERC721_Receiver` magic value `0x150b7a02` (which invokes `onERC721Received`). If the recipient contract supports the `IAccount` magic value `0x50b70dcb`, `supportsInterface` should return `TRUE`. Otherwise, `safeTransferFrom` should fail. +therefore, the current ERC721 implementation requires that all contracts that support safe ERC721 transfers (both accounts and non-accounts) include the `supportsInterface` method. Further, `supportsInterface` should return `TRUE` if the recipient contract supports the `IERC721Receiver` magic value `0x150b7a02` (which invokes `onERC721Received`). If the recipient contract supports the `IAccount` magic value `0x50b70dcb`, `supportsInterface` should return `TRUE`. Otherwise, `safeTransferFrom` should fail. -#### IERC721_Receiver +#### IERC721Receiver Interface for any contract that wants to support safeTransfers from ERC721 asset contracts. ```cairo @contract_interface -namespace IERC721_Receiver: +namespace IERC721Receiver: func onERC721Received( operator: felt, from_: felt, @@ -233,9 +233,9 @@ Further, this implementation stores supported interfaces in a mapping (similar t ERC721 presets have been created to allow for quick deployments as-is. To be as explicit as possible, each preset includes the additional features they offer in the contract name. For example: -- `ERC721_Mintable_Burnable` includes `mint` and `burn` -- `ERC721_Mintable_Pausable` includes `mint`, `pause`, and `unpause` -- `ERC721_Enumerable_Mintable_Burnable` includes `mint`, `burn`, and [IERC721_Enumerable](#ierc721_enumerable) methods +- `ERC721MintableBurnable` includes `mint` and `burn` +- `ERC721MintablePausable` includes `mint`, `pause`, and `unpause` +- `ERC721EnumerableMintableBurnable` includes `mint`, `burn`, and [IERC721Enumerable](#ierc721enumerable) methods Ready-to-use presets are a great option for testing and prototyping. See [Presets](#presets). @@ -268,36 +268,36 @@ end The following contract presets are ready to deploy and can be used as-is for quick prototyping and testing. Each preset includes a contract owner, which is set in the `constructor`, to offer simple access control on sensitive methods such as `mint` and `burn`. -### ERC721_Mintable_Burnable +### ERC721MintableBurnable -The `ERC721_Mintable_Burnable` preset offers a quick and easy setup for creating NFTs. The contract owner can create tokens with `mint`, whereas token owners can destroy their tokens with `burn`. +The `ERC721MintableBurnable` preset offers a quick and easy setup for creating NFTs. The contract owner can create tokens with `mint`, whereas token owners can destroy their tokens with `burn`. -### ERC721_Mintable_Pausable +### ERC721MintablePausable -The `ERC721_Mintable_Pausable` preset creates a contract with pausable token transfers and minting capabilities. This preset proves useful for scenarios such as preventing trades until the end of an evaluation period and having an emergency switch for freezing all token transfers in the event of a large bug. In this preset, only the contract owner can `mint`, `pause`, and `unpause`. +The `ERC721MintablePausable` preset creates a contract with pausable token transfers and minting capabilities. This preset proves useful for scenarios such as preventing trades until the end of an evaluation period and having an emergency switch for freezing all token transfers in the event of a large bug. In this preset, only the contract owner can `mint`, `pause`, and `unpause`. -### ERC721_Enumerable_Mintable_Burnable +### ERC721EnumerableMintableBurnable -The `ERC721_Enumerable_Mintable_Burnable` preset adds enumerability of all the token ids in the contract as well as all token ids owned by each account. This allows contracts to publish its full list of NFTs and make them discoverable. +The `ERC721EnumerableMintableBurnable` preset adds enumerability of all the token ids in the contract as well as all token ids owned by each account. This allows contracts to publish its full list of NFTs and make them discoverable. In regard to implementation, contracts should expose the following view methods: -- `ERC721_Enumerable.total_supply` -- `ERC721_Enumerable.token_by_index` -- `ERC721_Enumerable.token_of_owner_by_index` +- `ERC721Enumerable.total_supply` +- `ERC721Enumerable.token_by_index` +- `ERC721Enumerable.token_of_owner_by_index` In order for the tokens to be correctly indexed, the contract should also use the following methods (which supercede some of the base `ERC721` methods): -- `ERC721_Enumerable.transfer_from` -- `ERC721_Enumerable.safe_transfer_from` -- `ERC721_Enumerable._mint` -- `ERC721_Enumerable._burn` +- `ERC721Enumerable.transfer_from` +- `ERC721Enumerable.safe_transfer_from` +- `ERC721Enumerable._mint` +- `ERC721Enumerable._burn` -#### IERC721_Enumerable +#### IERC721Enumerable ```cairo @contract_interface -namespace IERC721_Enumerable: +namespace IERC721Enumerable: func totalSupply() -> (totalSupply: Uint256): end @@ -309,17 +309,17 @@ namespace IERC721_Enumerable: end ``` -### ERC721_Metadata +### ERC721Metadata -The `ERC721_Metadata` extension allows your smart contract to be interrogated for its name and for details about the assets which your NFTs represent. +The `ERC721Metadata` extension allows your smart contract to be interrogated for its name and for details about the assets which your NFTs represent. -We follow OpenZeppelin's Solidity approach of integrating the Metadata methods `name`, `symbol`, and `tokenURI` into all ERC721 implementations. If preferred, a contract can be created that does not import the Metadata methods from the `ERC721_base` library. Note that the `IERC721_Metadata` interface id should be removed from the constructor as well. +We follow OpenZeppelin's Solidity approach of integrating the Metadata methods `name`, `symbol`, and `tokenURI` into all ERC721 implementations. If preferred, a contract can be created that does not import the Metadata methods from the `ERC721` library. Note that the `IERC721Metadata` interface id should be removed from the constructor as well. -#### IERC721_Metadata +#### IERC721Metadata ```cairo @contract_interface -namespace IERC721_Metadata: +namespace IERC721Metadata: func name() -> (name: felt): end @@ -333,7 +333,7 @@ end ## Utilities -### ERC721_Holder +### ERC721Holder Implementation of the `IERC721Receiver` interface. @@ -554,7 +554,7 @@ tokenId: Uint256 --- -### IERC721_Metadata API +### IERC721Metadata API ```cairo func name() -> (name: felt): @@ -613,7 +613,7 @@ tokenURI: felt --- -### IERC721_Enumerable API +### IERC721Enumerable API ```cairo @@ -674,7 +674,7 @@ tokenId: Uint256 --- -### IERC721_Receiver API +### IERC721Receiver API ```cairo func onERC721Received( diff --git a/docs/Extensibility.md b/docs/Extensibility.md index a743efa6e..a939970c2 100644 --- a/docs/Extensibility.md +++ b/docs/Extensibility.md @@ -64,15 +64,15 @@ Presets are pre-written contracts that extend from our library of contracts. The Some presets are: -* [Account](../src/openzeppelin/account/Account.cairo) +* [Account](../src/openzeppelin/account/presets/Account.cairo) * [ERC165](../tests/mocks/ERC165.cairo) -* [ERC20_Mintable](../src/openzeppelin/token/erc20/ERC20_Mintable.cairo) -* [ERC20_Pausable](../src/openzeppelin/token/erc20/ERC20_Pausable.cairo) -* [ERC20_Upgradeable](../src/openzeppelin/token/erc20/ERC20_Upgradeable.cairo) -* [ERC20](../src/openzeppelin/token/erc20/ERC20.cairo) -* [ERC721_Mintable_Burnable](../src/openzeppelin/token/erc721/ERC721_Mintable_Burnable.cairo) -* [ERC721_Mintable_Pausable](../src/openzeppelin/token/erc721/ERC721_Mintable_Pausable.cairo) -* [ERC721_Enumerable_Mintable_Burnable](../src/openzeppelin/token/erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo) +* [ERC20Mintable](../src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo) +* [ERC20Pausable](../src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo) +* [ERC20Upgradeable](../src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo) +* [ERC20](../src/openzeppelin/token/erc20/presets/ERC20.cairo) +* [ERC721MintableBurnable](../src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo) +* [ERC721MintablePausable](../src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo) +* [ERC721EnumerableMintableBurnable](../src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo) ## Function names and coding style diff --git a/docs/Introspection.md b/docs/Introspection.md index 042d4e48f..d3057c839 100644 --- a/docs/Introspection.md +++ b/docs/Introspection.md @@ -21,7 +21,7 @@ The ERC165 standard allows smart contracts to exercise [type introspection](http Cairo contracts, like Ethereum contracts, have no native concept of an interface, so applications must usually simply trust they are not making an incorrect call. For trusted setups this is a non-issue, but often unknown and untrusted third-party addresses need to be interacted with. There may even not be any direct calls to them! (e.g. ERC20 tokens may be sent to a contract that lacks a way to transfer them out of it, locking them forever). In these cases, a contract declaring its interface can be very helpful in preventing errors. -It should be noted that the [constants library](../src/openzeppelin/utils/constants.cairo) includes constant variables referencing all of the interface ids used in these contracts. This allows for more legible code i.e. using `IERC165_ID` instead of `0x01ffc9a7`. +It should be noted that the [constants library](../src/openzeppelin/utils/constants/library.cairo) includes constant variables referencing all of the interface ids used in these contracts. This allows for more legible code i.e. using `IERC165_ID` instead of `0x01ffc9a7`. ### Interface calculations @@ -32,14 +32,14 @@ In order to ensure EVM/StarkNet compatibility, interface identifiers are not cal For a contract to declare its support for a given interface, the contract should import the ERC165 library and register its support. It's recommended to register interface support upon contract deployment through a constructor either directly or indirectly (as an initializer) like this: ```cairo -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.introspection.erc165.library import ERC165 INTERFACE_ID = 0x12345678 @constructor func constructor{ - syscall_ptr : felt*, - pedersen_ptr : HashBuiltin*, + syscall_ptr: felt*, + pedersen_ptr: HashBuiltin*, range_check_ptr }(): ERC165.register_interface(INTERFACE_ID) @@ -52,7 +52,7 @@ end To query a target contract's support for an interface, the querying contract should call `supportsInterface` through IERC165 with the target contract's address and the queried interface id. Here's an example: ```cairo -from openzeppelin.introspection.IERC165 import IERC165 +from openzeppelin.introspection.erc165.IERC165 import IERC165 INTERFACE_ID = 0x12345678 @@ -68,7 +68,7 @@ func check_support{ end ``` -> Please note that `supportsInterface` is camelCased because it is an exposed contract method as part of ERC165's interface. This differs from library methods (such as `supports_interface` from the [ERC165 library](../src/openzeppelin/introspection/ERC165.cairo)) which are snake_cased and not exposed. See the [Function names and coding style](../docs/Extensibility.md#function-names-and-coding-style) for more details. +> Please note that `supportsInterface` is camelCased because it is an exposed contract method as part of ERC165's interface. This differs from library methods (such as `supports_interface` from the [ERC165 library](../src/openzeppelin/introspection/erc165/library.cairo)) which are snake_cased and not exposed. See the [Function names and coding style](../docs/Extensibility.md#function-names-and-coding-style) for more details. ### IERC165 diff --git a/docs/Proxies.md b/docs/Proxies.md index b9b12914d..cd6b2aeb9 100644 --- a/docs/Proxies.md +++ b/docs/Proxies.md @@ -77,7 +77,7 @@ In the case of contract upgrades, it is achieved by simply changing the proxy's ### Proxy contract -The [Proxy contract](../src/openzeppelin/upgrades/Proxy.cairo) includes two core methods: +The [Proxy contract](../src/openzeppelin/upgrades/presets/Proxy.cairo) includes two core methods: 1. The `__default__` method is a fallback method that redirects a function call and associated calldata to the implementation contract. @@ -110,7 +110,7 @@ The implementation contract should NOT: For a full implementation contract example, please see: -* [Proxiable implementation](../tests/mocks/proxiable_implementation.cairo) +* [Proxiable implementation](../tests/mocks/ProxiableImplementation.cairo) ## Upgrades library API @@ -284,8 +284,8 @@ To upgrade a contract, the implementation contract should include an `upgrade` m For a full deployment and upgrade implementation, please see: -* [Upgrades V1](../tests/mocks/upgrades_v1_mock.cairo) -* [Upgrades V2](../tests/mocks/upgrades_v2_mock.cairo) +* [Upgrades V1](../tests/mocks/UpgradesMockV1.cairo) +* [Upgrades V2](../tests/mocks/UpgradesMockV2.cairo) ### Declaring contracts @@ -311,5 +311,5 @@ Presets are pre-written contracts that extend from our library of contracts. The Some presets include: -* [ERC20_Upgradeable](../src/openzeppelin/token/erc20/ERC20_Upgradeable.cairo) +* [ERC20Upgradeable](../src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo) * more to come! have an idea? [open an issue](https://github.com/OpenZeppelin/cairo-contracts/issues/new/choose)! diff --git a/docs/Security.md b/docs/Security.md index 5a372d6cf..11024e361 100644 --- a/docs/Security.md +++ b/docs/Security.md @@ -19,7 +19,7 @@ The Initializable library provides a simple mechanism that mimics the functional The recommended pattern with Initializable is to include a check that the Initializable state is `False` and invoke `initialize` in the target function like this: ```cairo -from openzeppelin.security.initializable import Initializable +from openzeppelin.security.initializable.library import Initializable @external func foo{ @@ -44,7 +44,7 @@ The Pausable library allows contracts to implement an emergency stop mechanism. To use the Pausable library, the contract should include `pause` and `unpause` functions (which should be protected). For methods that should be available only when not paused, insert `assert_not_paused`. For methods that should be available only when paused, insert `assert_paused`. For example: ```cairo -from openzeppelin.security.pausable import Pausable +from openzeppelin.security.pausable.library import Pausable @external func whenNotPaused{ @@ -75,17 +75,17 @@ end For a list of full implementations utilizing the Pausable library, see: -* [ERC20_Pausable](../src/openzeppelin/token/erc20/ERC20_Pausable.cairo) -* [ERC721_Mintable_Pausable](../src/openzeppelin/token/erc721/ERC721_Mintable_Pausable.cairo) +* [ERC20Pausable](../src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo) +* [ERC721MintablePausable](../src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo) ## Reentrancy Guard A [reentrancy attack](https://gus-tavo-guim.medium.com/reentrancy-attack-on-smart-contracts-how-to-identify-the-exploitable-and-an-example-of-an-attack-4470a2d8dfe4) occurs when the caller is able to obtain more resources than allowed by recursively calling a target’s function. -Since Cairo does not support modifiers like Solidity, the [`reentrancyguard`](../src/openzeppelin/security/reentrancyguard.cairo) library exposes two methods `_start` and `_end` to protect functions against reentrancy attacks. The protected function must call `ReentrancyGuard._start` before the first function statement, and `ReentrancyGuard._end` before the return statement, as shown below: +Since Cairo does not support modifiers like Solidity, the [`reentrancy_guard`](../src/openzeppelin/security/reentrancyguard/library.cairo) library exposes two methods `_start` and `_end` to protect functions against reentrancy attacks. The protected function must call `ReentrancyGuard._start` before the first function statement, and `ReentrancyGuard._end` before the return statement, as shown below: ```cairo -from openzeppelin.security.reentrancyguard import ReentrancyGuard +from openzeppelin.security.reentrancyguard.library import ReentrancyGuard func test_function{ syscall_ptr : felt*, @@ -103,12 +103,12 @@ end ### SafeUint256 -The SafeUint256 namespace in the [SafeMath library](../src/openzeppelin/security/safemath.cairo) offers arithmetic for unsigned 256-bit integers (uint256) by leveraging Cairo's Uint256 library and integrating overflow checks. Some of Cairo's Uint256 functions do not revert upon overflows. For instance, `uint256_add` will return a bit carry when the sum exceeds 256 bits. This library includes an additional assertion ensuring values do not overflow. +The SafeUint256 namespace in the [SafeMath library](../src/openzeppelin/security/safemath/library.cairo) offers arithmetic for unsigned 256-bit integers (uint256) by leveraging Cairo's Uint256 library and integrating overflow checks. Some of Cairo's Uint256 functions do not revert upon overflows. For instance, `uint256_add` will return a bit carry when the sum exceeds 256 bits. This library includes an additional assertion ensuring values do not overflow. Using SafeUint256 methods is rather straightforward. Simply import SafeUint256 and insert the arithmetic method like this: ```cairo -from openzeppelin.security.safemath import SafeUint256 +from openzeppelin.security.safemath.library import SafeUint256 func add_two_uints{ syscall_ptr: felt*, diff --git a/docs/Utilities.md b/docs/Utilities.md index 78f3ade88..66b81ffbf 100644 --- a/docs/Utilities.md +++ b/docs/Utilities.md @@ -1,6 +1,6 @@ # Utilities -The following documentation provides context, reasoning, and examples for methods and constants found in `tests/utils.py`. +The following documentation provides context, reasoning, and examples for methods and constants found in `tests/utils.py`. > Expect this module to evolve (as it has already done). @@ -27,7 +27,7 @@ The following documentation provides context, reasoning, and examples for method ## Constants -To ease the readability of Cairo contracts, this project includes reusable [constants variables](../src/openzeppelin/utils/constants.cairo) like `UINT8_MAX`, or EIP165 interface IDs such as `IERC165_ID` or `IERC721_ID`. For more information on how interface ids are calculated, see the [ERC165 documentation](../docs/Introspection.md#interface-calculations). +To ease the readability of Cairo contracts, this project includes reusable [constants variables](../src/openzeppelin/utils/constants/library.cairo) like `UINT8_MAX`, or EIP165 interface IDs such as `IERC165_ID` or `IERC721_ID`. For more information on how interface ids are calculated, see the [ERC165 documentation](../docs/Introspection.md#interface-calculations). ## Strings diff --git a/src/openzeppelin/access/accesscontrol.cairo b/src/openzeppelin/access/accesscontrol/library.cairo similarity index 95% rename from src/openzeppelin/access/accesscontrol.cairo rename to src/openzeppelin/access/accesscontrol/library.cairo index 7ddce3b0a..2bfbe9eeb 100644 --- a/src/openzeppelin/access/accesscontrol.cairo +++ b/src/openzeppelin/access/accesscontrol/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Cairo Contracts v0.2.1 (access/accesscontrol.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (access/accesscontrol/library.cairo) %lang starknet @@ -7,8 +7,8 @@ from starkware.starknet.common.syscalls import get_caller_address from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.bool import TRUE, FALSE -from openzeppelin.introspection.ERC165 import ERC165 -from openzeppelin.utils.constants import IACCESSCONTROL_ID +from openzeppelin.introspection.erc165.library import ERC165 +from openzeppelin.utils.constants.library import IACCESSCONTROL_ID # # Events diff --git a/src/openzeppelin/access/ownable.cairo b/src/openzeppelin/access/ownable/library.cairo similarity index 96% rename from src/openzeppelin/access/ownable.cairo rename to src/openzeppelin/access/ownable/library.cairo index c9de2c1bf..5546bdfe0 100644 --- a/src/openzeppelin/access/ownable.cairo +++ b/src/openzeppelin/access/ownable/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (access/ownable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (access/ownable/library.cairo) %lang starknet diff --git a/src/openzeppelin/account/library.cairo b/src/openzeppelin/account/library.cairo index 149b86eae..edba3c170 100644 --- a/src/openzeppelin/account/library.cairo +++ b/src/openzeppelin/account/library.cairo @@ -14,9 +14,9 @@ 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.cairo_secp.signature import verify_eth_signature_uint256 -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.introspection.erc165.library import ERC165 -from openzeppelin.utils.constants import IACCOUNT_ID +from openzeppelin.utils.constants.library import IACCOUNT_ID # # Storage @@ -162,7 +162,7 @@ namespace Account: # 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, + # 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]) @@ -330,5 +330,5 @@ namespace Account: _from_call_array_to_call(call_array_len - 1, call_array + AccountCallArray.SIZE, calldata, calls + Call.SIZE) return () end - + end diff --git a/src/openzeppelin/account/Account.cairo b/src/openzeppelin/account/presets/Account.cairo similarity index 93% rename from src/openzeppelin/account/Account.cairo rename to src/openzeppelin/account/presets/Account.cairo index af78fbbd1..9039d8b87 100644 --- a/src/openzeppelin/account/Account.cairo +++ b/src/openzeppelin/account/presets/Account.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (account/Account.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (account/presets/Account.cairo) %lang starknet @@ -7,7 +7,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin, from openzeppelin.account.library import Account, AccountCallArray -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.introspection.erc165.library import ERC165 # # Constructor @@ -39,7 +39,7 @@ end @view func get_nonce{ - syscall_ptr : felt*, + syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr }() -> (res: felt): diff --git a/src/openzeppelin/account/AddressRegistry.cairo b/src/openzeppelin/account/presets/AddressRegistry.cairo similarity index 82% rename from src/openzeppelin/account/AddressRegistry.cairo rename to src/openzeppelin/account/presets/AddressRegistry.cairo index c001af637..812314055 100644 --- a/src/openzeppelin/account/AddressRegistry.cairo +++ b/src/openzeppelin/account/presets/AddressRegistry.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (account/AddressRegistry.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (account/presets/AddressRegistry.cairo) %lang starknet @@ -12,7 +12,7 @@ end @external func get_L1_address{ - syscall_ptr : felt*, + syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr }(L2_address: felt) -> (res: felt): @@ -22,7 +22,7 @@ end @external func set_L1_address{ - syscall_ptr : felt*, + syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr }(new_L1_address: felt): diff --git a/src/openzeppelin/account/EthAccount.cairo b/src/openzeppelin/account/presets/EthAccount.cairo similarity index 93% rename from src/openzeppelin/account/EthAccount.cairo rename to src/openzeppelin/account/presets/EthAccount.cairo index 26e1300d9..603083016 100644 --- a/src/openzeppelin/account/EthAccount.cairo +++ b/src/openzeppelin/account/presets/EthAccount.cairo @@ -1,11 +1,11 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (account/EthAccount.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (account/presets/EthAccount.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin, BitwiseBuiltin -from openzeppelin.account.library import Account, AccountCallArray -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.account.library import Account, AccountCallArray +from openzeppelin.introspection.erc165.library import ERC165 # # Constructor @@ -37,7 +37,7 @@ end @view func get_nonce{ - syscall_ptr : felt*, + syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr }() -> (res: felt): diff --git a/src/openzeppelin/introspection/IERC165.cairo b/src/openzeppelin/introspection/erc165/IERC165.cairo similarity index 67% rename from src/openzeppelin/introspection/IERC165.cairo rename to src/openzeppelin/introspection/erc165/IERC165.cairo index 17673b474..1c2124431 100644 --- a/src/openzeppelin/introspection/IERC165.cairo +++ b/src/openzeppelin/introspection/erc165/IERC165.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (introspection/IERC165.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (introspection/erc165/IERC165.cairo) %lang starknet diff --git a/src/openzeppelin/introspection/ERC165.cairo b/src/openzeppelin/introspection/erc165/library.cairo similarity index 88% rename from src/openzeppelin/introspection/ERC165.cairo rename to src/openzeppelin/introspection/erc165/library.cairo index f6302d54e..9444a6295 100644 --- a/src/openzeppelin/introspection/ERC165.cairo +++ b/src/openzeppelin/introspection/erc165/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (introspection/ERC165.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (introspection/erc165/library.cairo) %lang starknet @@ -7,7 +7,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.math import assert_not_equal from starkware.cairo.common.bool import TRUE -from openzeppelin.utils.constants import INVALID_ID, IERC165_ID +from openzeppelin.utils.constants.library import INVALID_ID, IERC165_ID @storage_var func ERC165_supported_interfaces(interface_id: felt) -> (is_supported: felt): diff --git a/src/openzeppelin/security/initializable.cairo b/src/openzeppelin/security/initializable/library.cairo similarity index 89% rename from src/openzeppelin/security/initializable.cairo rename to src/openzeppelin/security/initializable/library.cairo index c6504602f..2298433e3 100644 --- a/src/openzeppelin/security/initializable.cairo +++ b/src/openzeppelin/security/initializable/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (security/initializable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (security/initializable/library.cairo) %lang starknet @@ -12,8 +12,8 @@ end namespace Initializable: - func initialized{ - syscall_ptr : felt*, + func initialized{ + syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr }() -> (res: felt): @@ -22,7 +22,7 @@ namespace Initializable: end func initialize{ - syscall_ptr : felt*, + syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr }(): diff --git a/src/openzeppelin/security/pausable.cairo b/src/openzeppelin/security/pausable/library.cairo similarity index 96% rename from src/openzeppelin/security/pausable.cairo rename to src/openzeppelin/security/pausable/library.cairo index 2cebcab73..784e01d37 100644 --- a/src/openzeppelin/security/pausable.cairo +++ b/src/openzeppelin/security/pausable/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (security/pausable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (security/pausable/library.cairo) %lang starknet diff --git a/src/openzeppelin/security/reentrancyguard.cairo b/src/openzeppelin/security/reentrancyguard/library.cairo similarity index 97% rename from src/openzeppelin/security/reentrancyguard.cairo rename to src/openzeppelin/security/reentrancyguard/library.cairo index 02d0b938a..74d14be81 100644 --- a/src/openzeppelin/security/reentrancyguard.cairo +++ b/src/openzeppelin/security/reentrancyguard/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (security/reentrancyguard.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (security/reentrancyguard/library.cairo) %lang starknet diff --git a/src/openzeppelin/security/safemath.cairo b/src/openzeppelin/security/safemath/library.cairo similarity index 97% rename from src/openzeppelin/security/safemath.cairo rename to src/openzeppelin/security/safemath/library.cairo index a07525373..095b7d23e 100644 --- a/src/openzeppelin/security/safemath.cairo +++ b/src/openzeppelin/security/safemath/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (security/safemath.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (security/safemath/library.cairo) %lang starknet @@ -12,7 +12,7 @@ from starkware.cairo.common.uint256 import ( namespace SafeUint256: - # Adds two integers. + # Adds two integers. # Reverts if the sum overflows. func add{ syscall_ptr: felt*, diff --git a/src/openzeppelin/token/erc20/interfaces/IERC20.cairo b/src/openzeppelin/token/erc20/IERC20.cairo similarity index 84% rename from src/openzeppelin/token/erc20/interfaces/IERC20.cairo rename to src/openzeppelin/token/erc20/IERC20.cairo index 3eb543aed..cbe51674c 100644 --- a/src/openzeppelin/token/erc20/interfaces/IERC20.cairo +++ b/src/openzeppelin/token/erc20/IERC20.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/interfaces/IERC20.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/IERC20.cairo) %lang starknet @@ -29,8 +29,8 @@ namespace IERC20: end func transferFrom( - sender: felt, - recipient: felt, + sender: felt, + recipient: felt, amount: Uint256 ) -> (success: felt): end diff --git a/src/openzeppelin/token/erc20/library.cairo b/src/openzeppelin/token/erc20/library.cairo index 29383204c..3d9146081 100644 --- a/src/openzeppelin/token/erc20/library.cairo +++ b/src/openzeppelin/token/erc20/library.cairo @@ -9,8 +9,8 @@ from starkware.cairo.common.math import assert_not_zero, assert_lt from starkware.cairo.common.bool import FALSE from starkware.cairo.common.uint256 import Uint256, uint256_check, uint256_eq, uint256_not -from openzeppelin.security.safemath import SafeUint256 -from openzeppelin.utils.constants import UINT8_MAX +from openzeppelin.security.safemath.library import SafeUint256 +from openzeppelin.utils.constants.library import UINT8_MAX # # Events diff --git a/src/openzeppelin/token/erc20/ERC20.cairo b/src/openzeppelin/token/erc20/presets/ERC20.cairo similarity index 97% rename from src/openzeppelin/token/erc20/ERC20.cairo rename to src/openzeppelin/token/erc20/presets/ERC20.cairo index 35735138d..310239884 100644 --- a/src/openzeppelin/token/erc20/ERC20.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/ERC20.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/presets/ERC20.cairo) %lang starknet diff --git a/src/openzeppelin/token/erc20/ERC20_Mintable.cairo b/src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo similarity index 96% rename from src/openzeppelin/token/erc20/ERC20_Mintable.cairo rename to src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo index ead9170f8..cac568e07 100644 --- a/src/openzeppelin/token/erc20/ERC20_Mintable.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20Mintable.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/ERC20_Mintable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/presets/ERC20Mintable.cairo) %lang starknet @@ -7,7 +7,7 @@ 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 import Ownable +from openzeppelin.access.ownable.library import Ownable from openzeppelin.token.erc20.library import ERC20 @constructor diff --git a/src/openzeppelin/token/erc20/ERC20_Pausable.cairo b/src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo similarity index 95% rename from src/openzeppelin/token/erc20/ERC20_Pausable.cairo rename to src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo index 573e52582..994c6e9bc 100644 --- a/src/openzeppelin/token/erc20/ERC20_Pausable.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20Pausable.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/ERC20_Pausable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/presets/ERC20Pausable.cairo) %lang starknet @@ -7,8 +7,8 @@ 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 import Ownable -from openzeppelin.security.pausable import Pausable +from openzeppelin.access.ownable.library import Ownable +from openzeppelin.security.pausable.library import Pausable from openzeppelin.token.erc20.library import ERC20 @constructor diff --git a/src/openzeppelin/token/erc20/ERC20_Upgradeable.cairo b/src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo similarity index 97% rename from src/openzeppelin/token/erc20/ERC20_Upgradeable.cairo rename to src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo index 24940e212..e7252a697 100644 --- a/src/openzeppelin/token/erc20/ERC20_Upgradeable.cairo +++ b/src/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/ERC20_Upgradeable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc20/presets/ERC20Upgradeable.cairo) %lang starknet %builtins pedersen range_check diff --git a/src/openzeppelin/token/erc721/interfaces/IERC721.cairo b/src/openzeppelin/token/erc721/IERC721.cairo similarity index 81% rename from src/openzeppelin/token/erc721/interfaces/IERC721.cairo rename to src/openzeppelin/token/erc721/IERC721.cairo index 5cfa80998..1cfa73924 100644 --- a/src/openzeppelin/token/erc721/interfaces/IERC721.cairo +++ b/src/openzeppelin/token/erc721/IERC721.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/interfaces/IERC721.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/IERC721.cairo) %lang starknet @@ -14,9 +14,9 @@ namespace IERC721: end func safeTransferFrom( - from_: felt, - to: felt, - tokenId: Uint256, + from_: felt, + to: felt, + tokenId: Uint256, data_len: felt, data: felt* ): diff --git a/src/openzeppelin/token/erc721/interfaces/IERC721_Metadata.cairo b/src/openzeppelin/token/erc721/IERC721Metadata.cairo similarity index 69% rename from src/openzeppelin/token/erc721/interfaces/IERC721_Metadata.cairo rename to src/openzeppelin/token/erc721/IERC721Metadata.cairo index ef4b2bc52..415a5a894 100644 --- a/src/openzeppelin/token/erc721/interfaces/IERC721_Metadata.cairo +++ b/src/openzeppelin/token/erc721/IERC721Metadata.cairo @@ -1,12 +1,12 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/interfaces/IERC721_Metadata.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/IERC721Metadata.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC721_Metadata: +namespace IERC721Metadata: func name() -> (name: felt): end diff --git a/src/openzeppelin/token/erc721/interfaces/IERC721_Receiver.cairo b/src/openzeppelin/token/erc721/IERC721Receiver.cairo similarity index 65% rename from src/openzeppelin/token/erc721/interfaces/IERC721_Receiver.cairo rename to src/openzeppelin/token/erc721/IERC721Receiver.cairo index 0e7178983..ee9d0b602 100644 --- a/src/openzeppelin/token/erc721/interfaces/IERC721_Receiver.cairo +++ b/src/openzeppelin/token/erc721/IERC721Receiver.cairo @@ -1,18 +1,18 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/interfaces/IERC721_Receiver.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/IERC721Receiver.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC721_Receiver: +namespace IERC721Receiver: func onERC721Received( operator: felt, from_: felt, tokenId: Uint256, data_len: felt, data: felt* - ) -> (selector: felt): + ) -> (selector: felt): end end diff --git a/src/openzeppelin/token/erc721_enumerable/interfaces/IERC721_Enumerable.cairo b/src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo similarity index 71% rename from src/openzeppelin/token/erc721_enumerable/interfaces/IERC721_Enumerable.cairo rename to src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo index 0bf95b6a7..9f223342b 100644 --- a/src/openzeppelin/token/erc721_enumerable/interfaces/IERC721_Enumerable.cairo +++ b/src/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo @@ -1,12 +1,12 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721_enumerable/interfaces/IERC721_Enumerable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/enumerable/IERC721Enumerable.cairo) %lang starknet from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IERC721_Enumerable: +namespace IERC721Enumerable: func totalSupply() -> (totalSupply: Uint256): end diff --git a/src/openzeppelin/token/erc721_enumerable/library.cairo b/src/openzeppelin/token/erc721/enumerable/library.cairo similarity index 66% rename from src/openzeppelin/token/erc721_enumerable/library.cairo rename to src/openzeppelin/token/erc721/enumerable/library.cairo index fda447356..1930d964a 100644 --- a/src/openzeppelin/token/erc721_enumerable/library.cairo +++ b/src/openzeppelin/token/erc721/enumerable/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721_enumerable/library.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/enumerable/library.cairo) %lang starknet @@ -9,37 +9,37 @@ from starkware.cairo.common.uint256 import ( Uint256, uint256_lt, uint256_eq, uint256_check ) -from openzeppelin.introspection.ERC165 import ERC165 -from openzeppelin.security.safemath import SafeUint256 +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 import IERC721_ENUMERABLE_ID +from openzeppelin.utils.constants.library import IERC721_ENUMERABLE_ID # # Storage # @storage_var -func ERC721_Enumerable_all_tokens_len() -> (res: Uint256): +func ERC721Enumerable_all_tokens_len() -> (res: Uint256): end @storage_var -func ERC721_Enumerable_all_tokens(index: Uint256) -> (token_id: Uint256): +func ERC721Enumerable_all_tokens(index: Uint256) -> (token_id: Uint256): end @storage_var -func ERC721_Enumerable_all_tokens_index(token_id: Uint256) -> (index: Uint256): +func ERC721Enumerable_all_tokens_index(token_id: Uint256) -> (index: Uint256): end @storage_var -func ERC721_Enumerable_owned_tokens(owner: felt, index: Uint256) -> (token_id: Uint256): +func ERC721Enumerable_owned_tokens(owner: felt, index: Uint256) -> (token_id: Uint256): end @storage_var -func ERC721_Enumerable_owned_tokens_index(token_id: Uint256) -> (index: Uint256): +func ERC721Enumerable_owned_tokens_index(token_id: Uint256) -> (index: Uint256): end -namespace ERC721_Enumerable: +namespace ERC721Enumerable: # # Constructor @@ -63,7 +63,7 @@ namespace ERC721_Enumerable: pedersen_ptr: HashBuiltin*, range_check_ptr }() -> (total_supply: Uint256): - let (total_supply) = ERC721_Enumerable_all_tokens_len.read() + let (total_supply) = ERC721Enumerable_all_tokens_len.read() return (total_supply) end @@ -76,13 +76,13 @@ namespace ERC721_Enumerable: alloc_locals uint256_check(index) # Ensures index argument is less than total_supply - let (len: Uint256) = ERC721_Enumerable.total_supply() + let (len: Uint256) = ERC721Enumerable.total_supply() let (is_lt) = uint256_lt(index, len) - with_attr error_message("ERC721_Enumerable: global index out of bounds"): + with_attr error_message("ERC721Enumerable: global index out of bounds"): assert is_lt = TRUE end - let (token_id: Uint256) = ERC721_Enumerable_all_tokens.read(index) + let (token_id: Uint256) = ERC721Enumerable_all_tokens.read(index) return (token_id) end @@ -96,11 +96,11 @@ namespace ERC721_Enumerable: # 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("ERC721_Enumerable: owner index out of bounds"): + with_attr error_message("ERC721Enumerable: owner index out of bounds"): assert is_lt = TRUE end - let (token_id: Uint256) = ERC721_Enumerable_owned_tokens.read(owner, index) + let (token_id: Uint256) = ERC721Enumerable_owned_tokens.read(owner, index) return (token_id) end @@ -173,12 +173,12 @@ func _add_token_to_all_tokens_enumeration{ syscall_ptr: felt*, range_check_ptr }(token_id: Uint256): - let (supply: Uint256) = ERC721_Enumerable_all_tokens_len.read() - ERC721_Enumerable_all_tokens.write(supply, token_id) - ERC721_Enumerable_all_tokens_index.write(token_id, supply) + 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)) - ERC721_Enumerable_all_tokens_len.write(new_supply) + ERC721Enumerable_all_tokens_len.write(new_supply) return () end @@ -188,19 +188,19 @@ func _remove_token_from_all_tokens_enumeration{ range_check_ptr }(token_id: Uint256): alloc_locals - let (supply: Uint256) = ERC721_Enumerable_all_tokens_len.read() + let (supply: Uint256) = ERC721Enumerable_all_tokens_len.read() let (last_token_index: Uint256) = SafeUint256.sub_le(supply, Uint256(1, 0)) - let (token_index: Uint256) = ERC721_Enumerable_all_tokens_index.read(token_id) - let (last_token_id: Uint256) = ERC721_Enumerable_all_tokens.read(last_token_index) + let (token_index: Uint256) = ERC721Enumerable_all_tokens_index.read(token_id) + let (last_token_id: Uint256) = ERC721Enumerable_all_tokens.read(last_token_index) - ERC721_Enumerable_all_tokens.write(last_token_index, Uint256(0, 0)) - ERC721_Enumerable_all_tokens_index.write(token_id, Uint256(0, 0)) - ERC721_Enumerable_all_tokens_len.write(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: - ERC721_Enumerable_all_tokens_index.write(last_token_id, token_index) - ERC721_Enumerable_all_tokens.write(token_index, last_token_id) + ERC721Enumerable_all_tokens_index.write(last_token_id, token_index) + ERC721Enumerable_all_tokens.write(token_index, last_token_id) return () end return () @@ -213,8 +213,8 @@ func _add_token_to_owner_enumeration{ range_check_ptr }(to: felt, token_id: Uint256): let (length: Uint256) = ERC721.balance_of(to) - ERC721_Enumerable_owned_tokens.write(to, length, token_id) - ERC721_Enumerable_owned_tokens_index.write(token_id, length) + ERC721Enumerable_owned_tokens.write(to, length, token_id) + ERC721Enumerable_owned_tokens_index.write(token_id, length) return () end @@ -228,19 +228,19 @@ func _remove_token_from_owner_enumeration{ 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) = ERC721_Enumerable_owned_tokens_index.read(token_id) + 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: - ERC721_Enumerable_owned_tokens_index.write(token_id, Uint256(0, 0)) - ERC721_Enumerable_owned_tokens.write(from_, last_token_index, Uint256(0, 0)) + 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) = ERC721_Enumerable_owned_tokens.read(from_, last_token_index) - ERC721_Enumerable_owned_tokens.write(from_, token_index, last_token_id) - ERC721_Enumerable_owned_tokens_index.write(last_token_id, token_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 diff --git a/src/openzeppelin/token/erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo b/src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo similarity index 86% rename from src/openzeppelin/token/erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo rename to src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo index 9218003cb..afdafd366 100644 --- a/src/openzeppelin/token/erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo +++ b/src/openzeppelin/token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo @@ -1,15 +1,15 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/enumerable/presets/ERC721EnumerableMintableBurnable.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.uint256 import Uint256 -from openzeppelin.access.ownable import Ownable -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.access.ownable.library import Ownable +from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.token.erc721.library import ERC721 -from openzeppelin.token.erc721_enumerable.library import ERC721_Enumerable +from openzeppelin.token.erc721.enumerable.library import ERC721Enumerable # # Constructor @@ -26,7 +26,7 @@ func constructor{ owner: felt ): ERC721.initializer(name, symbol) - ERC721_Enumerable.initializer() + ERC721Enumerable.initializer() Ownable.initializer(owner) return () end @@ -41,7 +41,7 @@ func totalSupply{ syscall_ptr: felt*, range_check_ptr }() -> (totalSupply: Uint256): - let (totalSupply: Uint256) = ERC721_Enumerable.total_supply() + let (totalSupply: Uint256) = ERC721Enumerable.total_supply() return (totalSupply) end @@ -51,7 +51,7 @@ func tokenByIndex{ syscall_ptr: felt*, range_check_ptr }(index: Uint256) -> (tokenId: Uint256): - let (tokenId: Uint256) = ERC721_Enumerable.token_by_index(index) + let (tokenId: Uint256) = ERC721Enumerable.token_by_index(index) return (tokenId) end @@ -61,7 +61,7 @@ func tokenOfOwnerByIndex{ syscall_ptr: felt*, range_check_ptr }(owner: felt, index: Uint256) -> (tokenId: Uint256): - let (tokenId: Uint256) = ERC721_Enumerable.token_of_owner_by_index(owner, index) + let (tokenId: Uint256) = ERC721Enumerable.token_of_owner_by_index(owner, index) return (tokenId) end @@ -189,7 +189,7 @@ func transferFrom{ to: felt, tokenId: Uint256 ): - ERC721_Enumerable.transfer_from(from_, to, tokenId) + ERC721Enumerable.transfer_from(from_, to, tokenId) return () end @@ -205,7 +205,7 @@ func safeTransferFrom{ data_len: felt, data: felt* ): - ERC721_Enumerable.safe_transfer_from(from_, to, tokenId, data_len, data) + ERC721Enumerable.safe_transfer_from(from_, to, tokenId, data_len, data) return () end @@ -216,7 +216,7 @@ func mint{ range_check_ptr }(to: felt, tokenId: Uint256): Ownable.assert_only_owner() - ERC721_Enumerable._mint(to, tokenId) + ERC721Enumerable._mint(to, tokenId) return () end @@ -227,7 +227,7 @@ func burn{ range_check_ptr }(tokenId: Uint256): ERC721.assert_only_token_owner(tokenId) - ERC721_Enumerable._burn(tokenId) + ERC721Enumerable._burn(tokenId) return () end diff --git a/src/openzeppelin/token/erc721/utils/ERC721_Holder.cairo b/src/openzeppelin/token/erc721/enumerable/presets/utils/ERC721Holder.cairo similarity index 78% rename from src/openzeppelin/token/erc721/utils/ERC721_Holder.cairo rename to src/openzeppelin/token/erc721/enumerable/presets/utils/ERC721Holder.cairo index 9524f7fb3..711c92d2b 100644 --- a/src/openzeppelin/token/erc721/utils/ERC721_Holder.cairo +++ b/src/openzeppelin/token/erc721/enumerable/presets/utils/ERC721Holder.cairo @@ -1,14 +1,14 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/utils/ERC721_Holder.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/presets/utils/ERC721Holder.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.uint256 import Uint256 -from openzeppelin.utils.constants import IERC721_RECEIVER_ID +from openzeppelin.utils.constants.library import IERC721_RECEIVER_ID -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.introspection.erc165.library import ERC165 @view func onERC721Received( diff --git a/src/openzeppelin/token/erc721/library.cairo b/src/openzeppelin/token/erc721/library.cairo index 1dc352779..685754ff1 100644 --- a/src/openzeppelin/token/erc721/library.cairo +++ b/src/openzeppelin/token/erc721/library.cairo @@ -9,11 +9,11 @@ from starkware.cairo.common.math import assert_not_zero, assert_not_equal from starkware.cairo.common.bool import TRUE, FALSE from starkware.cairo.common.uint256 import Uint256, uint256_check -from openzeppelin.introspection.ERC165 import ERC165 -from openzeppelin.introspection.IERC165 import IERC165 -from openzeppelin.security.safemath import SafeUint256 -from openzeppelin.token.erc721.interfaces.IERC721_Receiver import IERC721_Receiver -from openzeppelin.utils.constants import ( +from openzeppelin.introspection.erc165.library import ERC165 +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 ) @@ -542,7 +542,7 @@ func _check_onERC721Received{ let (caller) = get_caller_address() let (is_supported) = IERC165.supportsInterface(to, IERC721_RECEIVER_ID) if is_supported == TRUE: - let (selector) = IERC721_Receiver.onERC721Received( + let (selector) = IERC721Receiver.onERC721Received( to, caller, from_, diff --git a/src/openzeppelin/token/erc721/ERC721_Mintable_Burnable.cairo b/src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo similarity index 95% rename from src/openzeppelin/token/erc721/ERC721_Mintable_Burnable.cairo rename to src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo index 49fea3a8a..a0d44f6f0 100644 --- a/src/openzeppelin/token/erc721/ERC721_Mintable_Burnable.cairo +++ b/src/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo @@ -1,13 +1,13 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/ERC721_Mintable_Burnable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/presets/ERC721MintableBurnable.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.uint256 import Uint256 -from openzeppelin.access.ownable import Ownable -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.access.ownable.library import Ownable +from openzeppelin.introspection.erc165.library import ERC165 from openzeppelin.token.erc721.library import ERC721 # diff --git a/src/openzeppelin/token/erc721/ERC721_Mintable_Pausable.cairo b/src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo similarity index 95% rename from src/openzeppelin/token/erc721/ERC721_Mintable_Pausable.cairo rename to src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo index 170695de1..39a0084fd 100644 --- a/src/openzeppelin/token/erc721/ERC721_Mintable_Pausable.cairo +++ b/src/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo @@ -1,14 +1,14 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/ERC721_Mintable_Pausable.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (token/erc721/presets/ERC721MintablePausable.cairo) %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.uint256 import Uint256 -from openzeppelin.access.ownable import Ownable -from openzeppelin.introspection.ERC165 import ERC165 -from openzeppelin.security.pausable import Pausable +from openzeppelin.access.ownable.library import Ownable +from openzeppelin.introspection.erc165.library import ERC165 +from openzeppelin.security.pausable.library import Pausable from openzeppelin.token.erc721.library import ERC721 # diff --git a/src/openzeppelin/upgrades/Proxy.cairo b/src/openzeppelin/upgrades/presets/Proxy.cairo similarity index 95% rename from src/openzeppelin/upgrades/Proxy.cairo rename to src/openzeppelin/upgrades/presets/Proxy.cairo index e38d858b9..9007f5960 100644 --- a/src/openzeppelin/upgrades/Proxy.cairo +++ b/src/openzeppelin/upgrades/presets/Proxy.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (upgrades/Proxy.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (upgrades/presets/Proxy.cairo) %lang starknet diff --git a/src/openzeppelin/utils/constants.cairo b/src/openzeppelin/utils/constants/library.cairo similarity index 86% rename from src/openzeppelin/utils/constants.cairo rename to src/openzeppelin/utils/constants/library.cairo index e1e2fde1c..234577f96 100644 --- a/src/openzeppelin/utils/constants.cairo +++ b/src/openzeppelin/utils/constants/library.cairo @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# OpenZeppelin Contracts for Cairo v0.2.1 (utils/constants.cairo) +# OpenZeppelin Contracts for Cairo v0.2.1 (utils/constants/library.cairo) %lang starknet diff --git a/tests/account/test_Account.py b/tests/account/test_Account.py index d5d0fe8cb..6dfd29aa0 100644 --- a/tests/account/test_Account.py +++ b/tests/account/test_Account.py @@ -14,7 +14,7 @@ def contract_classes(): account_cls = get_contract_class('Account') init_cls = get_contract_class("Initializable") - attacker_cls = get_contract_class("account_reentrancy") + attacker_cls = get_contract_class("AccountReentrancy") return account_cls, init_cls, attacker_cls diff --git a/tests/account/test_EthAccount.py b/tests/account/test_EthAccount.py index 3b2439f2f..09bfdfe7c 100644 --- a/tests/account/test_EthAccount.py +++ b/tests/account/test_EthAccount.py @@ -16,7 +16,7 @@ def contract_defs(): account_cls = get_contract_class('EthAccount') init_cls = get_contract_class("Initializable") - attacker_cls = get_contract_class("account_reentrancy") + attacker_cls = get_contract_class("AccountReentrancy") return account_cls, init_cls, attacker_cls diff --git a/tests/mocks/AccessControl.cairo b/tests/mocks/AccessControl.cairo index 6401e1a75..c1f8cd0bf 100644 --- a/tests/mocks/AccessControl.cairo +++ b/tests/mocks/AccessControl.cairo @@ -4,9 +4,9 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.starknet.common.syscalls import get_caller_address -from openzeppelin.access.accesscontrol import AccessControl -from openzeppelin.introspection.ERC165 import ERC165 -from openzeppelin.utils.constants import DEFAULT_ADMIN_ROLE +from openzeppelin.access.accesscontrol.library import AccessControl +from openzeppelin.introspection.erc165.library import ERC165 +from openzeppelin.utils.constants.library import DEFAULT_ADMIN_ROLE @constructor func constructor{ diff --git a/tests/mocks/account_reentrancy.cairo b/tests/mocks/AccountReentrancy.cairo similarity index 97% rename from tests/mocks/account_reentrancy.cairo rename to tests/mocks/AccountReentrancy.cairo index 26b99cd1e..7efaab6fd 100644 --- a/tests/mocks/account_reentrancy.cairo +++ b/tests/mocks/AccountReentrancy.cairo @@ -1,3 +1,5 @@ +# SPDX-License-Identifier: MIT + %lang starknet from starkware.starknet.common.syscalls import call_contract, get_caller_address, get_tx_info, get_contract_address diff --git a/tests/mocks/ERC165.cairo b/tests/mocks/ERC165.cairo index d46e3c6aa..38a231787 100644 --- a/tests/mocks/ERC165.cairo +++ b/tests/mocks/ERC165.cairo @@ -4,7 +4,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin -from openzeppelin.introspection.ERC165 import ERC165 +from openzeppelin.introspection.erc165.library import ERC165 @view func supportsInterface{ diff --git a/tests/mocks/ERC20_Burnable_mock.cairo b/tests/mocks/ERC20BurnableMock.cairo similarity index 100% rename from tests/mocks/ERC20_Burnable_mock.cairo rename to tests/mocks/ERC20BurnableMock.cairo diff --git a/tests/mocks/ERC721_SafeMintable_mock.cairo b/tests/mocks/ERC721SafeMintableMock.cairo similarity index 96% rename from tests/mocks/ERC721_SafeMintable_mock.cairo rename to tests/mocks/ERC721SafeMintableMock.cairo index d36ea911d..d22978619 100644 --- a/tests/mocks/ERC721_SafeMintable_mock.cairo +++ b/tests/mocks/ERC721SafeMintableMock.cairo @@ -1,12 +1,13 @@ +# SPDX-License-Identifier: MIT + %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin from starkware.cairo.common.uint256 import Uint256 from openzeppelin.token.erc721.library import ERC721 -from openzeppelin.introspection.ERC165 import ERC165 - -from openzeppelin.access.ownable import Ownable +from openzeppelin.introspection.erc165.library import ERC165 +from openzeppelin.access.ownable.library import Ownable # # Constructor diff --git a/tests/mocks/Initializable.cairo b/tests/mocks/Initializable.cairo index f3dedf1b8..eb1aa8fd8 100644 --- a/tests/mocks/Initializable.cairo +++ b/tests/mocks/Initializable.cairo @@ -4,7 +4,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin -from openzeppelin.security.initializable import Initializable +from openzeppelin.security.initializable.library import Initializable @view diff --git a/tests/mocks/Ownable.cairo b/tests/mocks/Ownable.cairo index 9d45c2c71..bd4a57efb 100644 --- a/tests/mocks/Ownable.cairo +++ b/tests/mocks/Ownable.cairo @@ -4,7 +4,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.starknet.common.syscalls import get_caller_address -from openzeppelin.access.ownable import Ownable +from openzeppelin.access.ownable.library import Ownable from starkware.cairo.common.bool import TRUE @constructor diff --git a/tests/mocks/Pausable.cairo b/tests/mocks/Pausable.cairo index b353bc7e1..1266d4e08 100644 --- a/tests/mocks/Pausable.cairo +++ b/tests/mocks/Pausable.cairo @@ -6,7 +6,7 @@ from starkware.starknet.common.syscalls import get_caller_address from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.bool import TRUE, FALSE -from openzeppelin.security.pausable import Pausable +from openzeppelin.security.pausable.library import Pausable @storage_var func drastic_measure_taken() -> (res: felt): diff --git a/tests/mocks/proxiable_implementation.cairo b/tests/mocks/ProxiableImplementation.cairo similarity index 100% rename from tests/mocks/proxiable_implementation.cairo rename to tests/mocks/ProxiableImplementation.cairo diff --git a/tests/mocks/reentrancy_attacker_mock.cairo b/tests/mocks/ReentrancyAttackerMock.cairo similarity index 100% rename from tests/mocks/reentrancy_attacker_mock.cairo rename to tests/mocks/ReentrancyAttackerMock.cairo diff --git a/tests/mocks/reentrancy_mock.cairo b/tests/mocks/ReentrancyMock.cairo similarity index 97% rename from tests/mocks/reentrancy_mock.cairo rename to tests/mocks/ReentrancyMock.cairo index 8aa7fc2fd..45df9faf0 100644 --- a/tests/mocks/reentrancy_mock.cairo +++ b/tests/mocks/ReentrancyMock.cairo @@ -7,7 +7,7 @@ from starkware.cairo.common.math_cmp import is_le from starkware.cairo.common.bool import TRUE from starkware.starknet.common.syscalls import get_contract_address -from openzeppelin.security.reentrancyguard import ReentrancyGuard +from openzeppelin.security.reentrancyguard.library import ReentrancyGuard @contract_interface namespace IReentrancyGuardAttacker: diff --git a/tests/mocks/safemath_mock.cairo b/tests/mocks/SafeMathMock.cairo similarity index 95% rename from tests/mocks/safemath_mock.cairo rename to tests/mocks/SafeMathMock.cairo index 0384e9303..b362acbe1 100644 --- a/tests/mocks/safemath_mock.cairo +++ b/tests/mocks/SafeMathMock.cairo @@ -5,7 +5,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin from starkware.cairo.common.uint256 import Uint256 -from openzeppelin.security.safemath import SafeUint256 +from openzeppelin.security.safemath.library import SafeUint256 @view func uint256_add{ diff --git a/tests/mocks/upgrades_v1_mock.cairo b/tests/mocks/UpgradesMockV1.cairo similarity index 100% rename from tests/mocks/upgrades_v1_mock.cairo rename to tests/mocks/UpgradesMockV1.cairo diff --git a/tests/mocks/upgrades_v2_mock.cairo b/tests/mocks/UpgradesMockV2.cairo similarity index 100% rename from tests/mocks/upgrades_v2_mock.cairo rename to tests/mocks/UpgradesMockV2.cairo diff --git a/tests/security/test_reentrancy.py b/tests/security/test_reentrancy.py index c9e2972fc..e826a68dc 100644 --- a/tests/security/test_reentrancy.py +++ b/tests/security/test_reentrancy.py @@ -11,7 +11,7 @@ async def reentrancy_mock(): starknet = await Starknet.empty() contract = await starknet.deploy( - contract_class=get_contract_class("reentrancy_mock"), + contract_class=get_contract_class("ReentrancyMock"), constructor_calldata=[INITIAL_COUNTER] ) @@ -29,7 +29,7 @@ async def test_reentrancy_guard_deploy(reentrancy_mock): @pytest.mark.asyncio async def test_reentrancy_guard_remote_callback(reentrancy_mock): contract, starknet = reentrancy_mock - attacker = await starknet.deploy("tests/mocks/reentrancy_attacker_mock.cairo") + attacker = await starknet.deploy("tests/mocks/ReentrancyAttackerMock.cairo") # should not allow remote callback await assert_revert( contract.count_and_call(attacker.contract_address).invoke(), diff --git a/tests/security/test_safemath.py b/tests/security/test_safemath.py index 221c24e41..6bf499ac5 100644 --- a/tests/security/test_safemath.py +++ b/tests/security/test_safemath.py @@ -11,7 +11,7 @@ async def safemath_mock(): starknet = await Starknet.empty() safemath = await starknet.deploy( - contract_class=get_contract_class("safemath_mock") + contract_class=get_contract_class("SafeMathMock") ) return safemath diff --git a/tests/signers.py b/tests/signers.py index a6a605ead..c4c670eb0 100644 --- a/tests/signers.py +++ b/tests/signers.py @@ -31,12 +31,12 @@ class MockSigner(): (contract_address, 'another_method', [arg_1, arg_2]) ] ) - + """ def __init__(self, private_key): self.signer = Signer(private_key) self.public_key = self.signer.public_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) @@ -60,10 +60,10 @@ class MockEthSigner(): Parameters ---------- private_key : int - + """ def __init__(self, private_key): - self.signer = eth_keys.keys.PrivateKey(private_key) + self.signer = eth_keys.keys.PrivateKey(private_key) self.eth_address = int(self.signer.public_key.to_checksum_address(),0) async def send_transaction(self, account, to, selector_name, calldata, nonce=None, max_fee=0): @@ -84,8 +84,8 @@ async def send_transactions(self, account, calls, nonce=None, max_fee=0): message_hash = get_transaction_hash( account.contract_address, call_array, calldata, nonce, max_fee ) - - signature = self.signer.sign_msg_hash((message_hash).to_bytes(32, byteorder="big")) + + signature = self.signer.sign_msg_hash((message_hash).to_bytes(32, byteorder="big")) sig_r = to_uint(signature.r) sig_s = to_uint(signature.s) diff --git a/tests/token/erc20/test_ERC20.py b/tests/token/erc20/test_ERC20.py index 958fae49b..762b9b7f9 100644 --- a/tests/token/erc20/test_ERC20.py +++ b/tests/token/erc20/test_ERC20.py @@ -97,7 +97,7 @@ async def test_constructor_exceed_max_decimals(erc20_factory): starknet = await Starknet.empty() await assert_revert( starknet.deploy( - contract_path("openzeppelin/token/erc20/ERC20.cairo"), + contract_path("openzeppelin/token/erc20/presets/ERC20.cairo"), constructor_calldata=[ NAME, SYMBOL, diff --git a/tests/token/erc20/test_ERC20_Burnable_mock.py b/tests/token/erc20/test_ERC20BurnableMock.py similarity index 98% rename from tests/token/erc20/test_ERC20_Burnable_mock.py rename to tests/token/erc20/test_ERC20BurnableMock.py index 78164625c..acfa72506 100644 --- a/tests/token/erc20/test_ERC20_Burnable_mock.py +++ b/tests/token/erc20/test_ERC20BurnableMock.py @@ -21,7 +21,7 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - erc20_cls = get_contract_class('ERC20_Burnable_mock') + erc20_cls = get_contract_class('ERC20BurnableMock') return account_cls, erc20_cls diff --git a/tests/token/erc20/test_ERC20_Mintable.py b/tests/token/erc20/test_ERC20Mintable.py similarity index 98% rename from tests/token/erc20/test_ERC20_Mintable.py rename to tests/token/erc20/test_ERC20Mintable.py index 9a364b7eb..5c699f388 100644 --- a/tests/token/erc20/test_ERC20_Mintable.py +++ b/tests/token/erc20/test_ERC20Mintable.py @@ -23,7 +23,7 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - erc20_cls = get_contract_class('ERC20_Mintable') + erc20_cls = get_contract_class('ERC20Mintable') return account_cls, erc20_cls diff --git a/tests/token/erc20/test_ERC20_Pausable.py b/tests/token/erc20/test_ERC20Pausable.py similarity index 99% rename from tests/token/erc20/test_ERC20_Pausable.py rename to tests/token/erc20/test_ERC20Pausable.py index fa92b2d9f..4d3be2b4b 100644 --- a/tests/token/erc20/test_ERC20_Pausable.py +++ b/tests/token/erc20/test_ERC20Pausable.py @@ -20,7 +20,7 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - erc20_cls = get_contract_class('ERC20_Pausable') + erc20_cls = get_contract_class('ERC20Pausable') return account_cls, erc20_cls diff --git a/tests/token/erc20/test_ERC20_Upgradeable.py b/tests/token/erc20/test_ERC20Upgradeable.py similarity index 98% rename from tests/token/erc20/test_ERC20_Upgradeable.py rename to tests/token/erc20/test_ERC20Upgradeable.py index 21b40e89e..f67716173 100644 --- a/tests/token/erc20/test_ERC20_Upgradeable.py +++ b/tests/token/erc20/test_ERC20Upgradeable.py @@ -21,7 +21,7 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - token_cls = get_contract_class('ERC20_Upgradeable') + token_cls = get_contract_class('ERC20Upgradeable') proxy_cls = get_contract_class('Proxy') return account_cls, token_cls, proxy_cls diff --git a/tests/token/erc721_enumerable/test_ERC721_Enumerable_Mintable_Burnable.py b/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py similarity index 94% rename from tests/token/erc721_enumerable/test_ERC721_Enumerable_Mintable_Burnable.py rename to tests/token/erc721/test_ERC721EnumerableMintableBurnable.py index ff7633ad7..c20c75aaf 100644 --- a/tests/token/erc721_enumerable/test_ERC721_Enumerable_Mintable_Burnable.py +++ b/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py @@ -26,7 +26,7 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - erc721_cls = get_contract_class('ERC721_Enumerable_Mintable_Burnable') + erc721_cls = get_contract_class('ERC721EnumerableMintableBurnable') return account_cls, erc721_cls @@ -134,7 +134,7 @@ async def test_tokenOfOwnerByIndex_greater_than_supply(erc721_minted): await assert_revert( erc721.tokenOfOwnerByIndex( account.contract_address, tokens_plus_one).invoke(), - reverted_with="ERC721_Enumerable: owner index out of bounds" + reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -144,7 +144,7 @@ async def test_tokenOfOwnerByIndex_owner_with_no_tokens(erc721_minted): await assert_revert( erc721.tokenOfOwnerByIndex(RECIPIENT, to_uint(1)).invoke(), - reverted_with="ERC721_Enumerable: owner index out of bounds" + reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -176,7 +176,7 @@ async def test_tokenOfOwnerByIndex_transfer_all_tokens(erc721_minted): # than the target's balance await assert_revert(erc721.tokenOfOwnerByIndex( account.contract_address, to_uint(0)).invoke(), - reverted_with="ERC721_Enumerable: owner index out of bounds" + reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -210,7 +210,7 @@ async def test_tokenOfOwnerByIndex_safe_transfer_all_tokens(erc721_minted): # than the target's balance await assert_revert(erc721.tokenOfOwnerByIndex( account.contract_address, to_uint(0)).invoke(), - reverted_with="ERC721_Enumerable: owner index out of bounds" + reverted_with="ERC721Enumerable: owner index out of bounds" ) @@ -234,7 +234,7 @@ async def test_tokenByIndex_greater_than_supply(erc721_minted): await assert_revert( erc721.tokenByIndex(to_uint(5)).invoke(), - reverted_with="ERC721_Enumerable: global index out of bounds" + reverted_with="ERC721Enumerable: global index out of bounds" ) @@ -259,7 +259,7 @@ async def test_tokenByIndex_burn_last_token(erc721_minted): await assert_revert( erc721.tokenByIndex(tokens_minus_one).invoke(), - reverted_with="ERC721_Enumerable: global index out of bounds" + reverted_with="ERC721Enumerable: global index out of bounds" ) @@ -296,7 +296,7 @@ async def test_tokenByIndex_burn_and_mint(erc721_minted): await assert_revert( erc721.tokenByIndex(to_uint(0)).invoke(), - reverted_with="ERC721_Enumerable: global index out of bounds" + reverted_with="ERC721Enumerable: global index out of bounds" ) # mint new tokens diff --git a/tests/token/erc721/test_ERC721_Mintable_Burnable.py b/tests/token/erc721/test_ERC721MintableBurnable.py similarity index 99% rename from tests/token/erc721/test_ERC721_Mintable_Burnable.py rename to tests/token/erc721/test_ERC721MintableBurnable.py index e93f41223..beebb23b4 100644 --- a/tests/token/erc721/test_ERC721_Mintable_Burnable.py +++ b/tests/token/erc721/test_ERC721MintableBurnable.py @@ -33,8 +33,8 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - erc721_cls = get_contract_class('ERC721_Mintable_Burnable') - erc721_holder_cls = get_contract_class('ERC721_Holder') + erc721_cls = get_contract_class('ERC721MintableBurnable') + erc721_holder_cls = get_contract_class('ERC721Holder') unsupported_cls = get_contract_class('Initializable') return account_cls, erc721_cls, erc721_holder_cls, unsupported_cls diff --git a/tests/token/erc721/test_ERC721_Mintable_Pausable.py b/tests/token/erc721/test_ERC721MintablePausable.py similarity index 98% rename from tests/token/erc721/test_ERC721_Mintable_Pausable.py rename to tests/token/erc721/test_ERC721MintablePausable.py index 88394c3c7..792f6dbe5 100644 --- a/tests/token/erc721/test_ERC721_Mintable_Pausable.py +++ b/tests/token/erc721/test_ERC721MintablePausable.py @@ -19,8 +19,8 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - erc721_cls = get_contract_class('ERC721_Mintable_Pausable') - erc721_holder_cls = get_contract_class('ERC721_Holder') + erc721_cls = get_contract_class('ERC721MintablePausable') + erc721_holder_cls = get_contract_class('ERC721Holder') return account_cls, erc721_cls, erc721_holder_cls diff --git a/tests/token/erc721/test_ERC721_SafeMintable_mock.py b/tests/token/erc721/test_ERC721SafeMintableMock.py similarity index 98% rename from tests/token/erc721/test_ERC721_SafeMintable_mock.py rename to tests/token/erc721/test_ERC721SafeMintableMock.py index 69660e998..093013a7f 100644 --- a/tests/token/erc721/test_ERC721_SafeMintable_mock.py +++ b/tests/token/erc721/test_ERC721SafeMintableMock.py @@ -18,8 +18,8 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - erc721_cls = get_contract_class('ERC721_SafeMintable_mock') - erc721_holder_cls = get_contract_class('ERC721_Holder') + erc721_cls = get_contract_class('ERC721SafeMintableMock') + erc721_holder_cls = get_contract_class('ERC721Holder') unsupported_cls = get_contract_class('Initializable') return account_cls, erc721_cls, erc721_holder_cls, unsupported_cls diff --git a/tests/upgrades/test_Proxy.py b/tests/upgrades/test_Proxy.py index 8a0a58aa0..a78fe9135 100644 --- a/tests/upgrades/test_Proxy.py +++ b/tests/upgrades/test_Proxy.py @@ -18,7 +18,7 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - implementation_cls = get_contract_class('proxiable_implementation') + implementation_cls = get_contract_class('ProxiableImplementation') proxy_cls = get_contract_class('Proxy') return account_cls, implementation_cls, proxy_cls diff --git a/tests/upgrades/test_upgrades.py b/tests/upgrades/test_upgrades.py index 816b1d627..1d75dfa56 100644 --- a/tests/upgrades/test_upgrades.py +++ b/tests/upgrades/test_upgrades.py @@ -20,8 +20,8 @@ @pytest.fixture(scope='module') def contract_classes(): account_cls = get_contract_class('Account') - v1_cls = get_contract_class('upgrades_v1_mock') - v2_cls = get_contract_class('upgrades_v2_mock') + v1_cls = get_contract_class('UpgradesMockV1') + v2_cls = get_contract_class('UpgradesMockV2') proxy_cls = get_contract_class('Proxy') return account_cls, v1_cls, v2_cls, proxy_cls diff --git a/tests/utils.py b/tests/utils.py index 1be6546d7..1db2061f1 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -99,7 +99,7 @@ async def assert_revert(fun, reverted_with=None): async def assert_revert_entry_point(fun, invalid_selector): selector_hex = hex(get_selector_from_name(invalid_selector)) entry_point_msg = f"Entry point {selector_hex} not found in contract" - + await assert_revert(fun, entry_point_msg)