Crate openzeppelin_stylus

Crate openzeppelin_stylus 

Source
Expand description

§OpenZeppelin Contracts for Stylus

A library for secure smart contract development written in Rust for Arbitrum Stylus. This library offers common smart contract primitives and affordances that take advantage of the nature of Stylus.

§Usage

To start using it, add openzeppelin-stylus to your Cargo.toml, or simply run cargo add openzeppelin-stylus.

[dependencies]
openzeppelin-stylus = "x.x.x"

We recommend pinning to a specific version – expect rapid iteration.

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 MyContract {
    pub erc20: Erc20,
}

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

#[public]
impl IErc20 for MyContract {
    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)
    }
}

Modules§

access
Contracts providing ways to restrict who can access the functions of another contracts or when they can do it.
finance
Primitives for financial systems.
proxy
This is a low-level set of contracts implementing different proxy patterns with and without upgradeability.
token
Token standards.
utils
Common Smart Contracts utilities.