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

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.2"

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-rc.0"

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 = "=0.9.0"

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

use openzeppelin_stylus::token::erc20::{self, Erc20, IErc20};
use stylus_sdk::{
    alloy_primitives::{Address, U256},
    prelude::*,
};

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

#[public]
#[implements(IErc20<Error = erc20::Error>)]
impl Erc20Example {}

#[public]
impl IErc20 for Erc20Example {
    type Error = erc20::Error;

    fn total_supply(&self) -> U256 {
        self.erc20.total_supply()
    }

    fn balance_of(&self, account: Address) -> U256 {
        self.erc20.balance_of(account)
    }

    fn transfer(
        &mut self,
        to: Address,
        value: U256,
    ) -> Result<bool, Self::Error> {
        self.erc20.transfer(to, value)
    }

    fn allowance(&self, owner: Address, spender: Address) -> U256 {
        self.erc20.allowance(owner, spender)
    }

    fn approve(
        &mut self,
        spender: Address,
        value: U256,
    ) -> Result<bool, Self::Error> {
        self.erc20.approve(spender, value)
    }

    fn transfer_from(
        &mut self,
        from: Address,
        to: Address,
        value: U256,
    ) -> Result<bool, Self::Error> {
        self.erc20.transfer_from(from, to, value)
    }
}

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.

About

A library for secure smart contract development written in Rust

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 27