Skip to content

OpenZeppelin Contracts written in Cairo for Starknet, a decentralized ZK Rollup

License

Notifications You must be signed in to change notification settings

Josephtran102/cairo-contracts

 
 

Repository files navigation

OpenZeppelin Contracts for Cairo

Lint and test

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

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

Usage

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

Prepare the environment

Simply install Cairo and scarb.

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 src

Install the library

Edit scarb.toml and add:

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

Build the project to download it:

$ scarb build

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

Using the library

Open src/lib.cairo and write your contract.

For example, this how to extend the ERC20 standard contract:

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

    #[storage]
    struct Storage {}

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

        let mut unsafe_state = ERC20::unsafe_new_contract_state();
        ERC20::InternalImpl::initializer(ref unsafe_state, name, symbol);
        ERC20::InternalImpl::_mint(ref unsafe_state, recipient, initial_supply);
    }

    #[external(v0)]
    fn name(self: @ContractState) -> felt252 {
        let unsafe_state = ERC20::unsafe_new_contract_state();
        ERC20::ERC20Impl::name(@unsafe_state)
    }

    ...
}

Unsupported

DualCase dispatchers rely on Sierra's ability to catch a revert to resume execution. Currently, Starknet live chains (testnets and mainnet) don't implement that behavior. Starknet's testing framework does support it.

Learn

Cairo

Tooling

Development

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

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

Run tests

scarb test

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 to SECURITY.md for more details.

Contribute

OpenZeppelin Contracts for Cairo exists thanks to its contributors. There are many ways you can participate and help build high quality software. Check out the contribution guide!

License

OpenZeppelin Contracts for Cairo is released under the MIT License.

About

OpenZeppelin Contracts written in Cairo for Starknet, a decentralized ZK Rollup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%