Skip to content

Commit

Permalink
Merge pull request #11 from rmrk-team/feature/basic-nesting-update
Browse files Browse the repository at this point in the history
add rustfmt + updated storage to Mapping
  • Loading branch information
PierreOssun authored Nov 25, 2022
2 parents bfce2ab + 9736a84 commit 2830d26
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 77 deletions.
59 changes: 59 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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 = []
36 changes: 28 additions & 8 deletions contracts/rmrk/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion logics/impls/rmrk/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Error definition for RMRK contract
//!
use openbrush::traits::String;

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
Expand Down
116 changes: 71 additions & 45 deletions logics/impls/rmrk/nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +59,7 @@ where
if children.contains(child_nft) {
return Err(PSP34Error::Custom(String::from(
RmrkError::AlreadyAddedChild.as_str(),
)));
)))
}
}
Ok(())
Expand All @@ -70,22 +79,26 @@ where
if children.contains(child_nft) {
return Err(PSP34Error::Custom(String::from(
RmrkError::AddingPendingChild.as_str(),
)));
)))
}
}
Ok(())
}

/// 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::<NestingData>()
let mut child_nfts = self
.data::<NestingData>()
.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::<NestingData>()
.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
Expand All @@ -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::<NestingData>()
.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::<NestingData>()
.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::<NestingData>()
let mut child_nfts = self
.data::<NestingData>()
.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::<NestingData>()
.pending_children
.insert(&parent_token_id, &child_nfts);
}
}

/// Remove the child to the list of pending children
Expand All @@ -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::<NestingData>()
.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::<NestingData>()
.pending_children
.insert(&parent_token_id, &child_nfts);

Ok(())
}
Expand All @@ -171,7 +197,7 @@ where
if token_owner != caller {
return Err(PSP34Error::Custom(String::from(
RmrkError::NotAuthorised.as_str(),
)));
)))
}
}
Ok(())
Expand Down
Loading

0 comments on commit 2830d26

Please sign in to comment.