--- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -1114,16 +1114,8 @@ #[weight = ::WeightInfo::create_item(data.len())] #[transactional] pub fn create_item(origin, collection_id: CollectionId, owner: T::AccountId, data: CreateItemData) -> DispatchResult { - let sender = ensure_signed(origin)?; - - let target_collection = Self::get_collection(collection_id)?; - - Self::can_create_items_in_collection(&target_collection, &sender, &owner, 1)?; - Self::validate_create_item_args(&target_collection, &data)?; - Self::create_item_no_validation(&target_collection, owner, data)?; - - Ok(()) + Self::create_item_internal(sender, collection_id, owner, data) } /// This method creates multiple items in a collection created with CreateCollection method. @@ -1763,6 +1755,15 @@ } impl Module { + pub fn create_item_internal(sender: T::AccountId, collection_id: CollectionId, owner: T::AccountId, data: CreateItemData) -> DispatchResult { + let target_collection = Self::get_collection(collection_id)?; + + Self::can_create_items_in_collection(&target_collection, &sender, &owner, 1)?; + Self::validate_create_item_args(&target_collection, &data)?; + Self::create_item_no_validation(&target_collection, owner, data)?; + + Ok(()) + } pub fn transfer_internal(sender: T::AccountId, recipient: T::AccountId, target_collection: &CollectionHandle, item_id: TokenId, value: u128) -> DispatchResult { // Limits check --- a/runtime/src/chain_extension.rs +++ b/runtime/src/chain_extension.rs @@ -21,6 +21,14 @@ use sp_runtime::AccountId32; use crate::Vec; +/// Create item parameters +#[derive(Debug, PartialEq, Encode, Decode)] +pub struct NFTExtCreateItem { + pub owner: ::AccountId, + pub collection_id: u32, + pub data: CreateItemData, +} + /// Transfer parameters #[derive(Debug, PartialEq, Encode, Decode)] pub struct NFTExtTransfer { @@ -36,6 +44,7 @@ impl ChainExtension for NFTExtension { fn call(func_id: u32, env: Environment) -> Result where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { // The memory of the vm stores buf in scale-codec @@ -44,31 +53,36 @@ let mut env = env.buf_in_buf_out(); let input: NFTExtTransfer = env.read_as()?; - // Sender to AccountId32 - let mut bytes_sender: [u8; 32] = [0; 32]; - let addr_vec_sender: Vec = env.ext().caller().encode(); - for i in 0..32 { - bytes_sender[i] = addr_vec_sender[i]; - } - let sender = AccountId32::from(bytes_sender); + let collection = pallet_nft::Module::::get_collection(input.collection_id)?; - // Recipient to AccountId32 - let mut bytes_rec: [u8; 32] = [0; 32]; - let addr_vec_rec: Vec = input.recipient.encode(); - for i in 0..32 { - bytes_rec[i] = addr_vec_rec[i]; + match pallet_nft::Module::::transfer_internal( + env.ext().caller().clone(), + input.recipient, + &collection, + input.token_id, + input.amount, + ) { + Ok(_) => Ok(RetVal::Converging(func_id)), + _ => Err(DispatchError::Other("Transfer error")) } - let recipient = AccountId32::from(bytes_rec); + }, + 1 => { + // Create Item + let mut env = env.buf_in_buf_out(); + let input: NFTExtCreateItem = env.read_as()?; - let collection = pallet_nft::Module::::get_collection(input.collection_id)?; - - match pallet_nft::Module::::transfer_internal(sender, recipient, &collection, input.token_id, input.amount) { + match pallet_nft::Module::::create_item_internal( + env.ext().address().clone(), + input.collection_id, + input.owner, + input.data, + ) { Ok(_) => Ok(RetVal::Converging(func_id)), - _ => Err(DispatchError::Other("Transfer error")) + _ => Err(DispatchError::Other("CreateItem error")) } }, - _ => { - panic!("Passed unknown func_id to test chain extension: {}", func_id); + _ => { + panic!("Passed unknown func_id to test chain extension: {}", func_id); } } }