Browse Source

video nft: rework cancel_auction extrinsic

iorveth 3 years ago
parent
commit
a1da81a37d

+ 0 - 20
Cargo.lock

@@ -2435,7 +2435,6 @@ dependencies = [
  "pallet-transaction-payment",
  "pallet-transaction-payment-rpc-runtime-api",
  "pallet-utility",
- "pallet-vnft-auction",
  "pallet-working-group",
  "parity-scale-codec",
  "serde",
@@ -4335,25 +4334,6 @@ dependencies = [
  "sp-std",
 ]
 
-[[package]]
-name = "pallet-vnft-auction"
-version = "3.1.1"
-dependencies = [
- "frame-support",
- "frame-system",
- "pallet-balances",
- "pallet-common",
- "pallet-content",
- "pallet-timestamp",
- "parity-scale-codec",
- "serde",
- "sp-arithmetic",
- "sp-core",
- "sp-io",
- "sp-runtime",
- "sp-std",
-]
-
 [[package]]
 name = "pallet-working-group"
 version = "3.1.1"

+ 4 - 4
runtime-modules/content/src/errors.rs

@@ -67,8 +67,8 @@ decl_error! {
         // Auction Errors
         // ---------------------
 
-        /// Auction does not exist
-        AuctionDoesNotExist,
+        /// Auction for given video did not start
+        AuctionDidNotStart,
 
         /// Vnft auction for given video_id have been started already
         AuctionAlreadyStarted,
@@ -85,8 +85,8 @@ decl_error! {
         /// Actor origin authorization error happened
         ActorOriginAuthError,
 
-        /// Actor not authorized to issue nft
-        ActorNotAuthorizedToIssueNft,
+        /// Actor not authorized to manage auction
+        ActorNotAuthorizedToManageAuction,
 
         /// Given origin does not own vnft
         DoesNotOwnVNFT,

+ 34 - 27
runtime-modules/content/src/lib.rs

@@ -952,7 +952,7 @@ decl_module! {
 
             let video_id = auction_params.video_id;
 
-            // Ensure given video exist
+            // Ensure given video exists
             let video = Self::ensure_video_exists(&video_id)?;
 
             let auctioneer_account_id = Self::authorize_auctioneer(origin, &auctioneer, &auction_params, &video)?;
@@ -961,7 +961,7 @@ decl_module! {
             Self::validate_auction_params(&auction_params, &video)?;
 
             // Ensure nft auction is not started
-            ensure!(!video.is_nft_auction_started(), Error::<T>::AuctionAlreadyStarted);
+            video.ensure_nft_auction_is_not_started::<T>()?;
 
             //
             // == MUTATION SAFE ==
@@ -978,39 +978,46 @@ decl_module! {
             Self::deposit_event(RawEvent::AuctionStarted(auctioneer, auction_params));
         }
 
-        // /// Cancel video auction
-        // #[weight = 10_000_000] // TODO: adjust weight
-        // pub fn cancel_auction(
-        //     origin,
-        //     auctioneer: ContentActor<CuratorGroupId<T>, CuratorId<T>, MemberId<T>>,
-        //     auction_id: AuctionId<VideoId<T>, T::VNFTId>,
-        // ) {
+        /// Cancel video auction
+        #[weight = 10_000_000] // TODO: adjust weight
+        pub fn cancel_auction(
+            origin,
+            auctioneer: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
+            video_id: T::VideoId,
+        ) {
 
-        //     Self::authorize_content_actor(origin, &auctioneer)?;
+            // Ensure given video exists
+            let video = Self::ensure_video_exists(&video_id)?;
 
-        //     // Ensure auction for given video id exists
-        //     let auction = Self::ensure_auction_exists(auction_id)?;
+            Self::authorize_content_actor(origin, &auctioneer)?;
 
-        //     // Ensure given auction has no participants
-        //     auction.ensure_is_not_active::<T>()?;
+            // Ensure auction for given video id exists
+            let auction = video.ensure_nft_auction_started::<T>()?;
 
-        //     // Ensure given conntent actor is auctioneer
-        //     auction.ensure_is_auctioneer::<T>(&auctioneer)?;
+            // Return if auction round time expired
+            let now = pallet_timestamp::Module::<T>::now();
+            if auction.is_nft_auction_round_time_expired(now) {
+                return Ok(())
+            }
 
-        //     //
-        //     // == MUTATION SAFE ==
-        //     //
+            // Ensure given conntent actor is auctioneer
+            auction.ensure_is_auctioneer::<T>(&auctioneer)?;
 
-        //     // Try complete previous auction
-        //     if Self::try_complete_auction(&auction) {
-        //         return Ok(())
-        //     }
+            // Ensure auction is not active
+            auction.ensure_is_not_active::<T>()?;
+
+            //
+            // == MUTATION SAFE ==
+            //
 
-        //     <AuctionById<T>>::remove(auction_id);
+            // Update the video
+            let video = video.cancel_auction();
 
-        //     // Trigger event
-        //     Self::deposit_event(RawEvent::AuctionCancelled(auctioneer, auction_id));
-        // }
+            VideoById::<T>::insert(video_id, video);
+
+            // Trigger event
+            Self::deposit_event(RawEvent::AuctionCancelled(auctioneer, video_id));
+        }
 
         // /// Make auction bid
         // #[weight = 10_000_000] // TODO: adjust weight

+ 38 - 4
runtime-modules/content/src/types.rs

@@ -232,14 +232,14 @@ pub struct VideoRecord<
 }
 
 impl<
-        ChannelId,
-        SeriesId,
-        AccountId: PartialEq + Default,
+        ChannelId: Clone,
+        SeriesId: Clone,
+        AccountId: PartialEq + Default + Clone,
         Moment: BaseArithmetic + Copy,
         CuratorGroupId: Default + Copy,
         CuratorId: Default + Copy,
         MemberId: Default + Copy,
-        Balance,
+        Balance: Clone,
     >
     VideoRecord<
         ChannelId,
@@ -280,6 +280,32 @@ impl<
         )
     }
 
+    /// Ensure nft auction is not started
+    pub fn ensure_nft_auction_is_not_started<T: Trait>(&self) -> DispatchResult {
+        ensure!(
+            !self.is_nft_auction_started(),
+            Error::<T>::AuctionAlreadyStarted
+        );
+        Ok(())
+    }
+
+    pub fn ensure_nft_auction_started<T: Trait>(
+        &self,
+    ) -> Result<
+        AuctionRecord<AccountId, Moment, CuratorGroupId, CuratorId, MemberId, Balance>,
+        Error<T>,
+    > {
+        if let NFTStatus::Owned(OwnedNFT {
+            transactional_status: TransactionalStatus::Auction(auction),
+            ..
+        }) = &self.nft_status
+        {
+            Ok(auction.clone())
+        } else {
+            Err(Error::<T>::AuctionDidNotStart)
+        }
+    }
+
     pub fn ensure_nft_transactional_status_is_idle<T: Trait>(&self) -> DispatchResult {
         let is_idle = matches!(
             self.nft_status,
@@ -308,6 +334,14 @@ impl<
         }
         self
     }
+
+    /// Cancels nft auction
+    pub fn cancel_auction(mut self) -> Self {
+        if let NFTStatus::Owned(owned_nft) = &mut self.nft_status {
+            owned_nft.transactional_status = TransactionalStatus::Idle;
+        }
+        self
+    }
 }
 
 /// Video alias type for simplification.

+ 12 - 21
runtime-modules/content/src/vnft_auction/mod.rs

@@ -26,34 +26,25 @@ impl<T: Trait> Module<T> {
 
                 video.ensure_vnft_ownership::<T>(&account_id)?;
             } else {
-                return Err(Error::<T>::AuctionDoesNotExist.into())
+                return Err(Error::<T>::ActorNotAuthorizedToManageAuction.into());
             }
         } else {
             // TODO: Move to common pallet
-            ensure_actor_authorized_to_create_channel::<T>(origin, actor)
-                .map_err(|_| Error::<T>::ActorNotAuthorizedToIssueNft)?;
+            Self::authorize_content_actor(origin, actor)?;
         }
         Ok(account_id)
     }
 
