Skip to content

Commit

Permalink
added approval
Browse files Browse the repository at this point in the history
  • Loading branch information
Maar-io committed Nov 3, 2022
1 parent cc06627 commit 47df267
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
34 changes: 34 additions & 0 deletions contracts/rmrk/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,39 @@ pub mod rmrk_contract {
ink_env::debug_println!("####### !!!!! TransferFailed");
Err(PSP34Error::Custom("TransferFailed".to_string()))
}

#[ink(message)]
fn approve(
&mut self,
operator: AccountId,
id: Option<Id>,
approved: bool,
) -> Result<(), PSP34Error> {
ink_env::debug_println!(
"####### approve ({:?},{:?}) approved{:?} operator:{:?}",
self.minting.rmrk_collection_id,
id,
approved,
operator,
);
self._approve_for(operator, id.clone(), approved)?;
ink_env::debug_println!("####### _approve_for OK");
if let Some(Id::U64(token_id)) = id {
if token_id > u32::MAX as u64 {
return Err(PSP34Error::Custom("TokenIdOverflow".to_string()));
}
// Uniques pallet is defined to take u32 as item/token id
UniquesExt::approve_transfer(
self.minting.rmrk_collection_id,
token_id as u32,
operator,
)
.map_err(|_| PSP34Error::Custom("UniquesApproveFailed".to_string()))?;
ink_env::debug_println!("####### approve OK");
return Ok(());
}
ink_env::debug_println!("####### !!!!! ApproveFailed");
Err(PSP34Error::Custom("ApproveFailed".to_string()))
}
}
}
11 changes: 0 additions & 11 deletions logics/impls/rmrk/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ where
Ok(())
}

/// Create new collection
default fn create_collection(&mut self) -> Result<(), RmrkError> {
ink_env::debug_println!("####### creating Uniques collection");
let create_result = UniquesExt::create(self.data::<data::Data>().rmrk_collection_id);
ink_env::debug_println!(
"####### initializing RMRK contract, create_result: {:?}",
create_result
);
Ok(())
}

/// Maximum amount of mintable tokens in this contract
default fn max_supply(&self) -> u64 {
self.data::<data::Data>().max_supply
Expand Down
6 changes: 1 addition & 5 deletions logics/traits/mint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::impls::rmrk::errors::RmrkError;
// use openbrush::contracts::psp34::*;
use ink_prelude::string::String;
use openbrush::{
modifiers,
Expand All @@ -16,12 +15,9 @@ pub trait RmrkMintable {
#[modifiers(non_reentrant)]
fn mint(&mut self, to: AccountId, mint_amount: u64) -> Result<(), RmrkError>;

// #[ink(message)]
// fn nft_mint_directly_to_nft(&self, parent: AccountIdOrCollectionNftTuple) -> Result<(), RmrkError>;

/// Create new collection
#[ink(message, payable)]
fn create_collection(&mut self) -> Result<(), RmrkError>;

/// Maximum amount of mintable tokens in this contract
#[ink(message)]
fn max_supply(&self) -> u64;
Expand Down
44 changes: 40 additions & 4 deletions logics/uniques_extension/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use ink_env::AccountId;
use scale::{
Decode,
Encode,
// HasCompact,
};

// type Balance = <DefaultEnvironment as Environment>::Balance;
Expand Down Expand Up @@ -40,6 +39,32 @@ impl UniquesExt {
.handle_error_code::<UniquesError>()
.call(&(collection_id, item_id, to))
}

/// Approve the operator for the item in Uniques pallet
pub fn approve_transfer(
collection_id: u32,
item_id: u32,
operator: AccountId,
) -> Result<(), UniquesError> {
::ink_env::chain_extension::ChainExtensionMethod::build(0x000200A3)
.input::<(u32, u32, AccountId)>()
.output::<()>()
.handle_error_code::<UniquesError>()
.call(&(collection_id, item_id, operator))
}

/// Cancel approval for the item's operator in Uniques pallet
pub fn cancel_approval(
collection_id: u32,
item_id: u32,
maybe_check_delegate: AccountId,
) -> Result<(), UniquesError> {
::ink_env::chain_extension::ChainExtensionMethod::build(0x000200A4)
.input::<(u32, u32, AccountId)>()
.output::<()>()
.handle_error_code::<UniquesError>()
.call(&(collection_id, item_id, maybe_check_delegate))
}
}

#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
Expand Down Expand Up @@ -91,7 +116,7 @@ pub enum UniquesError {
UnknownError = 99,
UnImplemented = 100,
}

// TODO check new/changed error codes in latest pallet_uniques
impl ink_env::chain_extension::FromStatusCode for UniquesError {
fn from_status_code(status_code: u32) -> Result<(), Self> {
match status_code {
Expand All @@ -102,8 +127,19 @@ impl ink_env::chain_extension::FromStatusCode for UniquesError {
4 => Err(Self::WrongOwner),
5 => Err(Self::BadWitness),
6 => Err(Self::InUse),
7 => Err(Self::Frozen),

7 => Err(Self::WrongDelegate),
8 => Err(Self::NoDelegate),
9 => Err(Self::Unapproved),
10 => Err(Self::Unaccepted),
11 => Err(Self::Locked),
12 => Err(Self::MaxSupplyReached),
13 => Err(Self::MaxSupplyAlreadySet),
14 => Err(Self::MaxSupplyTooSmall),
15 => Err(Self::NextIdNotUsed),
16 => Err(Self::NotForSale),
17 => Err(Self::BidTooLow),
18 => Err(Self::NextIdNotUsed),
19 => Err(Self::UnImplemented),
99 => Err(Self::UnknownError),
_ => panic!("encountered unknown status code"),
}
Expand Down

0 comments on commit 47df267

Please sign in to comment.