Package: openzeppelin-cairo-contracts
OpenZeppelin Contracts for CairoA library for secure smart contract developmentwritten in Cairo forStarkNet, a decentralized ZK Rollup.Usage⚠️ WARNING! ⚠️This repo contains highly experimental code.
Expect rapid iteration.Use at your own risk.First time?Before installing Cairo on your machine, you need to installgmp:sudoaptinstall-ylibgmp3-dev# linuxbrewinstallgmp# macIf you have any troubles installing gmp on your Apple M1 computer,here’s a list of potential solutions.Set up your projectCreate a directory for your project, thencdinto it and create a Python virtual environment.mkdirmy-projectcdmy-project
python3-mvenvenvsourceenv/bin/activateInstall theNiledevelopment environment and then runinitto kickstart a new project. Nile will create the project directory structure and installthe Cairo language, alocal network, and atesting framework.pipinstallcairo-nile
nileinitInstall the librarypipinstallopenzeppelin-cairo-contracts⚠️ Warning! ⚠️Installing directly themainbranch may contain incomplete or breaking implementations, downloadofficial releasesonly.Use a basic presetPresets are ready-to-use contracts that you can deploy right away. They also serve as examples of how to use library modules.Read more about presets.// contracts/MyToken.cairo

%lang starknet

from openzeppelin.token.erc20.presets.ERC20 import (
    constructor,
    name,
    symbol,
    totalSupply,
    decimals,
    balanceOf,
    allowance,
    transfer,
    transferFrom,
    approve,
    increaseAllowance,
    decreaseAllowance
)Compile and deploy it right away:nilecompile

niledeployMyToken<name><symbol><decimals><initial_supply><recipient>--aliasmy_tokenNote that<initial_supply>is expected to be two integers i.e.10. SeeUint256for more information.Write a custom contract using library modulesRead more about libraries.%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.uint256 import Uint256
from openzeppelin.security.pausable.library import Pausable
from openzeppelin.token.erc20.library import ERC20

(...)

@external
func transfer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
    recipient: felt, amount: Uint256
) -> (success: felt) {
    Pausable.assert_not_paused();
    return ERC20.transfer(recipient, amount);
}LearnDocumentationCheck out thefull documentation site! Featuring:AccountsERC20ERC721ERC1155Contract extensibility patternProxies and upgradesSecurityUtilitiesCairoStarkNet official documentationCairo language documentationPerama'sCairo by exampleCairo 101 workshopsNileGetting started with StarkNet using NileHow to manage smart contract deployments with NileDevelopmentSet up the projectClone the repositorygitclonegit@github.com:OpenZeppelin/cairo-contracts.gitcdinto it and create a Python virtual environment:cdcairo-contracts
python3-mvenvenvsourceenv/bin/activateInstall dependencies:python-mpipinstall.Compile the contractsnilecompile--directorysrc

