Logo

Tutorial OpenZeppelin Contracts for Cairo

Simple to understand and test creating contracts via Zeppelin

WARNING THAT IT'S BETTER TO USE A NEW WALLET IF YOU WANT TO TRY AND LEARN

OpenZeppelin for Cairo

A library for secure smart contract development written in Cairo for Starknet, a decentralized ZK Rollup.

This repo contains highly experimental code. It has no code coverage checks. It hasn't been audited. Use at your own risk.

Expect rapid iteration. Some contracts or features are not ready to be deployed. Check the Unsupported section below.

Creating a Package with Scarb

This command will generate a new package directory named my_package with the following structure:

Step 1: Set up your project

Create a new project and cd into it.

                    
scarb new my_project && cd my_project
                    
                

The contents of my_project should look like this:

                    
ls
                    
                

Scarb.toml(file) src(directory)

zeppelin

Step 2: Install the library

Edit scarb.toml and add:

                    
nano /home/users/my_project/Scarb.toml
                    
                

Edit scarb.toml and add:

                    
[dependencies]
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.8.0-beta.0" }
                    
                

Step 3: Build the project to download it:

                    
scarb build
                    
                

Expected output:

                    
Updating git repository https://github.com/OpenZeppelin/cairo-contracts
Compiling my_project v0.1.0 (~/my_project/Scarb.toml)
Finished release target(s) in 6 seconds
                    
                

Step 4: Using the library

Open src/lib.cairo and write your contract. For example, this is how to write an ERC20-compliant contract:

                    
#[starknet::contract]
mod MyToken {
    use openzeppelin::token::erc20::ERC20Component;
    use starknet::ContractAddress;

    component!(path: ERC20Component, storage: erc20, event: ERC20Event);

    #[abi(embed_v0)]
    impl ERC20Impl = ERC20Component::ERC20Impl;
    #[abi(embed_v0)]
    impl ERC20MetadataImpl = ERC20Component::ERC20MetadataImpl;
    impl ERC20InternalImpl = ERC20Component::InternalImpl;

    #[storage]
    struct Storage {
        #[substorage(v0)]
        erc20: ERC20Component::Storage
    }

    #[event]
    #[derive(Drop, starknet::Event)]
    enum Event {
        #[flat]
        ERC20Event: ERC20Component::Event
    }

    #[constructor]
    fn constructor(
        ref self: ContractState,
        initial_supply: u256,
        recipient: ContractAddress
    ) {
        let name = 'MyToken';
        let symbol = 'MTK';

        self.erc20.initializer(name, symbol);
        self.erc20._mint(recipient, initial_supply);
    }
}
                    
                

Step 4: For the next step, you can go to this and continue with the step to deploy the contract

Deploy Testnet Contract

Contributing to OpenZeppelin Contracts for Cairo

Contribution to the OpenZeppelin Contract for Cairo. Please take 5' time to review the items listed below to ensure that your contribution is combined as soon as possible. As a contributor, you are expected to fork this repository, work on your own fork, and then submit a pull request. Withdraw requests will be reviewed and eventually merged into the main repo. See "Fork-a-Repo" to see how it works.

Development

OpenZeppelin Contracts for Cairo exists thanks to its contributors. There are many ways you can participate and help build high-quality software, make sure to check out the contribution guide in advance.

Set up the project

Clone the repository:

                    
git clone git@github.com:OpenZeppelin/cairo-contracts.git
                    
                

cd into it and build:

                    
cd cairo-contracts
scarb build
                    
                

Expected output:

                    
Compiling lib(openzeppelin) openzeppelin v0.8.0-beta.0 (~/cairo-contracts/Scarb.toml)
Compiling starknet-contract(openzeppelin) openzeppelin v0.8.0-beta.0 (~/cairo-contracts/Scarb.toml)
Finished release target(s) in 16 seconds
                    
                

Run tests

                    
scarb test
                    
                
Starkli and Scarb

Created by: NaufalPrtm.

Official Reference Docs: openzeppelin cairo 0.8.0-beta.0.

Official Blog Docs: openzeppelin.

Reference github: openzeppelin contracts for cairo.

Starknet Blog Starknet Blog.

Starknet Deploy Testnet Contract Deploy Testnet Contract.