Skip to content

Commit

Permalink
refactor: remove DefaultStorageLayout macro (#200)
Browse files Browse the repository at this point in the history
Seems there is a more simple way to initialize contract for `motsu` unit
tests.
Remove macro that we don't need to use.

Follows up #51

#### PR Checklist

- [-] Tests
- [-] Documentation
  • Loading branch information
qalisander authored Jul 22, 2024
1 parent 7694478 commit 5eeb152
Show file tree
Hide file tree
Showing 20 changed files with 28 additions and 288 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion contracts/src/access/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ sol_storage! {
}

/// State of an `AccessControl` contract.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct AccessControl {
/// Role identifier -> Role information.
mapping(bytes32 => RoleData) _roles;
Expand Down
1 change: 0 additions & 1 deletion contracts/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub enum Error {

sol_storage! {
/// State of an `Ownable` contract.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Ownable {
/// The current owner of this contract.
address _owner;
Expand Down
1 change: 0 additions & 1 deletion contracts/src/token/erc20/extensions/capped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub enum Error {
sol_storage! {
/// State of a Capped Contract.
#[allow(clippy::pub_underscore_fields)]
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Capped {
/// A cap to the supply of tokens.
uint256 _cap;
Expand Down
1 change: 0 additions & 1 deletion contracts/src/token/erc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pub enum Error {

sol_storage! {
/// State of an `Erc20` token.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Erc20 {
/// Maps users to balances.
mapping(address => uint256) _balances;
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/token/erc721/extensions/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub enum Error {

sol_storage! {
/// State of an Enumerable extension.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Erc721Enumerable {
/// Maps owners to a mapping of indices to tokens ids.
mapping(address => mapping(uint256 => uint256)) _owned_tokens;
Expand Down Expand Up @@ -309,6 +308,7 @@ impl Erc721Enumerable {
#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::{address, uint, Address, U256};
use motsu::prelude::*;
use stylus_sdk::msg;

use super::{Erc721Enumerable, Error, IErc721Enumerable};
Expand Down
1 change: 0 additions & 1 deletion contracts/src/token/erc721/extensions/uri_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ sol! {

sol_storage! {
/// Uri Storage.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Erc721UriStorage {
/// Optional mapping for token URIs.
mapping(uint256 => string) _token_uris;
Expand Down
1 change: 0 additions & 1 deletion contracts/src/token/erc721/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ sol_interface! {

sol_storage! {
/// State of an [`Erc721`] token.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Erc721 {
/// Maps tokens to owners.
mapping(uint256 => address) _owners;
Expand Down
1 change: 0 additions & 1 deletion contracts/src/utils/nonces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub enum Error {

sol_storage! {
/// State of a Nonces Contract.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Nonces {
/// Mapping from address to its nonce.
mapping(address => uint256) _nonces;
Expand Down
1 change: 0 additions & 1 deletion contracts/src/utils/pausable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub enum Error {

sol_storage! {
/// State of a Pausable Contract.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Pausable {
/// Indicates whether the contract is `Paused`.
bool _paused;
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/utils/structs/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const HEX_FF: U256 = uint!(0xff_U256);

sol_storage! {
/// State of bit map.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct BitMap {
/// Inner laying mapping.
mapping(uint256 => uint256) _data;
Expand Down Expand Up @@ -96,6 +95,7 @@ impl BitMap {
#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::{private::proptest::proptest, U256};
use motsu::prelude::*;

use crate::utils::structs::bitmap::BitMap;

Expand Down
1 change: 0 additions & 1 deletion contracts/src/utils/structs/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub enum Error {

sol_storage! {
/// State of the checkpoint library contract.
#[cfg_attr(all(test, feature = "std"), derive(motsu::DefaultStorageLayout))]
pub struct Trace160 {
/// Stores checkpoints in a dynamic array sorted by key.
Checkpoint160[] _checkpoints;
Expand Down
58 changes: 0 additions & 58 deletions lib/motsu-proc/src/default_storage_layout.rs

This file was deleted.

49 changes: 0 additions & 49 deletions lib/motsu-proc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Procedural macro definitions used in `motsu`.
use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput};

/// Shorthand to print nice errors.
///
Expand All @@ -15,7 +14,6 @@ macro_rules! error {
}};
}

mod default_storage_layout;
mod test;

/// Defines a unit test that provides access to Stylus' execution context.
Expand Down Expand Up @@ -57,50 +55,3 @@ mod test;
pub fn test(attr: TokenStream, input: TokenStream) -> TokenStream {
test::test(&attr, input)
}

/// Automatically implements the `Default` trait for a struct that uses
/// `sol_storage!`.
///
/// This macro initializes the struct fields based on how they are laid out in
/// the EVM state trie. It is intended to be a helper for tests to avoid having
/// to implement `Default` for each contract.
///
/// # Usage
///
/// To use this macro, simply add `#[derive(motsu::DefaultStorageLayout)]` to
/// your `sol_storage!` struct. Make sure all the fields in your struct are
/// compatible with Stylus' storage, that means they implement the `StorageType`
/// trait.
///
/// # Examples
///
/// ```rust,ignore
/// sol_storage! {
/// #[derive(motsu::DefaultStorageLayout)]
/// pub struct Erc20 {
/// /// Maps users to balances.
/// mapping(address => uint256) _balances;
/// /// Maps users to a mapping of each spender's allowance.
/// mapping(address => mapping(address => uint256)) _allowances;
/// /// The total supply of the token.
/// uint256 _total_supply;
/// }
/// }
/// ```
///
/// ## Notice
///
/// For now this macro only works with structs that use the
/// `sol_storage!` macro, which allows you to use the solidity syntax.
/// If you want to write your contracts using `#[solidity_storage]`
/// and the Rust syntax, you will have to implement `Default` yourself.
///
/// ## See Also
///
/// - [Layout of State Variables in Storage](https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html)

#[proc_macro_derive(DefaultStorageLayout)]
pub fn derive_stylus_default(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
default_storage_layout::impl_default_storage_layout(&input)
}
130 changes: 0 additions & 130 deletions lib/motsu-proc/tests/default.rs

This file was deleted.

1 change: 1 addition & 0 deletions lib/motsu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "0.1.0"
const-hex.workspace = true
once_cell.workspace = true
tiny-keccak.workspace = true
stylus-sdk.workspace = true
motsu-proc = { path = "../motsu-proc" }

[lints]
Expand Down
Loading

0 comments on commit 5eeb152

Please sign in to comment.