From 9736a8455c1ad7ce9aa310ace64b83242f1ac9e6 Mon Sep 17 00:00:00 2001 From: PierreOssun Date: Thu, 24 Nov 2022 17:17:11 +0100 Subject: [PATCH] add rustfmt + updated storage to Mapping --- .rustfmt.toml | 59 +++++++++++++++ contracts/rmrk/lib.rs | 36 +++++++--- logics/impls/rmrk/errors.rs | 1 - logics/impls/rmrk/nesting.rs | 116 ++++++++++++++++++------------ logics/impls/rmrk/psp34_custom.rs | 41 +++++++---- logics/impls/rmrk/types.rs | 17 +++-- logics/traits/nesting.rs | 8 ++- logics/traits/psp34_custom.rs | 11 ++- 8 files changed, 212 insertions(+), 77 deletions(-) create mode 100644 .rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..89c7888 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,59 @@ +max_width = 100 +hard_tabs = false +tab_spaces = 4 +newline_style = "Auto" +use_small_heuristics = "Default" +indent_style = "Block" +wrap_comments = false +format_code_in_doc_comments = false +comment_width = 80 +normalize_comments = true # changed +normalize_doc_attributes = false +format_strings = false +format_macro_matchers = false +format_macro_bodies = true +empty_item_single_line = true +struct_lit_single_line = true +fn_single_line = false +where_single_line = false +imports_indent = "Block" +imports_layout = "Vertical" # changed +imports_granularity = "Crate" # changed +reorder_imports = true +reorder_modules = true +reorder_impl_items = false +type_punctuation_density = "Wide" +space_before_colon = false +space_after_colon = true +spaces_around_ranges = false +binop_separator = "Front" +remove_nested_parens = true +combine_control_expr = false # changed +overflow_delimited_expr = false +struct_field_align_threshold = 0 +enum_discrim_align_threshold = 0 +match_arm_blocks = true +force_multiline_blocks = true # changed +fn_args_layout = "Tall" +brace_style = "SameLineWhere" +control_brace_style = "AlwaysSameLine" +trailing_semicolon = false # changed +trailing_comma = "Vertical" +match_block_trailing_comma = false +blank_lines_upper_bound = 1 +blank_lines_lower_bound = 0 +edition = "2021" # changed +version = "One" +merge_derives = true +use_try_shorthand = true # changed +use_field_init_shorthand = true # changed +force_explicit_abi = true +condense_wildcard_suffixes = false +color = "Auto" +unstable_features = true # changed +disable_all_formatting = false +skip_children = false +hide_parse_errors = false +error_on_line_overflow = false +error_on_unformatted = false +ignore = [] \ No newline at end of file diff --git a/contracts/rmrk/lib.rs b/contracts/rmrk/lib.rs index 9120a20..c8ea8cc 100644 --- a/contracts/rmrk/lib.rs +++ b/contracts/rmrk/lib.rs @@ -4,20 +4,34 @@ #[openbrush::contract] pub mod rmrk_contract { use ink_env; - use ink_lang::codegen::{EmitEvent, Env}; + use ink_lang::codegen::{ + EmitEvent, + Env, + }; use ink_storage::traits::SpreadAllocate; use openbrush::{ contracts::{ ownable::*, - psp34::extensions::{enumerable::*, metadata::*}, + psp34::extensions::{ + enumerable::*, + metadata::*, + }, reentrancy_guard::*, }, - traits::{Storage, String}, + traits::{ + Storage, + String, + }, }; use rmrk::{ - impls::rmrk::{types, *}, - traits::nesting::*, - traits::psp34_custom::*, + impls::rmrk::{ + types, + *, + }, + traits::{ + nesting::*, + psp34_custom::*, + }, }; // Event definitions @@ -224,10 +238,16 @@ pub mod rmrk_contract { mod tests { use super::*; use crate::rmrk_contract::PSP34Error::*; - use ink_env::{pay_with_call, test}; + use ink_env::{ + pay_with_call, + test, + }; use ink_lang as ink; use ink_prelude::string::String as PreludeString; - use rmrk::impls::rmrk::{errors::RmrkError, psp34_custom::CustomInternal}; + use rmrk::impls::rmrk::{ + errors::RmrkError, + psp34_custom::CustomInternal, + }; const PRICE: Balance = 100_000_000_000_000_000; const BASE_URI: &str = "ipfs://myIpfsUri/"; const MAX_SUPPLY: u64 = 10; diff --git a/logics/impls/rmrk/errors.rs b/logics/impls/rmrk/errors.rs index 4c33cea..9a8fccb 100644 --- a/logics/impls/rmrk/errors.rs +++ b/logics/impls/rmrk/errors.rs @@ -1,5 +1,4 @@ //! Error definition for RMRK contract -//! use openbrush::traits::String; #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)] diff --git a/logics/impls/rmrk/nesting.rs b/logics/impls/rmrk/nesting.rs index 66456be..2df1de3 100644 --- a/logics/impls/rmrk/nesting.rs +++ b/logics/impls/rmrk/nesting.rs @@ -20,15 +20,24 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! This module enables nesting of RMRK or any other NFT which inherits PSP34. -use crate::impls::rmrk::errors::RmrkError; -use crate::impls::rmrk::types::*; -pub use crate::traits::nesting::{Internal, Nesting, NestingEvents}; +use crate::impls::rmrk::{ + errors::RmrkError, + types::*, +}; +pub use crate::traits::nesting::{ + Internal, + Nesting, + NestingEvents, +}; use ink_env::CallFlags; -use ink_prelude::collections::BTreeSet; use ink_prelude::vec::Vec; use openbrush::{ contracts::psp34::extensions::enumerable::*, - traits::{AccountId, Storage, String}, + traits::{ + AccountId, + Storage, + String, + }, }; /// Implement internal helper trait for Nesting @@ -50,7 +59,7 @@ where if children.contains(child_nft) { return Err(PSP34Error::Custom(String::from( RmrkError::AlreadyAddedChild.as_str(), - ))); + ))) } } Ok(()) @@ -70,7 +79,7 @@ where if children.contains(child_nft) { return Err(PSP34Error::Custom(String::from( RmrkError::AddingPendingChild.as_str(), - ))); + ))) } } Ok(()) @@ -78,14 +87,18 @@ where /// Add the child to the list of accepted children default fn add_to_accepted(&mut self, parent_token_id: Id, child_nft: ChildNft) { - self.data::() + let mut child_nfts = self + .data::() .accepted_children - .entry(parent_token_id.clone()) - .and_modify(|children| { - children.insert(child_nft.clone()); - }) - .or_insert_with(|| BTreeSet::from([child_nft.clone()])); - self._emit_child_accepted_event(&parent_token_id, &child_nft.0, &child_nft.1); + .get(&parent_token_id) + .unwrap_or(Vec::new()); + if !child_nfts.contains(&child_nft) { + child_nfts.push(child_nft.clone()); + self.data::() + .accepted_children + .insert(&parent_token_id, &child_nfts); + self._emit_child_accepted_event(&parent_token_id, &child_nft.0, &child_nft.1); + } } /// Remove the child to the list of accepted children @@ -94,34 +107,43 @@ where parent_token_id: &Id, child_nft: &ChildNft, ) -> Result<(), PSP34Error> { - if let Some(children) = self + let mut child_nfts = self .data::() .accepted_children - .get_mut(&parent_token_id) - { - if !children.remove(child_nft) { - return Err(PSP34Error::Custom(String::from( - RmrkError::ChildNotFound.as_str(), - ))); - } - } else { - return Err(PSP34Error::Custom(String::from( + .get(&parent_token_id) + .ok_or(PSP34Error::Custom(String::from( + RmrkError::InvalidParentId.as_str(), + )))?; + + let index = child_nfts + .iter() + .position(|x| x == child_nft) + .ok_or(PSP34Error::Custom(String::from( RmrkError::ChildNotFound.as_str(), - ))); - } + )))?; + child_nfts.remove(index); + + self.data::() + .accepted_children + .insert(&parent_token_id, &child_nfts); + self._emit_child_removed_event(&parent_token_id, &child_nft.0, &child_nft.1); Ok(()) } /// Add the child to the list of pending children default fn add_to_pending(&mut self, parent_token_id: Id, child_nft: ChildNft) { - self.data::() + let mut child_nfts = self + .data::() .pending_children - .entry(parent_token_id) - .and_modify(|children| { - children.insert(child_nft.clone()); - }) - .or_insert_with(|| BTreeSet::from([child_nft])); + .get(&parent_token_id) + .unwrap_or(Vec::new()); + if !child_nfts.contains(&child_nft) { + child_nfts.push(child_nft); + self.data::() + .pending_children + .insert(&parent_token_id, &child_nfts); + } } /// Remove the child to the list of pending children @@ -130,21 +152,25 @@ where parent_token_id: &Id, child_nft: &ChildNft, ) -> Result<(), PSP34Error> { - if let Some(children) = self + let mut child_nfts = self .data::() .pending_children - .get_mut(parent_token_id) - { - if !children.remove(child_nft) { - return Err(PSP34Error::Custom(String::from( - RmrkError::ChildNotFound.as_str(), - ))); - } - } else { - return Err(PSP34Error::Custom(String::from( + .get(&parent_token_id) + .ok_or(PSP34Error::Custom(String::from( + RmrkError::InvalidParentId.as_str(), + )))?; + + let index = child_nfts + .iter() + .position(|x| x == child_nft) + .ok_or(PSP34Error::Custom(String::from( RmrkError::ChildNotFound.as_str(), - ))); - } + )))?; + child_nfts.remove(index); + + self.data::() + .pending_children + .insert(&parent_token_id, &child_nfts); Ok(()) } @@ -171,7 +197,7 @@ where if token_owner != caller { return Err(PSP34Error::Custom(String::from( RmrkError::NotAuthorised.as_str(), - ))); + ))) } } Ok(()) diff --git a/logics/impls/rmrk/psp34_custom.rs b/logics/impls/rmrk/psp34_custom.rs index 60930c5..8b394de 100644 --- a/logics/impls/rmrk/psp34_custom.rs +++ b/logics/impls/rmrk/psp34_custom.rs @@ -1,5 +1,4 @@ //! Set of functions commonly used with PSP34 contract -//! // Copyright (c) 2022 Astar Network // @@ -22,19 +21,35 @@ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -use ink_prelude::string::{String as PreludeString, ToString}; +use ink_prelude::string::{ + String as PreludeString, + ToString, +}; -use crate::impls::rmrk::errors::RmrkError; -use crate::impls::rmrk::types::Psp34CustomData; -pub use crate::traits::psp34_custom::{CustomInternal, Psp34Custom}; +use crate::impls::rmrk::{ + errors::RmrkError, + types::Psp34CustomData, +}; +pub use crate::traits::psp34_custom::{ + CustomInternal, + Psp34Custom, +}; use openbrush::{ contracts::{ ownable::*, - psp34::extensions::{enumerable::*, metadata::*}, + psp34::extensions::{ + enumerable::*, + metadata::*, + }, reentrancy_guard::*, }, modifiers, - traits::{AccountId, Balance, Storage, String}, + traits::{ + AccountId, + Balance, + Storage, + String, + }, }; impl Psp34Custom for T @@ -63,7 +78,7 @@ where self.data::().last_token_id += 1; self._emit_transfer_event(None, Some(caller), Id::U64(token_id)); - return Ok(()); + return Ok(()) } /// Mint one or more tokens @@ -150,12 +165,12 @@ where (mint_amount as u128).checked_mul(self.data::().price_per_mint) { if transfered_value == value { - return Ok(()); + return Ok(()) } } return Err(PSP34Error::Custom(String::from( RmrkError::BadMintValue.as_str(), - ))); + ))) } /// Check amount of tokens to be minted @@ -163,7 +178,7 @@ where if mint_amount == 0 { return Err(PSP34Error::Custom(String::from( RmrkError::CannotMintZeroTokens.as_str(), - ))); + ))) } if let Some(amount) = self .data::() @@ -171,12 +186,12 @@ where .checked_add(mint_amount) { if amount <= self.data::().max_supply { - return Ok(()); + return Ok(()) } } return Err(PSP34Error::Custom(String::from( RmrkError::CollectionIsFull.as_str(), - ))); + ))) } /// Check if token is minted diff --git a/logics/impls/rmrk/types.rs b/logics/impls/rmrk/types.rs index 5ecacfc..2f5ba48 100644 --- a/logics/impls/rmrk/types.rs +++ b/logics/impls/rmrk/types.rs @@ -1,16 +1,21 @@ //! Types definition for RMRK contract -//! -use ink_prelude::collections::{BTreeMap, BTreeSet}; -use openbrush::traits::Balance; -use openbrush::{contracts::psp34::Id, traits::AccountId}; +use ink_prelude::vec::Vec; +use ink_storage::Mapping; +use openbrush::{ + contracts::psp34::Id, + traits::{ + AccountId, + Balance, + }, +}; pub const STORAGE_NESTING_KEY: u32 = openbrush::storage_unique_key!(NestingData); #[derive(Default, Debug)] #[openbrush::upgradeable_storage(STORAGE_NESTING_KEY)] pub struct NestingData { - pub pending_children: BTreeMap>, - pub accepted_children: BTreeMap>, + pub pending_children: Mapping>, + pub accepted_children: Mapping>, } #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)] diff --git a/logics/traits/nesting.rs b/logics/traits/nesting.rs index 8968652..9ba8cb9 100644 --- a/logics/traits/nesting.rs +++ b/logics/traits/nesting.rs @@ -1,6 +1,12 @@ //! Trait definitions for Nesting module use crate::impls::rmrk::types::*; -use openbrush::{contracts::psp34::Id, contracts::psp34::PSP34Error, traits::AccountId}; +use openbrush::{ + contracts::psp34::{ + Id, + PSP34Error, + }, + traits::AccountId, +}; #[openbrush::wrapper] pub type NestingRef = dyn Nesting; diff --git a/logics/traits/psp34_custom.rs b/logics/traits/psp34_custom.rs index 3661028..1cad073 100644 --- a/logics/traits/psp34_custom.rs +++ b/logics/traits/psp34_custom.rs @@ -1,10 +1,15 @@ //! Set of functions commonly used with PSP34 contract -//! use ink_prelude::string::String as PreludeString; use openbrush::{ - contracts::psp34::{Id, PSP34Error}, - traits::{AccountId, Balance}, + contracts::psp34::{ + Id, + PSP34Error, + }, + traits::{ + AccountId, + Balance, + }, }; #[openbrush::wrapper]