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

feat: Add Stylus Constructors #522

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ all = "warn"

[workspace.dependencies]
# Stylus SDK related
stylus-sdk = { version = "0.7.0", default-features = false, features = [
stylus-sdk = { git = "https://github.com/OffchainLabs/stylus-sdk-rs.git", branch = "gligneul/constructors", default-features = false, features = [
"mini-alloc",
] }

Expand Down
6 changes: 6 additions & 0 deletions contracts/src/access/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@

#[public]
impl AccessControl {
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
/// Constructor
#[constructor]
pub fn constructor(&mut self) {
self._grant_role(Self::DEFAULT_ADMIN_ROLE.into(), msg::sender());
}

/// Returns `true` if `account` has been granted `role`.
///
/// # Arguments
Expand Down Expand Up @@ -411,26 +417,26 @@
.insert(msg::sender(), true);
}

#[motsu::test]

Check failure on line 420 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 420 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 420 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 420 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn default_role_is_default_admin(contract: AccessControl) {
let role_admin = contract.get_role_admin(ROLE.into());
assert_eq!(role_admin, AccessControl::DEFAULT_ADMIN_ROLE);
}

#[motsu::test]

Check failure on line 426 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 426 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 426 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 426 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn default_admin_roles_admin_is_itself(contract: AccessControl) {
const DEFAULT_ADMIN_ROLE: [u8; 32] = AccessControl::DEFAULT_ADMIN_ROLE;
let role_admin = contract.get_role_admin(DEFAULT_ADMIN_ROLE.into());
assert_eq!(role_admin, DEFAULT_ADMIN_ROLE);
}

#[motsu::test]

Check failure on line 433 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 433 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 433 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 433 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn non_admin_cannot_grant_role_to_others(contract: AccessControl) {
let err = contract.grant_role(ROLE.into(), ALICE).unwrap_err();
assert!(matches!(err, Error::UnauthorizedAccount(_)));
}

#[motsu::test]

Check failure on line 439 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 439 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 439 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 439 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn accounts_can_be_granted_roles_multiple_times(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);

Expand All @@ -440,7 +446,7 @@
assert!(has_role);
}

#[motsu::test]

Check failure on line 449 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 449 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 449 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 449 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn not_granted_roles_can_be_revoked(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);

Expand All @@ -451,7 +457,7 @@
assert!(!has_role);
}

#[motsu::test]

Check failure on line 460 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 460 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 460 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 460 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn admin_can_revoke_role(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
Expand All @@ -463,7 +469,7 @@
assert!(!has_role);
}

#[motsu::test]

Check failure on line 472 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 472 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 472 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 472 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn non_admin_cannot_revoke_role(contract: AccessControl) {
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);

Expand All @@ -473,7 +479,7 @@
assert!(matches!(err, Error::UnauthorizedAccount(_)));
}

#[motsu::test]

Check failure on line 482 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 482 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 482 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 482 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn roles_can_be_revoked_multiple_times(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);

Expand All @@ -483,7 +489,7 @@
assert!(!has_role);
}

#[motsu::test]

Check failure on line 492 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 492 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 492 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 492 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn bearer_can_renounce_role(contract: AccessControl) {
_grant_role_to_msg_sender(contract, ROLE);

Expand All @@ -494,7 +500,7 @@
assert!(!has_role);
}

#[motsu::test]

Check failure on line 503 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 503 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 503 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied

Check failure on line 503 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

the function or associated item `default` exists for struct `AccessControl`, but its trait bounds were not satisfied
fn only_sender_can_renounce(contract: AccessControl) {
_grant_role_to_msg_sender(contract, ROLE);
let err = contract.renounce_role(ROLE.into(), ALICE).unwrap_err();
Expand Down
19 changes: 19 additions & 0 deletions contracts/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ impl IOwnable for Ownable {
}
}

// TODO: uncomment once multiple public attributes are supported
// NOTE: cannot include constructor in the above #[public], as constructor is
// not part of the `IOwnable2Step` trait
// #[public]
// impl Ownable {
// /// Constructor
// #[constructor]
// pub fn constructor(&mut self, initial_owner: Address) -> Result<(),
// Error> { if initial_owner.is_zero() {
// return Err(OwnableError::InvalidOwner(OwnableInvalidOwner {
// owner: Address::ZERO,
// })
// .into());
// }
// self._transfer_ownership(initial_owner);
// Ok(())
// }
// }

impl Ownable {
/// Checks if the [`msg::sender`] is set as the owner.
///
Expand Down
19 changes: 19 additions & 0 deletions contracts/src/access/ownable_two_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ impl IOwnable2Step for Ownable2Step {
}
}

// TODO: uncomment once multiple public attributes are supported
// NOTE: cannot include constructor in the above #[public], as constructor is
// not part of the `IOwnable2Step` trait
// #[public]
// impl Ownable2Step {
// /// Constructor
// #[constructor]
// pub fn constructor(&mut self, initial_owner: Address) -> Result<(),
// Error> { if initial_owner.is_zero() {
// return Err(OwnableError::InvalidOwner(OwnableInvalidOwner {
// owner: Address::ZERO,
// })
// .into());
// }
// self._transfer_ownership(initial_owner);
// Ok(())
// }
// }

impl Ownable2Step {
/// Transfers ownership of the contract to a new account (`new_owner`) and
/// sets [`Self::pending_owner`] to `Address::ZERO` to avoid situations
Expand Down
47 changes: 0 additions & 47 deletions examples/access-control/src/constructor.sol

This file was deleted.

5 changes: 5 additions & 0 deletions examples/access-control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ pub const TRANSFER_ROLE: [u8; 32] =
#[public]
#[inherit(Erc20, AccessControl)]
impl AccessControlExample {
#[constructor]
pub fn constructor(&mut self) {
self.access.constructor();
}

pub fn make_admin(&mut self, account: Address) -> Result<(), Vec<u8>> {
self.access.only_role(AccessControl::DEFAULT_ADMIN_ROLE.into())?;
self.access.grant_role(TRANSFER_ROLE.into(), account)?;
Expand Down
Loading
Loading