3 unstable releases

new 0.2.0 Apr 22, 2025
0.1.0 Oct 16, 2024
0.1.0-rc Oct 15, 2024

#23 in #arbitrum

Download history 13/week @ 2024-12-31 89/week @ 2025-01-07 63/week @ 2025-01-14 88/week @ 2025-01-21 190/week @ 2025-01-28 91/week @ 2025-02-04 55/week @ 2025-02-11 120/week @ 2025-02-18 92/week @ 2025-02-25 46/week @ 2025-03-04 80/week @ 2025-03-11 26/week @ 2025-03-18 67/week @ 2025-03-25 34/week @ 2025-04-01 47/week @ 2025-04-08 10/week @ 2025-04-15

159 downloads per month
Used in openzeppelin-stylus

MIT license

10KB
97 lines

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: Automatically computes Solidity-compatible INTERFACE_ID constants for traits.
  • #[selector] Attribute: Overrides function names to align with Solidity method signatures.

Usage

#[interface_id]

Annotate a Rust trait with #[interface_id] to compute the Solidity-compatible INTERFACE_ID:

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 generate an INTERFACE_ID constant 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.

Dependencies

~0.6–1MB
~21K SLoC