From b489139e3bba136d3888ff807ec6793a0e568182 Mon Sep 17 00:00:00 2001
From: Ermal Kaleci <ermalkaleci@gmail.com>
Date: Sat, 16 Nov 2019 09:09:38 +0100
Subject: [PATCH] added start into AuctionInfo and ensure auction is started
 (#31)

---
 auction/src/lib.rs    | 13 ++++++++++---
 auction/src/tests.rs  | 16 +++++++++++++++-
 traits/src/auction.rs |  2 ++
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/auction/src/lib.rs b/auction/src/lib.rs
index ea3cf27..ca51cf7 100644
--- a/auction/src/lib.rs
+++ b/auction/src/lib.rs
@@ -49,13 +49,19 @@ decl_module! {
 			let from = ensure_signed(origin)?;
 
 			let mut auction = <Auctions<T>>::get(id).ok_or(Error::AuctionNotExist)?;
+
+			let block_number = <paint_system::Module<T>>::block_number();
+
+			// make sure auction is started
+			ensure!(block_number >= auction.start, Error::AuctionNotStarted.into());
+
 			if let Some(ref current_bid) = auction.bid {
 				ensure!(value > current_bid.1, Error::InvalidBidPrice.into());
 			} else {
 				ensure!(value > 0.into(), Error::InvalidBidPrice.into());
 			}
 			let bid_result = T::Handler::on_new_bid(
-				<paint_system::Module<T>>::block_number(),
+				block_number,
 				id,
 				(from.clone(), value),
 				auction.bid.clone(),
@@ -102,6 +108,7 @@ decl_error! {
 	/// Error for auction module.
 	pub enum Error {
 		AuctionNotExist,
+		AuctionNotStarted,
 		BidNotAccepted,
 		InvalidBidPrice,
 	}
@@ -133,8 +140,8 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
 		Ok(())
 	}
 
-	fn new_auction(_start: T::BlockNumber, end: Option<T::BlockNumber>) -> Self::AuctionId {
-		let auction = AuctionInfo { bid: None, end: end };
+	fn new_auction(start: T::BlockNumber, end: Option<T::BlockNumber>) -> Self::AuctionId {
+		let auction = AuctionInfo { bid: None, start, end };
 		let auction_id = Self::auctions_count();
 		<AuctionsCount<T>>::mutate(|n| *n += Self::AuctionId::from(1));
 		<Auctions<T>>::insert(auction_id, auction);
diff --git a/auction/src/tests.rs b/auction/src/tests.rs
index d05b083..d45ef5f 100644
--- a/auction/src/tests.rs
+++ b/auction/src/tests.rs
@@ -21,6 +21,7 @@ fn update_auction_should_work() {
 			0,
 			AuctionInfo {
 				bid: Some((ALICE, 100)),
+				start: 10,
 				end: Some(100)
 			}
 		));
@@ -35,6 +36,7 @@ fn auction_info_should_work() {
 			AuctionModule::auction_info(0),
 			Some(AuctionInfo {
 				bid: None,
+				start: 10,
 				end: Some(100)
 			})
 		);
@@ -44,14 +46,26 @@ fn auction_info_should_work() {
 #[test]
 fn bid_should_work() {
 	ExtBuilder::default().build().execute_with(|| {
-		assert_eq!(AuctionModule::new_auction(10, Some(100)), 0);
+		assert_eq!(AuctionModule::new_auction(1, Some(100)), 0);
 		assert_ok!(AuctionModule::bid(Some(ALICE).into(), 0, 20));
 		assert_eq!(
 			AuctionModule::auction_info(0),
 			Some(AuctionInfo {
 				bid: Some((ALICE, 20)),
+				start: 1,
 				end: Some(100)
 			})
 		);
 	});
 }
+
+#[test]
+fn bid_should_fail() {
+	ExtBuilder::default().build().execute_with(|| {
+		assert_eq!(AuctionModule::new_auction(10, Some(100)), 0);
+		assert_eq!(
+			AuctionModule::bid(Some(ALICE).into(), 0, 20),
+			Err(Error::AuctionNotStarted.into())
+		);
+	});
+}
diff --git a/traits/src/auction.rs b/traits/src/auction.rs
index 5ceb4f1..e603e47 100644
--- a/traits/src/auction.rs
+++ b/traits/src/auction.rs
@@ -12,6 +12,8 @@ use sr_primitives::{
 pub struct AuctionInfo<AccountId, Balance, BlockNumber> {
 	/// Current bidder and bid price.
 	pub bid: Option<(AccountId, Balance)>,
+	/// Define which block this auction will be started.
+	pub start: BlockNumber,
 	/// Define which block this auction will be ended.
 	pub end: Option<BlockNumber>,
 }
-- 
GitLab