Skip to content

Conversation

@d1r1
Copy link
Contributor

@d1r1 d1r1 commented Sep 23, 2025

Summary

Introduces #[constructor] macro for contract initialization and unifies Contract/Storage implementation to reduce code duplication.

Changes

New Features

  • #[constructor] macro for defining contract initialization separately from runtime methods
  • Supports trait implementations where constructor can't be part of the trait

Bug Fixes

  • Fixed undefined variable errors in deploy method generation
  • Removed unnecessary function_id imports from generated code

Refactoring

  • Unified Contract and Storage derives to share same implementation
  • Extracted common deploy logic to ParsedMethod::generate_deploy_body()
  • Removed redundant Contract implementation

Usage

// Separate initialization from trait implementation
#[constructor(mode = "solidity")]
impl<SDK> Token<SDK> {
    pub fn constructor(&mut self, supply: U256) { }
}

#[router(mode = "solidity")]
impl<SDK> TokenInterface for Token<SDK> {
    fn transfer(&mut self, to: Address, amount: U256) -> bool { }
}

Breaking Changes

  • Internal Contract derive implementation changed (API remains compatible)

Remove function_id attributes from generated impl blocks and eliminate
the unnecessary import statement. The function_id attributes are already
processed during macro expansion and don't need to be present in the
final generated code.

This change makes the generated code cleaner and removes the dependency
on an empty proc macro that was only acting as a placeholder.
Fix the deploy method generation that was incorrectly referencing
undefined variables (param0, param1, param2) in the else branch.
The shared generate_deploy_body method now properly handles all
parameter counts without conditional branches.

Refactor duplicate code between router and constructor macros into
a shared method in ParsedMethod for better maintainability.
Remove redundant Contract implementation and unify it with Storage macro.
Both macros now share the same underlying implementation, with Contract
for main contracts and Storage for nested structures.

BREAKING CHANGE: The old Contract macro implementation has been replaced.
Projects using #[derive(Contract)] should still work but now use the
Storage implementation under the hood.
@d1r1 d1r1 force-pushed the feat/constructor-macro branch from 247b53f to 21e7d08 Compare September 24, 2025 08:40
@d1r1 d1r1 merged commit 1dda187 into devel Sep 24, 2025
4 checks passed
@d1r1 d1r1 deleted the feat/constructor-macro branch September 24, 2025 08:49
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.

2 participants