🤖CompilingallCairocontractsinthesrcdirectory
🔨Compilingsrc/openzeppelin/token/erc20/library.cairo
🔨Compilingsrc/openzeppelin/token/erc20/presets/ERC20Mintable.cairo
🔨Compilingsrc/openzeppelin/token/erc20/presets/ERC20Pausable.cairo
🔨Compilingsrc/openzeppelin/token/erc20/presets/ERC20Upgradeable.cairo
🔨Compilingsrc/openzeppelin/token/erc20/presets/ERC20.cairo
🔨Compilingsrc/openzeppelin/token/erc20/IERC20.cairo
🔨Compilingsrc/openzeppelin/token/erc721/enumerable/library.cairo
🔨Compilingsrc/openzeppelin/token/erc721/library.cairo
🔨Compilingsrc/openzeppelin/token/erc721/utils/ERC721Holder.cairo
🔨Compilingsrc/openzeppelin/token/erc721/presets/ERC721MintablePausable.cairo
🔨Compilingsrc/openzeppelin/token/erc721/presets/ERC721MintableBurnable.cairo
🔨Compilingsrc/openzeppelin/token/erc721/presets/ERC721EnumerableMintableBurnable.cairo
🔨Compilingsrc/openzeppelin/token/erc721/IERC721.cairo
🔨Compilingsrc/openzeppelin/token/erc721/IERC721Metadata.cairo
🔨Compilingsrc/openzeppelin/token/erc721/IERC721Receiver.cairo
🔨Compilingsrc/openzeppelin/token/erc721/enumerable/IERC721Enumerable.cairo
🔨Compilingsrc/openzeppelin/access/ownable/library.cairo
🔨Compilingsrc/openzeppelin/security/reentrancyguard/library.cairo
🔨Compilingsrc/openzeppelin/security/safemath/library.cairo
🔨Compilingsrc/openzeppelin/security/pausable/library.cairo
🔨Compilingsrc/openzeppelin/security/initializable/library.cairo
🔨Compilingsrc/openzeppelin/utils/constants/library.cairo
🔨Compilingsrc/openzeppelin/introspection/erc165/library.cairo
🔨Compilingsrc/openzeppelin/introspection/erc165/IERC165.cairo
🔨Compilingsrc/openzeppelin/upgrades/library.cairo
🔨Compilingsrc/openzeppelin/upgrades/presets/Proxy.cairo
🔨Compilingsrc/openzeppelin/account/library.cairo
🔨Compilingsrc/openzeppelin/account/presets/EthAccount.cairo
🔨Compilingsrc/openzeppelin/account/presets/Account.cairo
🔨Compilingsrc/openzeppelin/account/presets/AddressRegistry.cairo
🔨Compilingsrc/openzeppelin/account/IAccount.cairo
✅DoneRun testsRun tests usingtox, tox automatically creates an isolated testing environment:tox======================testsessionstarts======================platformlinux--Python3.7.2,pytest-7.1.2,py-1.11.0,pluggy-1.0.0
rootdir:/home/readme/cairo-contracts,configfile:tox.ini
plugins:asyncio-0.18.3,xdist-2.5.0,forked-1.4.0,web3-5.29.0,typeguard-2.13.3
asyncio:mode=auto
gw0[185]/gw1[185]......................................................................................
......................................................................................
............[100%]Run Tests in DockerFor M1 users or those who are having trouble with library/python versions you can alternatively run the tests within a docker container. Using the following as a Dockerfile placed in the root directory of the project:FROMpython:3.7RUNpipinstalltoxRUNmkdircairo-contractsCOPY.cairo-contractsWORKDIRcairo-contractsENTRYPOINTtoxAfter its placed there run:dockerbuild-tcairo-tests.
dockerruncairo-testsParallel TestingThis repo utilizes thepytest-xdistplugin which runs tests in parallel. This feature increases testing speed; however, conflicts with a shared state can occur since tests do not run in order. To overcome this, independent cached versions of contracts being tested should be provisioned to each test case. Here's a simple fixture example:fromutilsimportget_contract_class,cached_contract@pytest.fixturedeffoo_factory():# get contract classfoo_cls=get_contract_class('Foo')# deploy contractstarknet=awaitStarknet.empty()foo=awaitstarknet.deploy(contract_class=foo_cls)# copy the state and cache contractstate=starknet.state.copy()cached_foo=cached_contract(state,foo_cls,foo)returncached_fooSeeMemoizationin the Utilities documentation for a more thorough example on caching contracts.Note that this does not apply for stateless libraries such as SafeMath.Security⚠️ Warning! ⚠️
This project is still in a very early and experimental phase. It has never been audited nor thoroughly reviewed for security vulnerabilities. Do not use in production.Refer toSECURITY.mdfor more details.ContributeOpenZeppelin Contracts for Cairo exists thanks to its contributors. There are many ways you can participate and help build high quality software. Check out thecontributionguide!Markdown linterTo keep the markdown files neat and easy to edit, we utilize DavidAnson'smarkdownlintlinter. You can find the listed ruleshere. Note that the following rules are disabled:MD013: line lengthto enable paragraphs without internal line breaksMD033: inline HTMLto enable .md files to have duplicate headers and separate them by identifiersBefore creating a PR, check that documentation changes are compliant with our markdown rules by running:tox-elintLicenseOpenZeppelin Contracts for Cairo is released under theMIT License.