git.delta.rocks / unique-network / refs/commits / 172f80a3eea4

difftreelog

Merge pull request #147 from usetech-llc/feature/mint_extension

Yaroslav Bolyukin2021-05-05parents: #b8ab366 #397f5bd.patch.diff
in: master
Feature/mint extension

2 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
1117
1118 let sender = ensure_signed(origin)?;1117 let sender = ensure_signed(origin)?;
1119
1120 let target_collection = Self::get_collection(collection_id)?;1118 Self::create_item_internal(sender, collection_id, owner, data)
1121
1122 Self::can_create_items_in_collection(&target_collection, &sender, &owner, 1)?;
1123 Self::validate_create_item_args(&target_collection, &data)?;
1124 Self::create_item_no_validation(&target_collection, owner, data)?;
1125
1126 Ok(())
1127 }1119 }
11281120
1129 /// This method creates multiple items in a collection created with CreateCollection method.1121 /// This method creates multiple items in a collection created with CreateCollection method.
1763}1755}
17641756
1765impl<T: Config> Module<T> {1757impl<T: Config> Module<T> {
1758 pub fn create_item_internal(sender: T::AccountId, collection_id: CollectionId, owner: T::AccountId, data: CreateItemData) -> DispatchResult {
1759 let target_collection = Self::get_collection(collection_id)?;
1760
1761 Self::can_create_items_in_collection(&target_collection, &sender, &owner, 1)?;
1762 Self::validate_create_item_args(&target_collection, &data)?;
1763 Self::create_item_no_validation(&target_collection, owner, data)?;
1764
1765 Ok(())
1766 }
17661767
1767 pub fn transfer_internal(sender: T::AccountId, recipient: T::AccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {1768 pub fn transfer_internal(sender: T::AccountId, recipient: T::AccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {
1768 // Limits check1769 // Limits check
modifiedruntime/src/chain_extension.rsdiffbeforeafterboth
21use sp_runtime::AccountId32;21use sp_runtime::AccountId32;
22use crate::Vec;22use crate::Vec;
23
24/// Create item parameters
25#[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}
2331
24/// Transfer parameters32/// Transfer parameters
25#[derive(Debug, PartialEq, Encode, Decode)]33#[derive(Debug, PartialEq, Encode, Decode)]
36impl<C: Config> ChainExtension<C> for NFTExtension {44impl<C: Config> ChainExtension<C> for NFTExtension {
37 fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>45 fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>
38 where46 where
47 E: Ext<T = C>,
39 <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,48 <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
40 {49 {
41 // The memory of the vm stores buf in scale-codec50 // The memory of the vm stores buf in scale-codec
44 let mut env = env.buf_in_buf_out();53 let mut env = env.buf_in_buf_out();
45 let input: NFTExtTransfer<E> = env.read_as()?;54 let input: NFTExtTransfer<E> = env.read_as()?;
46
47 // Sender to AccountId32
48 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);
54
55 // Recipient to AccountId32
56 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);
6255
63 let collection = pallet_nft::Module::<Runtime>::get_collection(input.collection_id)?;56 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;
6457
65 match pallet_nft::Module::<Runtime>::transfer_internal(sender, recipient, &collection, input.token_id, input.amount) {58 match pallet_nft::Module::<C>::transfer_internal(
59 env.ext().caller().clone(),
60 input.recipient,
61 &collection,
62 input.token_id,
63 input.amount,
64 ) {
66 Ok(_) => Ok(RetVal::Converging(func_id)),65 Ok(_) => Ok(RetVal::Converging(func_id)),
67 _ => Err(DispatchError::Other("Transfer error"))66 _ => Err(DispatchError::Other("Transfer error"))
68 }67 }
69 },68 },
69 1 => {
70 // Create Item
71 let mut env = env.buf_in_buf_out();
72 let input: NFTExtCreateItem<E> = env.read_as()?;
73
74 match pallet_nft::Module::<C>::create_item_internal(
75 env.ext().address().clone(),
76 input.collection_id,
77 input.owner,
78 input.data,
79 ) {
80 Ok(_) => Ok(RetVal::Converging(func_id)),
81 _ => Err(DispatchError::Other("CreateItem error"))
82 }
83 },
70 _ => {84 _ => {
71 panic!("Passed unknown func_id to test chain extension: {}", func_id);85 panic!("Passed unknown func_id to test chain extension: {}", func_id);
72 }86 }