Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macros package and the with_components macro #1282

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ericnordelo
Copy link
Member

Quickstart #1258

NOTE: A summary of the features included in the macro is added below after the example:

Example:

Simplifies this:

#[starknet::contract]
pub mod MyToken {
    use openzeppelin_access::ownable::OwnableComponent;
    use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
    use starknet::ContractAddress;

    component!(path: ERC20Component, storage: erc20, event: ERC20Event);
    component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

    impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;
    impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

    #[storage]
    pub struct Storage {
        #[substorage(v0)]
        erc20: ERC20Component::Storage,
        #[substorage(v0)]
        ownable: OwnableComponent::Storage,
    }

    #[event]
    #[derive(Drop, starknet::Event)]
    enum Event {
        #[flat]
        ERC20Event: ERC20Component::Event,
        #[flat]
        OwnableEvent: OwnableComponent::Event,
    }

    #[constructor]
    fn constructor(ref self: ContractState, owner: ContractAddress) {
        self.ownable.initializer(owner);
        self.erc20.initializer("MyToken", "MTK");
    }
}

Into this:

#[with_components(ERC20, Ownable)]
#[starknet::contract]
pub mod MyToken {
    use openzeppelin_token::erc20::ERC20HooksEmptyImpl;
    use starknet::ContractAddress;

    #[storage]
    pub struct Storage {}

    #[constructor]
    fn constructor(ref self: ContractState, owner: ContractAddress) {
        self.ownable.initializer(owner);
        self.erc20.initializer("MyToken", "MTK");
    }
}

Summary of features:

IT DOES:

  • Accepts multiple components from an internal whitelist (currently only ERC20 and Ownable for the POC).
  • Validates that the contract has the #[startknet::contract] attribute, shows an error in compilation otherwise.
  • Validates that the contract has the corresponding initializers in the constructor, shows a warning listing the missing initializers otherwise.
  • Adds the use clauses for each component (i.e: use openzeppelin_access::ownable::OwnableComponent;)
  • Adds the corresponding component! macros (i.e: component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);).
  • Adds the corresponding internal implementations (i.e: impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;)
  • Adds the storage entries to the Storage struct, using the lowercase version of the component name without the component keyword (i.e: ownable: OwnableComponent::Storage,)
  • Adds the event entries to the Event enum, and creates the enum if it is missing.

IT DOES NOT:

  • Adds the external implementations or hooks.
  • Create the constructor and/or initializers.

Things to be considered still:

  • openzeppelin_access vs openzeppelin::access in use clauses (configurable notation?)
  • Configurable storage and event entries per component.
  • Add default external embedded impls?
  • Add warnings to other things, like missing camelCase interface.

Some may be addressed in different PRs/issues.

NOTE: I have plans to include a with_component macro which will accept only a single component but more configuration, like replacing the storage entry. People then may use both macros together (with_components and with_component)

PR Checklist

  • Tests
  • Documentation
  • Added entry to CHANGELOG.md
  • Tried the feature on a public network

@ericnordelo ericnordelo linked an issue Jan 6, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide macros as part of the library
1 participant