Skip to content

OpenZeppelin/rust-contracts-stylus

Repository files navigation

OpenZeppelin Contracts for Stylus

A library for secure smart contract development written in Rust for Arbitrum Stylus.

Features

  • Security-first smart contracts, ported from the openzeppelin-contracts library.
  • First-class no_std support.
  • Solidity constructors powered by koba.
  • Unit and integration test affordances, used in our own tests.

Usage

You can import OpenZeppelin Contracts from crates.io by adding the following line to your Cargo.toml (We recommend pinning to a specific version):

[dependencies]
openzeppelin-stylus = "=0.1.1"

If you want to use some of our newest features before they are fully stable or audited, you can try the latest alpha version of the library. We release a new alpha version every ~3 weeks.

[dependencies]
openzeppelin-stylus = "=0.2.0-alpha.4"

We put great effort in testing the contracts before releasing an alpha, but these are not yet audited and we don't guarantee any backwards compatibility between alpha version.

Note

This library is designed to be no_std, which helps reduce wasm size. If you want your project to be no_std as well, ensure that your dependencies are not importing the standard library. You can achieve this by setting default-features = false for relevant dependencies in your Cargo.toml. For example:

[dependencies]
alloy-primitives = { version = "=0.8.20", default-features = false }
stylus-sdk = { version = "=0.8.1", default-features = false, features = [
  "mini-alloc",
] }

You will also need to define your own panic handler for cargo stylus check to pass. Here's an example of a simple panic handler you can use in your lib.rs file:

#[cfg(target_arch = "wasm32")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

The library also works on an std environment, without the need to define a panic handler or making extra changes to your project.

Once defined as a dependency, use one of our pre-defined implementations by importing them:

use stylus_sdk::prelude::*;
use openzeppelin_stylus::token::erc20::Erc20;

#[entrypoint]
#[storage]
struct Erc20Example {
    #[borrow]
    erc20: Erc20,
}

#[public]
#[inherit(Erc20)]
impl Erc20Example {}

For a more complex display of what this library offers, refer to our examples.

For a full example that includes deploying and querying a contract, see the basic example.

For more information on what this library will include in the future, see our roadmap.

Contribute

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

Security

Past audits can be found in audits/.

Refer to our Security Policy for more details.

License

OpenZeppelin Contracts for Stylus is released under the MIT License.