diff --git a/CHANGELOG.md b/CHANGELOG.md index 74b3d29be..a60154ade 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed (Breaking) +- All contract state fields are no longer public. #500 +- `Erc721Consecutive::_mint_consecutive` turned into an internal function. #500 - Bump cargo-stylus to v0.5.8. #493 - Constants `TYPE_HASH`, `FIELDS`, `SALT` and `TYPED_DATA_PREFIX`, and type `DomainSeparatorTuple` are no longer exported from `utils::cryptography::eip712`. #478 - Bump Stylus SDK to v0.7.0. #433 diff --git a/contracts/src/access/control.rs b/contracts/src/access/control.rs index ed42fd5d9..aa6d5b4d9 100644 --- a/contracts/src/access/control.rs +++ b/contracts/src/access/control.rs @@ -122,8 +122,7 @@ pub struct RoleData { #[storage] pub struct AccessControl { /// Role identifier -> Role information. - #[allow(clippy::used_underscore_binding)] - pub _roles: StorageMap, RoleData>, + pub(crate) roles: StorageMap, RoleData>, } /// Interface for an [`AccessControl`] contract. #[interface_id] @@ -247,7 +246,7 @@ impl IAccessControl for AccessControl { #[must_use] fn has_role(&self, role: B256, account: Address) -> bool { - self._roles.getter(role).has_role.get(account) + self.roles.getter(role).has_role.get(account) } fn only_role(&self, role: B256) -> Result<(), Self::Error> { @@ -256,7 +255,7 @@ impl IAccessControl for AccessControl { #[must_use] fn get_role_admin(&self, role: B256) -> B256 { - *self._roles.getter(role).admin_role + *self.roles.getter(role).admin_role } fn grant_role( @@ -314,7 +313,7 @@ impl AccessControl { /// * [`RoleAdminChanged`]. pub fn _set_role_admin(&mut self, role: B256, new_admin_role: B256) { let previous_admin_role = self.get_role_admin(role); - self._roles.setter(role).admin_role.set(new_admin_role); + self.roles.setter(role).admin_role.set(new_admin_role); evm::log(RoleAdminChanged { role, previous_admin_role, @@ -366,7 +365,7 @@ impl AccessControl { if self.has_role(role, account) { false } else { - self._roles.setter(role).has_role.insert(account, true); + self.roles.setter(role).has_role.insert(account, true); evm::log(RoleGranted { role, account, sender: msg::sender() }); true } @@ -388,7 +387,7 @@ impl AccessControl { /// * [`RoleRevoked`]. pub fn _revoke_role(&mut self, role: B256, account: Address) -> bool { if self.has_role(role, account) { - self._roles.setter(role).has_role.insert(account, false); + self.roles.setter(role).has_role.insert(account, false); evm::log(RoleRevoked { role, account, sender: msg::sender() }); true } else { diff --git a/contracts/src/access/ownable.rs b/contracts/src/access/ownable.rs index 6fe36f793..8267bac7e 100644 --- a/contracts/src/access/ownable.rs +++ b/contracts/src/access/ownable.rs @@ -69,8 +69,7 @@ impl MethodError for Error { #[storage] pub struct Ownable { /// The current owner of this contract. - #[allow(clippy::used_underscore_binding)] - pub _owner: StorageAddress, + pub(crate) owner: StorageAddress, } /// Interface for an [`Ownable`] contract. @@ -132,7 +131,7 @@ impl IOwnable for Ownable { type Error = Error; fn owner(&self) -> Address { - self._owner.get() + self.owner.get() } fn transfer_ownership( @@ -193,8 +192,8 @@ impl Ownable { /// /// * [`OwnershipTransferred`]. pub fn _transfer_ownership(&mut self, new_owner: Address) { - let previous_owner = self._owner.get(); - self._owner.set(new_owner); + let previous_owner = self.owner.get(); + self.owner.set(new_owner); evm::log(OwnershipTransferred { previous_owner, new_owner }); } } @@ -211,7 +210,7 @@ mod tests { #[motsu::test] fn reads_owner(contract: Contract, alice: Address) { - contract.init(alice, |contract| contract._owner.set(alice)); + contract.init(alice, |contract| contract.owner.set(alice)); let owner = contract.sender(alice).owner(); assert_eq!(owner, alice); } @@ -222,7 +221,7 @@ mod tests { alice: Address, bob: Address, ) { - contract.init(alice, |contract| contract._owner.set(alice)); + contract.init(alice, |contract| contract.owner.set(alice)); contract .sender(alice) @@ -238,7 +237,7 @@ mod tests { alice: Address, bob: Address, ) { - contract.init(alice, |contract| contract._owner.set(bob)); + contract.init(alice, |contract| contract.owner.set(bob)); let err = contract.sender(alice).transfer_ownership(bob).unwrap_err(); assert!(matches!(err, Error::UnauthorizedAccount(_))); @@ -249,7 +248,7 @@ mod tests { contract: Contract, alice: Address, ) { - contract.init(alice, |contract| contract._owner.set(alice)); + contract.init(alice, |contract| contract.owner.set(alice)); let err = contract .sender(alice) @@ -263,7 +262,7 @@ mod tests { contract: Contract, alice: Address, ) { - contract.init(alice, |contract| contract._owner.set(alice)); + contract.init(alice, |contract| contract.owner.set(alice)); contract .sender(alice) @@ -279,7 +278,7 @@ mod tests { alice: Address, bob: Address, ) { - contract.init(alice, |contract| contract._owner.set(bob)); + contract.init(alice, |contract| contract.owner.set(bob)); let err = contract.sender(alice).renounce_ownership().unwrap_err(); assert!(matches!(err, Error::UnauthorizedAccount(_))); @@ -291,7 +290,7 @@ mod tests { alice: Address, bob: Address, ) { - contract.init(alice, |contract| contract._owner.set(bob)); + contract.init(alice, |contract| contract.owner.set(bob)); contract.sender(alice)._transfer_ownership(bob); let owner = contract.sender(alice).owner(); diff --git a/contracts/src/access/ownable_two_step.rs b/contracts/src/access/ownable_two_step.rs index c3006ad5a..2219aac4e 100644 --- a/contracts/src/access/ownable_two_step.rs +++ b/contracts/src/access/ownable_two_step.rs @@ -61,11 +61,11 @@ pub enum Error { #[storage] pub struct Ownable2Step { /// [`Ownable`] contract. - #[allow(clippy::used_underscore_binding)] - pub _ownable: Ownable, + // We leave the parent [`Ownable`] contract instance public, so that + // inheritting contract have access to its internal functions. + pub ownable: Ownable, /// Pending owner of the contract. - #[allow(clippy::used_underscore_binding)] - pub _pending_owner: StorageAddress, + pub(crate) pending_owner: StorageAddress, } /// Interface for an [`Ownable2Step`] contract. @@ -157,19 +157,19 @@ impl IOwnable2Step for Ownable2Step { type Error = Error; fn owner(&self) -> Address { - self._ownable.owner() + self.ownable.owner() } fn pending_owner(&self) -> Address { - self._pending_owner.get() + self.pending_owner.get() } fn transfer_ownership( &mut self, new_owner: Address, ) -> Result<(), Self::Error> { - self._ownable.only_owner()?; - self._pending_owner.set(new_owner); + self.ownable.only_owner()?; + self.pending_owner.set(new_owner); let current_owner = self.owner(); evm::log(OwnershipTransferStarted { @@ -193,7 +193,7 @@ impl IOwnable2Step for Ownable2Step { } fn renounce_ownership(&mut self) -> Result<(), Error> { - self._ownable.only_owner()?; + self.ownable.only_owner()?; self._transfer_ownership(Address::ZERO); Ok(()) } @@ -216,8 +216,8 @@ impl Ownable2Step { /// /// * [`crate::access::ownable::OwnershipTransferred`]. fn _transfer_ownership(&mut self, new_owner: Address) { - self._pending_owner.set(Address::ZERO); - self._ownable._transfer_ownership(new_owner); + self.pending_owner.set(Address::ZERO); + self.ownable._transfer_ownership(new_owner); } } @@ -234,7 +234,7 @@ mod tests { #[motsu::test] fn reads_owner(contract: Contract, alice: Address) { contract.init(alice, |contract| { - contract._ownable._owner.set(alice); + contract.ownable.owner.set(alice); }); let owner = contract.sender(alice).owner(); assert_eq!(owner, alice); @@ -247,7 +247,7 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._pending_owner.set(bob); + contract.pending_owner.set(bob); }); let pending_owner = contract.sender(alice).pending_owner(); @@ -261,7 +261,7 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(alice); + contract.ownable.owner.set(alice); }); contract @@ -280,7 +280,7 @@ mod tests { dave: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(bob); + contract.ownable.owner.set(bob); }); let err = contract.sender(alice).transfer_ownership(dave).unwrap_err(); @@ -297,8 +297,8 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(bob); - contract._pending_owner.set(alice); + contract.ownable.owner.set(bob); + contract.pending_owner.set(alice); }); contract @@ -317,8 +317,8 @@ mod tests { dave: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(bob); - contract._pending_owner.set(dave); + contract.ownable.owner.set(bob); + contract.pending_owner.set(dave); }); let err = contract.sender(alice).accept_ownership().unwrap_err(); @@ -335,7 +335,7 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(alice); + contract.ownable.owner.set(alice); }); contract @@ -356,7 +356,7 @@ mod tests { #[motsu::test] fn renounces_ownership(contract: Contract, alice: Address) { contract.init(alice, |contract| { - contract._ownable._owner.set(alice); + contract.ownable.owner.set(alice); }); contract @@ -373,7 +373,7 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(bob); + contract.ownable.owner.set(bob); }); let err = contract.sender(alice).renounce_ownership().unwrap_err(); @@ -390,8 +390,8 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(alice); - contract._pending_owner.set(bob); + contract.ownable.owner.set(alice); + contract.pending_owner.set(bob); }); contract @@ -409,8 +409,8 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(alice); - contract._pending_owner.set(bob); + contract.ownable.owner.set(alice); + contract.pending_owner.set(bob); }); contract @@ -429,7 +429,7 @@ mod tests { dave: Address, ) { contract.init(alice, |contract| { - contract._ownable._owner.set(alice); + contract.ownable.owner.set(alice); }); contract diff --git a/contracts/src/finance/vesting_wallet.rs b/contracts/src/finance/vesting_wallet.rs index 0b8243923..dcbea4fa5 100644 --- a/contracts/src/finance/vesting_wallet.rs +++ b/contracts/src/finance/vesting_wallet.rs @@ -99,21 +99,19 @@ pub enum Error { #[storage] pub struct VestingWallet { /// [`Ownable`] contract. + // We leave the parent [`Ownable`] contract instance public, so that + // inheritting contract have access to its internal functions. pub ownable: Ownable, /// Amount of Ether already released. - #[allow(clippy::used_underscore_binding)] - pub _released: StorageU256, + pub(crate) released: StorageU256, /// Amount of ERC-20 tokens already released. - #[allow(clippy::used_underscore_binding)] - pub _erc20_released: StorageMap, + pub(crate) erc20_released: StorageMap, /// Start timestamp. - #[allow(clippy::used_underscore_binding)] - pub _start: StorageU64, + pub(crate) start: StorageU64, /// Vesting duration. - #[allow(clippy::used_underscore_binding)] - pub _duration: StorageU64, + pub(crate) duration: StorageU64, /// [`SafeErc20`] contract. - pub safe_erc20: SafeErc20, + safe_erc20: SafeErc20, } /// NOTE: Implementation of [`TopLevelStorage`] to be able use `&mut self` when @@ -369,11 +367,11 @@ impl IVestingWallet for VestingWallet { fn receive_ether(&self) {} fn start(&self) -> U256 { - U256::from(self._start.get()) + U256::from(self.start.get()) } fn duration(&self) -> U256 { - U256::from(self._duration.get()) + U256::from(self.duration.get()) } fn end(&self) -> U256 { @@ -384,12 +382,12 @@ impl IVestingWallet for VestingWallet { #[selector(name = "released")] fn released_eth(&self) -> U256 { - self._released.get() + self.released.get() } #[selector(name = "released")] fn released_erc20(&self, token: Address) -> U256 { - self._erc20_released.get(token) + self.erc20_released.get(token) } #[selector(name = "releasable")] @@ -414,7 +412,7 @@ impl IVestingWallet for VestingWallet { fn release_eth(&mut self) -> Result<(), Self::Error> { let amount = self.releasable_eth(); - self._released.add_assign_checked( + self.released.add_assign_checked( amount, "total released should not exceed `U256::MAX`", ); @@ -433,7 +431,7 @@ impl IVestingWallet for VestingWallet { let amount = self.releasable_erc20(token)?; let owner = self.ownable.owner(); - self._erc20_released.setter(token).add_assign_checked( + self.erc20_released.setter(token).add_assign_checked( amount, "total released should not exceed `U256::MAX`", ); @@ -532,8 +530,8 @@ mod tests { fn init(&mut self, start: u64, duration: u64) -> (U64, U64) { let start = U64::from(start); let duration = U64::from(duration); - self._start.set(start); - self._duration.set(duration); + self.start.set(start); + self.duration.set(duration); (start, duration) } } diff --git a/contracts/src/lib.rs b/contracts/src/lib.rs index efca4003b..5a83ce0b4 100644 --- a/contracts/src/lib.rs +++ b/contracts/src/lib.rs @@ -38,11 +38,7 @@ impl MyContract { } ``` */ -#![allow( - clippy::pub_underscore_fields, - clippy::module_name_repetitions, - clippy::used_underscore_items -)] +#![allow(clippy::module_name_repetitions, clippy::used_underscore_items)] #![cfg_attr(not(feature = "std"), no_std, no_main)] #![cfg_attr(coverage_nightly, feature(coverage_attribute))] #![deny(rustdoc::broken_intra_doc_links)] diff --git a/contracts/src/token/erc1155/extensions/metadata_uri.rs b/contracts/src/token/erc1155/extensions/metadata_uri.rs index 030378c8e..5af8e833f 100644 --- a/contracts/src/token/erc1155/extensions/metadata_uri.rs +++ b/contracts/src/token/erc1155/extensions/metadata_uri.rs @@ -36,8 +36,7 @@ mod sol { pub struct Erc1155MetadataUri { /// Used as the URI for all token types by relying on ID substitution, /// e.g. https://token-cdn-domain/{id}.json. - #[allow(clippy::used_underscore_binding)] - pub _uri: StorageString, + pub(crate) uri: StorageString, } /// Interface for the optional metadata functions from the ERC-1155 standard. @@ -61,7 +60,7 @@ impl IErc1155MetadataUri for Erc1155MetadataUri { /// Clients calling this function must replace the `id` substring with /// the actual token type ID. fn uri(&self, _id: U256) -> String { - self._uri.get_string() + self.uri.get_string() } } @@ -90,7 +89,7 @@ mod tests { ) { let uri = String::from("https://token-cdn-domain/\\{id\\}.json"); contract.init(alice, |contract| { - contract._uri.set_str(uri.clone()); + contract.uri.set_str(uri.clone()); }); let token_id = uint!(1_U256); diff --git a/contracts/src/token/erc1155/extensions/supply.rs b/contracts/src/token/erc1155/extensions/supply.rs index bf30f7dfd..855537b8e 100644 --- a/contracts/src/token/erc1155/extensions/supply.rs +++ b/contracts/src/token/erc1155/extensions/supply.rs @@ -1,7 +1,7 @@ //! Extension of ERC-1155 that adds tracking of total supply per token id. //! //! Useful for scenarios where Fungible and Non-fungible tokens have to be -//! clearly identified. Note: While a `_total_supply` of 1 might mean the +//! clearly identified. Note: While a `total_supply` of 1 might mean the //! corresponding is an NFT, there are no guarantees that no other tokens //! with the same id are not going to be minted. //! @@ -33,11 +33,9 @@ pub struct Erc1155Supply { /// [`Erc1155`] contract. pub erc1155: Erc1155, /// Mapping from token id to total supply. - #[allow(clippy::used_underscore_binding)] - pub _total_supply: StorageMap, + pub(crate) total_supply: StorageMap, /// Total supply of all token ids. - #[allow(clippy::used_underscore_binding)] - pub _total_supply_all: StorageU256, + pub(crate) total_supply_all: StorageU256, } /// Required interface of a [`Erc1155Supply`] contract. @@ -70,11 +68,11 @@ pub trait IErc1155Supply { impl IErc1155Supply for Erc1155Supply { fn total_supply(&self, id: U256) -> U256 { - self._total_supply.get(id) + self.total_supply.get(id) } fn total_supply_all(&self) -> U256 { - *self._total_supply_all + *self.total_supply_all } fn exists(&self, id: U256) -> bool { @@ -235,16 +233,16 @@ impl Erc1155Supply { if from.is_zero() { for (&token_id, &value) in token_ids.iter().zip(values.iter()) { - self._total_supply.setter(token_id).add_assign_checked( + self.total_supply.setter(token_id).add_assign_checked( value, - "should not exceed `U256::MAX` for `_total_supply`", + "should not exceed `U256::MAX` for `total_supply`", ); } let total_mint_value = values.iter().sum(); - self._total_supply_all.add_assign_checked( + self.total_supply_all.add_assign_checked( total_mint_value, - "should not exceed `U256::MAX` for `_total_supply_all`", + "should not exceed `U256::MAX` for `total_supply_all`", ); } @@ -255,7 +253,7 @@ impl Erc1155Supply { * values[i] <= balance_of(from, token_ids[i]) <= * total_supply(token_ids[i]) */ - self._total_supply.setter(token_id).sub_assign_unchecked(value); + self.total_supply.setter(token_id).sub_assign_unchecked(value); } let total_burn_value: U256 = values.into_iter().sum(); @@ -264,7 +262,7 @@ impl Erc1155Supply { * total_burn_value = sum_i(values[i]) <= * sum_i(total_supply(ids[i])) <= total_supply_all */ - self._total_supply_all.sub_assign_unchecked(total_burn_value); + self.total_supply_all.sub_assign_unchecked(total_burn_value); } Ok(()) } @@ -458,7 +456,7 @@ mod tests { } #[motsu::test] - #[should_panic = "should not exceed `U256::MAX` for `_total_supply`"] + #[should_panic = "should not exceed `U256::MAX` for `total_supply`"] fn mint_panics_on_total_supply_overflow( contract: Contract, alice: Address, @@ -481,7 +479,7 @@ mod tests { } #[motsu::test] - #[should_panic = "should not exceed `U256::MAX` for `_total_supply_all`"] + #[should_panic = "should not exceed `U256::MAX` for `total_supply_all`"] fn mint_panics_on_total_supply_all_overflow( contract: Contract, alice: Address, diff --git a/contracts/src/token/erc1155/extensions/uri_storage.rs b/contracts/src/token/erc1155/extensions/uri_storage.rs index e54728dd5..29d9254ae 100644 --- a/contracts/src/token/erc1155/extensions/uri_storage.rs +++ b/contracts/src/token/erc1155/extensions/uri_storage.rs @@ -16,11 +16,9 @@ use super::metadata_uri::{IErc1155MetadataUri, URI}; #[storage] pub struct Erc1155UriStorage { /// Optional base URI. - #[allow(clippy::used_underscore_binding)] - pub _base_uri: StorageString, + pub(crate) base_uri: StorageString, /// Optional mapping for token URIs. - #[allow(clippy::used_underscore_binding)] - pub _token_uris: StorageMap, + pub(crate) token_uris: StorageMap, } impl Erc1155UriStorage { @@ -51,12 +49,12 @@ impl Erc1155UriStorage { token_id: U256, metadata_uri: &impl IErc1155MetadataUri, ) -> String { - let token_uri = self._token_uris.get(token_id).get_string(); + let token_uri = self.token_uris.get(token_id).get_string(); if token_uri.is_empty() { metadata_uri.uri(token_id) } else { - self._base_uri.get_string() + &token_uri + self.base_uri.get_string() + &token_uri } } @@ -79,18 +77,18 @@ impl Erc1155UriStorage { token_uri: String, metadata_uri: &impl IErc1155MetadataUri, ) { - self._token_uris.setter(token_id).set_str(token_uri); + self.token_uris.setter(token_id).set_str(token_uri); evm::log(URI { value: self.uri(token_id, metadata_uri), id: token_id }); } - /// Sets `base_uri` as the `_base_uri` for all tokens. + /// Sets `base_uri` as the `base_uri` for all tokens. /// /// # Arguments /// /// * `&mut self` - Write access to the contract's state. /// * `base_uri` - New base URI. pub fn set_base_uri(&mut self, base_uri: String) { - self._base_uri.set_str(base_uri); + self.base_uri.set_str(base_uri); } } @@ -129,7 +127,7 @@ mod tests { let uri = "https://some.metadata/token/uri"; contract.init(alice, |contract| { - contract.metadata_uri._uri.set_str(uri.to_owned()); + contract.metadata_uri.uri.set_str(uri.to_owned()); }); assert_eq!(uri, contract.sender(alice).uri(TOKEN_ID)); @@ -153,7 +151,7 @@ mod tests { contract.init(alice, |contract| { contract .uri_storage - ._token_uris + .token_uris .setter(TOKEN_ID) .set_str(token_uri.to_owned()); }); @@ -170,10 +168,10 @@ mod tests { let token_uri = "/some/token/uri"; contract.init(alice, |contract| { - contract.uri_storage._base_uri.set_str(base_uri.to_owned()); + contract.uri_storage.base_uri.set_str(base_uri.to_owned()); contract .uri_storage - ._token_uris + .token_uris .setter(TOKEN_ID) .set_str(token_uri.to_owned()); }); @@ -193,10 +191,10 @@ mod tests { let token_uri = "https://some.short/token/uri"; contract.init(alice, |contract| { - contract.metadata_uri._uri.set_str(uri.to_owned()); + contract.metadata_uri.uri.set_str(uri.to_owned()); contract .uri_storage - ._token_uris + .token_uris .setter(TOKEN_ID) .set_str(token_uri.to_owned()); }); @@ -212,7 +210,7 @@ mod tests { let token_uri = "https://some.short/token/uri".to_string(); contract.init(alice, |contract| { - contract.metadata_uri._uri.set_str(uri.to_owned()); + contract.metadata_uri.uri.set_str(uri.to_owned()); contract.uri_storage.set_token_uri( TOKEN_ID, token_uri.clone(), @@ -234,7 +232,7 @@ mod tests { assert_eq!( base_uri, - contract.sender(alice).uri_storage._base_uri.get_string() + contract.sender(alice).uri_storage.base_uri.get_string() ); } } diff --git a/contracts/src/token/erc1155/mod.rs b/contracts/src/token/erc1155/mod.rs index 7d7163cd5..ea7b3aff4 100644 --- a/contracts/src/token/erc1155/mod.rs +++ b/contracts/src/token/erc1155/mod.rs @@ -197,11 +197,9 @@ impl MethodError for Error { #[storage] pub struct Erc1155 { /// Maps users to balances. - #[allow(clippy::used_underscore_binding)] - pub _balances: StorageMap>, + pub(crate) balances: StorageMap>, /// Maps owners to a mapping of operator approvals. - #[allow(clippy::used_underscore_binding)] - pub _operator_approvals: + pub(crate) operator_approvals: StorageMap>, } @@ -361,7 +359,7 @@ impl IErc1155 for Erc1155 { type Error = Error; fn balance_of(&self, account: Address, id: U256) -> U256 { - self._balances.get(id).get(account) + self.balances.get(id).get(account) } fn balance_of_batch( @@ -389,7 +387,7 @@ impl IErc1155 for Erc1155 { } fn is_approved_for_all(&self, account: Address, operator: Address) -> bool { - self._operator_approvals.get(account).get(operator) + self.operator_approvals.get(account).get(operator) } fn safe_transfer_from( @@ -705,7 +703,7 @@ impl Erc1155 { operator, })); } - self._operator_approvals.setter(owner).setter(operator).set(approved); + self.operator_approvals.setter(owner).setter(operator).set(approved); evm::log(ApprovalForAll { account: owner, operator, approved }); Ok(()) } @@ -985,16 +983,16 @@ impl Erc1155 { }, )); } - self._balances + self.balances .setter(token_id) .setter(from) .sub_assign_unchecked(value); } if !to.is_zero() { - self._balances.setter(token_id).setter(to).add_assign_checked( + self.balances.setter(token_id).setter(to).add_assign_checked( value, - "should not exceed `U256::MAX` for `_balances`", + "should not exceed `U256::MAX` for `balances`", ); } @@ -1252,7 +1250,7 @@ mod tests { bob: Address, ) { contract.init(alice, |contract| { - contract._operator_approvals.setter(alice).setter(bob).set(false); + contract.operator_approvals.setter(alice).setter(bob).set(false); }); contract diff --git a/contracts/src/token/erc20/extensions/capped.rs b/contracts/src/token/erc20/extensions/capped.rs index 959e090aa..b97dbfd54 100644 --- a/contracts/src/token/erc20/extensions/capped.rs +++ b/contracts/src/token/erc20/extensions/capped.rs @@ -21,7 +21,7 @@ mod sol { sol! { /// Indicates an error related to the operation that failed - /// because `total_supply` exceeded the `_cap`. + /// because `total_supply` exceeded the `cap`. #[derive(Debug)] #[allow(missing_docs)] error ERC20ExceededCap(uint256 increased_supply, uint256 cap); @@ -38,7 +38,7 @@ mod sol { #[derive(SolidityError, Debug)] pub enum Error { /// Indicates an error related to the operation that failed - /// because `total_supply` exceeded the `_cap`. + /// because `total_supply` exceeded the `cap`. ExceededCap(ERC20ExceededCap), /// Indicates an error related to the operation that failed /// because the supplied `cap` is not a valid cap value. @@ -49,15 +49,14 @@ pub enum Error { #[storage] pub struct Capped { /// A cap to the supply of tokens. - #[allow(clippy::used_underscore_binding)] - pub _cap: StorageU256, + pub(crate) cap: StorageU256, } #[public] impl Capped { /// Returns the cap on the token's total supply. pub fn cap(&self) -> U256 { - self._cap.get() + self.cap.get() } } @@ -74,11 +73,11 @@ mod tests { #[motsu::test] fn cap_works(contract: Contract, alice: Address) { let value = uint!(2024_U256); - contract.init(alice, |contract| contract._cap.set(value)); + contract.init(alice, |contract| contract.cap.set(value)); assert_eq!(contract.sender(alice).cap(), value); let value = uint!(1_U256); - contract.init(alice, |contract| contract._cap.set(value)); + contract.init(alice, |contract| contract.cap.set(value)); assert_eq!(contract.sender(alice).cap(), value); } } diff --git a/contracts/src/token/erc20/extensions/flash_mint.rs b/contracts/src/token/erc20/extensions/flash_mint.rs index cd7699f76..573a3bd8c 100644 --- a/contracts/src/token/erc20/extensions/flash_mint.rs +++ b/contracts/src/token/erc20/extensions/flash_mint.rs @@ -117,9 +117,9 @@ mod borrower { #[storage] pub struct Erc20FlashMint { /// Fee applied when doing flash loans. - pub flash_fee_value: StorageU256, + pub(crate) flash_fee_value: StorageU256, /// Receiver address of the flash fee. - pub flash_fee_receiver_address: StorageAddress, + pub(crate) flash_fee_receiver_address: StorageAddress, } /// NOTE: Implementation of [`TopLevelStorage`] to be able use `&mut self` when diff --git a/contracts/src/token/erc20/extensions/metadata.rs b/contracts/src/token/erc20/extensions/metadata.rs index b0c46c37d..e465c84dc 100644 --- a/contracts/src/token/erc20/extensions/metadata.rs +++ b/contracts/src/token/erc20/extensions/metadata.rs @@ -19,8 +19,7 @@ use crate::utils::Metadata; #[storage] pub struct Erc20Metadata { /// [`Metadata`] contract. - #[allow(clippy::used_underscore_binding)] - pub _metadata: Metadata, + pub(crate) metadata: Metadata, } /// Interface for the optional metadata functions from the ERC-20 standard. @@ -64,11 +63,11 @@ pub trait IErc20Metadata { #[public] impl IErc20Metadata for Erc20Metadata { fn name(&self) -> String { - self._metadata.name() + self.metadata.name() } fn symbol(&self) -> String { - self._metadata.symbol() + self.metadata.symbol() } fn decimals(&self) -> u8 { diff --git a/contracts/src/token/erc20/extensions/permit.rs b/contracts/src/token/erc20/extensions/permit.rs index 800a29734..feef2faa5 100644 --- a/contracts/src/token/erc20/extensions/permit.rs +++ b/contracts/src/token/erc20/extensions/permit.rs @@ -77,11 +77,13 @@ pub enum Error { #[storage] pub struct Erc20Permit { /// [`Erc20`] contract. + // We leave the parent ERC-20 contract instance public, so that inheritting + // contract have access to its internal functions. pub erc20: Erc20, /// [`Nonces`] contract. - pub nonces: Nonces, + pub(crate) nonces: Nonces, /// Contract implementing [`IEip712`] trait. - pub eip712: T, + pub(crate) eip712: T, } /// NOTE: Implementation of [`TopLevelStorage`] to be able use `&mut self` when diff --git a/contracts/src/token/erc20/mod.rs b/contracts/src/token/erc20/mod.rs index de34acb93..1d3de353e 100644 --- a/contracts/src/token/erc20/mod.rs +++ b/contracts/src/token/erc20/mod.rs @@ -128,14 +128,12 @@ impl MethodError for Error { #[storage] pub struct Erc20 { /// Maps users to balances. - #[allow(clippy::used_underscore_binding)] - pub _balances: StorageMap, + pub(crate) balances: StorageMap, /// Maps users to a mapping of each spender's allowance. - #[allow(clippy::used_underscore_binding)] - pub _allowances: StorageMap>, + pub(crate) allowances: + StorageMap>, /// The total supply of the token. - #[allow(clippy::used_underscore_binding)] - pub _total_supply: StorageU256, + pub(crate) total_supply: StorageU256, } /// Required interface of an [`Erc20`] compliant contract. @@ -272,11 +270,11 @@ impl IErc20 for Erc20 { type Error = Error; fn total_supply(&self) -> U256 { - self._total_supply.get() + self.total_supply.get() } fn balance_of(&self, account: Address) -> U256 { - self._balances.get(account) + self.balances.get(account) } fn transfer( @@ -290,7 +288,7 @@ impl IErc20 for Erc20 { } fn allowance(&self, owner: Address, spender: Address) -> U256 { - self._allowances.get(owner).get(spender) + self.allowances.get(owner).get(spender) } fn approve( @@ -361,7 +359,7 @@ impl Erc20 { })); } - self._allowances.setter(owner).insert(spender, value); + self.allowances.setter(owner).insert(spender, value); if emit_event { evm::log(Approval { owner, spender, value }); } @@ -423,7 +421,7 @@ impl Erc20 { /// /// # Panics /// - /// * If `_total_supply` exceeds `U256::MAX`. + /// * If `total_supply` exceeds `U256::MAX`. pub fn _mint( &mut self, account: Address, @@ -460,7 +458,7 @@ impl Erc20 { /// /// # Panics /// - /// * If `_total_supply` exceeds `U256::MAX`. It may happen during `mint` + /// * If `total_supply` exceeds `U256::MAX`. It may happen during `mint` /// operation. pub fn _update( &mut self, @@ -470,13 +468,13 @@ impl Erc20 { ) -> Result<(), Error> { if from.is_zero() { // Mint operation. Overflow check required: the rest of the code - // assumes that `_total_supply` never overflows. - self._total_supply.add_assign_checked( + // assumes that `total_supply` never overflows. + self.total_supply.add_assign_checked( value, - "should not exceed `U256::MAX` for `_total_supply`", + "should not exceed `U256::MAX` for `total_supply`", ); } else { - let from_balance = self._balances.get(from); + let from_balance = self.balances.get(from); if from_balance < value { return Err(Error::InsufficientBalance( ERC20InsufficientBalance { @@ -487,20 +485,20 @@ impl Erc20 { )); } // Overflow not possible: - // `value` <= `from_balance` <= `_total_supply`. - self._balances.setter(from).set(from_balance - value); + // `value` <= `from_balance` <= `total_supply`. + self.balances.setter(from).set(from_balance - value); } if to.is_zero() { // Overflow not possible: - // `value` <= `_total_supply` or - // `value` <= `from_balance` <= `_total_supply`. - self._total_supply.sub_assign_unchecked(value); + // `value` <= `total_supply` or + // `value` <= `from_balance` <= `total_supply`. + self.total_supply.sub_assign_unchecked(value); } else { // Overflow not possible: // `balance_to` + `value` is at most `total_supply`, // which fits into a `U256`. - self._balances.setter(to).add_assign_unchecked(value); + self.balances.setter(to).add_assign_unchecked(value); } evm::log(Transfer { from, to, value }); @@ -614,7 +612,7 @@ mod tests { } #[motsu::test] - #[should_panic = "should not exceed `U256::MAX` for `_total_supply`"] + #[should_panic = "should not exceed `U256::MAX` for `total_supply`"] fn update_mint_errors_arithmetic_overflow( contract: Contract, alice: Address, @@ -630,7 +628,7 @@ mod tests { ._update(Address::ZERO, alice, U256::MAX) .expect("should mint tokens"); // Mint action should NOT work: - // overflow on `_total_supply`. + // overflow on `total_supply`. let _result = contract.sender(alice)._update(Address::ZERO, alice, one); } @@ -676,7 +674,7 @@ mod tests { } #[motsu::test] - #[should_panic = "should not exceed `U256::MAX` for `_total_supply`"] + #[should_panic = "should not exceed `U256::MAX` for `total_supply`"] fn mint_errors_arithmetic_overflow( contract: Contract, alice: Address, diff --git a/contracts/src/token/erc20/utils/safe_erc20.rs b/contracts/src/token/erc20/utils/safe_erc20.rs index 1594dbd9d..6b3a86ce8 100644 --- a/contracts/src/token/erc20/utils/safe_erc20.rs +++ b/contracts/src/token/erc20/utils/safe_erc20.rs @@ -87,7 +87,7 @@ mod token { /// State of a [`SafeErc20`] Contract. #[storage] -pub struct SafeErc20 {} +pub struct SafeErc20; /// NOTE: Implementation of [`TopLevelStorage`] to be able use `&mut self` when /// calling other contracts and not `&mut (impl TopLevelStorage + diff --git a/contracts/src/token/erc721/extensions/consecutive.rs b/contracts/src/token/erc721/extensions/consecutive.rs index cba70b94b..6ce09473b 100644 --- a/contracts/src/token/erc721/extensions/consecutive.rs +++ b/contracts/src/token/erc721/extensions/consecutive.rs @@ -11,8 +11,8 @@ //! contract construction. This ability is regained after construction. During //! construction, only batch minting is allowed. //! -//! Fields `_first_consecutive_id` (used to offset first token id) and -//! `_max_batch_size` (used to restrict maximum batch size) can be assigned +//! Fields `first_consecutive_id` (used to offset first token id) and +//! `max_batch_size` (used to restrict maximum batch size) can be assigned //! during construction with `koba` (stylus construction tooling) within //! solidity constructor file. //! @@ -59,20 +59,16 @@ pub struct Erc721Consecutive { /// [`Erc721`] contract. pub erc721: Erc721, /// [`Trace`] contract for sequential ownership. - #[allow(clippy::used_underscore_binding)] - pub _sequential_ownership: Trace, + pub(crate) sequential_ownership: Trace, /// [`BitMap`] contract for sequential burn of tokens. - #[allow(clippy::used_underscore_binding)] - pub _sequential_burn: BitMap, + pub(crate) sequential_burn: BitMap, /// Used to offset the first token id in `next_consecutive_id` calculation. - #[allow(clippy::used_underscore_binding)] - pub _first_consecutive_id: StorageU96, + pub(crate) first_consecutive_id: StorageU96, /// Maximum size of a batch of consecutive tokens. This is designed to /// limit stress on off-chain indexing services that have to record one /// entry per token, and have protections against "unreasonably large" /// batches of tokens. - #[allow(clippy::used_underscore_binding)] - pub _max_batch_size: StorageU96, + pub(crate) max_batch_size: StorageU96, } pub use sol::*; @@ -262,11 +258,11 @@ impl Erc721Consecutive { // Otherwise, check the token was not burned, and fetch ownership from // the anchors. - if self._sequential_burn.get(token_id) { + if self.sequential_burn.get(token_id) { Address::ZERO } else { // NOTE: Bounds already checked. No need for safe cast of token_id - self._sequential_ownership.lower_lookup(U96::from(token_id)).into() + self.sequential_ownership.lower_lookup(U96::from(token_id)).into() } } @@ -291,13 +287,12 @@ impl Erc721Consecutive { /// /// * [`erc721::Error::InvalidReceiver`] - If `to` is `Address::ZERO`. /// * [`Error::ExceededMaxBatchMint`] - If `batch_size` exceeds - /// [`Erc721Consecutive::_max_batch_size`]. + /// `max_batch_size` of the contract. /// /// # Events /// /// * [`ConsecutiveTransfer`]. - #[cfg(all(test, feature = "std"))] - fn _mint_consecutive( + pub fn _mint_consecutive( &mut self, to: Address, batch_size: U96, @@ -323,7 +318,7 @@ impl Erc721Consecutive { // Push an ownership checkpoint & emit event. let last = next + batch_size - uint!(1_U96); - self._sequential_ownership.push(last, to.into())?; + self.sequential_ownership.push(last, to.into())?; // The invariant required by this function is preserved because the // new sequential_ownership checkpoint is attributing @@ -377,10 +372,10 @@ impl Erc721Consecutive { // and the token_id was minted in a batch && token_id < U256::from(self._next_consecutive_id()) // and the token was never marked as burnt - && !self._sequential_burn.get(token_id) + && !self.sequential_burn.get(token_id) { // record burn - self._sequential_burn.set(token_id); + self.sequential_burn.set(token_id); } Ok(previous_owner) @@ -394,7 +389,7 @@ impl Erc721Consecutive { /// /// * `&self` - Read access to the contract's state. fn _next_consecutive_id(&self) -> U96 { - match self._sequential_ownership.latest_checkpoint() { + match self.sequential_ownership.latest_checkpoint() { None => self._first_consecutive_id(), Some((latest_id, _)) => latest_id + uint!(1_U96), } @@ -407,7 +402,7 @@ impl Erc721Consecutive { /// /// * `&self` - Read access to the contract's state. fn _first_consecutive_id(&self) -> U96 { - self._first_consecutive_id.get() + self.first_consecutive_id.get() } /// Maximum size of consecutive token's batch. @@ -419,7 +414,7 @@ impl Erc721Consecutive { /// /// * `&self` - Read access to the contract's state. fn _max_batch_size(&self) -> U96 { - self._max_batch_size.get() + self.max_batch_size.get() } } @@ -455,19 +450,16 @@ impl Erc721Consecutive { // event. self._approve(Address::ZERO, token_id, Address::ZERO, false)?; self.erc721 - ._balances + .balances .setter(from) .sub_assign_unchecked(uint!(1_U256)); } if !to.is_zero() { - self.erc721 - ._balances - .setter(to) - .add_assign_unchecked(uint!(1_U256)); + self.erc721.balances.setter(to).add_assign_unchecked(uint!(1_U256)); } - self.erc721._owners.setter(token_id).set(to); + self.erc721.owners.setter(token_id).set(to); evm::log(Transfer { from, to, token_id }); Ok(from) } @@ -729,7 +721,7 @@ impl Erc721Consecutive { } } - self.erc721._token_approvals.setter(token_id).set(to); + self.erc721.token_approvals.setter(token_id).set(to); Ok(()) } @@ -785,8 +777,8 @@ mod tests { receivers: Vec
, batches: Vec, ) { - contract._first_consecutive_id.set(FIRST_CONSECUTIVE_TOKEN_ID); - contract._max_batch_size.set(MAX_BATCH_SIZE); + contract.first_consecutive_id.set(FIRST_CONSECUTIVE_TOKEN_ID); + contract.max_batch_size.set(MAX_BATCH_SIZE); for (to, batch_size) in receivers.into_iter().zip(batches) { contract ._mint_consecutive(to, batch_size) diff --git a/contracts/src/token/erc721/extensions/enumerable.rs b/contracts/src/token/erc721/extensions/enumerable.rs index cef2ab119..a01547eea 100644 --- a/contracts/src/token/erc721/extensions/enumerable.rs +++ b/contracts/src/token/erc721/extensions/enumerable.rs @@ -64,17 +64,13 @@ pub enum Error { #[storage] pub struct Erc721Enumerable { /// Maps owners to a mapping of indices to tokens ids. - #[allow(clippy::used_underscore_binding)] - pub _owned_tokens: StorageMap>, - /// Maps tokens ids to indices in `_owned_tokens`. - #[allow(clippy::used_underscore_binding)] - pub _owned_tokens_index: StorageMap, + pub(crate) owned_tokens: StorageMap>, + /// Maps tokens ids to indices in `owned_tokens`. + pub(crate) owned_tokens_index: StorageMap, /// Stores all tokens ids. - #[allow(clippy::used_underscore_binding)] - pub _all_tokens: StorageVec, - /// Maps indices at `_all_tokens` to tokens ids. - #[allow(clippy::used_underscore_binding)] - pub _all_tokens_index: StorageMap, + pub(crate) all_tokens: StorageVec, + /// Maps indices at `all_tokens` to tokens ids. + pub(crate) all_tokens_index: StorageMap, } /// This is the interface of the optional `Enumerable` extension @@ -141,7 +137,7 @@ impl IErc721Enumerable for Erc721Enumerable { owner: Address, index: U256, ) -> Result { - let token = self._owned_tokens.getter(owner).get(index); + let token = self.owned_tokens.getter(owner).get(index); if token.is_zero() { Err(ERC721OutOfBoundsIndex { owner, index }.into()) @@ -151,12 +147,12 @@ impl IErc721Enumerable for Erc721Enumerable { } fn total_supply(&self) -> U256 { - let tokens_length = self._all_tokens.len(); + let tokens_length = self.all_tokens.len(); U256::from(tokens_length) } fn token_by_index(&self, index: U256) -> Result { - self._all_tokens.get(index).ok_or( + self.all_tokens.get(index).ok_or( ERC721OutOfBoundsIndex { owner: Address::ZERO, index }.into(), ) } @@ -191,8 +187,8 @@ impl Erc721Enumerable { erc721: &impl IErc721, ) -> Result<(), erc721::Error> { let length = erc721.balance_of(to)? - uint!(1_U256); - self._owned_tokens.setter(to).setter(length).set(token_id); - self._owned_tokens_index.setter(token_id).set(length); + self.owned_tokens.setter(to).setter(length).set(token_id); + self.owned_tokens_index.setter(token_id).set(length); Ok(()) } @@ -207,20 +203,20 @@ impl Erc721Enumerable { pub fn _add_token_to_all_tokens_enumeration(&mut self, token_id: U256) { let index = self.total_supply(); - self._all_tokens_index.setter(token_id).set(index); - self._all_tokens.push(token_id); + self.all_tokens_index.setter(token_id).set(index); + self.all_tokens.push(token_id); } /// Function to remove a token from this extension's /// ownership-tracking data structures. /// /// Note that while the token is not assigned a new owner, - /// the `self._owned_tokens_index` mapping is NOT updated: + /// the `self.owned_tokens_index` mapping is NOT updated: /// this allows for gas optimizations e.g. /// when performing a transfer operation (avoiding double writes). /// /// This has O(1) time complexity, but alters the order - /// of the `self._owned_tokens` array. + /// of the `self.owned_tokens` array. /// /// # Arguments /// @@ -244,9 +240,9 @@ impl Erc721Enumerable { // we store the last token in the index of the token to delete, // and then delete the last slot (swap and pop). let last_token_index = erc721.balance_of(from)?; - let token_index = self._owned_tokens_index.get(token_id); + let token_index = self.owned_tokens_index.get(token_id); - let mut owned_tokens_by_owner = self._owned_tokens.setter(from); + let mut owned_tokens_by_owner = self.owned_tokens.setter(from); // When the token to delete is the last token, // the swap operation is unnecessary. @@ -256,11 +252,11 @@ impl Erc721Enumerable { // Move the last token to the slot of the to-delete token. owned_tokens_by_owner.setter(token_index).set(last_token_id); // Update the moved token's index. - self._owned_tokens_index.setter(last_token_id).set(token_index); + self.owned_tokens_index.setter(last_token_id).set(token_index); } // This also deletes the contents at the last position of the array. - self._owned_tokens_index.delete(token_id); + self.owned_tokens_index.delete(token_id); owned_tokens_by_owner.delete(last_token_index); Ok(()) @@ -270,7 +266,7 @@ impl Erc721Enumerable { /// token tracking data structures. /// /// This has O(1) time complexity, - /// but alters the order of the `self._all_tokens` array. + /// but alters the order of the `self.all_tokens` array. /// /// # Arguments /// @@ -287,8 +283,8 @@ impl Erc721Enumerable { // To prevent a gap in the tokens array, // we store the last token in the index of the token to delete, // and then delete the last slot (swap and pop). - let last_token_index = U256::from(self._all_tokens.len() - 1); - let token_index = self._all_tokens_index.get(token_id); + let last_token_index = U256::from(self.all_tokens.len() - 1); + let token_index = self.all_tokens_index.get(token_id); // When the token to delete is the last token, // the swap operation is unnecessary. @@ -298,22 +294,22 @@ impl Erc721Enumerable { // to avoid the gas cost of adding an 'if' statement // (like in `self._remove_token_from_owner_enumeration`). let last_token_id = self - ._all_tokens + .all_tokens .get(last_token_index) .expect("token at given index must exist"); // Move the last token to the slot of the to-delete token. - self._all_tokens + self.all_tokens .setter(token_index) .expect("slot at given `token_index` must exist") .set(last_token_id); // Update the moved token's index. - self._all_tokens_index.setter(last_token_id).set(token_index); + self.all_tokens_index.setter(last_token_id).set(token_index); // This also deletes the contents at the last position of the array. - self._all_tokens_index.delete(token_id); - self._all_tokens.pop(); + self.all_tokens_index.delete(token_id); + self.all_tokens.pop(); } /// See [`erc721::Erc721::_increase_balance`]. diff --git a/contracts/src/token/erc721/extensions/metadata.rs b/contracts/src/token/erc721/extensions/metadata.rs index 862c44e51..265e4639c 100644 --- a/contracts/src/token/erc721/extensions/metadata.rs +++ b/contracts/src/token/erc721/extensions/metadata.rs @@ -20,11 +20,9 @@ use crate::{ #[storage] pub struct Erc721Metadata { /// [`Metadata`] contract. - #[allow(clippy::used_underscore_binding)] - pub _metadata: Metadata, + pub(crate) metadata: Metadata, /// Base URI for tokens. - #[allow(clippy::used_underscore_binding)] - pub _base_uri: StorageString, + pub(crate) base_uri: StorageString, } /// Interface for the optional metadata functions from the ERC-721 standard. @@ -51,11 +49,11 @@ pub trait IErc721Metadata { #[public] impl IErc721Metadata for Erc721Metadata { fn name(&self) -> String { - self._metadata.name() + self.metadata.name() } fn symbol(&self) -> String { - self._metadata.symbol() + self.metadata.symbol() } } @@ -79,7 +77,7 @@ impl Erc721Metadata { /// /// * `&self` - Read access to the contract's state. pub fn base_uri(&self) -> String { - self._base_uri.get_string() + self.base_uri.get_string() } /// Returns the Uniform Resource Identifier (URI) for `token_id` token. diff --git a/contracts/src/token/erc721/extensions/uri_storage.rs b/contracts/src/token/erc721/extensions/uri_storage.rs index 687788615..9d92a8e87 100644 --- a/contracts/src/token/erc721/extensions/uri_storage.rs +++ b/contracts/src/token/erc721/extensions/uri_storage.rs @@ -37,8 +37,7 @@ mod sol { #[storage] pub struct Erc721UriStorage { /// Optional mapping for token URIs. - #[allow(clippy::used_underscore_binding)] - pub _token_uris: StorageMap, + pub token_uris: StorageMap, } impl Erc721UriStorage { @@ -54,7 +53,7 @@ impl Erc721UriStorage { /// /// * [`MetadataUpdate`]. pub fn _set_token_uri(&mut self, token_id: U256, token_uri: String) { - self._token_uris.setter(token_id).set_str(token_uri); + self.token_uris.setter(token_id).set_str(token_uri); evm::log(MetadataUpdate { token_id }); } @@ -96,9 +95,9 @@ impl Erc721UriStorage { erc721: &impl IErc721, metadata: &Erc721Metadata, ) -> Result { - let _owner = erc721.owner_of(token_id)?; + erc721.owner_of(token_id)?; - let token_uri = self._token_uris.getter(token_id).get_string(); + let token_uri = self.token_uris.getter(token_id).get_string(); let base = metadata.base_uri(); // If there is no base URI, return the token URI. diff --git a/contracts/src/token/erc721/mod.rs b/contracts/src/token/erc721/mod.rs index 32f90299f..1386ebf67 100644 --- a/contracts/src/token/erc721/mod.rs +++ b/contracts/src/token/erc721/mod.rs @@ -183,17 +183,13 @@ impl MethodError for Error { #[storage] pub struct Erc721 { /// Maps tokens to owners. - #[allow(clippy::used_underscore_binding)] - pub _owners: StorageMap, + pub(crate) owners: StorageMap, /// Maps users to balances. - #[allow(clippy::used_underscore_binding)] - pub _balances: StorageMap, + pub(crate) balances: StorageMap, /// Maps tokens to approvals. - #[allow(clippy::used_underscore_binding)] - pub _token_approvals: StorageMap, + pub(crate) token_approvals: StorageMap, /// Maps owners to a mapping of operator approvals. - #[allow(clippy::used_underscore_binding)] - pub _operator_approvals: + pub(crate) operator_approvals: StorageMap>, } @@ -413,7 +409,7 @@ impl IErc721 for Erc721 { if owner.is_zero() { return Err(ERC721InvalidOwner { owner: Address::ZERO }.into()); } - Ok(self._balances.get(owner)) + Ok(self.balances.get(owner)) } fn owner_of(&self, token_id: U256) -> Result { @@ -486,7 +482,7 @@ impl IErc721 for Erc721 { } fn is_approved_for_all(&self, owner: Address, operator: Address) -> bool { - self._operator_approvals.get(owner).get(operator) + self.operator_approvals.get(owner).get(operator) } } @@ -514,7 +510,7 @@ impl Erc721 { /// * `token_id` - Token id as a number. #[must_use] pub fn _owner_of(&self, token_id: U256) -> Address { - self._owners.get(token_id) + self.owners.get(token_id) } /// Returns the approved address for `token_id`. @@ -526,7 +522,7 @@ impl Erc721 { /// * `token_id` - Token id as a number. #[must_use] pub fn _get_approved(&self, token_id: U256) -> Address { - self._token_approvals.get(token_id) + self.token_approvals.get(token_id) } /// Returns whether `spender` is allowed to manage `owner`'s tokens, or @@ -610,7 +606,7 @@ impl Erc721 { /// * `account` - Account to increase balance. /// * `value` - The number of tokens to increase balance. pub fn _increase_balance(&mut self, account: Address, value: U128) { - self._balances.setter(account).add_assign_unchecked(U256::from(value)); + self.balances.setter(account).add_assign_unchecked(U256::from(value)); } /// Transfers `token_id` from its current owner to `to`, or alternatively @@ -659,14 +655,14 @@ impl Erc721 { // Clear approval. No need to re-authorize or emit the `Approval` // event. self._approve(Address::ZERO, token_id, Address::ZERO, false)?; - self._balances.setter(from).sub_assign_unchecked(uint!(1_U256)); + self.balances.setter(from).sub_assign_unchecked(uint!(1_U256)); } if !to.is_zero() { - self._balances.setter(to).add_assign_unchecked(uint!(1_U256)); + self.balances.setter(to).add_assign_unchecked(uint!(1_U256)); } - self._owners.setter(token_id).set(to); + self.owners.setter(token_id).set(to); evm::log(Transfer { from, to, token_id }); Ok(from) } @@ -908,7 +904,7 @@ impl Erc721 { } } - self._token_approvals.setter(token_id).set(to); + self.token_approvals.setter(token_id).set(to); Ok(()) } @@ -938,7 +934,7 @@ impl Erc721 { return Err(ERC721InvalidOperator { operator }.into()); } - self._operator_approvals.setter(owner).setter(operator).set(approved); + self.operator_approvals.setter(owner).setter(operator).set(approved); evm::log(ApprovalForAll { owner, operator, approved }); Ok(()) } @@ -1445,7 +1441,7 @@ mod tests { .sender(alice) ._mint(bob, TOKEN_ID) .expect("should mint token to Bob"); - contract.sender(alice)._token_approvals.setter(TOKEN_ID).set(alice); + contract.sender(alice).token_approvals.setter(TOKEN_ID).set(alice); contract .sender(alice) .safe_transfer_from(bob, alice, TOKEN_ID) diff --git a/contracts/src/utils/cryptography/eip712.rs b/contracts/src/utils/cryptography/eip712.rs index f69d63809..18a24dd23 100644 --- a/contracts/src/utils/cryptography/eip712.rs +++ b/contracts/src/utils/cryptography/eip712.rs @@ -147,7 +147,7 @@ mod tests { address!("000000000000000000000000000000000000dEaD"); #[derive(Default)] - struct TestEIP712 {} + struct TestEIP712; impl IEip712 for TestEIP712 { const NAME: &'static str = "A Name"; diff --git a/contracts/src/utils/metadata.rs b/contracts/src/utils/metadata.rs index 6389ccfcc..fd7bda203 100644 --- a/contracts/src/utils/metadata.rs +++ b/contracts/src/utils/metadata.rs @@ -9,11 +9,9 @@ use stylus_sdk::{ #[storage] pub struct Metadata { /// Token name. - #[allow(clippy::used_underscore_binding)] - pub _name: StorageString, + pub(crate) name: StorageString, /// Token symbol. - #[allow(clippy::used_underscore_binding)] - pub _symbol: StorageString, + pub(crate) symbol: StorageString, } #[public] @@ -24,7 +22,7 @@ impl Metadata { /// /// * `&self` - Read access to the contract's state. pub fn name(&self) -> String { - self._name.get_string() + self.name.get_string() } /// Returns the symbol of the token, usually a shorter version of the name. @@ -33,6 +31,6 @@ impl Metadata { /// /// * `&self` - Read access to the contract's state. pub fn symbol(&self) -> String { - self._symbol.get_string() + self.symbol.get_string() } } diff --git a/contracts/src/utils/nonces.rs b/contracts/src/utils/nonces.rs index 9d26b0ce7..21088055c 100644 --- a/contracts/src/utils/nonces.rs +++ b/contracts/src/utils/nonces.rs @@ -39,8 +39,7 @@ pub enum Error { #[storage] pub struct Nonces { /// Mapping from address to its nonce. - #[allow(clippy::used_underscore_binding)] - pub _nonces: StorageMap, + pub(crate) nonces: StorageMap, } #[public] @@ -53,7 +52,7 @@ impl Nonces { /// * `owner` - The address for which to return the nonce. #[must_use] pub fn nonces(&self, owner: Address) -> U256 { - self._nonces.get(owner) + self.nonces.get(owner) } } @@ -69,9 +68,9 @@ impl Nonces { /// /// * If the nonce for the given `owner` exceeds `U256::MAX`. pub fn use_nonce(&mut self, owner: Address) -> U256 { - let nonce = self._nonces.get(owner); + let nonce = self.nonces.get(owner); - self._nonces + self.nonces .setter(owner) .add_assign_checked(ONE, "nonce should not exceed `U256::MAX`"); diff --git a/contracts/src/utils/pausable.rs b/contracts/src/utils/pausable.rs index 888818840..39f2242d6 100644 --- a/contracts/src/utils/pausable.rs +++ b/contracts/src/utils/pausable.rs @@ -68,8 +68,7 @@ pub enum Error { #[storage] pub struct Pausable { /// Indicates whether the contract is `Paused`. - #[allow(clippy::used_underscore_binding)] - pub _paused: StorageBool, + pub(crate) paused: StorageBool, } #[public] @@ -80,7 +79,7 @@ impl Pausable { /// /// * `&self` - Read access to the contract's state. fn paused(&self) -> bool { - self._paused.get() + self.paused.get() } } @@ -96,7 +95,7 @@ impl Pausable { /// * [`Error::EnforcedPause`] - If the contract is in `Paused` state. pub fn pause(&mut self) -> Result<(), Error> { self.when_not_paused()?; - self._paused.set(true); + self.paused.set(true); evm::log(Paused { account: msg::sender() }); Ok(()) } @@ -112,7 +111,7 @@ impl Pausable { /// * [`Error::ExpectedPause`] - If the contract is in `Unpaused` state. pub fn unpause(&mut self) -> Result<(), Error> { self.when_paused()?; - self._paused.set(false); + self.paused.set(false); evm::log(Unpaused { account: msg::sender() }); Ok(()) } @@ -128,7 +127,7 @@ impl Pausable { /// /// * [`Error::EnforcedPause`] - If the contract is in the `Paused` state. pub fn when_not_paused(&self) -> Result<(), Error> { - if self._paused.get() { + if self.paused.get() { return Err(Error::EnforcedPause(EnforcedPause {})); } Ok(()) @@ -145,7 +144,7 @@ impl Pausable { /// /// * [`Error::ExpectedPause`] - If the contract is in `Unpaused` state. pub fn when_paused(&self) -> Result<(), Error> { - if !self._paused.get() { + if !self.paused.get() { return Err(Error::ExpectedPause(ExpectedPause {})); } Ok(()) @@ -163,10 +162,10 @@ mod tests { unsafe impl TopLevelStorage for Pausable {} fn construct_paused(contract: &mut Pausable) { - contract._paused.set(true); + contract.paused.set(true); } fn construct_unpaused(contract: &mut Pausable) { - contract._paused.set(false); + contract.paused.set(false); } #[motsu::test] diff --git a/contracts/src/utils/structs/bitmap.rs b/contracts/src/utils/structs/bitmap.rs index 206eeb5f7..ade9e6848 100644 --- a/contracts/src/utils/structs/bitmap.rs +++ b/contracts/src/utils/structs/bitmap.rs @@ -26,8 +26,7 @@ const HEX_FF: U256 = uint!(0xff_U256); #[storage] pub struct BitMap { /// Inner laying mapping. - #[allow(clippy::used_underscore_binding)] - pub _data: StorageMap, + pub(crate) data: StorageMap, } impl BitMap { @@ -40,7 +39,7 @@ impl BitMap { pub fn get(&self, index: U256) -> bool { let bucket = Self::get_bucket(index); let mask = Self::get_mask(index); - let value = self._data.get(bucket); + let value = self.data.get(bucket); (value & mask) != U256::ZERO } @@ -66,7 +65,7 @@ impl BitMap { pub fn set(&mut self, index: U256) { let bucket = Self::get_bucket(index); let mask = Self::get_mask(index); - let mut value = self._data.setter(bucket); + let mut value = self.data.setter(bucket); let prev = value.get(); value.set(prev | mask); } @@ -79,7 +78,7 @@ impl BitMap { pub fn unset(&mut self, index: U256) { let bucket = Self::get_bucket(index); let mask = Self::get_mask(index); - let mut value = self._data.setter(bucket); + let mut value = self.data.setter(bucket); let prev = value.get(); value.set(prev & !mask); } diff --git a/contracts/src/utils/structs/checkpoints/mod.rs b/contracts/src/utils/structs/checkpoints/mod.rs index 0e7b67716..babf5e68d 100644 --- a/contracts/src/utils/structs/checkpoints/mod.rs +++ b/contracts/src/utils/structs/checkpoints/mod.rs @@ -51,19 +51,16 @@ impl MethodError for Error { #[storage] pub struct Trace { /// Stores checkpoints in a dynamic array sorted by key. - #[allow(clippy::used_underscore_binding)] - pub _checkpoints: StorageVec>, + pub(crate) checkpoints: StorageVec>, } /// State of a [`Checkpoint`] contract. #[storage] pub struct Checkpoint { /// The key of the checkpoint. Used as a sorting key. - #[allow(clippy::used_underscore_binding)] - pub _key: S::KeyStorage, + pub(crate) key: S::KeyStorage, /// The value corresponding to the key. - #[allow(clippy::used_underscore_binding)] - pub _value: S::ValueStorage, + pub(crate) value: S::ValueStorage, } impl Trace { @@ -107,7 +104,7 @@ impl Trace { if pos == len { S::Value::ZERO } else { - self._index(pos)._value.get() + self._index(pos).value.get() } } @@ -125,7 +122,7 @@ impl Trace { if pos == U256::ZERO { S::Value::ZERO } else { - self._index(pos - uint!(1_U256))._value.get() + self._index(pos - uint!(1_U256)).value.get() } } @@ -147,7 +144,7 @@ impl Trace { if len > uint!(5_U256) { let mid = len - len.sqrt(); - if key < self._index(mid)._key.get() { + if key < self._index(mid).key.get() { high = mid; } else { low = mid + uint!(1_U256); @@ -159,7 +156,7 @@ impl Trace { if pos == U256::ZERO { S::Value::ZERO } else { - self._index(pos - uint!(1_U256))._value.get() + self._index(pos - uint!(1_U256)).value.get() } } @@ -174,7 +171,7 @@ impl Trace { if pos == U256::ZERO { S::Value::ZERO } else { - self._index(pos - uint!(1_U256))._value.get() + self._index(pos - uint!(1_U256)).value.get() } } @@ -191,7 +188,7 @@ impl Trace { None } else { let checkpoint = self._index(pos - uint!(1_U256)); - Some((checkpoint._key.get(), checkpoint._value.get())) + Some((checkpoint.key.get(), checkpoint.value.get())) } } @@ -201,7 +198,7 @@ impl Trace { /// /// * `&self` - Read access to the checkpoint's state. pub fn length(&self) -> U256 { - U256::from(self._checkpoints.len()) + U256::from(self.checkpoints.len()) } /// Returns checkpoint at given position. @@ -215,10 +212,10 @@ impl Trace { /// * `&self` - Read access to the checkpoint's state. /// * `pos` - Index of the checkpoint. pub fn at(&self, pos: U32) -> (S::Key, S::Value) { - let guard = self._checkpoints.get(pos).unwrap_or_else(|| { + let guard = self.checkpoints.get(pos).unwrap_or_else(|| { panic!("should get checkpoint at index `{pos}`") }); - (guard._key.get(), guard._value.get()) + (guard.key.get(), guard.value.get()) } /// Pushes a (`key`, `value`) pair into an ordered list of checkpoints, @@ -243,8 +240,8 @@ impl Trace { let pos = self.length(); if pos > U256::ZERO { let last = self._index(pos - uint!(1_U256)); - let last_key = last._key.get(); - let last_value = last._value.get(); + let last_key = last.key.get(); + let last_value = last.value.get(); // Checkpoint keys must be non-decreasing. if last_key > key { @@ -253,7 +250,7 @@ impl Trace { // Update or push new checkpoint if last_key == key { - self._index_mut(pos - uint!(1_U256))._value.set(value); + self._index_mut(pos - uint!(1_U256)).value.set(value); } else { self._unchecked_push(key, value); } @@ -286,7 +283,7 @@ impl Trace { ) -> U256 { while low < high { let mid = low.average(high); - if self._index(mid)._key.get() > key { + if self._index(mid).key.get() > key { high = mid; } else { low = mid + uint!(1_U256); @@ -317,7 +314,7 @@ impl Trace { ) -> U256 { while low < high { let mid = low.average(high); - if self._index(mid)._key.get() < key { + if self._index(mid).key.get() < key { low = mid + uint!(1_U256); } else { high = mid; @@ -338,7 +335,7 @@ impl Trace { /// /// * If `pos` exceeds [`Self::length`]. fn _index(&self, pos: U256) -> StorageGuard> { - self._checkpoints + self.checkpoints .get(pos) .unwrap_or_else(|| panic!("should get checkpoint at index `{pos}`")) } @@ -355,7 +352,7 @@ impl Trace { /// /// * If `pos` exceeds [`Self::length`]. fn _index_mut(&mut self, pos: U256) -> StorageGuardMut> { - self._checkpoints + self.checkpoints .setter(pos) .unwrap_or_else(|| panic!("should get checkpoint at index `{pos}`")) } @@ -368,9 +365,9 @@ impl Trace { /// * `key` - Checkpoint key to insert. /// * `value` - Checkpoint value corresponding to insertion `key`. fn _unchecked_push(&mut self, key: S::Key, value: S::Value) { - let mut new_checkpoint = self._checkpoints.grow(); - new_checkpoint._key.set(key); - new_checkpoint._value.set(value); + let mut new_checkpoint = self.checkpoints.grow(); + new_checkpoint.key.set(key); + new_checkpoint.value.set(value); } } diff --git a/examples/ownable-two-step/src/lib.rs b/examples/ownable-two-step/src/lib.rs index 79f4a9a77..03ffcc13c 100644 --- a/examples/ownable-two-step/src/lib.rs +++ b/examples/ownable-two-step/src/lib.rs @@ -27,7 +27,7 @@ impl Ownable2StepExample { to: Address, value: U256, ) -> Result<(), Vec> { - self.ownable._ownable.only_owner()?; + self.ownable.ownable.only_owner()?; self.erc20.transfer(to, value)?; Ok(()) }