difftreelog
Merge commit 'b3fda41c65ef3d5198749d60622f0184af244734' into feature/NFTPAR-366_upstream_updates
in: master
3 files changed
pallets/nft/src/eth/mod.rsdiffbeforeafterboth117 let recipient = T::CrossAccountId::from_eth(recipient);117 let recipient = T::CrossAccountId::from_eth(recipient);118118119 <Module<T>>::transfer_internal(119 <Module<T>>::transfer_internal(120 sender,120 &sender,121 recipient,121 &recipient,122 &collection,122 &collection,123 1,123 1,124 amount,124 amount,143 let spender = T::CrossAccountId::from_eth(spender);143 let spender = T::CrossAccountId::from_eth(spender);144144145 <Module<T>>::approve_internal(145 <Module<T>>::approve_internal(146 sender,146 &sender,147 spender,147 &spender,148 &collection,148 &collection,149 1,149 1,150 amount,150 amount,160 let token_id = token_id.try_into().map_err(|_| "bad token id")?;160 let token_id = token_id.try_into().map_err(|_| "bad token id")?;161161162 <Module<T>>::approve_internal(162 <Module<T>>::approve_internal(163 sender,163 &sender,164 approved,164 &approved,165 &collection,165 &collection,166 token_id,166 token_id,167 1,167 1,176 let recipient = T::CrossAccountId::from_eth(recipient);176 let recipient = T::CrossAccountId::from_eth(recipient);177177178 <Module<T>>::transfer_from_internal(178 <Module<T>>::transfer_from_internal(179 sender,179 &sender,180 from,180 &from,181 recipient,181 &recipient,182 &collection,182 &collection,183 1,183 1,184 amount,184 amount,195 let token_id = token_id.try_into().map_err(|_| "bad token id")?;195 let token_id = token_id.try_into().map_err(|_| "bad token id")?;196196197 <Module<T>>::transfer_from_internal(197 <Module<T>>::transfer_from_internal(198 sender,198 &sender,199 from,199 &from,200 recipient,200 &recipient,201 &collection,201 &collection,202 token_id,202 token_id,203 1,203 1,pallets/nft/src/lib.rsdiffbeforeafterboth871 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);871 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);872 let collection = Self::get_collection(collection_id)?;872 let collection = Self::get_collection(collection_id)?;873873 Self::check_owner_or_admin_permissions(&collection, &sender)?;874 Self::toggle_white_list_internal(874875 &sender,876 &collection,875 <WhiteList<T>>::insert(collection_id, address.as_sub(), true);877 &address,876 878 true,879 )?;880877 Ok(())881 Ok(())878 }882 }896 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);900 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);897 let collection = Self::get_collection(collection_id)?;901 let collection = Self::get_collection(collection_id)?;902898 Self::check_owner_or_admin_permissions(&collection, &sender)?;903 Self::toggle_white_list_internal(899904 &sender,900 <WhiteList<T>>::remove(collection_id, address.as_sub());905 &collection,906 &address,907 false,908 )?;901909902 Ok(())910 Ok(())11401141 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1148 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);11421143 let target_collection = Self::get_collection(collection_id)?;1149 let collection = Self::get_collection(collection_id)?;114411501145 Self::can_create_items_in_collection(&target_collection, &sender, &owner, 1)?;1151 Self::create_item_internal(&sender, &collection, &owner, data);1146 Self::validate_create_item_args(&target_collection, &data)?;1147 Self::create_item_no_validation(&target_collection, owner, data)?;114811521149 Self::submit_logs(target_collection)?;1153 Self::submit_logs(collection)?;1150 Ok(())1154 Ok(())1151 }1155 }115211561178 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1182 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1179 let collection = Self::get_collection(collection_id)?;1183 let collection = Self::get_collection(collection_id)?;118011841181 Self::create_multiple_items_internal(sender, &collection, owner, items_data)?;1185 Self::create_multiple_items_internal(&sender, &collection, &owner, items_data)?;118211861183 Self::submit_logs(collection)?;1187 Self::submit_logs(collection)?;1184 Ok(())1188 Ok(())1239 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1243 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1240 let collection = Self::get_collection(collection_id)?;1244 let collection = Self::get_collection(collection_id)?;124112451242 Self::transfer_internal(sender, recipient, &collection, item_id, value)?;1246 Self::transfer_internal(&sender, &recipient, &collection, item_id, value)?;124312471244 Self::submit_logs(collection)?;1248 Self::submit_logs(collection)?;1245 Ok(())1249 Ok(())1267 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1270 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1268 let collection = Self::get_collection(collection_id)?;1271 let collection = Self::get_collection(collection_id)?;126912721270 Self::approve_internal(sender, spender, &collection, item_id, amount)?;1273 Self::approve_internal(&sender, &spender, &collection, item_id, amount)?;127112741272 Self::submit_logs(collection)?;1275 Self::submit_logs(collection)?;1273 Ok(())1276 Ok(())1299 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1301 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1300 let collection = Self::get_collection(collection_id)?;1302 let collection = Self::get_collection(collection_id)?;130113031302 Self::transfer_from_internal(sender, from, recipient, &collection, item_id, value)?;1304 Self::transfer_from_internal(&sender, &from, &recipient, &collection, item_id, value)?;130313051304 Self::submit_logs(collection)?;1306 Self::submit_logs(collection)?;1305 Ok(())1307 Ok(())1306 }1308 }13071308 // #[weight = 0]1309 // #[weight = 0]1309 // pub fn safe_transfer_from(origin, collection_id: CollectionId, item_id: TokenId, new_owner: T::AccountId) -> DispatchResult {13101311 // // let no_perm_mes = "You do not have permissions to modify this collection";1310 // // let no_perm_mes = "You do not have permissions to modify this collection";1312 // // ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);1311 // // ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);1342 ) -> DispatchResult {1341 ) -> DispatchResult {1343 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1342 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1344 1343 1345 let target_collection = Self::get_collection(collection_id)?;1344 let collection = Self::get_collection(collection_id)?;13451346 Self::set_variable_meta_data_internal(sender, &target_collection, item_id, data)?;1346 Self::set_variable_meta_data_internal(&sender, &collection, item_id, data)?;134713471348 Ok(())1348 Ok(())1349 }1349 }1679}1679}168016801681impl<T: Config> Module<T> {1681impl<T: Config> Module<T> {1682 pub fn create_item_internal(sender: &T::CrossAccountId, collection: &CollectionHandle<T>, owner: &T::CrossAccountId, data: CreateItemData) -> DispatchResult {1683 Self::can_create_items_in_collection(&collection, &sender, &owner, 1)?;1684 Self::validate_create_item_args(&collection, &data)?;1685 Self::create_item_no_validation(&collection, owner, data)?;16861687 Ok(())1688 }168216891683 pub fn transfer_internal(sender: T::CrossAccountId, recipient: T::CrossAccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {1690 pub fn transfer_internal(sender: &T::CrossAccountId, recipient: &T::CrossAccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {1684 // Limits check1691 // Limits check1685 Self::is_correct_transfer(target_collection, &recipient)?;1692 Self::is_correct_transfer(target_collection, &recipient)?;168616931702 _ => ()1709 _ => ()1703 };1710 };170417111705 Self::deposit_event(RawEvent::Transfer(target_collection.id, item_id, sender, recipient, value));1712 Self::deposit_event(RawEvent::Transfer(target_collection.id, item_id, sender.clone(), recipient.clone(), value));170617131707 Ok(())1714 Ok(())1708 }1715 }170917161710 pub fn approve_internal(1717 pub fn approve_internal(1711 sender: T::CrossAccountId,1718 sender: &T::CrossAccountId,1712 spender: T::CrossAccountId,1719 spender: &T::CrossAccountId,1713 collection: &CollectionHandle<T>,1720 collection: &CollectionHandle<T>,1714 item_id: TokenId,1721 item_id: TokenId,1715 amount: u1281722 amount: u1281772 );1779 );1773 }1780 }177417811775 Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender, spender, allowance));1782 Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender.clone(), spender.clone(), allowance));1776 Ok(())1783 Ok(())1777 }1784 }177817851779 pub fn transfer_from_internal(1786 pub fn transfer_from_internal(1780 sender: T::CrossAccountId,1787 sender: &T::CrossAccountId,1781 from: T::CrossAccountId,1788 from: &T::CrossAccountId,1782 recipient: T::CrossAccountId,1789 recipient: &T::CrossAccountId,1783 collection: &CollectionHandle<T>,1790 collection: &CollectionHandle<T>,1784 item_id: TokenId,1791 item_id: TokenId,1785 amount: u128,1792 amount: u128,1841 }1848 }184218491843 pub fn set_variable_meta_data_internal(1850 pub fn set_variable_meta_data_internal(1844 sender: T::CrossAccountId,1851 sender: &T::CrossAccountId,1845 collection: &CollectionHandle<T>, 1852 collection: &CollectionHandle<T>, 1846 item_id: TokenId,1853 item_id: TokenId,1847 data: Vec<u8>,1854 data: Vec<u8>,1867 }1874 }186818751869 pub fn create_multiple_items_internal(1876 pub fn create_multiple_items_internal(1870 sender: T::CrossAccountId,1877 sender: &T::CrossAccountId,1871 collection: &CollectionHandle<T>,1878 collection: &CollectionHandle<T>,1872 owner: T::CrossAccountId,1879 owner: &T::CrossAccountId,1873 items_data: Vec<CreateItemData>,1880 items_data: Vec<CreateItemData>,1874 ) -> DispatchResult {1881 ) -> DispatchResult {1875 Self::can_create_items_in_collection(&collection, &sender, &owner, items_data.len() as u32)?;1882 Self::can_create_items_in_collection(&collection, &sender, &owner, items_data.len() as u32)?;1878 Self::validate_create_item_args(&collection, data)?;1885 Self::validate_create_item_args(&collection, data)?;1879 }1886 }1880 for data in &items_data {1887 for data in &items_data {1881 Self::create_item_no_validation(&collection, owner.clone(), data.clone())?;1888 Self::create_item_no_validation(&collection, owner, data.clone())?;1882 }1889 }188318901884 Ok(())1891 Ok(())1914 Ok(())1921 Ok(())1915 }1922 }19231924 pub fn toggle_white_list_internal(1925 sender: &T::CrossAccountId,1926 collection: &CollectionHandle<T>,1927 address: &T::CrossAccountId,1928 whitelisted: bool,1929 ) -> DispatchResult {1930 Self::check_owner_or_admin_permissions(&collection, &sender)?;19311932 if whitelisted {1933 <WhiteList<T>>::insert(collection.id, address.as_sub(), true);1934 } else {1935 <WhiteList<T>>::remove(collection.id, address.as_sub());1936 }19371938 Ok(())1939 }191619401917 fn is_correct_transfer(collection: &CollectionHandle<T>, recipient: &T::CrossAccountId) -> DispatchResult {1941 fn is_correct_transfer(collection: &CollectionHandle<T>, recipient: &T::CrossAccountId) -> DispatchResult {1918 let collection_id = collection.id;1942 let collection_id = collection.id;1984 Ok(())2008 Ok(())1985 }2009 }198620101987 fn create_item_no_validation(collection: &CollectionHandle<T>, owner: T::CrossAccountId, data: CreateItemData) -> DispatchResult {2011 fn create_item_no_validation(collection: &CollectionHandle<T>, owner: &T::CrossAccountId, data: CreateItemData) -> DispatchResult {1988 match data2012 match data1989 {2013 {1990 CreateItemData::NFT(data) => {2014 CreateItemData::NFT(data) => {runtime/src/chain_extension.rsdiffbeforeafterboth171718extern crate pallet_nft;18extern crate pallet_nft;19pub use pallet_nft::*;19pub use pallet_nft::*;20use crate::Runtime;20use pallet_nft::CrossAccountId;21use sp_runtime::AccountId32;22use crate::Vec;21use crate::Vec;2223/// Create item parameters24#[derive(Debug, PartialEq, Encode, Decode)]25pub struct NFTExtCreateItem<E: Ext> {26 pub owner: <E::T as SysConfig>::AccountId,27 pub collection_id: u32,28 pub data: CreateItemData,29}233024/// Transfer parameters31/// Transfer parameters25#[derive(Debug, PartialEq, Encode, Decode)]32#[derive(Debug, PartialEq, Encode, Decode)]30 pub amount: u128,37 pub amount: u128,31}38}3940#[derive(Debug, PartialEq, Encode, Decode)]41pub struct NFTExtCreateMultipleItems<E: Ext> {42 pub owner: <E::T as SysConfig>::AccountId,43 pub collection_id: u32,44 pub data: Vec<CreateItemData>,45}4647#[derive(Debug, PartialEq, Encode, Decode)]48pub struct NFTExtApprove<E: Ext> {49 pub spender: <E::T as SysConfig>::AccountId,50 pub collection_id: u32,51 pub item_id: u32,52 pub amount: u128,53}5455#[derive(Debug, PartialEq, Encode, Decode)]56pub struct NFTExtTransferFrom<E: Ext> {57 pub owner: <E::T as SysConfig>::AccountId,58 pub recipient: <E::T as SysConfig>::AccountId,59 pub collection_id: u32,60 pub item_id: u32,61 pub amount: u128,62}6364#[derive(Debug, PartialEq, Encode, Decode)]65pub struct NFTExtSetVariableMetaData {66 pub collection_id: u32,67 pub item_id: u32,68 pub data: Vec<u8>, 69}7071#[derive(Debug, PartialEq, Encode, Decode)]72pub struct NFTExtToggleWhiteList<E: Ext> {73 pub collection_id: u32,74 pub address: <E::T as SysConfig>::AccountId,75 pub whitelisted: bool,76}327733/// The chain Extension of NFT pallet78/// The chain Extension of NFT pallet34pub struct NFTExtension;79pub struct NFTExtension;358036impl<C: Config> ChainExtension<C> for NFTExtension {81impl<C: Config> ChainExtension<C> for NFTExtension {37 fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>82 fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>38 where83 where84 E: Ext<T = C>,39 <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,85 <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,40 {86 {41 // The memory of the vm stores buf in scale-codec87 // The memory of the vm stores buf in scale-codec44 let mut env = env.buf_in_buf_out();90 let mut env = env.buf_in_buf_out();45 let input: NFTExtTransfer<E> = env.read_as()?;91 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);629263 let collection = pallet_nft::Module::<Runtime>::get_collection(input.collection_id)?;93 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;649465 match pallet_nft::Module::<Runtime>::transfer_internal(95 match pallet_nft::Module::<C>::transfer_internal(66 <Runtime as Config>::CrossAccountId::from_sub(sender),96 &C::CrossAccountId::from_sub(env.ext().caller().clone()),67 <Runtime as Config>::CrossAccountId::from_sub(recipient),97 &C::CrossAccountId::from_sub(input.recipient),68 &collection,98 &collection,69 input.token_id,99 input.token_id,70 input.amount,100 input.amount,73 _ => Err(DispatchError::Other("Transfer error"))103 _ => Err(DispatchError::Other("Transfer error"))74 }104 }75 },105 },106 1 => {107 // Create Item108 let mut env = env.buf_in_buf_out();109 let input: NFTExtCreateItem<E> = env.read_as()?;110111 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;112113 match pallet_nft::Module::<C>::create_item_internal(114 &C::CrossAccountId::from_sub(env.ext().address().clone()),115 &collection,116 &C::CrossAccountId::from_sub(input.owner),117 input.data,118 ) {119 Ok(_) => Ok(RetVal::Converging(func_id)),120 _ => Err(DispatchError::Other("CreateItem error"))121 }122 },123 2 => {124 // Create multiple items125 let mut env = env.buf_in_buf_out();126 let input: NFTExtCreateMultipleItems<E> = env.read_as()?;127128 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;129130 match pallet_nft::Module::<C>::create_multiple_items_internal(131 &C::CrossAccountId::from_sub(env.ext().address().clone()),132 &collection,133 &C::CrossAccountId::from_sub(input.owner),134 input.data,135 ) {136 Ok(_) => Ok(RetVal::Converging(func_id)),137 _ => Err(DispatchError::Other("CreateMultipleItems error"))138 }139 },140 3 => {141 // Approve142 let mut env = env.buf_in_buf_out();143 let input: NFTExtApprove<E> = env.read_as()?;144145 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;146147 pallet_nft::Module::<C>::approve_internal(148 &C::CrossAccountId::from_sub(env.ext().address().clone()),149 &C::CrossAccountId::from_sub(input.spender),150 &collection,151 input.item_id,152 input.amount,153 )?;154 Ok(RetVal::Converging(func_id))155 },156 4 => {157 // Transfer from158 let mut env = env.buf_in_buf_out();159 let input: NFTExtTransferFrom<E> = env.read_as()?;160161 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;162163 pallet_nft::Module::<C>::transfer_from_internal(164 &C::CrossAccountId::from_sub(env.ext().address().clone()),165 &C::CrossAccountId::from_sub(input.owner),166 &C::CrossAccountId::from_sub(input.recipient),167 &collection,168 input.item_id,169 input.amount170 )?;171 Ok(RetVal::Converging(func_id))172 },173 5 => {174 // Set variable metadata175 let mut env = env.buf_in_buf_out();176 let input: NFTExtSetVariableMetaData = env.read_as()?;177178 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;179180 pallet_nft::Module::<C>::set_variable_meta_data_internal(181 &C::CrossAccountId::from_sub(env.ext().address().clone()),182 &collection,183 input.item_id,184 input.data,185 )?;186 Ok(RetVal::Converging(func_id))187 },188 6 => {189 // Toggle whitelist190 let mut env = env.buf_in_buf_out();191 let input: NFTExtToggleWhiteList<E> = env.read_as()?;192193 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;194195 pallet_nft::Module::<C>::toggle_white_list_internal(196 &C::CrossAccountId::from_sub(env.ext().address().clone()),197 &collection,198 &C::CrossAccountId::from_sub(input.address),199 input.whitelisted,200 )?;201 Ok(RetVal::Converging(func_id))202 }76 _ => {203 _ => {77 panic!("Passed unknown func_id to test chain extension: {}", func_id);204 panic!("Passed unknown func_id to test chain extension: {}", func_id);78 }205 }