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

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

ericnordelo
Copy link
Member

@ericnordelo ericnordelo commented Jan 2, 2025

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
@ericnordelo ericnordelo marked this pull request as ready for review January 22, 2025 14:10
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work on this @ericnordelo! I left some comments

Also, since so much of the component boilerplate is abstracted away, I don't think it's super obvious what the storage entry is for a given component. It'll be documented, for sure, but would it make sense just to simplify it further and use the same capitalized name as the whitelisted component i.e.

#[with_components(ERC20, Ownable)]
#[starknet::contract]
pub mod MyToken {
    (...)

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

This gives a point of reference inside the contract as opposed to attempting, for example, reentrancy_guard or reentrancyguard

docs/modules/ROOT/pages/api/macros.adoc Outdated Show resolved Hide resolved
packages/macros/src/tests/test_with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/tests/test_with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/tests/test_with_components.rs Outdated Show resolved Hide resolved
packages/macros/src/with_components.rs Outdated Show resolved Hide resolved
"AccessControl" => Some(ComponentInfo {
name: "AccessControlComponent".to_string(),
path: "openzeppelin_access::accesscontrol::AccessControlComponent".to_string(),
storage: "accesscontrol".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. I know the lib isn't entirely consistent (might be worth addressing in a separate issue), but I think it could get weird when we have timelock_controller vs accesscontrol and reentrancyguard

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will refactor them here (for macros). We may update presets and mocks in a separate issue later.

@ericnordelo
Copy link
Member Author

This gives a point of reference inside the contract as opposed to attempting, for example, reentrancy_guard or reentrancyguard

Since developers are already using snake case for storage members, I vote we stay with snake case, but split the words accordingly.

@andrew-fleming
Copy link
Collaborator

This gives a point of reference inside the contract as opposed to attempting, for example, reentrancy_guard or reentrancyguard

Since developers are already using snake case for storage members, I vote we stay with snake case, but split the words accordingly.

This is true...no strong pushback against keeping snake case, I just think reusing the name could be a bit more intuitive. If we wanted to make a change like this, it'd be better to do it now with the feature

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
2 participants