--- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -1213,11 +1213,13 @@ /// * Re-Fungible Mode: Must specify transferred portion (between 0 and 1) #[weight = ::WeightInfo::transfer()] #[transactional] - pub fn transfer(origin, recipient: T::AccountId, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResult { - let sender = ensure_signed(origin)?; + pub fn transfer(origin, recipient: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResult { + let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?); let collection = Self::get_collection(collection_id)?; - Self::transfer_internal(sender, recipient, &collection, item_id, value) + Self::transfer_internal(sender, recipient, &collection, item_id, value)?; + + Ok(()) } /// Set, change, or remove approved address to transfer the ownership of the NFT. @@ -1737,7 +1739,7 @@ impl Module { - pub fn transfer_internal(sender: T::AccountId, recipient: T::AccountId, target_collection: &CollectionHandle, item_id: TokenId, value: u128) -> DispatchResult { + pub fn transfer_internal(sender: T::CrossAccountId, recipient: T::CrossAccountId, target_collection: &CollectionHandle, item_id: TokenId, value: u128) -> DispatchResult { // Limits check Self::is_correct_transfer(target_collection, &recipient)?; @@ -1906,11 +1908,11 @@ Ok(()) } - fn is_correct_transfer(collection: &CollectionHandle, recipient: &T::AccountId) -> DispatchResult { + fn is_correct_transfer(collection: &CollectionHandle, recipient: &T::CrossAccountId) -> DispatchResult { let collection_id = collection.id; // check token limit and account token limit - let account_items: u32 = >::get(collection_id, recipient).len() as u32; + let account_items: u32 = >::get(collection_id, recipient.as_sub()).len() as u32; ensure!(collection.limits.account_token_ownership_limit > account_items, Error::::AccountTokenLimitExceeded); Ok(()) @@ -2264,29 +2266,31 @@ fn transfer_fungible( collection: &CollectionHandle, value: u128, - owner: &T::AccountId, - recipient: &T::AccountId, + owner: &T::CrossAccountId, + recipient: &T::CrossAccountId, ) -> DispatchResult { let collection_id = collection.id; - let mut balance = >::get(collection_id, owner); + let mut balance = >::get(collection_id, owner.as_sub()); ensure!(balance.value >= value, Error::::TokenValueTooLow); // Send balance to recipient (updates balanceOf of recipient) Self::add_fungible_item(collection, recipient, value)?; // update balanceOf of sender - >::insert(collection_id, (*owner).clone(), balance.value - value); + >::insert(collection_id, owner.as_sub(), balance.value - value); // Reduce or remove sender if balance.value == value { - >::remove(collection_id, owner); + >::remove(collection_id, owner.as_sub()); } else { balance.value -= value; - >::insert(collection_id, (*owner).clone(), balance); + >::insert(collection_id, owner.as_sub(), balance); } + Self::deposit_event(RawEvent::Transfer(collection.id, 1, owner.clone(), recipient.clone(), value)); + Ok(()) } @@ -2294,8 +2298,8 @@ collection: &CollectionHandle, item_id: TokenId, value: u128, - owner: T::AccountId, - new_owner: T::AccountId, + owner: T::CrossAccountId, + new_owner: T::CrossAccountId, ) -> DispatchResult { let collection_id = collection.id; let full_item = >::get(collection_id, item_id) @@ -2312,15 +2316,15 @@ ensure!(amount >= value, Error::::TokenValueTooLow); // update balance - let balance_old_owner = >::get(collection_id, item.owner.clone()) + let balance_old_owner = >::get(collection_id, item.owner.as_sub()) .checked_sub(value) .ok_or(Error::::NumOverflow)?; - >::insert(collection_id, item.owner.clone(), balance_old_owner); + >::insert(collection_id, item.owner.as_sub(), balance_old_owner); - let balance_new_owner = >::get(collection_id, new_owner.clone()) + let balance_new_owner = >::get(collection_id, new_owner.as_sub()) .checked_add(value) .ok_or(Error::::NumOverflow)?; - >::insert(collection_id, new_owner.clone(), balance_new_owner); + >::insert(collection_id, new_owner.as_sub(), balance_new_owner); let old_owner = item.owner.clone(); let new_owner_has_account = full_item.owner.iter().any(|i| i.owner == new_owner); @@ -2370,14 +2374,16 @@ >::insert(collection_id, item_id, new_full_item); } + Self::deposit_event(RawEvent::Transfer(collection.id, item_id, owner, new_owner, amount)); + Ok(()) } fn transfer_nft( collection: &CollectionHandle, item_id: TokenId, - sender: T::AccountId, - new_owner: T::AccountId, + sender: T::CrossAccountId, + new_owner: T::CrossAccountId, ) -> DispatchResult { let collection_id = collection.id; let mut item = >::get(collection_id, item_id) @@ -2389,15 +2395,15 @@ ); // update balance - let balance_old_owner = >::get(collection_id, item.owner.clone()) + let balance_old_owner = >::get(collection_id, item.owner.as_sub()) .checked_sub(1) .ok_or(Error::::NumOverflow)?; - >::insert(collection_id, item.owner.clone(), balance_old_owner); + >::insert(collection_id, item.owner.as_sub(), balance_old_owner); - let balancenew_owner = >::get(collection_id, new_owner.clone()) + let balance_new_owner = >::get(collection_id, new_owner.as_sub()) .checked_add(1) .ok_or(Error::::NumOverflow)?; - >::insert(collection_id, new_owner.clone(), balancenew_owner); + >::insert(collection_id, new_owner.as_sub(), balance_new_owner); // change owner let old_owner = item.owner.clone(); @@ -2407,6 +2413,8 @@ // update index collection Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?; + Self::deposit_event(RawEvent::Transfer(collection.id, item_id, sender, new_owner, 1)); + Ok(()) }