git.delta.rocks / unique-network / refs/commits / d6ccebb78459

difftreelog

Add chain extension to mint

Greg Zaitsev2021-05-04parent: #b8ab366.patch.diff
in: master

2 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -1114,16 +1114,8 @@
         #[weight = <T as Config>::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<T: Config> Module<T> {
+    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<T>, item_id: TokenId, value: u128) -> DispatchResult {
         // Limits check
modifiedruntime/src/chain_extension.rsdiffbeforeafterboth
before · runtime/src/chain_extension.rs
1//! NFT Chain Extension23//4// This file is subject to the terms and conditions defined in5// file 'LICENSE', which is part of this source code package.6//78use codec::{Decode, Encode};910pub use pallet_contracts::chain_extension::RetVal;11use pallet_contracts::chain_extension::{12    ChainExtension, Environment, Ext, InitState, SysConfig, UncheckedFrom,13};1415pub use frame_support::debug;16use frame_support::dispatch::DispatchError;1718extern crate pallet_nft;19pub use pallet_nft::*;20use crate::Runtime;21use sp_runtime::AccountId32;22use crate::Vec;2324/// Transfer parameters25#[derive(Debug, PartialEq, Encode, Decode)]26pub struct NFTExtTransfer<E: Ext> {27    pub recipient: <E::T as SysConfig>::AccountId,28    pub collection_id: u32,29    pub token_id: u32,30    pub amount: u128,31}3233/// The chain Extension of NFT pallet34pub struct NFTExtension;3536impl<C: Config> ChainExtension<C> for NFTExtension {37    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>38    where39        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,40    {41        // The memory of the vm stores buf in scale-codec42        match func_id {43            0 => {44                let mut env = env.buf_in_buf_out();45                let input: NFTExtTransfer<E> = env.read_as()?;4647                // Sender to AccountId3248                let mut bytes_sender: [u8; 32] = [0; 32];49                let addr_vec_sender: Vec<u8> = env.ext().caller().encode();50                for i in 0..32 {51                    bytes_sender[i] = addr_vec_sender[i];52                }53                let sender = AccountId32::from(bytes_sender);5455                // Recipient to AccountId3256                let mut bytes_rec: [u8; 32] = [0; 32];57                let addr_vec_rec: Vec<u8> = input.recipient.encode();58                for i in 0..32 {59                    bytes_rec[i] = addr_vec_rec[i];60                }61                let recipient = AccountId32::from(bytes_rec);6263                let collection = pallet_nft::Module::<Runtime>::get_collection(input.collection_id)?;6465                match pallet_nft::Module::<Runtime>::transfer_internal(sender, recipient, &collection, input.token_id, input.amount) {66                    Ok(_) => Ok(RetVal::Converging(func_id)),67                    _ => Err(DispatchError::Other("Transfer error"))68                }69            },70			_ => {71				panic!("Passed unknown func_id to test chain extension: {}", func_id);72            }73        }74    }75}
after · runtime/src/chain_extension.rs
1//! NFT Chain Extension23//4// This file is subject to the terms and conditions defined in5// file 'LICENSE', which is part of this source code package.6//78use codec::{Decode, Encode};910pub use pallet_contracts::chain_extension::RetVal;11use pallet_contracts::chain_extension::{12    ChainExtension, Environment, Ext, InitState, SysConfig, UncheckedFrom,13};1415pub use frame_support::debug;16use frame_support::dispatch::DispatchError;1718extern crate pallet_nft;19pub use pallet_nft::*;20use crate::Runtime;21use sp_runtime::AccountId32;22use crate::Vec;2324/// Create item parameters25#[derive(Debug, PartialEq, Encode, Decode)]26pub struct NFTExtCreateItem<E: Ext> {27    pub owner: <E::T as SysConfig>::AccountId,28    pub collection_id: u32,29    pub data: CreateItemData,30}3132/// Transfer parameters33#[derive(Debug, PartialEq, Encode, Decode)]34pub struct NFTExtTransfer<E: Ext> {35    pub recipient: <E::T as SysConfig>::AccountId,36    pub collection_id: u32,37    pub token_id: u32,38    pub amount: u128,39}4041/// The chain Extension of NFT pallet42pub struct NFTExtension;4344impl<C: Config> ChainExtension<C> for NFTExtension {45    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>46    where47        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,48    {49        // The memory of the vm stores buf in scale-codec50        match func_id {51            0 => {52                let mut env = env.buf_in_buf_out();53                let input: NFTExtTransfer<E> = env.read_as()?;5455                // Sender to AccountId3256                let mut bytes_sender: [u8; 32] = [0; 32];57                let addr_vec_sender: Vec<u8> = env.ext().caller().encode();58                for i in 0..32 {59                    bytes_sender[i] = addr_vec_sender[i];60                }61                let sender = AccountId32::from(bytes_sender);6263                // Recipient to AccountId3264                let mut bytes_rec: [u8; 32] = [0; 32];65                let addr_vec_rec: Vec<u8> = input.recipient.encode();66                for i in 0..32 {67                    bytes_rec[i] = addr_vec_rec[i];68                }69                let recipient = AccountId32::from(bytes_rec);7071                let collection = pallet_nft::Module::<Runtime>::get_collection(input.collection_id)?;7273                match pallet_nft::Module::<Runtime>::transfer_internal(sender, recipient, &collection, input.token_id, input.amount) {74                    Ok(_) => Ok(RetVal::Converging(func_id)),75                    _ => Err(DispatchError::Other("Transfer error"))76                }77            },78            1 => {79                // Create Item80                let mut env = env.buf_in_buf_out();81                let input: NFTExtCreateItem<E> = env.read_as()?;8283                // Contract address to AccountId3284                let mut bytes_contract: [u8; 32] = [0; 32];85                let addr_vec_contract: Vec<u8> = env.ext().address().encode();86                for i in 0..32 {87                    bytes_contract[i] = addr_vec_contract[i];88                }89                let sender = AccountId32::from(bytes_contract);9091                // Recipient to AccountId3292                let mut bytes_rec: [u8; 32] = [0; 32];93                let addr_vec_rec: Vec<u8> = input.owner.encode();94                for i in 0..32 {95                    bytes_rec[i] = addr_vec_rec[i];96                }97                let recipient = AccountId32::from(bytes_rec);9899                // Parse data100                let cdata = input.data.encode();101                let vdata: [u8; 0] = [0; 0];102                let item_data = CreateItemData::NFT(CreateNftData{103                    const_data: cdata,104                    variable_data: vdata.to_vec()105                });106107                match pallet_nft::Module::<Runtime>::create_item_internal(sender, input.collection_id, recipient, item_data) {108                    Ok(_) => Ok(RetVal::Converging(func_id)),109                    _ => Err(DispatchError::Other("CreateItem error"))110                }111            },112            _ => {113                panic!("Passed unknown func_id to test chain extension: {}", func_id);114            }115        }116    }117}