-    // /// Authorize participant under given member id
-    // pub(crate) fn authorize_participant(
-    //     origin: T::Origin,
-    //     member_id: MemberId<T>,
-    // ) -> Result<T::AccountId, Error<T>> {
-    //     T::MemberOriginValidator::ensure_actor_origin(origin, member_id)
-    //         .map_err(|_| Error::<T>::ActorOriginAuthError)
-    // }
-
-    // /// Authorize content actor
-    // pub(crate) fn authorize_content_actor(
-    //     origin: T::Origin,
-    //     actor: &ContentActor<CuratorGroupId<T>, CuratorId<T>, MemberId<T>>,
-    // ) -> Result<(), Error<T>> {
-    //     // TODO: Move to common pallet
-    //     content::ensure_actor_authorized_to_create_channel::<T>(origin.clone(), actor)
-    //
-    // }
+    /// Authorize content actor
+    pub(crate) fn authorize_content_actor(
+        origin: T::Origin,
+        actor: &ContentActor<CuratorGroupId<T>, CuratorId<T>, MemberId<T>>,
+    ) -> Result<(), Error<T>> {
+        // TODO: Move to common pallet
+        ensure_actor_authorized_to_create_channel::<T>(origin, actor)
+            .map_err(|_| Error::<T>::ActorNotAuthorizedToManageAuction)?;
+        Ok(())
+    }
 
     // /// Ensure auction participant has sufficient balance to make bid
     // pub(crate) fn ensure_has_sufficient_balance(

+ 5 - 0
runtime-modules/content/src/vnft_auction/types.rs

@@ -255,6 +255,11 @@ impl<
         );
         Ok(())
     }
+
+    /// Check whether auction round time expired
+    pub fn is_nft_auction_round_time_expired(&self, now: Moment) -> bool {
+        (now - self.last_bid_time) >= self.round_time
+    }
 }
 
 /// Auction alias type for simplification.