openzeppelin-stylus-proc

Crates.ioopenzeppelin-stylus-proc
lib.rsopenzeppelin-stylus-proc
version0.3.0
created_at2024-10-15 11:18:19.371376+00
updated_at2025-08-06 20:35:16.254121+00
descriptionProcedural macros for OpenZeppelin Stylus contracts
homepage
repositoryhttps://github.com/OpenZeppelin/rust-contracts-stylus
max_upload_size
id1409311
size12,813
Daniel Bigos (bidzyyys)

documentation

README

OpenZeppelin Stylus Procedural Macros

Procedural macros for OpenZeppelin Stylus Contracts, providing tools to streamline trait definitions, interface ID computation, and Solidity compatibility in Rust-based Stylus Contracts.

Overview

This crate offers procedural macros for OpenZeppelin Stylus Contracts, specifically targeting the computation of Solidity interfaceId value and enhancing trait implementations with clear and robust syntax.

Key Features

  • #[interface_id] Macro: Adds interface_id() function that computes Solidity-compatible interface ID for traits.
  • #[selector] Attribute: Overrides function names to align with Solidity method signatures.

Usage

#[interface_id]

Annotate a Rust trait with #[interface_id] to add the Solidity-compatible interface ID calculation:

use openzeppelin_stylus_proc::interface_id;

#[interface_id]
pub trait IErc721 {
    type Error: Into<alloc::vec::Vec<u8>>;

    fn balance_of(&self, owner: Address) -> Result<U256, Self::Error>;
    fn owner_of(&self, token_id: U256) -> Result<Address, Self::Error>;

    // ...
}

This will add interface_id() function that caluclates interface ID based on the XOR of the function selectors.

#[selector]

Override the Solidity function selector explicitly:

fn safe_transfer_from(
    &mut self,
    from: Address,
    to: Address,
    token_id: U256,
) -> Result<(), Self::Error>;

// Solidity allows function overloading, but Rust does not, so we map
// the Rust function name to the appropriate Solidity function name.
#[selector(name = "safeTransferFrom")]
fn safe_transfer_from_with_data(
    &mut self,
    from: Address,
    to: Address,
    token_id: U256,
    data: Bytes,
) -> Result<(), Self::Error>;

This ensures compatibility with Solidity's naming conventions.

Security

Refer to our Security Policy for more details.

Commit count: 475

cargo fmt